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

feat: support codegen for expressions involving arrays of symbolics #626

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/code.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import ..SymbolicUtils
import ..SymbolicUtils.Rewriters
import SymbolicUtils: @matchable, BasicSymbolic, Sym, Term, iscall, operation, arguments, issym,
symtype, sorted_arguments, metadata, isterm, term, maketerm
import SymbolicIndexingInterface: symbolic_type, NotSymbolic

##== state management ==##

Expand Down Expand Up @@ -169,13 +170,24 @@ function substitute_name(O, st)
end
end

function _is_array_of_symbolics(O)
# O is an array, not a symbolic array, and either has a non-symbolic eltype or contains elements that are
# symbolic or arrays of symbolics
return O isa AbstractArray && symbolic_type(O) == NotSymbolic() &&
(symbolic_type(eltype(O)) != NotSymbolic() ||
any(x -> symbolic_type(x) != NotSymbolic() || _is_array_of_symbolics(x), O))
end

function toexpr(O, st)
if issym(O)
O = substitute_name(O, st)
return issym(O) ? nameof(O) : toexpr(O, st)
end
O = substitute_name(O, st)

if _is_array_of_symbolics(O)
return toexpr(MakeArray(O, typeof(O)), st)
end
!iscall(O) && return O
op = operation(O)
expr′ = function_to_expr(op, O, st)
Expand Down
8 changes: 8 additions & 0 deletions test/code.jl
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,12 @@ nanmath_st.rewrites[:nanmath] = true
@test s1 == s2
end
end

let
@syms a b

t = term(sum, [a, b, a + b, 3a + 2b, sqrt(b)]; type = Number)
f = eval(toexpr(Func([a, b], [], t)))
@test f(1.0, 2.0) ≈ 13.0 + sqrt(2)
end
end
Loading