1

I'm using Python(x,y) / Spyder / Sympy to do some symbolic matrix calculations, which I then want to write into my C program.

Is there a tool (Python or not) that will take this resulting matrix written in Python style and spit it out as one or more lines of (valid) C code ?

E.g. if I define

A = [[a, 0] [b, 0]]
B = [[c, d] [0, 0]]

and then ask Python to compute and print A*B then I will get

Matrix([[a*c, a*d], [b*c d*c]])

which I then want to be converted to something like :

result[0][0] = a*c;
result[0][1] = a*d;
result[1][0] = b*c;
result[1][1] = d*c;

I can get the core of it done manually in Python, simply because basic arithmetic operators are written the same in Python and in C.

But as soon as we go to powers and trigonometry, things change a little bit ... and there may be other things I can't think off the top of my head right now that will appear down the line as my matrices get more complex and use more advanced standard math functions (and trust me, they will).

So some code or tool that would automate this for me would be great.

Charles
  • 988
  • 1
  • 11
  • 28
  • Do you need an automatic tool or a simple code is also valid? – Mattia Surricchio May 12 '20 at 12:44
  • I Googled _Python to C code generator_ and surprisingly nothing really useful came up. But links for [_attempts_ at such a thing did](https://www.python.org/about/success/cog/). – ryyker May 12 '20 at 12:45
  • Don't know if it's a meaningful solution, but you could iterate over your python matrix (assuming that it's a 2 dimensional matrix), compute strings like `result[0][0] = a*c;`, store them in a variable and then write a txt file – Mattia Surricchio May 12 '20 at 12:51
  • 1
    @Mattia Surricchio I don#t understand your question. If by "simple code" you mean some Python code to do what I need, then it would also be automatic (I'd just have to place my Python matrices somwhere and run that Python code over these without doing any C-code writing manually) and so I'd be happy. – Charles May 12 '20 at 12:59
  • Yes, sorry i expressed myself poorly. I was asking if a handwritten code was okay or you were searching for an existing python tool/library (such as numpy for vectors, matplotlib for plots ecc...) – Mattia Surricchio May 12 '20 at 13:01
  • Whatever automates this for any matrix dimension is good – Charles May 12 '20 at 13:13

1 Answers1

0

Following this question on arbitrary dimensional matrix:Arbitrary matrix, i used np.ndenumerate()to iterate over the matrix.

import numpy as np

arr = np.random.random([4,5,2,6])
print(arr)
c_code = ''

for idx, value in np.ndenumerate(arr):
    print(idx, value)
    index_c = ""
    for index in idx:
        #print('print index')
        #print(index)
        index_c = index_c + '[' + str(index) + ']'

    cell_content = arr[idx]

    #print('printing content')
    #print(cell_content)

    #print('print index_c')
    #print(str(index_c))

    single_entry = ''
    single_entry = 'arr'+str(index_c)+'='+ str(cell_content)

    #print('printing single entry')
    #print(single_entry)

    c_code = c_code + str(single_entry) + ';' + '\n'

print('Printing C_code')
print(c_code)

I left some test prints.

I used arr = np.random.random([4,5,2,6]) to generate a [4,5,2,6] dimensional matrix with random values (just for testing, you can use your own matrix).

The C code for the matrix is contained inside the c_code variable, which will be something like:

arr[3][3][1][2]=0.15905407213419076;
arr[3][3][1][3]=0.11357812551781832;
arr[3][3][1][4]=0.8884023323331601;

for example.

Mattia Surricchio
  • 1,362
  • 2
  • 21
  • 49
  • I was able to do that on my own too, but what if `cell_content`contains non-C syntax like `a**2` ? – Charles May 12 '20 at 14:10
  • That's another problem. You should also implement a manual conversion over those commands. So if you get that element, you map it over a C equivalent instruction. You could get the c_code from my function (which will place the string as it is on the right side of the "=" ) iterate over it and swap all the characters you need – Mattia Surricchio May 12 '20 at 14:17
  • In any case i don't think there's an easier way than brutal mapping between the two sintaxes. – Mattia Surricchio May 12 '20 at 14:18
  • But hasn't *someone* on the globe already done it and made it available ? – Charles May 12 '20 at 14:23
  • Well, you're asking for a language translator, which is not that easy and it's one of the main problems in Computer Science. Matlab has its own Matlab to C++ converter. You can have a look at Cython, which is a Python -> C converter : https://cython.org/ – Mattia Surricchio May 12 '20 at 14:24
  • https://stackoverflow.com/questions/4650243/convert-python-program-to-c-c-code – Mattia Surricchio May 12 '20 at 14:45
  • Yes I'm trying to set Cython up but I'm encountering problems in using it. – Charles May 12 '20 at 14:51
  • I've never used Cython so i don't know how to help. Why you need to convert your Python code into C? It's a problematic task in any case – Mattia Surricchio May 12 '20 at 14:54
  • My final program has to be in C, and it is also in C that I can test the whole chain. But for now I am not set on the central math model to use, so Python and Sympy offer me great flexibility because I can just write new formulas symbolically and Sympy will compute the final matrix I need. If I could just convert it in C ... – Charles May 12 '20 at 15:40
  • This moves to a more complex problem. If your matrix can contain functions (such as sin, cos ecc...), converting them into C code won't be that easy. A simple matrix converter won't be enough, since in standard C those function are not implemented at all, you would need to write them on your own (or find some external library). – Mattia Surricchio May 12 '20 at 15:49
  • Sure, but wouldn't there be some tool that already converts to some common if not standard sin and cos C functions, like sinf and cosf from math.h for example ? And then it would be configurable so I would specify what I need. I guess what that comes down to is search and replace in a string from a dictionary ... which is not so hard in Python I suppose. – Charles May 13 '20 at 07:18