0

I try to index a matrix in a summation like this

from sympy import *
vx1,vx2,vx3,vx4,vx5, vy1,vy2,vy3,vy4,vy5,  = symbols('vx1 vx2 vx3 vx4 vx5 vy1 vy2 vy3 vy4 vy5')
vx=Matrix([vx1,vx2,vx3,vx4,vx5])
vy=Matrix([vy1,vy2,vy3,vy4,vy5])
p, n = symbols('p n', integer=True)
vx[0]
vx[1]
vx[2]
vx[3]
summation(p, (p, 0, 4))
summation(vx[p], (p, 0, 4)) 

But it seems like sympy cannot do this:

NameError: IndexError: Invalid index a[p]

Is there a way?

Dirk
  • 1,789
  • 2
  • 21
  • 31

2 Answers2

5

If you want a symbolic index into a Matrix, use MatrixSymbol:

In [15]: vx = MatrixSymbol('vx', 1, 4)

In [16]: summation(vx[(0, p)], (p, 0, 4)).doit()
Out[16]: vx₀₀ + vx₀₁ + vx₀₂ + vx₀₃ + vx₀₄
asmeurer
  • 86,894
  • 26
  • 169
  • 240
0

How about the following?

>>> sum(vx[p] for p in range(5))
vx1 + vx2 + vx3 + vx4 + vx5
smichr
  • 16,948
  • 2
  • 27
  • 34
  • see also http://stackoverflow.com/questions/24834447/how-to-create-an-indexed-variable-in-sympy and http://stackoverflow.com/questions/26402387/sympy-summation-with-indexed-variable – smichr Oct 30 '14 at 14:49