9

I would like to know how to do

  • dot multiplication
  • cross multiplication
  • add/sub

of vectors with the sympy library. I have tried looking into the official documentation but I have had no luck or It was too complicated. Can anyone help me out on this?

I was trying to do this simple operation

a · b = |a| × |b| × cos(θ)
Vannen
  • 712
  • 2
  • 7
  • 17
  • 3
    I think it is important to emphasize the difference between NumPy and SymPy: NumPy does numerical calculations while SymPy is used for symbolic mathematics. So, if you only want to calculate solutions for given sets of values, NumPy is the right choice, when you want to develop equations or proove some relations, SymPy is the right choice. They are not a replacement for each other like the answers suggest. – Sven Painer Mar 20 '17 at 14:39

6 Answers6

5

You can do it as described here: https://docs.sympy.org/latest/modules/matrices/matrices.html?highlight=cross#sympy.matrices.matrices.MatrixBase.cross

For example:

>>> from sympy import Matrix
>>> M = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> v = Matrix([1, 1, 1])
>>> M.row(0).dot(v)
6
>>> M.col(0).dot(v)
12
>>> v = [3, 2, 1]
>>> M.row(0).dot(v)
10
Kepler
  • 155
  • 3
  • 6
4

To do vector dot/cross product multiplication with sympy, you have to import the basis vector object CoordSys3D. Here is a working code example below:

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

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

user32882
  • 5,094
  • 5
  • 43
  • 82
3

If you have symbolic vectors and need to use sympy it is actually very simple, just use the cross function as exemplified below:

import sympy as s
a,b,c,x,y,z = s.symbols("a,b,c,x,y,z")
v1 = s.Matrix([a,b,c])
v2 = s.Matrix([x,y,z])
cross_result = v1.cross(v2)
print(cross_result)

With output:

Matrix([
[ b*z - c*y],
[-a*z + c*x],
[ a*y - b*x]])
Caridorc
  • 6,222
  • 2
  • 31
  • 46
2

numpy is designed for this, it is a clean and fast way to do numerical calculations because it's 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 on numpy and sympy on google groups.

zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
  • I am actually using numpy for doing it. Thanks for mentioning it. Ignore those fanatics who downvoted you. – Vannen Mar 02 '14 at 12:59
  • 2
    I doubt they were SymPy devs. Most of us will refer you away from SymPy to NumPy if you are only doing things numerically. SymPy should be used if you want exact, symbolic answers. – asmeurer Mar 23 '14 at 23:26
  • @Vannen Fanatics? You asked how to do these operations in SymPy, so this answer is simply not fitting. Quite annoying to stumble upon answers like this when you actually want to do cross products with SymPy. – Knufy Einundzwanzig May 23 '23 at 08:56
2

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

There are examples on that doc and some code here. What exactly don't you understand? Maybe, try to be more specific.

The dot multiplication as you write it is explained in the doc with that 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(N.x, N.x)
1 # it is ||N.x||*||N.y||*cos(Nx,Ny)
dot(N.x, N.y)
0 # it is ||N.x||*||N.y||*cos(Nx,Ny)
A = N.orientnew('A', 'Axis', [q1, N.x])
dot(N.y, A.y)
cos(q1)

Also, you might consider doing it with numpy...

abrunet
  • 1,122
  • 17
  • 31
0

I had a similar issue, and the method followed was just to put the sympy equations/variables within np.array() and use np.cross(). for example,

np.cross(np.array(M),np.array(N))

where M & N are variables computed using sympy

halfelf
  • 9,737
  • 13
  • 54
  • 63
PuSh
  • 5
  • 3