【发布时间】:2015-07-27 07:35:05
【问题描述】:
我正在尝试将 python 函数嵌入到我的 c++ 代码中,以进行一些符号和代数运算。
我的想法: 我想根据一些输入变量创建一个带有符号变量的矩阵。这个矩阵我想在另一个函数中使用。此函数正在从符号矩阵中生成一些坐标。然后我想将此结果传递给我的 c++ 代码。
我的问题: 我在将这个矩阵从 python 中获取到我的 c++ 代码并返回到另一个函数作为输入时遇到了问题。因为我想通过 c++ 将符号矩阵传递给另一个应该从这个矩阵生成结果的 python 函数。只是为了尝试我的 python 代码和与 c++ 的集成,我编写了下面的代码。
编辑: 为了使我的问题更具体,我用 Python 编写了我想用这个函数做的全部工作。为了澄清,我需要一个生成符号矩阵的函数,另一个必须计算值。对于计算,这些值来自我的 C++ 代码内部。
这里是用 Python 编写的示例:
from sympy import *
import numpy as np
def main():
start_pos = [1,2,3]
end_pos = [4,5,6]
generic_function = generate_symbolic_transformation(start_pos,end_pos)
calculate_symbolic_transformation(generic_function,90,0.5);
# Calculation of the symbolic transformation depending on the input
def calculate_symbolic_transformation(generic_function,thetay_value,k_value):
thetay = symbols('thetay')
k = symbols('k')
transf_matrix = MatrixSymbol('tm',4,4)
transf_matrix = Matrix(transf_matrix)
transf_matrix = sympify(generic_function)
transf_matrix = transf_matrix.subs([(thetay,thetay_value),(k,k_value)])
print transf_matrix
return 1
# Generation of the symbolic transformation depending on the input
def generate_symbolic_transformation(Start_pos_coords,End_pos_coords):
# Symbolic startposition
Start_pos = MatrixSymbol('S',3,1)
Start_pos = Matrix(Start_pos)
Start_pos[0] = Start_pos_coords[0]
Start_pos[1] = Start_pos_coords[1]
Start_pos[2] = Start_pos_coords[2]
print Start_pos
# Symbolic endposition
End_pos = MatrixSymbol('E',3,1)
End_pos = Matrix(End_pos)
End_pos[0] = End_pos_coords[0]
End_pos[1] = End_pos_coords[1]
End_pos[2] = End_pos_coords[2]
print End_pos
# Symbolic rotation matric
R = MatrixSymbol('R',3,3)
# Symbolic transformation matric
T = MatrixSymbol('T',4,4)
# Necessary symbolic variabls
k = symbols('k')
thetax = symbols('thetax')
thetay = symbols('thetay')
thetaz = symbols('thetaz')
# For rotation of EulerAngles RzRyRx:
Rx = MatrixSymbol('Rx',3,3)
Ry = MatrixSymbol('Ry',3,3)
Rz = MatrixSymbol('Rz',3,3)
# Filling Rx rotation matric
# | 1 0 0 |
# | 0 -cos(thetax) sin(thetax) |
# | 0 sin(thetax) cos(thetax) |
Rx = Matrix(Rx)
Rx[0,0] = 1
Rx[0,1] = 0
Rx[0,2] = 0
Rx[1,0] = 0
Rx[1,1] = cos(thetax)
Rx[1,2] = -sin(thetax)
Rx[2,0] = 0
Rx[2,1] = sin(thetax)
Rx[2,2] = cos(thetax)
# Filling Ry rotation matric
# | cos(thetay) 0 sin(thetay) |
# | 0 1 0 |
# | -sin(thetay) 0 cos(thetay) |
Ry = Matrix(Ry)
Ry[0,0] = cos(thetay)
Ry[0,1] = 0
Ry[0,2] = sin(thetay)
Ry[1,0] = 0
Ry[1,1] = 1
Ry[1,2] = 0
Ry[2,0] = -sin(thetay)
Ry[2,1] = 0
Ry[2,2] = cos(thetay)
# Filling Rz rotation matric
# | cos(thetaz) -sin(thetaz) 0 |
# | sin(thetaz) cos(thetaz) 0 |
# | 0 0 1 |
Rz = Matrix(Rz)
Rz[0,0] = cos(thetaz)
Rz[0,1] = -sin(thetaz)
Rz[0,2] = 0
Rz[1,0] = sin(thetaz)
Rz[1,1] = cos(thetaz)
Rz[1,2] = 0
Rz[2,0] = 0
Rz[2,1] = 0
Rz[2,2] = 1
# Generating the rotation matric
R = Rz*Ry*Rx
# Generating the linear translation
# Symbolic 3D line function
Translation = MatrixSymbol('Tl',3,1)
Translation = Start_pos + k * (End_pos-Start_pos)
# Integrate it into the transformation matric
# | R T |
# | 000 1 |
T = Matrix(T)
i=0
for r in range(4):
for c in range(4):
if (c < 3 and r < 3):
T[r,c] = R[r,c]
elif (c == 3 and r < 3):
T[r,c] = Translation[i]
++i
elif (c < 3 and r == 3):
T[r,c] = 0
else:
T[r,c] = 1
## Save the created matrics with symbolic variables into global object
T = T.subs([(thetax,0),(thetaz,0)])
return T
if __name__ == "__main__":
main()
【问题讨论】:
-
你必须提取一个最小的例子。还请修正拼写,它是 C++ 而不是 c++,是 Python 而不是 python(除非指的是动物)和 I 而不是 i(当指的是你自己时)。哦,请在网上搜索您收到的错误的含义,您的问题根本不是新问题!
-
抱歉我的拼写错误。如果我的问题不是新问题,那么我太笨了,无法找到正确的答案。我只知道我想放回 Python 函数的变量 tmatrix 存在一些问题。如果我注释掉“打印 tmatrix”,一切正常!如果在网上搜索并没有得到任何答案,我会尝试从一周开始运行它!
-
你能告诉我我的问题是什么吗?我在整个网络上搜索了一个多星期,但没有任何进展。说我的问题不是新问题是没有帮助的!
-
您需要减少导致问题的代码量。代码是否没有做它实际上应该做的事情并不重要,只要它证明了这个问题。在此处发布该最小示例代码,以便人们可以复制它。您可能会在途中发现自己的错误。此外,请确保您知道“访问冲突”的含义。最后,切换问题的内容也无济于事。如果一个问题得到解决而其他问题出现,则作为一个新问题。
标签: c++ numpy compiler-errors sympy python-embedding