Python Forum
Python/C API Matrix to Numpy
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python/C API Matrix to Numpy
#1
I am running Python3.6

Running similar example from
https://docs.python.org/3.6/extending/em...-embedding

I want to pass in a matrix into a Python program from a Python/C API program. The Python program will then convert the matrix into a Numpy array.

For example
Python Program:
import numpy as np

def mat_vector_mul(aP):
    #aP np.array([[ 5, 1 ,3],
    #            [ 1, 1 ,1],
    #            [ 1, 2 ,1]])
    a = np.array([[aP])
    b = np.array([1, 2, 3])
    print(a.dot(b))
Python/C API program calling Python Program mat_vector_mul function.
//https://docs.scipy.org/doc/numpy/reference/generated/numpy.matmul.html
#include</usr/include/python3.6m/Python.h>

int main(){

  PyObject *pFunc, *pValue, *pModule, *pNumpyArray;

  Py_Initialize();

  PyRun_SimpleString("import sys");
  PyRun_SimpleString("sys.path.append(\".\")");

  pModule = PyImport_ImportModule("mat_vector");
  

  if(pModule){
    pFunc = PyObject_GetAttrString(pModule,"mat_vector_mul");
    pNumpyArray = PyObject_GetAttrString(pModule,"[5, 1, 3],
                                                  [1, 1, 1],
                                                  [1, 2, 1]");
    
    Py_DECREF(pModule);
    if(pFunc && PyCallable_Check(pFunc)){
        printf("[+] Function is Is callable\n");

    }

  }else{
    printf("[-] Module not loaded\n");
  }

  printf("Here\n");
  pValue = PyObject_CallFunctionObjArgs(pFunc,pNumpyArray,NULL);
  if( pValue != NULL){
    printf("[+] Success\n");
    Py_DECREF(pFunc);
    Py_DECREF(pValue);
    Py_DECREF(pNumpyArray);

  }else{
    Py_DECREF(pFunc);
    Py_DECREF(pValue);
    printf("[-] Error\n");

    }


  Py_Finalize();


  return 0;
}
The Python/C API program will call the Python program and pass in a matrix. The Python program will then convert argument into Numpy array.

Is this possible? Will I have to make this myself in C then convert it into a PyList?

Please advise
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Numpy] How to store different data type in one numpy array? water 7 299 Mar-26-2024, 02:18 PM
Last Post: snippsat
  Numpy returns "TypeError: unsupported operand type(s) for *: 'numpy.ufunc' and 'int'" kalle 2 2,530 Jul-19-2022, 06:31 AM
Last Post: paul18fr
  convert 'A B C' to numpy float matrix rezabma 4 2,491 Feb-27-2020, 09:48 AM
Last Post: rezabma
  Add column to numpy matrix Gigux 1 4,406 Aug-02-2019, 12:05 AM
Last Post: scidam
  "erlarge" a numpy-matrix to numpy-array PhysChem 2 2,933 Apr-09-2019, 04:54 PM
Last Post: PhysChem
  counting the occurence of a specified number in a numpy-matrix PhysChem 1 2,387 Apr-03-2019, 01:37 PM
Last Post: PhysChem
  python 3 and unique matrix frame 4 3,045 Sep-20-2018, 11:31 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020