Le lundi 09 mai 2011 à 17:30 -0700, Luke a écrit :
> Here is a more explicit example of what Gilbert is talking about:
> In [1]: from sympy import *
> In [2]: from pydy import *
> In [3]: x = symbols('x')
> In [4]: N = ReferenceFrame('N')
> In [5]: type(N[1])
> Out[5]: pydy.pydy.UnitVector
> In [9]: test = x*N[1] + x*x*N[2]
> In [10]: type(test)
> Out[10]: sympy.core.add.Add
> In [11]: test2 = Vector(test)
> In [12]: test2
> Out[12]: x*n1> + x**2*n2>
> In [13]: type(test2)
> Out[13]: pydy.pydy.Vector
> In [14]: test3 = x*test2
> In [15]: test3
> Out[15]: x*x*n1> + x**2*n2>
> In [16]: type(test3)
> Out[16]: sympy.core.mul.Mul
>
> As you can see, in Out[15], the multiplication of x and test2 is being
> printed in a way that doesn't make sense, and in Out[16] we can see
> that this multiplication isn't resulting in another Vector object,
> instead it is of type Mul.
>
> Currently, to create a Vector object, you need to explicitly call
> Vector on a sympy expression that is composed from terms that have
> UnitVectors in them, or pass a dictionary with UnitVectors as the keys
> and sympy expressions as the values.
>
> Once you have that Vector object, you might want to multiply it by a
> scalar (sympy expression) and have it return another Vector object.
> This could be done using a somewhat user unfriendly approach:
>
> In [22]: Vector(dict([(k, x*v) for k, v in test2.dict.items()]))
> Out[22]: x**2*n1> + x**3*n2>
>
> This gets the job done, but it isn't very convenient.
>
> So basically, the question is whether
> 1) Is it easy enough in Sympy to make something like x*aVectorObject
> evaluate to another Vector object? Where the x is a "scalar part"
> (probably a sympy Expression) and aVectorObject is of type Vector
> (which currently subclasses from Basic)?
No, currently, it's not possible to do it correctly. Expr objects don't
expect to be multiplied by non-Expr objects. However, changing this
should be possible: you'd have to add to all relevant __mul__ methods
something like 'if not isinstance(other, Expr): return NotImplemented'.
And then, you'll only have to fight Python's labyrinthine model for
double-dispatch inside your vector classes.
> 2) Or is it ok to have to be more explicit about creating Vector
> objects by using the notation Vector(x**2*N[1] + x**3*N[2]) or
> Vector({N[1]: x**2, N[2]:x**3})?
Depends what you call ok. For me, this looks ugly and inconvenient.
> Additionally, there are other types of products that make sense for
> Vector and UnitVector objects, namely dot, cross, and outer products.
> So the stuff above would only be for multiplying a Vector by a scalar.
> I think all the other types of products have to make use of explicit
> method calls since there would be no way to know which type of product
> would be implied.
Please, try to make the interface dot(v1, v2), and not v1.dot(v2).
--
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.