Skip to content

Commit

Permalink
Merge pull request #789 from davidschlegel/master
Browse files Browse the repository at this point in the history
Metadata functionality for ComplexTerm
  • Loading branch information
shashi authored Aug 16, 2023
2 parents ee3e491 + a5944db commit d70b4ef
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Symbolics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ using Setfield
import DomainSets: Domain
@reexport using SymbolicUtils

import SymbolicUtils: similarterm, istree, operation, arguments, symtype
import SymbolicUtils: similarterm, istree, operation, arguments, symtype, metadata

import SymbolicUtils: Term, Add, Mul, Pow, Sym, Div, BasicSymbolic,
FnType, @rule, Rewriters, substitute,
Expand Down
4 changes: 2 additions & 2 deletions src/arrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function operation(a::ArrayOp)
end
function arguments(a::ArrayOp)
isnothing(a.term) ? [a.output_idx, a.expr, a.reduce,
a.term, a.shape, a.ranges, a.metadata] :
a.term, a.shape, a.ranges, metadata(a)] :
arguments(a.term)
end

Expand Down Expand Up @@ -722,7 +722,7 @@ function scalarize(arr)
elseif arr isa Num
wrap(scalarize(unwrap(arr)))
elseif istree(arr) && symtype(arr) <: Number
t = similarterm(arr, operation(arr), map(scalarize, arguments(arr)), symtype(arr), metadata=arr.metadata)
t = similarterm(arr, operation(arr), map(scalarize, arguments(arr)), symtype(arr), metadata=metadata(arr))
istree(t) ? scalarize_op(operation(t), t) : t
else
arr
Expand Down
1 change: 1 addition & 0 deletions src/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ symtype(a::ComplexTerm{T}) where T = Complex{T}
istree(a::ComplexTerm) = true
operation(a::ComplexTerm{T}) where T = Complex{T}
arguments(a::ComplexTerm) = [a.re, a.im]
metadata(a::ComplexTerm) = a.re.metadata

function similarterm(t::ComplexTerm, f, args, symtype; metadata=nothing)
if f <: Complex
Expand Down
8 changes: 8 additions & 0 deletions test/complex.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Symbolics, Test
using SymbolicUtils: metadata

@variables a b::Real z::Complex (Z::Complex)[1:10]

Expand Down Expand Up @@ -26,3 +27,10 @@ end
@test repr(z) == "z"
@test repr(a + b*im) == "a + b*im"
end

@testset "metadata" begin
z1 = z+1.0
@test_nowarn substitute(z1, z=>1.0im)
@test metadata(z1) == z1.im.metadata
@test metadata(z1) == z1.re.metadata
end

0 comments on commit d70b4ef

Please sign in to comment.