Skip to content

add True, False #292

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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions src/SymEngine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ include("dense-matrix.jl")

function __init__()
init_constants()

global TrueFalseMap = CMapBasicBasic()
TrueFalseMap[True] = ONE
TrueFalseMap[False] = ZERO
end

end
12 changes: 10 additions & 2 deletions src/mathops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ Base.one(::Type{T}) where {T<:BasicType} = BasicType(Basic(1))
## Math constants
## no oo!

for op in [:IM, :PI, :E, :EulerGamma, :Catalan, :GoldenRatio, :oo, :zoo, :NAN]
for op in [:IM, :PI, :E, :EulerGamma, :Catalan, :GoldenRatio, :oo, :zoo,
:NEGINFINITY, :NAN, :ZERO, :ONE, :MINUSONE, :True, :False]
@eval begin
const $op = Basic(C_NULL)
end
Expand Down Expand Up @@ -123,15 +124,22 @@ function init_constants()
@init_constant GoldenRatio GoldenRatio
@init_constant oo infinity
@init_constant zoo complex_infinity
@init_constant NEGINFINITY neginfinity
@init_constant NAN nan
@init_constant ZERO zero
@init_constant ONE one
@init_constant MINUSONE minus_one
ccall((:bool_set_true, libsymengine), Nothing, (Ref{Basic},), True)
ccall((:bool_set_false, libsymengine), Nothing, (Ref{Basic},), False)
end

## ## Conversions
## Conversions
Base.convert(::Type{Basic}, x::Irrational{:π}) = PI
Base.convert(::Type{Basic}, x::Irrational{:e}) = E
Base.convert(::Type{Basic}, x::Irrational{:γ}) = EulerGamma
Base.convert(::Type{Basic}, x::Irrational{:catalan}) = Catalan
Base.convert(::Type{Basic}, x::Irrational{:φ}) = (1 + Basic(5)^Basic(1//2))/2
Base.convert(::Type{Basic}, x::Bool) = x ? True : False
Base.convert(::Type{BasicType}, x::Irrational) = BasicType(convert(Basic, x))

## Logical operators
Expand Down
3 changes: 3 additions & 0 deletions src/numerics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ function evalf(b::Basic, bits::Integer=53, real::Bool=false)
if status == 0
return c
else
b′ = subs(b, TrueFalseMap) # replace any True/False with 1/0
b′ != b && return evalf(b′, bits, real)
throw(ArgumentError("symbolic value cannot be evaluated to a numeric value"))
end
end
Expand Down Expand Up @@ -91,6 +93,7 @@ N(::Val{:RealMPFR}, b::Basic) = _convert(BigFloat, b)
N(::Val{:Complex}, b::Basic) = complex(N(real(b)), N(imag(b)))
N(::Val{:ComplexMPC}, b::Basic) = complex(N(real(b)), N(imag(b)))
N(::Val{:ComplexDouble}, b::Basic) = complex(N(real(b)), N(imag(b)))
N(::Val{:BooleanAtom}, b::Basic) = b == True ? 1 : 0

N(::Val{:NaN}, b::Basic) = NaN
function N(::Val{:Infty}, b::Basic)
Expand Down
2 changes: 1 addition & 1 deletion src/subs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function _convert(::Type{Expr}, ex::Basic)

if fn == :Symbol
return nameof(ex)
elseif (fn in number_types) || (fn == :Constant)
elseif (fn in number_types) || (fn == :Constant) || (fn == :BooleanAtom)
return N(ex)
end

Expand Down
1 change: 1 addition & 0 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ Basic(x::Basic) = x
Base.promote_rule(::Type{Basic}, ::Type{S}) where {S<:Number} = Basic
Base.promote_rule(::Type{S}, ::Type{Basic}) where {S<:Number} = Basic
Base.promote_rule(::Type{S}, ::Type{Basic}) where {S<:AbstractIrrational} = Basic
Base.promote_rule(::Type{Bool}, ::Type{Basic}) = Basic

## Class ID
get_type(s::Basic) = ccall((:basic_get_type, libsymengine), UInt, (Ref{Basic},), s)
Expand Down
1 change: 0 additions & 1 deletion src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,3 @@ function have_component(comp::String)
false
end
end

7 changes: 7 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,13 @@ expr = x^3 + 3*x^2*y + 3*x*y^2 + y^3 + 1
end
end

@testset "Symbolic Booleans" begin
@test N(True) == 1
@test N(False) == 0
v = Basic(1) + True
@test N(v) == 1 + true
end

@test round(Basic(3.14)) == 3.0
@test round(Basic(3.14); digits=1) == 3.1

Expand Down
Loading