-
Notifications
You must be signed in to change notification settings - Fork 154
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
fix: make @register_array_symbolic
overload promote_symtype
#1129
Merged
ChrisRackauckas
merged 1 commit into
JuliaSymbolics:master
from
AayushSabharwal:as/register-array-promote-symtype
May 6, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
using Symbolics | ||
import Symbolics: getsource, getdefaultval, wrap, unwrap, getname | ||
import SymbolicUtils: Term, symtype, FnType, BasicSymbolic | ||
import SymbolicUtils: Term, symtype, FnType, BasicSymbolic, promote_symtype | ||
using LinearAlgebra | ||
using Test | ||
|
||
|
@@ -9,34 +9,57 @@ Symbolics.@register_symbolic fff(t) | |
@test isequal(fff(t), Symbolics.Num(Symbolics.Term{Real}(fff, [Symbolics.value(t)]))) | ||
|
||
const SymMatrix{T,N} = Symmetric{T, AbstractArray{T, N}} | ||
many_vars = @variables t=0 a=1 x[1:4]=2 y(t)[1:4]=3 w[1:4] = 1:4 z(t)[1:4] = 2:5 p(..)[1:4] | ||
|
||
let | ||
@register_array_symbolic ggg(x::AbstractVector) begin | ||
container_type=SymMatrix | ||
size=(length(x) * 2, length(x) * 2) | ||
eltype=eltype(x) | ||
end false | ||
|
||
## @variables | ||
|
||
gg = ggg(x) | ||
|
||
@test ndims(gg) == 2 | ||
@test size(gg) == (8,8) | ||
@test eltype(gg) == Real | ||
@test symtype(unwrap(gg)) == SymMatrix{Real, 2} | ||
@test promote_symtype(ggg, symtype(unwrap(x))) == Any # no promote_symtype defined | ||
end | ||
let | ||
# redefine with promote_symtype | ||
@register_array_symbolic ggg(x::AbstractVector) begin | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's better to use a different function so that we can re-include the file. |
||
container_type=SymMatrix | ||
size=(length(x) * 2, length(x) * 2) | ||
eltype=eltype(x) | ||
end | ||
@test promote_symtype(ggg, symtype(unwrap(x))) == SymMatrix{Real} | ||
end | ||
|
||
# ndims specified | ||
@register_array_symbolic ggg(x::AbstractVector) begin | ||
container_type=SymMatrix | ||
size=(length(x) * 2, length(x) * 2) | ||
eltype=eltype(x) | ||
ndims = 2 | ||
end | ||
|
||
## @variables | ||
|
||
many_vars = @variables t=0 a=1 x[1:4]=2 y(t)[1:4]=3 w[1:4] = 1:4 z(t)[1:4] = 2:5 p(..)[1:4] | ||
|
||
@test promote_symtype(ggg, symtype(unwrap(x))) == SymMatrix{Real, 2} | ||
|
||
gg = ggg(x) | ||
|
||
@test ndims(gg) == 2 | ||
@test size(gg) == (8,8) | ||
@test eltype(gg) == Real | ||
@test symtype(unwrap(gg)) == SymMatrix{Real, 2} | ||
|
||
struct CanCallWithArray{T} | ||
params::T | ||
end | ||
|
||
ccwa = CanCallWithArray((length=10,)) | ||
@register_array_symbolic (c::CanCallWithArray)(x::AbstractArray, b::AbstractVector) begin | ||
size=(size(x, 1), length(b), c.params.length) | ||
eltype=Real | ||
end | ||
end false # without promote_symtype | ||
|
||
hh = CanCallWithArray((length=10,))(gg, x) | ||
hh = ccwa(gg, x) | ||
@test size(hh) == (8,4,10) | ||
@test eltype(hh) == Real | ||
@test isequal(arguments(unwrap(hh)), unwrap.([gg, x])) | ||
|
@@ -52,9 +75,34 @@ hh = CanCallWithArray((length=10,))(gg, x) | |
@test getdefaultval(z[3]) == 4 | ||
|
||
@test symtype(p) <: FnType{Tuple, Array{Real,1}} | ||
@test promote_symtype(ccwa, symtype(unwrap(gg)), symtype(unwrap(x))) == Any | ||
@test p(t)[1] isa Symbolics.Num | ||
|
||
|
||
struct CanCallWithArray2{T} | ||
params::T | ||
end | ||
|
||
ccwa = CanCallWithArray2((length=10,)) | ||
@register_array_symbolic (c::CanCallWithArray2)(x::AbstractArray, b::AbstractVector) begin | ||
size=(size(x, 1), length(b), c.params.length) | ||
eltype=Real | ||
end | ||
@test promote_symtype(ccwa, symtype(unwrap(gg)), symtype(unwrap(x))) == AbstractArray{Real} | ||
|
||
struct CanCallWithArray3{T} | ||
params::T | ||
end | ||
|
||
ccwa = CanCallWithArray3((length=10,)) | ||
# ndims specified | ||
@register_array_symbolic (c::CanCallWithArray3)(x::AbstractArray, b::AbstractVector) begin | ||
size=(size(x, 1), length(b), c.params.length) | ||
eltype=Real | ||
ndims = 3 | ||
end | ||
@test promote_symtype(ccwa, symtype(unwrap(gg)), symtype(unwrap(x))) == AbstractArray{Real, 3} | ||
|
||
## Wrapper types | ||
|
||
abstract type AbstractFoo{T} end | ||
|
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this
SymMatrix{Real, 2}
and the otherSymMatrix{Real}
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can't infer
ndims
if it's not specified in the macrocall, sincepromote_symtype
only gets the types of arguments to the function