Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Figure out how to allow the x**2+y**2 syntax with symbolic expressions #4

Closed
certik opened this issue Sep 28, 2020 · 7 comments
Closed

Comments

@certik
Copy link
Contributor

certik commented Sep 28, 2020

@ivan-pi posted an example here:

https://fortran-lang.discourse.group/t/code-generation-using-sympy/321/5

use symengine

type(sym_t) :: x, y
type(expr_t) :: f

call f%set([x,y], x**2 + y**2)
call fortran_function(f,'File','myfile')
end

I assume one must overload the + and ** operators, and then make it work with the sym_t type (I would just call it sym, as in Matlab). One issue is about ensuring it does not leak memory, i.e. in: c = a + b + c, it must automatically deallocate the intermediate expressions. The same in x**2+y**2, the operator ** first creates x**2, but then when you do x**2+y**2, it must deallocate the x**2. In C++ and in Python, when the expression goes out of scope, its destructor gets called which deallocates the memory.

@isuruf
Copy link
Member

isuruf commented Sep 28, 2020

Btw, why do you need to generate Fortran code if you are using symengine in Fortran? Isn't lambdify enough?

@ivan-pi
Copy link
Member

ivan-pi commented Sep 28, 2020

Does lambdify create a C++ function behind the scenes? Right now, I don't see how it can be pulled in to Fortran, since the Fortran interface should be available at compile time. I'm assuming it is possible to have same kind of dynamic Fortran object with a handle to a type(c_ptr) object, that would do some magic in C/C++ behind the scenes.

Fortran 2018 also opened a path to access allocatable and pointer entities from C, using the "ISO_Fortran_Binding.h" header file. I imagine this can also be used in some situations to provide a "native" feel to any kind of Fortran interface.

For the reason to do it in the first place, @certik wrote:

The overall idea is that in Matlab you don’t need to switch to another language (Python or Mathematica) to do symbolics, you simply use Matlab for everything: numerics, plotting, symbolics and code generation. In the same way, we could use Fortran down the road for all these tasks also. Until we do it, I can’t of course guarantee that it will be easy to use and natural, but if Matlab can do it, then I think Fortran (as a language) can do it too, as easily.

@isuruf
Copy link
Member

isuruf commented Sep 28, 2020

https://github.com/symengine/symengine/blob/065b7a9dbebaadee1547f1b9c472b7b76d935eea/symengine/cwrapper.h#L711-L713 creates a C++ function and stores it in the CLambdaRealDoubleVisitor object and you can call that function by using https://github.com/symengine/symengine/blob/065b7a9dbebaadee1547f1b9c472b7b76d935eea/symengine/cwrapper.h#L714-L716
This is a C interface, so you can easily call that from Fortran.

CLambdaRealDoubleVisitor creates a C++ lambda function behind the scenes and CLLVMDoubleVisitor compiles the function using LLVM behind the scenes, but these are implementation details you don't have to worry about because the interface is C.

@certik
Copy link
Contributor Author

certik commented Sep 28, 2020

@isuruf I think @ivan-pi's original use case was to write a script that generates Fortran code that gets compiled. It's just that instead of using Python one would use Fortran.

The use case you have in mind is to simply generate things on the fly using LLVM, and I think that would be useful too. The only disadvantage is that one then must link with SymEngine and LLVM in a production code, which might or might not be desirable.

@ivan-pi
Copy link
Member

ivan-pi commented Sep 29, 2020

Thanks @isuruf for the explanation. Since I am not familar with the API, I would have missed those functions entirely. I've managed to get a small example working in #3.

@rikardn
Copy link
Contributor

rikardn commented Sep 19, 2021

This issue has been addressed in #9

@rikardn
Copy link
Contributor

rikardn commented May 28, 2022

This now works after the merging of #9

@rikardn rikardn closed this as completed May 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants