Skip to content
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

Add Symbolic number constructor and conversion #602

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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{T}(x::Number) where {T}
Term{T}(identity, [convert(T, x)])
end
function Symbolic(x::T) where {T<:Number}
Symbolic{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
41 changes: 41 additions & 0 deletions test/basics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,44 @@ 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}
expected = Term{Int}(identity, [1])
@test isequal(Symbolic(1), expected)
@test isequal(convert(Symbolic{Int}, 1), expected)
@test isequal(convert(BasicSymbolic{Int}, 1), expected)
end

@testset "Float64" begin
@test Symbolic(1.0) isa Symbolic{Float64}
expected = Term{Float64}(identity, [1.0])
@test isequal(Symbolic(1.0), expected)
@test isequal(convert(Symbolic{Float64}, 1.0), expected)
@test isequal(convert(BasicSymbolic{Float64}, 1.0), expected)
end

@testset "Rational" begin
@test Symbolic(1//2) isa Symbolic{Rational{Int}}
expected = Term{Rational{Int}}(identity, [1//2])
@test isequal(Symbolic(1//2), expected)
@test isequal(convert(Symbolic{Rational{Int}}, 1//2), expected)
@test isequal(convert(BasicSymbolic{Rational{Int}}, 1//2), expected)
end

@testset "Complex" begin
@test Symbolic(1 + 2im) isa Symbolic{Complex{Int}}
expected = Term{Complex{Int}}(identity, [1 + 2im])
@test isequal(Symbolic(1 + 2im), expected)
@test isequal(convert(Symbolic{Complex{Int}}, 1 + 2im), expected)
@test isequal(convert(BasicSymbolic{Complex{Int}}, 1 + 2im), expected)
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
Loading