Skip to content

Commit

Permalink
Overload inv, one, oneunit, zero and remove type-piracy (#61)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
dlfivefifty authored Jan 14, 2025
1 parent cd38417 commit 6e4e2dc
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Infinities"
uuid = "e1ba4f0e-776d-440f-acd9-e1d2e9742647"
authors = ["Sheehan Olver <[email protected]>"]
version = "0.1.9"
version = "0.1.10"

[compat]
Aqua = "0.8"
Expand Down
16 changes: 15 additions & 1 deletion src/Infinities.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Infinities

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

Expand Down Expand Up @@ -42,6 +42,7 @@ one(::Type{Infinity}) = 1
oneunit(::Type{Infinity}) = 1
oneunit(::Infinity) = 1
zero(::Infinity) = 0
zero(::Type{Infinity}) = 0

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

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

one(::Type{RealInfinity}) = 1.0
oneunit(::Type{RealInfinity}) = 1.0
oneunit(::RealInfinity) = 1.0
zero(::RealInfinity) = 0.0
zero(::Type{RealInfinity}) = 0.0


#######
# ComplexInfinity
#######
Expand Down Expand Up @@ -110,6 +118,12 @@ angle(x::ComplexInfinity) = π*x.signbit

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

one(::Type{<:ComplexInfinity}) = one(ComplexF64)
oneunit(::Type{<:ComplexInfinity}) = oneunit(ComplexF64)
oneunit(::ComplexInfinity) = oneunit(ComplexF64)
zero(::ComplexInfinity) = zero(ComplexF64)
zero(::Type{<:ComplexInfinity}) = zero(ComplexF64)

Base.hash(::Infinity) = 0x020113134b21797f # made up


Expand Down
9 changes: 8 additions & 1 deletion src/algebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,11 @@ for OP in (:fld,:cld,:div)
$OP(x::IntegerInfinities, y::Real) = _inffcd(x, y)
$OP(::IntegerInfinities, ::IntegerInfinities) = NotANumber()
end
end
end

# Base.literal_pow

# inv
inv(::Union{Infinity,InfiniteCardinal}) = 0
inv(x::RealInfinity) = inv(float(x))
inv(x::ComplexInfinity) = zero(ComplexF64)
10 changes: 9 additions & 1 deletion src/cardinality.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,15 @@ end

Base.to_index(::Union{Infinity,InfiniteCardinal{0}}) = ℵ₀
Base.to_shape(::Union{Infinity,InfiniteCardinal{0}}) = ℵ₀
Base.to_shape(dims::Tuple{Vararg{Union{Infinity, Integer, AbstractUnitRange}}}) = map(Base.to_shape, dims)

# only vectors, matrices, 3-tensors are supported
Base.to_shape(dims::Tuple{Infinity, Vararg{Union{Integer, AbstractUnitRange}}}) = map(Base.to_shape, dims)
Base.to_shape(dims::Tuple{Infinity, Infinity, Vararg{Union{Integer, AbstractUnitRange}}}) = map(Base.to_shape, dims)
Base.to_shape(dims::Tuple{Union{Integer, AbstractUnitRange}, Infinity, Vararg{Union{Integer, AbstractUnitRange}}}) = map(Base.to_shape, dims)
Base.to_shape(dims::Tuple{Infinity, Infinity, Infinity, Vararg{Union{Integer, AbstractUnitRange}}}) = map(Base.to_shape, dims)
Base.to_shape(dims::Tuple{Infinity, Union{Integer, AbstractUnitRange}, Infinity, Vararg{Union{Integer, AbstractUnitRange}}}) = map(Base.to_shape, dims)
Base.to_shape(dims::Tuple{Union{Integer, AbstractUnitRange}, Infinity, Infinity, Vararg{Union{Integer, AbstractUnitRange}}}) = map(Base.to_shape, dims)
Base.to_shape(dims::Tuple{Union{Integer, AbstractUnitRange}, Union{Integer, AbstractUnitRange}, Infinity, Vararg{Union{Integer, AbstractUnitRange}}}) = map(Base.to_shape, dims)


##
Expand Down
31 changes: 30 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -296,12 +296,41 @@ using Aqua
@testin s
@test 2 s
end

@testset "Base.literal_pow" begin
@test Base.literal_pow(^, ℵ₀, Val(0)) ℵ₀^0 1
@test Base.literal_pow(^, ℵ₀, Val(1)) ℵ₀^1 ℵ₀
@test Base.literal_pow(^, ℵ₀, Val(-1)) ℵ₀^(-1) 0

@test Base.literal_pow(^, ∞, Val(0)) ^0 1
@test Base.literal_pow(^, ∞, Val(1)) ^1
@test Base.literal_pow(^, ∞, Val(-1)) ^(-1) 0

@test Base.literal_pow(^, -∞, Val(0)) (-∞)^0 1.0
@test Base.literal_pow(^, -∞, Val(1)) (-∞)^1 -
@test Base.literal_pow(^, -∞, Val(-1)) (-∞)^(-1) 0.0

@test Base.literal_pow(^, ComplexInfinity(0.1), Val(0)) ComplexInfinity(0.1)^0 1.0+0.0im
@test Base.literal_pow(^, ComplexInfinity(0.1), Val(1)) (ComplexInfinity(0.1))^1 ComplexInfinity(0.1)
@test Base.literal_pow(^, ComplexInfinity(0.1), Val(-1)) (ComplexInfinity(0.1))^(-1) 0.0+0.0im
end

@testset "one/zero/oneunit" begin
@test one(ℵ₀) one(∞) one(ℵ₀) oneunit(∞) one(Infinity) one(InfiniteCardinal{0}) oneunit(Infinity) oneunit(InfiniteCardinal{0}) 1
@test one(-∞) oneunit(-∞) one(RealInfinity) oneunit(RealInfinity) 1.0
@test one(exp(0.1im)∞) oneunit(exp(0.1im)∞) one(ComplexInfinity) oneunit(ComplexInfinity) 1.0+0.0im

@test zero(ℵ₀) zero(∞) zero(Infinity) zero(InfiniteCardinal{0}) 0
@test zero(-∞) zero(RealInfinity) 0.0
@test zero(exp(0.1im)∞) zero(ComplexInfinity) 0.0+0.0im
end
end



include("test_cardinality.jl")
include("test_ambiguity.jl")

@testset "Project quality" begin
Aqua.test_all(Infinities, piracies=(; broken=true))
Aqua.test_all(Infinities)
end
9 changes: 9 additions & 0 deletions test/test_cardinality.jl
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,15 @@ using Infinities, Base64, Base.Checked, Test
@test string(ℵ₁) == stringmime("text/plain", ℵ₁) == "ℵ₁"
@test Base.to_index(ℵ₀) Base.to_shape(ℵ₀) ℵ₀
@test Base.to_shape((∞,)) (ℵ₀,)
@test Base.to_shape((∞,∞)) (ℵ₀,ℵ₀)
@test Base.to_shape((∞,1)) (ℵ₀,1)
@test Base.to_shape((1,∞)) (1,ℵ₀)
@test Base.to_shape((∞,∞,∞)) (ℵ₀,ℵ₀,ℵ₀)
@test Base.to_shape((∞,1,∞)) (ℵ₀,1,ℵ₀)
@test Base.to_shape((∞,∞,1)) (ℵ₀,ℵ₀,1)
@test Base.to_shape((1,∞,∞)) (1,ℵ₀,ℵ₀)
@test Base.to_shape((1,1,∞)) (1,1,ℵ₀)


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

2 comments on commit 6e4e2dc

@dlfivefifty
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/123008

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.10 -m "<description of version>" 6e4e2dc311043f0cebf2e7bf1cc6cf2d44e5df35
git push origin v0.1.10

Please sign in to comment.