Skip to content

Commit 6e4e2dc

Browse files
authored
Overload inv, one, oneunit, zero and remove type-piracy (#61)
* Overload Base.literal_pow * Add inv and tests * remove inferrence test * Fix ambiguity, add tests * Support 3-tensors too * Update test_cardinality.jl * v0.1.10
1 parent cd38417 commit 6e4e2dc

File tree

6 files changed

+72
-5
lines changed

6 files changed

+72
-5
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "Infinities"
22
uuid = "e1ba4f0e-776d-440f-acd9-e1d2e9742647"
33
authors = ["Sheehan Olver <[email protected]>"]
4-
version = "0.1.9"
4+
version = "0.1.10"
55

66
[compat]
77
Aqua = "0.8"

src/Infinities.jl

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Infinities
22

3-
import Base: angle, isone, iszero, isinf, isfinite, abs, one, oneunit, zero, isless,
3+
import Base: angle, isone, iszero, isinf, isfinite, abs, one, oneunit, zero, isless, inv,
44
+, -, *, ==, <, , >, , fld, cld, div, mod, min, max, sign, signbit,
55
string, show, promote_rule, convert, getindex
66

@@ -42,6 +42,7 @@ one(::Type{Infinity}) = 1
4242
oneunit(::Type{Infinity}) = 1
4343
oneunit(::Infinity) = 1
4444
zero(::Infinity) = 0
45+
zero(::Type{Infinity}) = 0
4546

4647
struct RealInfinity <: Real
4748
signbit::Bool
@@ -70,6 +71,13 @@ show(io::IO, y::RealInfinity) = print(io, string(y))
7071

7172
Base.to_index(i::RealInfinity) = convert(Integer, i)
7273

74+
one(::Type{RealInfinity}) = 1.0
75+
oneunit(::Type{RealInfinity}) = 1.0
76+
oneunit(::RealInfinity) = 1.0
77+
zero(::RealInfinity) = 0.0
78+
zero(::Type{RealInfinity}) = 0.0
79+
80+
7381
#######
7482
# ComplexInfinity
7583
#######
@@ -110,6 +118,12 @@ angle(x::ComplexInfinity) = π*x.signbit
110118

111119
show(io::IO, x::ComplexInfinity) = print(io, "exp($(x.signbit)*im*π)∞")
112120

121+
one(::Type{<:ComplexInfinity}) = one(ComplexF64)
122+
oneunit(::Type{<:ComplexInfinity}) = oneunit(ComplexF64)
123+
oneunit(::ComplexInfinity) = oneunit(ComplexF64)
124+
zero(::ComplexInfinity) = zero(ComplexF64)
125+
zero(::Type{<:ComplexInfinity}) = zero(ComplexF64)
126+
113127
Base.hash(::Infinity) = 0x020113134b21797f # made up
114128

115129

src/algebra.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,11 @@ for OP in (:fld,:cld,:div)
8686
$OP(x::IntegerInfinities, y::Real) = _inffcd(x, y)
8787
$OP(::IntegerInfinities, ::IntegerInfinities) = NotANumber()
8888
end
89-
end
89+
end
90+
91+
# Base.literal_pow
92+
93+
# inv
94+
inv(::Union{Infinity,InfiniteCardinal}) = 0
95+
inv(x::RealInfinity) = inv(float(x))
96+
inv(x::ComplexInfinity) = zero(ComplexF64)

src/cardinality.jl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,15 @@ end
4343

4444
Base.to_index(::Union{Infinity,InfiniteCardinal{0}}) = ℵ₀
4545
Base.to_shape(::Union{Infinity,InfiniteCardinal{0}}) = ℵ₀
46-
Base.to_shape(dims::Tuple{Vararg{Union{Infinity, Integer, AbstractUnitRange}}}) = map(Base.to_shape, dims)
46+
47+
# only vectors, matrices, 3-tensors are supported
48+
Base.to_shape(dims::Tuple{Infinity, Vararg{Union{Integer, AbstractUnitRange}}}) = map(Base.to_shape, dims)
49+
Base.to_shape(dims::Tuple{Infinity, Infinity, Vararg{Union{Integer, AbstractUnitRange}}}) = map(Base.to_shape, dims)
50+
Base.to_shape(dims::Tuple{Union{Integer, AbstractUnitRange}, Infinity, Vararg{Union{Integer, AbstractUnitRange}}}) = map(Base.to_shape, dims)
51+
Base.to_shape(dims::Tuple{Infinity, Infinity, Infinity, Vararg{Union{Integer, AbstractUnitRange}}}) = map(Base.to_shape, dims)
52+
Base.to_shape(dims::Tuple{Infinity, Union{Integer, AbstractUnitRange}, Infinity, Vararg{Union{Integer, AbstractUnitRange}}}) = map(Base.to_shape, dims)
53+
Base.to_shape(dims::Tuple{Union{Integer, AbstractUnitRange}, Infinity, Infinity, Vararg{Union{Integer, AbstractUnitRange}}}) = map(Base.to_shape, dims)
54+
Base.to_shape(dims::Tuple{Union{Integer, AbstractUnitRange}, Union{Integer, AbstractUnitRange}, Infinity, Vararg{Union{Integer, AbstractUnitRange}}}) = map(Base.to_shape, dims)
4755

4856

4957
##

test/runtests.jl

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,12 +296,41 @@ using Aqua
296296
@testin s
297297
@test 2 s
298298
end
299+
300+
@testset "Base.literal_pow" begin
301+
@test Base.literal_pow(^, ℵ₀, Val(0)) ℵ₀^0 1
302+
@test Base.literal_pow(^, ℵ₀, Val(1)) ℵ₀^1 ℵ₀
303+
@test Base.literal_pow(^, ℵ₀, Val(-1)) ℵ₀^(-1) 0
304+
305+
@test Base.literal_pow(^, ∞, Val(0)) ^0 1
306+
@test Base.literal_pow(^, ∞, Val(1)) ^1
307+
@test Base.literal_pow(^, ∞, Val(-1)) ^(-1) 0
308+
309+
@test Base.literal_pow(^, -∞, Val(0)) (-∞)^0 1.0
310+
@test Base.literal_pow(^, -∞, Val(1)) (-∞)^1 -
311+
@test Base.literal_pow(^, -∞, Val(-1)) (-∞)^(-1) 0.0
312+
313+
@test Base.literal_pow(^, ComplexInfinity(0.1), Val(0)) ComplexInfinity(0.1)^0 1.0+0.0im
314+
@test Base.literal_pow(^, ComplexInfinity(0.1), Val(1)) (ComplexInfinity(0.1))^1 ComplexInfinity(0.1)
315+
@test Base.literal_pow(^, ComplexInfinity(0.1), Val(-1)) (ComplexInfinity(0.1))^(-1) 0.0+0.0im
316+
end
317+
318+
@testset "one/zero/oneunit" begin
319+
@test one(ℵ₀) one(∞) one(ℵ₀) oneunit(∞) one(Infinity) one(InfiniteCardinal{0}) oneunit(Infinity) oneunit(InfiniteCardinal{0}) 1
320+
@test one(-∞) oneunit(-∞) one(RealInfinity) oneunit(RealInfinity) 1.0
321+
@test one(exp(0.1im)∞) oneunit(exp(0.1im)∞) one(ComplexInfinity) oneunit(ComplexInfinity) 1.0+0.0im
322+
323+
@test zero(ℵ₀) zero(∞) zero(Infinity) zero(InfiniteCardinal{0}) 0
324+
@test zero(-∞) zero(RealInfinity) 0.0
325+
@test zero(exp(0.1im)∞) zero(ComplexInfinity) 0.0+0.0im
326+
end
299327
end
300328

301329

330+
302331
include("test_cardinality.jl")
303332
include("test_ambiguity.jl")
304333

305334
@testset "Project quality" begin
306-
Aqua.test_all(Infinities, piracies=(; broken=true))
335+
Aqua.test_all(Infinities)
307336
end

test/test_cardinality.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,15 @@ using Infinities, Base64, Base.Checked, Test
161161
@test string(ℵ₁) == stringmime("text/plain", ℵ₁) == "ℵ₁"
162162
@test Base.to_index(ℵ₀) Base.to_shape(ℵ₀) ℵ₀
163163
@test Base.to_shape((∞,)) (ℵ₀,)
164+
@test Base.to_shape((∞,∞)) (ℵ₀,ℵ₀)
165+
@test Base.to_shape((∞,1)) (ℵ₀,1)
166+
@test Base.to_shape((1,∞)) (1,ℵ₀)
167+
@test Base.to_shape((∞,∞,∞)) (ℵ₀,ℵ₀,ℵ₀)
168+
@test Base.to_shape((∞,1,∞)) (ℵ₀,1,ℵ₀)
169+
@test Base.to_shape((∞,∞,1)) (ℵ₀,ℵ₀,1)
170+
@test Base.to_shape((1,∞,∞)) (1,ℵ₀,ℵ₀)
171+
@test Base.to_shape((1,1,∞)) (1,1,ℵ₀)
172+
164173

165174
@testset "Set" begin
166175
s = Set([ℵ₀,ℵ₁,∞,1])

0 commit comments

Comments
 (0)