Hi, On Thu, Jan 14, 2010 at 01:12:16PM -0800, Martin Trinks wrote: > Hello, > > thanks for your answers. I know, that this representation is for > internal use only. But the thing is, that I made many calculations (in > fact I add one monomial after the other) and at some point I got > exactly the polynomials written down and then the error occured. But > so, the problem is earlier and I will look, why it is sorted in that > way. >
it's not a problem to use this internal representation, just you have
to remember to put terms in appropriate order (by default it's graded
lexicographic order). If you don't want to pay attention to order,
then use [(c_1, M_1), ..., (c_n, M_n)] representation (c_i will
be sympified and terms sorted), e.g.:
In [1]: from sympy import Poly
In [2]: c, m = [1, 1, 1, 5, 6, 3, 1], [(2, 3, 1), (1, 4, 1), (2, 2, 2),
(3, 1, 1), (2, 2, 1), (1, 3, 1), (4, 0, 0)]
In [3]: c
Out[3]: [1, 1, 1, 5, 6, 3, 1]
In [4]: m
Out[4]: [(2, 3, 1), (1, 4, 1), (2, 2, 2), (3, 1, 1), (2, 2, 1), (1, 3,
1), (4, 0, 0)]
(note wrong terms order above)
In [5]: zip(c, m)
Out[5]: [(1, (2, 3, 1)), (1, (1, 4, 1)), (1, (2, 2, 2)), (5, (3, 1, 1)),
(6, (2, 2, 1)), (3, (1, 3, 1)), (1, (4, 0, 0))]
In [6]: Poly(_, x, y, z)
Out[6]: Poly(x**2*y**3*z + x**2*y**2*z**2 + x*y**4*z + 5*x**3*y*z +
6*x**2*y**2*z + 3*x*y**3*z + x**4, x, y, z)
In [7]: Poly([(1, (2, 2, 2))], x, y, z)
Out[7]: Poly(x**2*y**2*z**2, x, y, z)
In [8]: _6 + _7
Out[8]: Poly(x**2*y**3*z + 2*x**2*y**2*z**2 + x*y**4*z + 5*x**3*y*z +
6*x**2*y**2*z + 3*x*y**3*z + x**4, x, y, z)
In [9]: _.subs({x: 1, y: 1, z: 1})
Out[9]: 19
You can use dict as well (dict(zip(m, c))), but in this case you
will have to sympify arguments on your own.
> I am looking forward to using version 0.7.0.
>
> Martin
>
> On 14 Jan., 19:40, Mateusz Paprocki <[email protected]> wrote:
> > Hi Martin,
> >
> >
> >
> > On Thu, Jan 14, 2010 at 02:44:06AM -0800, Martin Trinks wrote:
> > > Hello,
> >
> > > I have to do some calculations with polynomials (in fact only
> > > addition) and found the following problem:
> >
> > > import sympy as sy
> > > >>> sy.__version__
> > > '0.6.6'
> > > >>> x, y, z = sy.symbols('xyz')
> > > >>> Q = sy.Poly(((sy.S(1), sy.S(1), sy.S(1), sy.S(5), sy.S(6), sy.S(3),
> > > >>> sy.S(1)), ((2, 3, 1), (1, 4, 1), (2, 2, 2), (3, 1, 1), (2, 2, 1), (1,
> > > >>> 3, 1), (4, 0, 0))), x, y, z)
> > > >>> Q
> > > Poly(x**2*y**3*z + x*y**4*z + x**2*y**2*z**2 + 5*x**3*y*z +
> > > 6*x**2*y**2*z + 3*x*y**3*z + x**4, x, y, z)
> > > >>> S = sy.Poly(((sy.S(1),), ((2, 2, 2),)), x, y, z)
> > > >>> S
> > > Poly(x**2*y**2*z**2, x, y, z)
> > > >>> P = Q + S
> > > >>> P
> > > Poly(x**2*y**2*z**2 + x**2*y**3*z + x*y**4*z + x**2*y**2*z**2 +
> > > 5*x**3*y*z + 6*x**2*y**2*z + 3*x*y**3*z + x**4, x, y, z)
> > > >>> P.as_basic()
> > > x**2*y**2*z**2 + 6*z*x**2*y**2 + z*x**2*y**3 + 3*x*z*y**3 + 5*y*z*x**3
> > > + x**4 + x*z*y**4
> > > >>> P.subs({x:1, y:1, z:1})
> > > 18
> >
> > > As you can see the problem is with the monomial x**2*y**2*z**2, which
> > > appears two times in P.monoms. In the following further calculation
> > > and also the output for P are not correct.
> >
> > the problem starts here:
> >
> > > ((2, 3, 1), (1, 4, 1), (2, 2, 2), (3, 1, 1), (2, 2, 1), (1, 3, 1), (4, 0,
> > > 0))
> > > ^^^^^^^^^^^^^^^^^^^^
> >
> > You construct a Poly instance using a representation that is reserved
> > for internal use. When you do so you must provide the list with elements
> > sorted appropriately, which is:
> >
> > ((2, 3, 1), (2, 2, 2), (1, 4, 1), (3, 1, 1), (2, 2, 1), (1, 3, 1), (4, 0,
> > 0))
> >
> > in this case. If you don't want to think about such details, use other
> > representations (see help(Poly) or Poly? for details).
> >
> > In [1]: import sympy as sy
> >
> > In [2]: Q = sy.Poly(((sy.S(1), sy.S(1), sy.S(1), sy.S(5), sy.S(6),
> > sy.S(3), sy.S(1)), ((2, 3, 1), (2, 2, 2), (1, 4, 1), (3, 1, 1), (2, 2,
> > 1), (1, 3, 1), (4, 0, 0))), x, y, z)
> >
> > In [3]: S = sy.Poly(((sy.S(1),), ((2, 2, 2),)), x, y, z)
> >
> > In [4]: P = Q + S
> >
> > In [5]: P
> > Out[5]: Poly(x**2*y**3*z + 2*x**2*y**2*z**2 + x*y**4*z + 5*x**3*y*z +
> > 6*x**2*y**2*z + 3*x*y**3*z + x**4, x, y, z)
> >
> > In [6]: P.subs({x: 1, y: 1, z: 1})
> > Out[6]: 19
> >
> > btw. In version 0.7.0 you will be only allowed to pass a dict or Basic
> > expression to Poly class constructor.
> >
> > --
> > Mateusz
> >
> > signature.asc
> > < 1 KBAnzeigenHerunterladen
> --
> 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.
>
>
--
Mateusz
signature.asc
Description: This is a digitally signed message part
