2017-10-17 53 views
1

我对SymPy,SciPy,NumPy等相当陌生,所以也许有更简单的方法来实现我的目标,即定义一个索引 4x4变换矩阵一个4杆连杆。除了使我的代码中的序列遍历序列{A ... A }之外,我没有对维度的任何迫切需求。当使用张量数组时,在SymPy中输入错误

下面的代码生成以下错误。

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

在另一方面,像A0更换A[:,:,i]下方并产生一个4x4的阵列的符号如预期,所以也许我没有正确定义片 - 但是,没有其他的片排列似乎工作。 (我也有与代码的最后一行隐含行延续的问题,所以我把一切都在线。)

我会很感激在如何解决这个问题的任何建议。

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

分配给SymPy阵列片丢失,您可以通过使用numpy的去解决这个不足。

导入NumPy的为:

import numpy 

的SymPy阵列转换为NumPy的阵列(类型的对象,从而包含SymPy表达式):

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

运行的一个修改后的版本循环:

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]] 
    ...:  

将生成的NumPy数组转换回SymPy数组:

In [20]: A = Array(A_numpy) 

打印它的内容:

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⎦⎦ 
+0

这是完美的;这正是我所需要的。 非常感谢! –

相关问题