PolyLib包装器

时间:2019-06-19 00:39:40

标签: python anaconda

我有一个python代码,我想通过使用julia调用它来在PyCall中使用它,但是当我在anaconda(Spyder或Jupyter笔记本)中运行此代码时,它显示了我此The kernel appears to have died. It will restart automatically. 您会在下面找到代码。

我试图删除一些行以知道问题出在哪里,但是我什么也没得到。

python
from ctypes import *
import numpy as np

polylib = cdll.LoadLibrary("libpolylib64.so")

class Matrix(Structure):
    _fields_ = [
        ("NbRows", c_uint32),
        ("NbColumns", c_uint32),
        ("p", POINTER(POINTER(c_int64))),
        ("p_Init", POINTER(c_int64)),
        ("p_Init_size", c_int32)
    ]

def smith_normal_form(A):
    pM0 = polylib.Matrix_Alloc(3, 3)
    M0 = Matrix.from_address(pM0);
    pM = pointer(M0)
    pU = POINTER(Matrix)()
    pV = POINTER(Matrix)()
    pP = POINTER(Matrix)()
    M = np.ctypeslib.as_array(M0.p_Init, shape=(3,3))
    M[:] = A
    polylib.Smith.argtypes=[POINTER(Matrix), POINTER(POINTER(Matrix)), POINTER(POINTER(Matrix)), POINTER(POINTER(Matrix))]
    polylib.Smith(pM, pointer(pU), pointer(pV), pointer(pP))
    U = np.ctypeslib.as_array(pU.contents.p_Init, shape=(3,3))
    V = np.ctypeslib.as_array(pV.contents.p_Init, shape=(3,3))
    P = np.ctypeslib.as_array(pP.contents.p_Init, shape=(3,3))

    return U, P, V

A = np.array([[1,2,3],[-3,2,0],[1,0,0]])
U, P, V = smith_normal_form(A)
print(U.dot(P).dot(V)-A)

但是当我在pycharm中运行它时,它可以工作,并且向我显示了这是3x3矩阵的初始化的输出:

[[0 0 0]
 [0 0 0]
 [0 0 0]]

0 个答案:

没有答案