13

How to create vector of symbolic variables in sympy? I want to do something like

x, x1, x2, x3 = symbols ('x x1 x2 x3')
A = [x+x1,x+x2,x+x3]
B = A * Transpose(A)
print (B)

A is array of symbolic variables. I checked with sympy documentation, but couldn't figure out.

(Python 2.7.6, sympy 0.7.4.1)

Update:

I want to do something like

x, x1, x2, x3 = symbols ('x x1 x2 x3')
v1e = x+x1
v2e = x+x2
v3e = x+x3
v1 = v1e.subs(x1,1)
v2 = v2e.subs(x2,2)
v3 = v3e.subs(x3,3)
A = Matrix ([v1,v2,v3])
B = A * Transpose(A)
print (B)

But seems that there is problem with v1,.. putting as matrix elements. Any suggestions?

user7423098
  • 311
  • 2
  • 3
  • 9
  • Could you clarify, what exactly do you mean under "problem with `v1,...`putting as matrix elements"? (Note that currently there's a syntax error: unbalanced square brackets in `A = Matrix ([v1,v2,v3]])`, should be `A = Matrix ([[v1,v2,v3]])`. – Ilya V. Schurov Jan 30 '17 at 00:21
  • @IlyaV.Schurov Sorry, there was a mistake. Updated the question. – user7423098 Jan 30 '17 at 01:41
  • Again, could you clarify, what exactly do you mean under "problem with v1,...putting as matrix elements"? What kind of error messages do you have? Also, please, note that you have to put double square brackets: `A = Matrix ([[v1,v2,v3]])`. – Ilya V. Schurov Jan 30 '17 at 17:10
  • Your code works as it is now. As it stands, it gives the outer product. If you want the inner product use the double brackets as @IlyaV.Schurov suggested (this will create a row vector instead of a column vector), or use `Transpose(A)*A`. – asmeurer Jan 30 '17 at 20:12

1 Answers1

22

Vectors can be represented as Matrixes in sympy as column-vectors or row-vectors.

from sympy import symbols, Matrix, Transpose
x, x1, x2, x3 = symbols('x x1 x2 x3')
A = Matrix([[x+x1, x+x2, x+x3]])
B = A * Transpose(A)
# or B = A * A.T
print (B)
Ilya V. Schurov
  • 7,687
  • 2
  • 40
  • 78