1

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?

Mystery_Jay
  • 161
  • 5
  • Why not just `f = vector(5, n, x^n-1)`? Evaluation can be done by `subst` function. E.g. `subst(f, 'x, 1)` will output `[0, 0, 0, 0, 0]` as expected. – Piotr Semenov Feb 03 '20 at 11:29
  • @PiotrSemenov Ordinarily something like that would be okay (and what I'd use), but in the set up I have for a larger piece of code, I need it set up as I've described unfortunately – Mystery_Jay Feb 03 '20 at 11:46
  • You really have not explained adequately what you are trying to accomplish, so it hard to give good advice. You are trying to do something that isn't the way it is normally done and when told what the normal way is you say you cannot do it that way, without proper explanation. – Andrew Feb 04 '20 at 17:55
  • @Andrew This has come down to a mix of me trying to simplify my problem (so I could try and then adapt it myself for my own, slightly more difficult to learn how to approach my problem), and trying to avoid giving too much background which I didn't think was necessary (when clearly it was). I've edited my original question now in hope this makes the situation as to why I'm trying to do what I'm trying to do clear! – Mystery_Jay Feb 04 '20 at 20:39

1 Answers1

1

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.

Andrew
  • 873
  • 4
  • 10
  • Amazing - thank you so much for the extra insight! I'd never have considered using `subst` in this case. The alternative version may also prove useful for me in the future too! – Mystery_Jay Feb 05 '20 at 08:40