jannikmi / multivar_horner

python package implementing a multivariate Horner scheme for efficiently evaluating multivariate polynomials
https://multivar-horner.readthedocs.io/en/latest/
MIT License
27 stars 3 forks source link

export found horner factorisation to codes #23

Closed Cfather closed 2 years ago

Cfather commented 3 years ago

Hi, I wonder if there's any way to convert the string representation of a found horner factorisation to codes. To be more specific, I'm looking for some functions similar to 'ccode' in Matlab or 'export' in Mathematica. For example, 'x_1 (x_1 (x_1 (1.0 x_2) + 2.0 x_3) + 3.0 x_2 x_3) + 5.0' can give me

t1 = x_2 x_3; t2 = x_1 x_2 + 2.0 x_3; t3 = x_1 t2 + 3.0 t1; res = x_1 t3 + 5.0;

which I can compile it in C.

jannikmi commented 3 years ago

Thanks for bringing this up. Right now the package does not support this, but it would be a very nice feature to add. Instead of parsing and converting the string representation in some hacky way, I would propose starting with the internally stored "recipe" (as i call it in this context). This is an array representation of all the operations which need to be performed for evaluating the polynomial. This is the function performing the evaluation of a recipe: https://github.com/jannikmi/multivar_horner/blob/da27c954414960884d68fcfecc9c6817aaec3fd7/multivar_horner/helpers_fcts_numba.py#L48 Perhaps you could rewrite this function into compiling a string of operations in C syntax and exporting that into a .c file. I would really appreciate a PR for this as this would surely increase the evaluation performance. Feel free to ask questions if you have any doubts.

jannikmi commented 2 years ago

@Cfather have you started working on this? I might look into this soon, when I find time. A PR (even if incomplete) could help me get going. Let me know

Cfather commented 2 years ago

I'm sorry I finally switched to another route to evaluate my polynomials. So I haven't been working on that. But I'm very interested in this problem and believe it could be really useful in many areas. Please let me know if you need anything.

jannikmi commented 2 years ago

No worries. From your GitHub name I guess you are more than familiar with C so I'll let you review the code (and .c files i generate) :blush:

jannikmi commented 2 years ago

here is my current progress on this: https://github.com/jannikmi/multivar_horner/pull/24 evaluation already works with an automatically compiled C file (if gcc compiler is installed) and with poly.get_c_instructions() you can fetch the content of the corresponding file

Cfather commented 2 years ago

Thank you very much for your update!!! I will try my best to take a look when I have the time. Sorry, it's near the end of a crazy semester right now.

jannikmi commented 2 years ago

No worries. Just post here whenever you have time. The functionality is now fully implemented. Looking forward to any criticism or suggestions!