Skip to content

Commit

Permalink
Add symbolic number constructor and conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
bowenszhu committed May 25, 2024
1 parent 38ec2bb commit 093fbbe
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,57 @@ function basic_similarterm(t, f, args, stype; metadata=nothing)
end
end

"""
$(TYPEDSIGNATURES)
Construct a [`Symbolic`](@ref) object from a number `x`.
This function is used to create a symbolic representation of a number.
```jldoctest
julia> a = SymbolicUtils.Symbolic(3.14)
3.14
julia> typeof(a)
SymbolicUtils.BasicSymbolic{Float64}
julia> b = SymbolicUtils.Symbolic{Real}(6.28)
6.28
julia> typeof(b)
SymbolicUtils.BasicSymbolic{Real}
```
"""
function Symbolic(x::T) where {T<:Number}
Term{T}(identity, [x])
end
function Symbolic{T}(x::Number) where {T}
Term{T}(identity, [convert(T, x)])
end

"""
$(TYPEDSIGNATURES)
Convert a number `x` to a [`Symbolic`](@ref) object.
This function is used to convert a number to a symbolic representation.
It is called, for example, when assigning to an array (converts to the array's
element type) or assigning to a field of an object (converts to the declared type
of the field).
# Examples
```jldoctest
julia> a = convert(SymbolicUtils.BasicSymbolic{Real}, 3.14)
3.14
julia> typeof(a)
SymbolicUtils.BasicSymbolic{Real}
```
"""
function Base.convert(::Type{<:Symbolic{T}}, x::Number) where {T}
Symbolic{T}(x)
end

###
### Metadata
###
Expand Down
37 changes: 37 additions & 0 deletions test/basics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,40 @@ end
end
@test repr(sin(x) + sin(x)) == "sin(x) + sin(x)"
end

@testset "Symbolic Number Constructor and Conversion" begin
@testset "Int" begin
@test Symbolic(1) isa Symbolic{Int}
@test isequal(Symbolic(1), Symbolic{Int}(1))
@test isequal(convert(Symbolic{Int}, 1), Symbolic{Int}(1))
@test isequal(convert(BasicSymbolic{Int}, 1), Symbolic{Int}(1))
end

@testset "Float64" begin
@test Symbolic(1.0) isa Symbolic{Float64}
@test isequal(Symbolic(1.0), Symbolic{Float64}(1.0))
@test isequal(convert(Symbolic{Float64}, 1.0), Symbolic{Float64}(1.0))
@test isequal(convert(BasicSymbolic{Float64}, 1.0), Symbolic{Float64}(1.0))
end

@testset "Rational" begin
@test Symbolic(1//2) isa Symbolic{Rational{Int}}
@test isequal(Symbolic(1//2), Symbolic{Rational{Int}}(1//2))
@test isequal(convert(Symbolic{Rational{Int}}, 1//2), Symbolic{Rational{Int}}(1//2))
@test isequal(convert(BasicSymbolic{Rational{Int}}, 1//2), Symbolic{Rational{Int}}(1//2))
end

@testset "Complex" begin
@test Symbolic(1 + 2im) isa Symbolic{Complex{Int}}
@test isequal(Symbolic(1 + 2im), Symbolic{Complex{Int}}(1 + 2im))
@test isequal(convert(Symbolic{Complex{Int}}, 1 + 2im), Symbolic{Complex{Int}}(1 + 2im))
@test isequal(convert(BasicSymbolic{Complex{Int}}, 1 + 2im), Symbolic{Complex{Int}}(1 + 2im))
end

@testset "Assigning Number to Array" begin
@syms x y
arr = [x, y]
@test_nowarn arr[1] = 2
@test_nowarn arr[2] = 1.0
end
end

0 comments on commit 093fbbe

Please sign in to comment.