Skip to content

Commit 562f015

Browse files
Merge pull request #5468 from JuliaLang/sk/no-imaginary
No more ImaginaryUnit: `const im = Complex(false,true)`.
2 parents a196cf0 + 7bd82cf commit 562f015

File tree

4 files changed

+45
-70
lines changed

4 files changed

+45
-70
lines changed

base/bool.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ abs2(x::Bool) = x
4242
^(x::Bool, y::Bool) = x|!y
4343
^(x::Integer, y::Bool) = y ? x : one(x)
4444

45+
function *{T<:Number}(x::Bool, y::T)
46+
S = promote_type(Bool,T)
47+
z = convert(S,0)
48+
z = ifelse(signbit(y)==0, z, -z)
49+
ifelse(x, convert(S,y), z)
50+
end
51+
*(y::Number, x::Bool) = x * y
52+
4553
div(x::Bool, y::Bool) = y ? x : throw(DivideError())
4654
fld(x::Bool, y::Bool) = div(x,y)
4755
rem(x::Bool, y::Bool) = y ? false : throw(DivideError())

base/complex.jl

Lines changed: 37 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,41 @@ end
55
Complex(x::Real, y::Real) = Complex(promote(x,y)...)
66
Complex(x::Real) = Complex(x, zero(x))
77

8+
const im = Complex(false,true)
9+
810
typealias Complex128 Complex{Float64}
911
typealias Complex64 Complex{Float32}
1012
typealias Complex32 Complex{Float16}
1113

12-
sizeof(::Type{Complex128}) = 16
13-
sizeof(::Type{Complex64}) = 8
14-
sizeof(::Type{Complex32}) = 4
14+
sizeof{T<:Real}(::Type{Complex{T}}) = 2*sizeof(T)
1515

16-
real(z::Complex) = z.re
17-
imag(z::Complex) = z.im
18-
real(x::Real) = x
19-
imag(x::Real) = zero(x)
16+
convert{T<:Real}(::Type{Complex{T}}, x::Real) = Complex{T}(x,0)
17+
convert{T<:Real}(::Type{Complex{T}}, z::Complex) = Complex{T}(real(z),imag(z))
18+
convert{T<:Real}(::Type{T}, z::Complex) =
19+
isreal(z) ? convert(T,real(z)) : throw(InexactError())
2020

21-
convert{T<:Real}(::Type{Complex{T}}, x::Real) =
22-
Complex{T}(convert(T,x), convert(T,0))
23-
convert{T<:Real}(::Type{Complex{T}}, z::Complex{T}) = z
24-
convert{T<:Real}(::Type{Complex{T}}, z::Complex) =
25-
Complex{T}(convert(T,real(z)),convert(T,imag(z)))
26-
27-
convert{T<:Real}(::Type{T}, z::Complex) = (imag(z)==0 ? convert(T,real(z)) :
28-
throw(InexactError()))
21+
convert(::Type{Complex}, z::Complex) = z
22+
convert(::Type{Complex}, x::Real) = Complex(x)
2923

3024
promote_rule{T<:Real,S<:Real}(::Type{Complex{T}}, ::Type{S}) =
3125
Complex{promote_type(T,S)}
3226
promote_rule{T<:Real,S<:Real}(::Type{Complex{T}}, ::Type{Complex{S}}) =
3327
Complex{promote_type(T,S)}
3428

35-
complex(x, y) = Complex(x, y)
36-
complex(x) = Complex(x)
29+
real(z::Complex) = z.re
30+
imag(z::Complex) = z.im
31+
real(x::Real) = x
32+
imag(x::Real) = zero(x)
33+
reim(z) = (real(z), imag(z))
34+
35+
isreal(x::Real) = true
36+
isreal(z::Complex) = imag(z) == 0
37+
isimag(z::Number) = real(z) == 0
38+
isinteger(z::Complex) = isreal(z) && isinteger(real(z))
39+
isfinite(z::Complex) = isfinite(real(z)) && isfinite(imag(z))
40+
41+
complex(x::Real, y::Real) = Complex(x, y)
42+
complex(x::Real) = Complex(x)
3743
complex(z::Complex) = z
3844

3945
complex128(r::Float64, i::Float64) = Complex{Float64}(r, i)
@@ -50,32 +56,23 @@ for fn in _numeric_conversion_func_names
5056
@eval $fn(z::Complex) = complex($fn(real(z)),$fn(imag(z)))
5157
end
5258

