How to do point / cross vector multiplication using Sympy

I would like to know how to do it

  • point multiplication
  • cross multiplication
  • add / sub

vectors with sympy library. I tried to peek into the official documentation, but I had no luck, or it was too complicated. Can someone help me with this?

I tried to do this simple operation

a ยท b = |a| ร— |b| ร— cos(ฮธ) 
+7
python math sympy
source share
3 answers

numpy designed for this, it is a clean and fast way to do numerical calculations, as it is implemented in C.

 In [36]: x = [1, 2, 3] ...: y = [4, 5, 6] In [37]: import numpy as np ...: print np.dot(x, y) ...: print np.cross(x, y) ...: print np.add(x, y) #np.subtract, etc. 32 [-3 6 -3] [5 7 9] 

There is a discussion about numpy and sympy in google groups.

+4
source share

http://docs.sympy.org/0.7.2/modules/physics/mechanics/api/functions.html

There are examples and some code in this document. What exactly do you not understand? Perhaps try to be more specific.

The exact multiplication when writing this is explained in a document with this example:

 from sympy.physics.mechanics import ReferenceFrame, Vector, dot from sympy import symbols q1 = symbols('q1') N = ReferenceFrame('N') # so, ||x|| = ||y|| = ||z|| = 1 dot(Nx, Nx) 1 # it is ||Nx||*||Ny||*cos(Nx,Ny) dot(Nx, Ny) 0 # it is ||Nx||*||Ny||*cos(Nx,Ny) A = N.orientnew('A', 'Axis', [q1, Nx]) dot(Ny, Ay) cos(q1) 

Alternatively, you can think about it with numpy ...

+2
source share

To do vector point / cross product multiplication with sympy , you need to import the base CoordSys3D vector object. The following is an example of working code:

 from sympy.vector import CoordSys3D N = CoordSys3D('N') v1 = 2*N.i+3*Nj-Nk v2 = Ni-4*N.j+Nk v1.dot(v2) v1.cross(v2) #Alternately, can also do v1 & v2 v1 ^ v2 

Please note that the last 2 lines are not recommended in the sympy documentation. It is better to use methods explicitly. Personally, I think this is a matter of preference, however.

0
source share

All Articles