Skip to content

Commit 2cd86fc

Browse files
authored
Merge pull request #663 from SciML/s/fix-register
Wrap registered fn output in Num if any input is Num
2 parents 32056f7 + 555cc22 commit 2cd86fc

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/register_function.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ macro register(expr, Ts = [Num, Symbolic, Real])
1717
name(x::Expr) = ((@assert x.head == :(::)); :($value($(x.args[1]))))
1818

1919
Expr(:block,
20-
[:($f($(setinds(args, symbolic_args, ts)...)) = Term{Real}($f, [$(map(name, args)...)]))
20+
[quote
21+
function $f($(setinds(args, symbolic_args, ts)...))
22+
wrap = any(x->typeof(x) <: Num, tuple($(setinds(args, symbolic_args, ts)...),)) ? Num : identity
23+
wrap(Term{Real}($f, [$(map(name, args)...)]))
24+
end
25+
end
2126
for ts in types]...) |> esc
2227
end
2328

test/function_registration.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,9 @@ function run_test()
110110
@test fun([0.5], [u0], 0.) == [do_something_4(u0) * 2]
111111
end
112112
run_test()
113+
114+
using ModelingToolkit: arguments
115+
@variables a
116+
@register foo(x,y,z)
117+
@test 1 * foo(a,a,a) * Num(1) isa Num
118+
@test !any(x->x isa Num, arguments(value(1 * foo(a,a,a) * Num(1))))

0 commit comments

Comments
 (0)