Simple way of numerically evaluating Vectors

54 views
Skip to first unread message

scurrier

unread,
Sep 28, 2018, 12:15:50 PM9/28/18
to sympy
I have a Vector whose components are long expressions. I'd like to evaluate them so I can see the numerical value of the components. Is there an easy way to do so?

Seems like it should be simpler than the easiest way that I can come up with, which involves taking dot products. I was hoping for one function or method that could do this.

Specifically, the Vectors are sympy.physics.vector.vector.Vector objects. To give you an idea what mine look like, they are in a form kind of like this, but much longer:
(243 + cos(0.362)) * myframe.x + (sin(0.381) + 5) * my frame.y

I'd like to evaluate them to end up with something like this:
243.9351 * myframe.x + 5.371 * myframe.y

Aaron Meurer

unread,
Sep 28, 2018, 1:27:39 PM9/28/18
to sy...@googlegroups.com
The evalf method of the expression (like expr.evalf()) will evaluate
all the parts of the expression that it can to floating point numbers.

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sympy.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/b6a28405-c9fc-4d25-83fa-cd5c6c3bd40a%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

scurrier

unread,
Oct 1, 2018, 2:39:13 PM10/1/18
to sympy
That didn't work for me in SymPy 1.1.1.

AttributeError: 'Vector' object has no attribute 'evalf'

Aaron Meurer

unread,
Oct 1, 2018, 3:48:46 PM10/1/18
to sy...@googlegroups.com
Can you try it with the latest version (1.3)? If that doesn't work
please open an issue in our issue tracker.
https://github.com/sympy/sympy/issues/new

Aaron Meurer
On Mon, Oct 1, 2018 at 12:39 PM scurrier <shaun....@gmail.com> wrote:
>
> That didn't work for me in SymPy 1.1.1.
>
> AttributeError: 'Vector' object has no attribute 'evalf'
>
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sympy.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/dd78ecd0-d4ab-4bbe-ac6f-85b83daf5022%40googlegroups.com.

Jason Moore

unread,
Oct 1, 2018, 4:42:28 PM10/1/18
to sy...@googlegroups.com
I don't think the physics vectors are proper SymPy objects because they need to be mutable. evalf() was probably never implemented for them.

Jason

Jason Moore

unread,
Oct 1, 2018, 4:45:11 PM10/1/18
to sy...@googlegroups.com
Here is a workaround to get to the numbers:

In [1]: import sympy as sm

In [2]: from sympy.physics.vector import ReferenceFrame

In [3]: A = ReferenceFrame('A')

In [4]: v = 2*sm.cos(12) * A.x + 3 * sm.sin(-0.2) * A.y

In [5]: v
Out[5]: 2*cos(12)*A.x - 0.596007992385184*A.y

In [6]: v.evalf()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-6-38d207479e53> in <module>()
----> 1 v.evalf()


AttributeError: 'Vector' object has no attribute 'evalf'

In [7]: v.to_matrix(A).evalf()
Out[7]:
Matrix([
[  1.68770791746498],
[-0.596007992385184],
[                 0]])


scurrier

unread,
Oct 4, 2018, 11:37:19 AM10/4/18
to sympy
The workaround worked for me. Thank you.

Btw, I love SymPy. You guys rock, this software is really awesome.

Jason Moore

unread,
Oct 4, 2018, 12:01:06 PM10/4/18
to sy...@googlegroups.com

On Thu, Oct 4, 2018 at 8:37 AM scurrier <shaun....@gmail.com> wrote:
The workaround worked for me. Thank you.

Btw, I love SymPy. You guys rock, this software is really awesome.

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at https://groups.google.com/group/sympy.
Reply all
Reply to author
Forward
0 new messages