Add promote_symtype for ComposedFunction (fixes #650)#859
Add promote_symtype for ComposedFunction (fixes #650)#859AayushSabharwal merged 2 commits intoJuliaSymbolics:masterfrom
Conversation
Benchmark Results (Julia vlts)Time benchmarks
Memory benchmarks
|
Benchmark Results (Julia v1)Time benchmarks
Memory benchmarks
|
AayushSabharwal
left a comment
There was a problem hiding this comment.
Thanks for the contribution! Your tests look pretty good. However, some changes are necessary here. I've outlined them below. Once they're addressed, please feel free to ping me again for a review.
src/methods.jl
Outdated
| """ | ||
| function promote_symtype(f::Base.ComposedFunction, arg_symtypes...) | ||
| # Unwrap the composition into a vector [innermost, ..., outermost] | ||
| funcs = Base.unwrap_composed(f) |
There was a problem hiding this comment.
Base.unwrap_composed is internal. This approach also makes every subsequent promote_symtype in this function a dynamic dispatch, since Julia cannot infer the type of each element in funcs. ComposedFunction documents its fields: the wrapped functions can be obtained as .inner and .outer. A better approach would be to call promote_symtype on .inner with arg_symtypes, and pass the result of that to promote_symtype on .outer.
There was a problem hiding this comment.
Done
src/methods.jl
Outdated
| the argument to the next function. Multi-argument returns (tuples) are not | ||
| currently supported but could be added if needed. | ||
| """ | ||
| function promote_symtype(f::Base.ComposedFunction, arg_symtypes...) |
There was a problem hiding this comment.
| function promote_symtype(f::Base.ComposedFunction, arg_symtypes...) | |
| function promote_symtype(f::Base.ComposedFunction, arg_symtypes::TypeT...) |
There was a problem hiding this comment.
Done
|
Changes implemented as suggested . Replaced Base.unwrap_composed with the recursive .inner/.outer approach. |
540c557
into
JuliaSymbolics:master
Summary
Adds
promote_symtypesupport for composed functions (e.g.,sin ∘ sqrt).Problem
When you compose functions with
∘, SymbolicUtils doesn't know how to infer the result type, so it returnsAnyinstead of the actual type.Solution
Unwrap the composition and fold type information from inner to outer function.
Before:
After:
Testing
Added tests for 2, 3, and 4-function compositions with different input types. All pass.
Closes #650