Skip to content

Commit 01946d5

Browse files
authored
Merge pull request #429 from JuliaSymbolics/s/literal
LiteralReal
2 parents e2a6f8a + 07877fb commit 01946d5

File tree

2 files changed

+42
-22
lines changed

2 files changed

+42
-22
lines changed

src/methods.jl

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,18 @@ const diadic = [max, min, hypot, atan, mod, rem, copysign,
1919
polygamma, beta, logbeta]
2020
const previously_declared_for = Set([])
2121

22+
const basic_monadic = [-, +]
23+
const basic_diadic = [+, -, *, /, //, \, ^]
2224
#################### SafeReal #########################
23-
export SafeReal
25+
export SafeReal, LiteralReal
2426

2527
# ideally the relationship should be the other way around
2628
abstract type SafeReal <: Real end
2729

30+
################### LiteralReal #######################
31+
32+
abstract type LiteralReal <: Real end
33+
2834
#######################################################
2935

3036
assert_like(f, T) = nothing
@@ -41,14 +47,13 @@ function number_methods(T, rhs1, rhs2, options=nothing)
4147
exprs = []
4248

4349
skip_basics = options !== nothing ? options == :skipbasics : false
50+
only_basics = options !== nothing ? options == :onlybasics : false
4451
skips = Meta.isexpr(options, [:vcat, :hcat, :vect]) ? Set(options.args) : []
45-
basic_monadic = [-, +]
46-
basic_diadic = [+, -, *, /, //, \, ^]
4752

4853
rhs2 = :($assert_like(f, Number, a, b); $rhs2)
4954
rhs1 = :($assert_like(f, Number, a); $rhs1)
5055

51-
for f in (skip_basics ? diadic : vcat(basic_diadic, diadic))
56+
for f in (skip_basics ? diadic : only_basics ? basic_diadic : vcat(basic_diadic, diadic))
5257
nameof(f) in skips && continue
5358
for S in previously_declared_for
5459
push!(exprs, quote
@@ -69,7 +74,7 @@ function number_methods(T, rhs1, rhs2, options=nothing)
6974
push!(exprs, expr)
7075
end
7176

72-
for f in (skip_basics ? monadic : vcat(basic_monadic, monadic))
77+
for f in (skip_basics ? monadic : only_basics ? basic_monadic : vcat(basic_monadic, monadic))
7378
nameof(f) in skips && continue
7479
push!(exprs, :((f::$(typeof(f)))(a::$T) = $rhs1))
7580
end
@@ -87,28 +92,31 @@ end
8792
@number_methods(Mul, term(f, a), term(f, a, b), skipbasics)
8893
@number_methods(Pow, term(f, a), term(f, a, b), skipbasics)
8994
@number_methods(Div, term(f, a), term(f, a, b), skipbasics)
95+
@number_methods(Sym{<:LiteralReal}, term(f, a), term(f, a, b), onlybasics)
96+
@number_methods(Term{<:LiteralReal}, term(f, a), term(f, a, b), onlybasics)
9097

9198
for f in vcat(diadic, [+, -, *, \, /, ^])
9299
@eval promote_symtype(::$(typeof(f)),
93100
T::Type{<:Number},
94101
S::Type{<:Number}) = promote_type(T, S)
95-
@eval function promote_symtype(::$(typeof(f)),
96-
T::Type{<:SafeReal},
97-
S::Type{<:Real})
98-
X = promote_type(T, Real)
99-
X == Real ? SafeReal : X
100-
end
101-
@eval function promote_symtype(::$(typeof(f)),
102-
T::Type{<:Real},
103-
S::Type{<:SafeReal})
104-
X = promote_type(Real, S)
105-
X == Real ? SafeReal : X
106-
end
107-
@eval function promote_symtype(::$(typeof(f)),
108-
T::Type{<:SafeReal},
109-
S::Type{<:SafeReal})
110-
X = promote_type(Real, Real)
111-
X == Real ? SafeReal : X
102+
for R in [SafeReal, LiteralReal]
103+
@eval function promote_symtype(::$(typeof(f)),
104+
T::Type{<:$R},
105+
S::Type{<:Real})
106+
X = promote_type(T, Real)
107+
X == Real ? $R : X
108+
end
109+
@eval function promote_symtype(::$(typeof(f)),
110+
T::Type{<:Real},
111+
S::Type{<:$R})
112+
X = promote_type(Real, S)
113+
X == Real ? $R : X
114+
end
115+
@eval function promote_symtype(::$(typeof(f)),
116+
T::Type{<:$R},
117+
S::Type{<:$R})
118+
$R
119+
end
112120
end
113121
end
114122

@@ -118,6 +126,7 @@ Base.rem2pi(x::Symbolic{<:Number}, mode::Base.RoundingMode) = term(rem2pi, x, mo
118126
for f in monadic
119127
@eval promote_symtype(::$(typeof(f)), T::Type{<:Number}) = promote_type(T, Real)
120128
@eval promote_symtype(::$(typeof(f)), T::Type{<:SafeReal}) = SafeReal
129+
@eval promote_symtype(::$(typeof(f)), T::Type{<:LiteralReal}) = LiteralReal
121130
@eval (::$(typeof(f)))(a::Symbolic{<:Number}) = term($f, a)
122131
end
123132

test/basics.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,14 @@ end
257257
@test (2.5x/3x).den == 3
258258
@test (x/3x) == 1//3
259259
end
260+
261+
@testset "LiteralReal" begin
262+
@syms x::LiteralReal y::LiteralReal z::LiteralReal
263+
@test repr(x+x) == "x + x"
264+
@test repr(x*x) == "x*x"
265+
@test repr(x*x + x*x) == "x*x + x*x"
266+
for ex in [sin(x), x+x, x*x, x\x, x/x]
267+
@test typeof(sin(x)) <: Term{LiteralReal}
268+
end
269+
@test repr(sin(x) + sin(x)) == "sin(x) + sin(x)"
270+
end

0 commit comments

Comments
 (0)