6

I am trying to migrate my scripts from mathematica to sage. I am stuck in something that it seems elementary.

I need to work with arbitrarily large polynomials say of the form

a00 + a10*x + a01*y + a20 *x^2 + a11*x*y + ...

I consider them polynomials only on x and y and I need given such a polynomial P to get the list of its monomials.

For example if P = a20*x^2 + a12*x*y^2 I want a list of the form [a20*x^2,a12*x*y^2].

I figured out that a polynomial in sage has a class function called coefficients that returns the coefficients and a class function called monomials that returns the monomials without the coefficients. Multiplying these two list together, gives the result I want.

The problem is that for this to work I need to explicitly declare all the a's as variables with is something that is not always possible.

Is there any way to tell sage that anything of the form a[number][number] is a variable? Or is there any way to define a whole family of variables in sage?

In a perfect world I would like to make sage behave like mathematica, in the sense that anything which is not defines is considered a variable, but I guess this is too optimistic.

tst
  • 1,117
  • 2
  • 10
  • 21

2 Answers2

4

My answer is not fully addressing your question but one trick I found to define variables was to use the PolynomialRing(). For example:

sage: R = PolynomialRing(RR, 'c', 20)
sage: c = R.gens()
sage: pol=sum(c[i]*x^i for i in range(10));pol
c9*x^9 + c8*x^8 + c7*x^7 + c6*x^6 + c5*x^5 + c4*x^4 + c3*x^3 + c2*x^2 + c1*x + c0

and later on you can define them as variables to solve(), for example:

sage: variables=[SR(c[i]) for i in srange(0,len(eq_list))];
sage: solution = solve(eqs,variables);
Mafra
  • 141
  • 3
3

You'll almost certainly need some very minor string processing; the answers

are better than anything I can say. Naturally, this is possible to implement, but ...

In a perfect world I would like to make sage behave like mathematica, in the sense that anything which is not defines is considered a variable, but I guess this is too optimistic.

True; indeed, that goes against Python's (and hence Sage's) philosophy of "explicit is better than implicit"; there were arguments for a long time over whether even x should be predefined as a symbolic variable (it is!).

(And truthfully, given how often I make typos, I'd really rather not have any arbitrary thing be considered a symbolic variable.)

kcrisman
  • 4,374
  • 20
  • 41
  • I am amazed that there is no native way to do this. Anyway, the code snippet in the sage-support forum does the trick. Thank you. – tst Oct 23 '16 at 02:45
  • You're welcome. I think the reason that no native way was ever implemented is because of some disagreements on exactly how it would take place - e.g. subscript or just concatenation, not to mention array ... – kcrisman Oct 24 '16 at 02:04