On Wed, Sep 26, 2012 at 10:29 PM, Aaron Meurer <[email protected]> wrote:
> On Wed, Sep 26, 2012 at 10:44 PM, Geoffrey Irving <[email protected]> wrote:
>> Hello,
>>
>> I am porting some code over from Sage, and am wondering if it's
>> possible to reproduce Sage's nested algebra constructions.
>> Specifically, in Sage you can do something like
>>
>>     R = SR['x','y','z']
>>
>> which creates a multivariate polynomial with three unknowns where the
>> coefficients are themselves symbolic expressions (elements of the
>> symbolic ring SR).  Since R is a polynomial ring, there are various
>> ways to extract and manipulate the set of monomials and coefficients
>> which preserve the structure of the symbolic coefficient expressions,
>> which I am using for code generation purposes:
>> https://github.com/otherlab/simplicity.
>>
>> Is there a way to do this kind of nested algebra in sympy?
>
> Yes, you can do this and a lot more using the polys module.  For example:
>
> In [5]: Poly(x**2 + y*z + 2*t, t, domain=ZZ[x, y, z])
> Out[5]: Poly(2*t + x**2 + y*z, t, domain='ZZ[x,y,z]')
>
> Here I have created a polynomial in the variable t, with coefficients
> from ZZ[x, y, z] (ZZ means the integers).
>
> You can also use QQ (rationals), RR (floating point reals, though note
> that this doesn't always work at the moment), and EX, which is a
> generic domain that allows arbitrary coefficients.  If you don't
> choose one, it is generated automatically.  You can also create
> polynomials in terms of functions like sin(x).  For example of EX:
>
> In [9]: Poly(x**2 + y*z + 2*sin(x)*t, t, domain=EX)
> Out[9]: Poly(2*sin(x)*t + x**2 + y*z, t, domain='EX')
>
> Note that in this case, and in the case where one of the variables is
> a function, Poly does not know about any algebraic relationships in
> the variables, so you may get wrong results.
>
> I hope that answers your question.

That's exactly what I wanted.  Thanks!

>> Another question: if I know that two symbolic expression are
>> polynomials, will expand() produce results that are always safely
>> hashable for use in dictionaries?  If not, is there some other way to
>> convert a general symbolic expression into something that can safely
>> be compared for equality and hashed?
>
> All SymPy expressions are hashable.  This includes the regular
> expressions and instances of Poly. The only exception is Matrix, which
> is mutable by default (but we do have an ImmutableMatrix suitable for
> hashing).
>
> Note that if you are interested in mathematical equality, == and hash
> only compare structural information.  So for example, (x + y)**2 ==
> x**2 + 2*x*y + y**2 will come out as False.  If you are interested in
> mathematical equality, either use Poly (if you are only dealing with
> polynomials), which will canonicalize things, or subtract one
> expression from another and try to simplify it to zero.

Yep, Poly((x+y)**2) seems to do what I need.

One more presumably easy question: how do I get an ordered list of the
variables in a multivariate polynomial?  p.terms() gives me a list of
(powers,coefficient) pairs, but I don't know how to interpret the
powers without the ordering of the variables.  I checked through the
tab completed list but only found p.atoms(), which returns an
unordered set.

Geoffrey

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sympy?hl=en.

Reply via email to