Repository navigation
Add Lambdify - #17
Open
bourdin wants to merge 3 commits into
Open
Add Lambdify#17bourdin wants to merge 3 commits into
bourdin wants to merge 3 commits into
Conversation
CMake 4 no longer accepts cmake_minimum_required versions older than 3.5.
Lambdify turns expressions into numerical functions, like Lambdify in
symengine.py, using the lambda_real_double_visitor and llvm_double_visitor
C API:
call f%init([ptr(x), ptr(y)], [ptr(x**2 + y**2)], cse=.true., backend='llvm')
call f%eval(inps, outs) ! inps(nin, npts) -> outs(nout, npts)
call f%get_function(fn) ! LLVM compiled function as a procedure pointer
The LLVM backend is compiled in when SymEngine provides it
(HAVE_SYMENGINE_LLVM). Requires the get_num_inputs/outputs, call_batch and
get_function functions of the SymEngine C wrapper.
fpm does not link SymEngine by itself, and passes --link-flag before the object files, which requires --whole-archive with the GNU linker.
bourdin
marked this pull request as ready for review
October 19, 2026 16:03
Contributor
|
Please don't use the |
Contributor
Author
|
Done. I mainly wanted to make it clear that I do not take credit for writing most of the code. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
Lambdify, which turns symbolic expressions into numerical functions,like
Lambdifyin symengine.py:Depends on symengine/symengine#2191, which adds the C API this uses:
*_get_num_inputs/outputs,*_call_batch,llvm_double_visitor_get_function,and error codes from
*_init. CI builds SymEngine frommain, so it will failto link until that PR is merged. That is why this PR is a draft.
Changes
src/lambdify.F90(new modulesymengine_lambdify, exported bysymengine): theLambdifytype.init(args, exprs, cse, backend, opt_level)accepts arrays ofc_ptr(
[ptr(x), ...], likemax,finitesetandDenseMatrix) or arrays oftype(Basic).backendis'lambda'(default) or'llvm'.eval(inps, outs)evaluates at one point (rank 1) or at several points(rank 2,
inps(nin, npts),outs(nout, npts)). It checks array shapes.get_function(fn)returns the LLVM compiled function as a procedurepointer. It can be passed to other code, e.g. an ODE solver, and called
concurrently.
num_inputs(),num_outputs(),free().initagain. Assigning oneLambdifyto another stops with an errorinstead of silently sharing, and later double freeing, the C++ object.
under
#ifdef HAVE_SYMENGINE_LLVM, which CMake defines fromSymEngineConfig.cmake. Without it, the library does not reference any LLVMsymbol, and
backend='llvm'stops with a clear message.src/symengine_interface.f90:bind(c)interfaces for the lambda andLLVM double visitors. Their return codes are declared
integer(c_int),matching the C enum.
test/test_lambdify.F90: covers single and batch evaluation, multipleoutputs, CSE, re-initialization, the
type(Basic)form and, with LLVM, thellvmbackend and the procedure pointer.CMakeLists.txt: requires CMake 3.10. CMake 4 rejectscmake_minimum_required(VERSION 2.8).README.md: documentsLambdifyand how to build with fpm. fpm does notlink SymEngine by itself and passes
--link-flagbefore the object files, sothe GNU linker needs
--whole-archive. Commands for Linux and macOS are givenand tested.
Testing
Against SymEngine from symengine/symengine#2191, built with LLVM:
test_basicandtest_lambdifypass with gfortran 16 and flang 23 onmacOS arm64.
-Wall -Wextra -fcheck=allreports no warnings in the new code.free or use after free.
leaksreports nothing coming fromLambdify(onlyBasictemporaries, whichtest_basicalso leaks).test_lambdifyon macOS and inan Ubuntu 24.04 container (gfortran 13, LLVM 18), with and without
-DHAVE_SYMENGINE_LLVM.test_basiccrashes under fpm onmastertoo, because of bugs inbasic_interval, which are fixed in #18.🤖 Generated with Claude Code