53-
isreal{T<:Real}(z::Complex{T}) = imag(z) == 0
54-
isinteger(z::Complex) = isreal(z) && isinteger(real(z))
55-
56-
isfinite(z::Complex) = isfinite(real(z)) && isfinite(imag(z))
57-
reim(z) = (real(z), imag(z))
58-
5959
function complex_show(io::IO, z::Complex, compact::Bool)
6060
r, i = reim(z)
61-
if isnan(r) || isfinite(i)
62-
compact ? showcompact(io,r) : show(io,r)
63-
if signbit(i)==1 && !isnan(i)
64-
i = -i
65-
print(io, compact ? "-" : " - ")
66-
else
67-
print(io, compact ? "+" : " + ")
68-
end
69-
compact ? showcompact(io, i) : show(io, i)
70-
if !(isa(i,Integer) || isa(i,Rational) ||
71-
isa(i,FloatingPoint) && isfinite(i))
72-
print(io, "*")
73-
end
74-
print(io, "im")
61+
compact ? showcompact(io,r) : show(io,r)
62+
if signbit(i)==1 && !isnan(i)
63+
i = -i
64+
print(io, compact ? "-" : " - ")
7565
else
76-
print(io, "complex(",r,",",i,")")
66+
print(io, compact ? "+" : " + ")
67+
end
68+
compact ? showcompact(io, i) : show(io, i)
69+
if !(isa(i,Integer) && !isa(i,Bool) || isa(i,FloatingPoint) && isfinite(i))
70+
print(io, "*")
7771
end
72+
print(io, "im")
7873
end
74+
complex_show(io::IO, z::Complex{Bool}, compact::Bool) =
75+
print(io, z == im ? "im" : "Complex($(z.re),$(z.im))")
7976
show(io::IO, z::Complex) = complex_show(io, z, false)
8077
showcompact(io::IO, z::Complex) = complex_show(io, z, true)
8178

@@ -90,28 +87,8 @@ function write(s::IO, z::Complex)
9087
end
9188

9289

93-
## singleton type for imaginary unit constant ##
94-
95-
type ImaginaryUnit <: Number end
96-
const im = ImaginaryUnit()
97-
98-
convert{T<:Real}(::Type{Complex{T}}, ::ImaginaryUnit) = Complex{T}(zero(T),one(T))
99-
convert(::Type{Complex}, ::ImaginaryUnit) = Complex(real(im),imag(im))
100-
101-
real(::ImaginaryUnit) = int(0)
102-
imag(::ImaginaryUnit) = int(1)
103-
104-
promote_rule{T<:Complex}(::Type{ImaginaryUnit}, ::Type{T}) = T
105-
promote_rule{T<:Real}(::Type{ImaginaryUnit}, ::Type{T}) = Complex{T}
106-
107-
show(io::IO, ::ImaginaryUnit) = print(io, "im")
108-
109-
11090
## generic functions of complex numbers ##
11191

112-
convert(::Type{Complex}, z::Complex) = z
113-
convert(::Type{Complex}, x::Real) = complex(x)
114-
11592
==(z::Complex, w::Complex) = (real(z) == real(w)) & (imag(z) == imag(w))
11693
==(z::Complex, x::Real) = isreal(z) && real(z) == x
11794
==(x::Real, z::Complex) = isreal(z) && real(z) == x
@@ -127,28 +104,22 @@ inv(z::Complex) = conj(z)/abs2(z)
127104
inv{T<:Integer}(z::Complex{T}) = inv(float(z))
128105
sign(z::Complex) = z/abs(z)
129106

130-
(-)(::ImaginaryUnit) = complex(0, -1)
131107
-(z::Complex) = complex(-real(z), -imag(z))
132108
+(z::Complex, w::Complex) = complex(real(z) + real(w), imag(z) + imag(w))
133109
-(z::Complex, w::Complex) = complex(real(z) - real(w), imag(z) - imag(w))
134110
*(z::Complex, w::Complex) = complex(real(z) * real(w) - imag(z) * imag(w),
135111
real(z) * imag(w) + imag(z) * real(w))
136112

137113
# adding or multiplying real & complex is common
114+
*(x::Bool, z::Complex) = ifelse(x, z, zero(z))
115+
*(z::Complex, x::Bool) = ifelse(x, z, zero(z))
138116
*(x::Real, z::Complex) = complex(x * real(z), x * imag(z))
139117
*(z::Complex, x::Real) = complex(x * real(z), x * imag(z))
140118
+(x::Real, z::Complex) = complex(x + real(z), imag(z))
141119
+(z::Complex, x::Real) = complex(x + real(z), imag(z))
142120
-(x::Real, z::Complex) = complex(x - real(z), -imag(z))
143121
-(z::Complex, x::Real) = complex(real(z) - x, imag(z))
144122

145-
# multiplying by im is common
146-
*(z::ImaginaryUnit, w::ImaginaryUnit) = complex(-imag(z), real(z))
147-
*(z::ImaginaryUnit, x::Real) = complex(zero(x), x)
148-
*(x::Real, z::ImaginaryUnit) = complex(zero(x), x)
149-
*(z::ImaginaryUnit, w::Complex) = complex(-imag(w), real(w))
150-
*(w::Complex, z::ImaginaryUnit) = complex(-imag(w), real(w))
151-
152123
/(z::Number, w::Complex) = z*inv(w)
153124
/(a::Real , w::Complex) = a*inv(w)
154125
/(z::Complex, x::Real) = complex(real(z)/x, imag(z)/x)

base/constants.jl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ for op in {:+, :-, :*, :/, :^}
1818
@eval $op(x::MathConst, y::MathConst) = $op(float64(x),float64(y))
1919
end
2020

21-
*(x::MathConst, i::ImaginaryUnit) = float64(x)*i
22-
*(i::ImaginaryUnit, x::MathConst) = i*float64(x)
23-
2421
macro math_const(sym, val, def)
2522
esym = esc(sym)
2623
qsym = esc(Expr(:quote, sym))

base/exports.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ export
5151
GeneralizedSVD,
5252
Hermitian,
5353
Hessenberg,
54-
ImaginaryUnit,
5554
InsertionSort,
5655
IntSet,
5756
IO,

0 commit comments

Comments
 (0)