Type error in SymPy when using tensor arrays

2019-02-20 19:44发布

I'm fairly new to SymPy, SciPy, NumPy etc., so perhaps there are easier ways to accomplish my goal, which is to define a sequence of indexed 4x4 transformation matrices for a 4-bar linkage. I don't have any pressing need for the 3rd dimension, except to make it easy to iterate through the sequence {A0 ... A3} in my code.

The code below generates the following error.

TypeError: unorderable types: slice() >= int()

On the other hand, replacing A[:,:,i] with something like A0 below does generate a 4x4 symbolic array as expected, so perhaps I'm not defining the slice correctly--however, none of the other slice permutations seems to work either. (I'm also having problems with implied line continuation on the last line of code, so I put everything inline.)

I'd appreciate any suggestions on how to solve this issue.

from sympy import *
def plg_jac():
    a_3, a_4, d_2, theta_1, theta_4 = symbols('a_3, a_4, d_2, theta_1, theta_4')

#   The D-H table is defined by the following 4 vectors
    a       = Matrix([0, 0, a_3, -a_4])
    alpha   = Matrix([pi/2, pi/2, 0, 0])
    d       = Matrix([0, d_2, 0, 0])
    theta   = Matrix([theta_1, pi, 0, theta_4])

#   Initialise a mutable 4x4x4 array
    A = tensor.array.MutableDenseNDimArray(range(64), (4,4,4))
#   Now define A0 ... A3, the transformation matrices between links i & i+1
    for i in range(a.shape[0]):
        A[:,:,i] = Array([[cos(theta[i]), -sin(theta[i])*cos(alpha[i]), sin(theta[i])*sin(alpha[i]), a[i]*cos(theta[i])], [sin(theta[i]), cos(theta[i])*cos(alpha[i]), -cos(theta[i])*sin(alpha[i]), a[i]*sin(theta[i])], [0, sin(alpha[i]), cos(alpha[i]), d[i]], [0, 0, 0, 1]])

1条回答
冷血范
2楼-- · 2019-02-20 20:04

The assignment to slices of SymPy arrays are missing, you can go around this lack by using numpy.

Import NumPy as:

import numpy

Convert the SymPy array to a NumPy array (of type object, so as to contain SymPy expressions):

In [18]: A_numpy = numpy.array(A.tolist()).astype(object)

Run a modified version of your for loop:

In [19]: for i in range(a.shape[0]):
    ...:     A_numpy[:, :, i] = [[cos(theta[i]), -sin(theta[i])*cos(alpha[i]), s
    ...: in(theta[i])*sin(alpha[i]), a[i]*cos(theta[i])], [sin(theta[i]), cos(th
    ...: eta[i])*cos(alpha[i]), -cos(theta[i])*sin(alpha[i]), a[i]*sin(theta[i])
    ...: ], [0, sin(alpha[i]), cos(alpha[i]), d[i]], [0, 0, 0, 1]]
    ...:     

Convert the resulting NumPy array back to a SymPy array:

In [20]: A = Array(A_numpy)

Print it's contents:

In [21]: A
Out[21]: 
⎡⎡cos(θ₁)  -1  1     cos(θ₄)  ⎤  ⎡sin(θ₁)   0  0    sin(θ₄)  ⎤  ⎡0  0   0  0⎤ 
⎢⎢                            ⎥  ⎢                           ⎥  ⎢           ⎥ 
⎢⎢   0     0   0    -sin(θ₄)  ⎥  ⎢   0      0  1    cos(θ₄)  ⎥  ⎢1  1   0  0⎥ 
⎢⎢                            ⎥  ⎢                           ⎥  ⎢           ⎥ 
⎢⎢sin(θ₁)  0   0        0     ⎥  ⎢-cos(θ₁)  1  0       0     ⎥  ⎢0  0   1  1⎥ 
⎢⎢                            ⎥  ⎢                           ⎥  ⎢           ⎥ 
⎣⎣   0     0   a₃  -a₄⋅cos(θ₄)⎦  ⎣   0      0  0  -a₄⋅sin(θ₄)⎦  ⎣0  d₂  0  0⎦ 

 ⎡0  0  0  0⎤⎤
 ⎢          ⎥⎥
 ⎢0  0  0  0⎥⎥
 ⎢          ⎥⎥
 ⎢0  0  0  0⎥⎥
 ⎢          ⎥⎥
 ⎣1  1  1  1⎦⎦
查看更多
登录 后发表回答