Skip to content

Commit 2cf4fc1

Browse files
authored
Merge pull request #409 from gronniger/add_generic_ndims_broadcast
ndims() and broadcast() definitions for generic symbolic types
2 parents 0d1a329 + 3857617 commit 2cf4fc1

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/methods.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,6 @@ Base.literal_pow(::typeof(^), x::Symbolic{<:Number}, ::Val{p}) where {p} = Base.
155155
# Array-like operations
156156
Base.size(x::Symbolic{<:Number}) = ()
157157
Base.length(x::Symbolic{<:Number}) = 1
158-
Base.ndims(x::Symbolic{<:Number}) = 0
158+
Base.ndims(x::Symbolic{T}) where {T} = Base.ndims(T)
159+
Base.ndims(::Type{<:Symbolic{T}}) where {T} = Base.ndims(T)
160+
Base.broadcastable(x::Symbolic{T}) where {T<:Number} = Ref(x)

test/basics.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using SymbolicUtils: Sym, FnType, Term, Add, Mul, Pow, symtype, operation, arguments
1+
using SymbolicUtils: Symbolic, Sym, FnType, Term, Add, Mul, Pow, symtype, operation, arguments
22
using SymbolicUtils
33
using IfElse: ifelse
44
using Test
@@ -134,6 +134,16 @@ end
134134
@eqtest x // a == x / a
135135
end
136136

137+
@testset "array-like operations" begin
138+
abstract type SquareDummy end
139+
Base.:*(a::Symbolic{SquareDummy}, b) = b^2
140+
@syms s t a::SquareDummy A[1:2, 1:2]
141+
142+
@test isequal(ndims(A), 2)
143+
@test_broken isequal(a.*[1 (s+t); t pi], [1 (s+t)^2; t^2 pi^2])
144+
@test isequal(s.*[1 (s+t); t pi], [s s*(s+t); s*t s*pi])
145+
end
146+
137147
@testset "err test" begin
138148
@syms t()
139149
@test_throws ErrorException t(2)

0 commit comments

Comments
 (0)