I'm trying to get a vector of polynomials, but within the vector have each polynomial defined by a function in Pari.
For example, I want to be able to output a vector of this form:
[f(x) = x-1 , f(x) = x^2 - 1, f(x) = x^3 - 1, f(x) = x^4 - 1, f(x) = x^5 - 1]
A simple vector construction of vector( 5, n, f(x) = x^n-1)
doesn't work, outputting [(x)->my(i=1);x^i-1, (x)->my(i=2);x^i-1, (x)->my(i=3);x^i-1, (x)->my(i=4);x^i-1, (x)->my(i=5);x^i-1]
.
Is there a way of doing this quite neatly?
Update:
I have a function which takes a polynomial in two variables (say x and y), replaces one of those variables (say y) with exp(I*t), and then integrates this between t=0 and t=1, giving a single variable polynomial in x: int(T)=intnum(t=0,1,T(x,exp(I*t)))
Because of the way this is defined, I have to explicitly define a polynomial T(x,y)=...
, and then calculate int(T)
. Simply putting in a polynomial, say int(x*y)-1
, returns:
*** at top-level: int(x*y-1)
*** ^----------
*** in function int: intnum(t=0,1,T(x,exp(I*t)))
*** ^--------------
*** not a function in function call
*** Break loop: type 'break' to go back to GP prompt
I want to be able to do this for many polynomials, without having to manually type T(x,y)=...
for every single one. My plan is to try and do this using the apply
feature (so, putting all the polynomials in a vector - for a simple example, vector(5, n, x^n*y-1)
). However, because of the way I've defined int
, I would need to have each entry in the vector defined as T(x,y)=...
, which is where my original question spawned from.
Defining T(x,y)=vector(5, n, x^n*y-1)
doesn't seem to help with what I want to calculate. And because of how int
is defined, I can't think of any other way to go about trying to tackle this.
Any ideas?
The PARI inbuilt intnum
function takes as its third argument an expression rather than a function. This expression can make use of the variable t
. (Several inbuilt functions behave like this - they are not real functions).
Your int
function can be defined as follows:
int(p)=intnum(t=0, 1, subst(p, y, exp(I*t)))
It takes as an argument a polynomial p
and then it substitutes for y
when required to do so.
You can then use int(x*y)
which returns (0.84147098480789650665250232163029899962 + 0.45969769413186028259906339255702339627*I)*x
'.
Similarly you can use apply
with a vector of polynomials. For example:
apply(int, vector(5, n, x^n*y-1))
Coming back to your original proposal - it's not technically wrong and will work. I just wouldn't recommend it over the subst
method, but perhaps if you are were wanting to perform numerical integration over a class of functions that were not representable as polynomials. Let's suppose int
is defined as:
int(T)=intnum(t=0,1,T(x,exp(I*t)))
You can invoke it using the syntax int((x,y) -> x*y)
. The arrow is the PARI syntax for creating an anonymous function. (This is the difference between an expression and a function - you cannot create your own functions that work like PARI inbuilt functions)
You may even use it with a vector of functions:
apply(int, vector(5, n, (x,y)->x^n*y-1))
I am using the syntax (x,y)->x^n*y-1
here which is preferable to the f(x,y)=x^n*y-1
you had in your question, but they are essentially the same. (the latter form also defines f
as a side effect which is not wanted so it is better to use anonymous functions.