Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将Python嵌入C++;调用两个函数并传递数组 我试图将Python函数嵌入到C++代码中,进行一些符号和代数运算。_C++_Numpy_Compiler Errors_Sympy_Python Embedding - Fatal编程技术网

将Python嵌入C++;调用两个函数并传递数组 我试图将Python函数嵌入到C++代码中,进行一些符号和代数运算。

将Python嵌入C++;调用两个函数并传递数组 我试图将Python函数嵌入到C++代码中,进行一些符号和代数运算。,c++,numpy,compiler-errors,sympy,python-embedding,C++,Numpy,Compiler Errors,Sympy,Python Embedding,我的想法是: 我想根据一些输入变量创建一个带有符号变量的矩阵。这个矩阵我想在以后的另一个函数中使用。此函数用于从符号矩阵生成一些坐标。然后我想把这个结果传递给我的C++代码。 我的问题是: 我有一个问题,要把这个矩阵从Python中导出到C++代码中,并返回到另一个函数作为输入。因为我想把符号矩阵通过C++传递给另一个Python函数,它应该产生这个矩阵的结果。只需尝试我的Python代码,并将其集成到C++中,我就编写了下面的代码。 编辑: 为了使我的问题更具体,我用Python编写了我想用这

我的想法是: 我想根据一些输入变量创建一个带有符号变量的矩阵。这个矩阵我想在以后的另一个函数中使用。此函数用于从符号矩阵生成一些坐标。然后我想把这个结果传递给我的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()
来自sympy导入的
*
将numpy作为np导入
def main():
开始位置=[1,2,3]
结束位置=[4,5,6]
通用函数=生成符号转换(开始位置、结束位置)
计算符号变换(通用函数,90,0.5);
#根据输入计算符号变换
def计算符号转换(通用函数、日期值、k值):
thetay=符号(“thetay”)
k=符号('k')
变换矩阵=矩阵符号('tm',4,4)
变换矩阵=矩阵(变换矩阵)
transf_矩阵=同构(通用函数)
transf_矩阵=transf_矩阵.subs([(日期,日期值),(k,k值)])
打印转换矩阵
返回1
#根据输入生成符号变换
def生成符号转换(开始位置坐标、结束位置坐标):
#符号起始位置
开始位置=矩阵符号('S',3,1)
开始位置=矩阵(开始位置)
开始位置[0]=开始位置坐标[0]
开始位置[1]=开始位置坐标[1]
开始位置[2]=开始位置坐标[2]
打印开始位置
#符号结束位置
End_pos=矩阵符号('E',3,1)
结束位置=矩阵(结束位置)
结束位置[0]=结束位置坐标[0]
结束位置[1]=结束位置坐标[1]
结束位置[2]=结束位置坐标[2]
打印结束位置
#符号旋转矩阵
R=矩阵符号('R',3,3)
#符号变换矩阵
T=矩阵符号('T',4,4)
#必要符号变量
k=符号('k')
θ=符号(‘θ’)
thetay=符号(“thetay”)
θ=符号(‘θ’)
#对于EulerAngles RzRyRx的旋转:
Rx=矩阵符号('Rx',3,3)
Ry=矩阵符号('Ry',3,3)
Rz=矩阵符号('Rz',3,3)
#填充旋转矩阵
#   |   1           0                   0         |
#| 0-cos(thetax)sin(thetax)|
#| 0 sin(thetax)cos(thetax)|
Rx=矩阵(Rx)
Rx[0,0]=1
Rx[0,1]=0
Rx[0,2]=0
Rx[1,0]=0
Rx[1,1]=cos(θ)
Rx[1,2]=-sin(θ)
Rx[2,0]=0
Rx[2,1]=sin(θ)
Rx[2,2]=cos(θ)
#旋转矩阵
#| cos(thetay)0 sin(thetay)|
#   |          0            1           0          |
#|-sin(thetay)0 cos(thetay)|
Ry=矩阵(Ry)
Ry[0,0]=cos(天)
Ry[0,1]=0
Ry[0,2]=sin(天)
Ry[1,0]=0
Ry[1,1]=1
Ry[1,2]=0
Ry[2,0]=-sin(天)
Ry[2,1]=0
Ry[2,2]=cos(thetay)
#填充Rz旋转矩阵
#| cos(thetaz)-sin(thetaz)0|
#| sin(thetaz)cos(thetaz)0|
#   |          0            0            1     |
Rz=矩阵(Rz)
Rz[0,0]=cos(θ)
Rz[0,1]=-sin(θ)
Rz[0,2]=0
Rz[1,0]=sin(θ)
Rz[1,1]=cos(θ)
Rz[1,2]=0
Rz[2,0]=0
Rz[2,1]=0
Rz[2,2]=1
#旋转矩阵的生成
R=Rz*Ry*Rx
#生成线性平移
#符号三维线函数
翻译=矩阵符号('Tl',3,1)
翻译=开始位置+k*(结束位置-开始位置)
#将其集成到转换矩阵中
#| R T|
#   |   000     1    |
T=矩阵(T)
i=0
对于范围(4)内的r:
对于范围(4)内的c:
如果(c<3和r<3):
T[r,c]=r[r,c]
elif(c==3,r<3):
T[r,c]=翻译[i]
++我
elif(c<3,r==3):
T[r,c]=0
其他:
T[r,c]=1
##将创建的带有符号变量的矩阵保存到全局对象中
T=T.subs([(θ,0),(θ,0)])
返回T
如果名称=“\uuuuu main\uuuuuuuu”:
main()

这里有几个错误:

  • Python函数
    generate\u symbolic\u transformation
    抛出一个异常,使
    resultObj
    be
    NULL
    。这会进一步传播并导致崩溃

  • 即使
    resultObj
    不是
    NULL
    ,它也不会被正确地返回给调用者,因为
    CallPlugIn\u生成\u符号\u转换的最后两行
    确保只在
    NULL
    时返回值

  • 但这些都是具体的问题。我还想提出一些一般性建议,帮助您尽早发现问题,节省时间和精力:

  • 处理Python错误。至少,如果Python C/API函数返回
    NULL
    ,则打印错误。例如:
  • 
    如果(!resultObj)
    {
    PyErr_Print();
    }
    

    这将导致类似以下内容的错误消息:

    回溯(最近一次调用上次):文件 “/home/sterin/ClionProjects/numpy1/numpy_test.py”,第15行,in 生成