Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining a family of variables in sage

Tags:

variables

sage

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.

like image 709
tst Avatar asked Oct 22 '16 18:10

tst


People also ask

How do you declare variables in Sage?

Create a symbolic variable with the name s. INPUT: args – A single string var('x y') , a list of strings var(['x','y']) , or multiple strings var('x', 'y') . A single string can be either a single variable name, or a space or comma separated list of variable names.

What keyword would you use to define an inline variable in a LINQ query?

C# Language LINQ Queries Defining a variable inside a Linq query (let keyword)


2 Answers

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

  • this way of getting lists of symbolic variables
  • this other way of getting them that is similar
  • this sage-support post

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.)

like image 107
kcrisman Avatar answered Sep 23 '22 09:09

kcrisman


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);
like image 32
Mafra Avatar answered Sep 23 '22 09:09

Mafra