# How to make a list of polynomials callable?

A polynomial is callable:

sage: R=PolynomialRing(QQ,5,"x")
sage: R.gens()
(x0, x1, x2, x3, x4)
sage: f=x0*x1-x3
sage: f
x0*x1 - x3
sage: f(1,1,1)
0


but a list of polynomial is not:

sage: L=[x0*x1, x2, x3*x4]
sage: L(1,1,1,1,1)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-38-7ed7fddd166b> in <module>
----> 1 L(Integer(1),Integer(1),Integer(1),Integer(1),Integer(1))

TypeError: 'list' object is not callable


Question: How to make a list of polynomials callable?

I would like a function MakeCallable as follows:

sage: def MakeCallable(L):
....:     ....
....:     return fL


where fL would be as follows (here L=[x0*x1, x2, x3*x4]):

sage: def fL(x0, x1, x2, x3, x4):
....:     return [x0*x1, x2, x3*x4]

edit retag close merge delete

Sort by » oldest newest most voted

You can use MakeCallable = vector:

sage: R=PolynomialRing(QQ,5,"x")
sage: R.inject_variables()
Defining x0, x1, x2, x3, x4
sage: L=vector([x0*x1, x2, x3*x4])
sage: L(1,1,1,1,1)
(1, 1, 1)

more

I need to use it in a situation as follows

sage: var('y0,y1')
(y0, y1)
sage: E=[y0,y1]
sage: R=PolynomialRing(QQ,2,"y")
sage: EE=[e.polynomial(R) for e in E]
sage: EE
[y0, y1]
sage: VE=vector(EE)


but I get the following error:

TypeError: unable to find a common ring for all elements

( 2022-04-13 18:38:08 +0200 )edit

Are you sure you want e.polynomial(R)? (Check the parent/ring of the element you get; it is probably not what you want.) I suppose you want e.polynomial(ring=R), a.k.a. R(e).

( 2022-04-13 18:45:16 +0200 )edit

Yes, thanks!

( 2022-04-13 18:55:11 +0200 )edit