Skip to content

Commit

Permalink
Revert broadening of arithmetic Any methods (revert #44564 and #45320
Browse files Browse the repository at this point in the history
…) (#45489)

* Revert "Remove type-unlimited unary `+` and `*` (#45320)"

This reverts commit 990b1f3.

* Revert "Generalize or restrict a few basic operators (#44564)"

This reverts commit cf1f717.

Also fixes merge conflicts in stdlib/LinearAlgebra/test/generic.jl

Co-authored-by: Daniel Karrasch <[email protected]>
  • Loading branch information
DilumAluthge and dkarrasch authored Jul 25, 2022
1 parent 665b03e commit 783a6aa
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 40 deletions.
5 changes: 0 additions & 5 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ Language changes
* New builtins `getglobal(::Module, ::Symbol[, order])` and `setglobal!(::Module, ::Symbol, x[, order])`
for reading from and writing to globals. `getglobal` should now be preferred for accessing globals over
`getfield`. ([#44137])
* A few basic operators have been generalized to more naturally support vector space structures:
unary minus falls back to scalar multiplication with -1, `-(x) = Int8(-1)*x`,
binary minus falls back to addition `-(x, y) = x + (-y)`, and, at the most generic level,
left- and right-division fall back to multiplication with the inverse from left and right,
respectively, as stated in the docstring. ([#44564])
* The `@invoke` macro introduced in 1.7 is now exported. Additionally, it now uses `Core.Typeof(x)`
rather than `Any` when a type annotation is omitted for an argument `x` so that types passed
as arguments are handled correctly. ([#45807])
Expand Down
6 changes: 1 addition & 5 deletions base/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -513,8 +513,6 @@ julia> identity("Well, what did you expect?")
identity(@nospecialize x) = x

+(x::Number) = x
-(x) = Int8(-1)*x
-(x, y) = x + (-y)
*(x::Number) = x
(&)(x::Integer) = x
(|)(x::Integer) = x
Expand Down Expand Up @@ -615,9 +613,7 @@ julia> inv(A) * x
-7.0
```
"""
\(x, y) = inv(x) * y

/(x, y) = x * inv(y)
\(x,y) = adjoint(adjoint(y)/adjoint(x))

# Core <<, >>, and >>> take either Int or UInt as second arg. Signed shift
# counts can shift in either direction, and are translated here to unsigned
Expand Down
3 changes: 1 addition & 2 deletions stdlib/Dates/docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,7 @@ ERROR: MethodError: no method matching *(::Date, ::Date)
[...]
julia> dt / dt2
ERROR: MethodError: no method matching inv(::Date)
[...]
ERROR: MethodError: no method matching /(::Date, ::Date)
julia> dt - dt2
4411 days
Expand Down
2 changes: 0 additions & 2 deletions stdlib/LinearAlgebra/src/adjtrans.jl
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,6 @@ pinv(v::TransposeAbsVec, tol::Real = 0) = pinv(conj(v.parent)).parent

## left-division \
\(u::AdjOrTransAbsVec, v::AdjOrTransAbsVec) = pinv(u) * v
\(u::AdjointAbsVec, y::Number) = adjoint(conj(y) / u.parent)
\(u::TransposeAbsVec, y::Number) = transpose(y / u.parent)


## right-division /
Expand Down
22 changes: 9 additions & 13 deletions stdlib/LinearAlgebra/test/bidiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ Random.seed!(1)
typediv=T.uplo == 'U' ? UpperTriangular : Matrix,
typediv2=T.uplo == 'U' ? UpperTriangular : Matrix)
TM = Matrix(T)
@test (T*x)::typemul TM*x #broken=eltype(x) <: Furlong
@test (T*x)::typemul TM*x #broken=eltype(x) <: Furlong
@test (x*T)::typemul x*TM #broken=eltype(x) <: Furlong
@test (x\T)::typediv x\TM #broken=eltype(T) <: Furlong
@test (T/x)::typediv TM/x #broken=eltype(T) <: Furlong
Expand All @@ -325,24 +325,20 @@ Random.seed!(1)
end
return nothing
end
if relty <: Integer
A = convert(Matrix{elty}, rand(1:10, n, n))
if (elty <: Complex)
A += im*convert(Matrix{elty}, rand(1:10, n, n))
end
else
A = rand(elty, n, n)
end
for t in (T, #=Furlong.(T)=#), (A, dv, ev) in ((A, dv, ev), #=(Furlong.(A), Furlong.(dv), Furlong.(ev))=#)
A = randn(n,n)
d = randn(n)
dl = randn(n-1)
t = T
for t in (T, #=Furlong.(T)=#), (A, d, dl) in ((A, d, dl), #=(Furlong.(A), Furlong.(d), Furlong.(dl))=#)
_bidiagdivmultest(t, 5, Bidiagonal, Bidiagonal)
_bidiagdivmultest(t, 5I, Bidiagonal, Bidiagonal, t.uplo == 'U' ? UpperTriangular : LowerTriangular)
_bidiagdivmultest(t, Diagonal(dv), Bidiagonal, Bidiagonal, t.uplo == 'U' ? UpperTriangular : LowerTriangular)
_bidiagdivmultest(t, Diagonal(d), Bidiagonal, Bidiagonal, t.uplo == 'U' ? UpperTriangular : LowerTriangular)
_bidiagdivmultest(t, UpperTriangular(A))
_bidiagdivmultest(t, UnitUpperTriangular(A))
_bidiagdivmultest(t, LowerTriangular(A), t.uplo == 'L' ? LowerTriangular : Matrix, t.uplo == 'L' ? LowerTriangular : Matrix, t.uplo == 'L' ? LowerTriangular : Matrix)
_bidiagdivmultest(t, UnitLowerTriangular(A), t.uplo == 'L' ? LowerTriangular : Matrix, t.uplo == 'L' ? LowerTriangular : Matrix, t.uplo == 'L' ? LowerTriangular : Matrix)
_bidiagdivmultest(t, Bidiagonal(dv, ev, :U), Matrix, Matrix, Matrix)
_bidiagdivmultest(t, Bidiagonal(dv, ev, :L), Matrix, Matrix, Matrix)
_bidiagdivmultest(t, Bidiagonal(d, dl, :U), Matrix, Matrix, Matrix)
_bidiagdivmultest(t, Bidiagonal(d, dl, :L), Matrix, Matrix, Matrix)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/test/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Random.seed!(1234323)
ainv = inv(a)
@test cond(a, 1) == opnorm(a, 1) *opnorm(ainv, 1)
@test cond(a, Inf) == opnorm(a, Inf)*opnorm(ainv, Inf)
@test cond(a[:, 1:5]) == (/)(reverse(extrema(svdvals(a[:, 1:5])))...)
@test cond(a[:, 1:5]) == (\)(extrema(svdvals(a[:, 1:5]))...)
@test_throws ArgumentError cond(a,3)
end
end
Expand Down
14 changes: 7 additions & 7 deletions stdlib/LinearAlgebra/test/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -804,8 +804,8 @@ end
U = UpperTriangular(randn(elty, K, K))
L = LowerTriangular(randn(elty, K, K))
D = Diagonal(randn(elty, K))
@test (U / D)::UpperTriangular{elty} UpperTriangular(Matrix(U) / Matrix(D)) rtol=2eps(real(elty))
@test (L / D)::LowerTriangular{elty} LowerTriangular(Matrix(L) / Matrix(D)) rtol=2eps(real(elty))
@test (U / D)::UpperTriangular{elty} == UpperTriangular(Matrix(U) / Matrix(D))
@test (L / D)::LowerTriangular{elty} == LowerTriangular(Matrix(L) / Matrix(D))
@test (D \ U)::UpperTriangular{elty} == UpperTriangular(Matrix(D) \ Matrix(U))
@test (D \ L)::LowerTriangular{elty} == LowerTriangular(Matrix(D) \ Matrix(L))
end
Expand All @@ -819,8 +819,8 @@ end
D0 = Diagonal(zeros(elty, K))
@test (D \ S)::Tridiagonal{elty} == Tridiagonal(Matrix(D) \ Matrix(S))
@test (D \ T)::Tridiagonal{elty} == Tridiagonal(Matrix(D) \ Matrix(T))
@test (S / D)::Tridiagonal{elty} Tridiagonal(Matrix(S) / Matrix(D)) rtol=2eps(real(elty))
@test (T / D)::Tridiagonal{elty} Tridiagonal(Matrix(T) / Matrix(D)) rtol=2eps(real(elty))
@test (S / D)::Tridiagonal{elty} == Tridiagonal(Matrix(S) / Matrix(D))
@test (T / D)::Tridiagonal{elty} == Tridiagonal(Matrix(T) / Matrix(D))
@test_throws SingularException D0 \ S
@test_throws SingularException D0 \ T
@test_throws SingularException S / D0
Expand Down Expand Up @@ -864,8 +864,8 @@ end
D = Diagonal(rand(1:20, K))
@test (D \ S)::Tridiagonal{Float64} == Tridiagonal(Matrix(D) \ Matrix(S))
@test (D \ T)::Tridiagonal{Float64} == Tridiagonal(Matrix(D) \ Matrix(T))
@test (S / D)::Tridiagonal{Float64} Tridiagonal(Matrix(S) / Matrix(D)) rtol=2eps()
@test (T / D)::Tridiagonal{Float64} Tridiagonal(Matrix(T) / Matrix(D)) rtol=2eps()
@test (S / D)::Tridiagonal{Float64} == Tridiagonal(Matrix(S) / Matrix(D))
@test (T / D)::Tridiagonal{Float64} == Tridiagonal(Matrix(T) / Matrix(D))
end

@testset "eigenvalue sorting" begin
Expand Down Expand Up @@ -973,7 +973,7 @@ end
@testset "divisions functionality" for elty in (Int, Float64, ComplexF64)
B = Diagonal(rand(elty,5,5))
x = rand(elty)
@test \(x, B) /(B, x) rtol=2eps()
@test \(x, B) == /(B, x)
end

@testset "promotion" begin
Expand Down
9 changes: 7 additions & 2 deletions stdlib/LinearAlgebra/test/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ end

@testset "Scaling with rdiv! and ldiv!" begin
@test rdiv!(copy(a), 5.) == a/5
@test ldiv!(5., copy(a)) == 5\a
@test ldiv!(zero(a), 5., copy(a)) == 5\a
@test ldiv!(5., copy(a)) == a/5
@test ldiv!(zero(a), 5., copy(a)) == a/5
end

@testset "Scaling with 3-argument mul!" begin
Expand Down Expand Up @@ -441,13 +441,18 @@ Base.:-(a::ModInt{n}, b::ModInt{n}) where {n} = ModInt{n}(a.k - b.k)
Base.:*(a::ModInt{n}, b::ModInt{n}) where {n} = ModInt{n}(a.k * b.k)
Base.:-(a::ModInt{n}) where {n} = ModInt{n}(-a.k)
Base.inv(a::ModInt{n}) where {n} = ModInt{n}(invmod(a.k, n))
Base.:/(a::ModInt{n}, b::ModInt{n}) where {n} = a*inv(b)

Base.zero(::Type{ModInt{n}}) where {n} = ModInt{n}(0)
Base.zero(::ModInt{n}) where {n} = ModInt{n}(0)
Base.one(::Type{ModInt{n}}) where {n} = ModInt{n}(1)
Base.one(::ModInt{n}) where {n} = ModInt{n}(1)
Base.conj(a::ModInt{n}) where {n} = a
LinearAlgebra.lupivottype(::Type{ModInt{n}}) where {n} = RowNonZero()
Base.adjoint(a::ModInt{n}) where {n} = ModInt{n}(conj(a))
Base.transpose(a::ModInt{n}) where {n} = a # see Issue 20978
LinearAlgebra.Adjoint(a::ModInt{n}) where {n} = adjoint(a)
LinearAlgebra.Transpose(a::ModInt{n}) where {n} = transpose(a)

@testset "Issue 22042" begin
A = [ModInt{2}(1) ModInt{2}(0); ModInt{2}(1) ModInt{2}(1)]
Expand Down
4 changes: 2 additions & 2 deletions test/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,8 @@ let err_str
@test occursin("MethodError: no method matching +(::$Int, ::Vector{Float64})", err_str)
@test occursin("For element-wise addition, use broadcasting with dot syntax: scalar .+ array", err_str)
err_str = @except_str rand(5) - 1//3 MethodError
@test occursin("MethodError: no method matching +(::Vector{Float64}, ::Rational{$Int})", err_str)
@test occursin("For element-wise addition, use broadcasting with dot syntax: array .+ scalar", err_str)
@test occursin("MethodError: no method matching -(::Vector{Float64}, ::Rational{$Int})", err_str)
@test occursin("For element-wise subtraction, use broadcasting with dot syntax: array .- scalar", err_str)
end


Expand Down
2 changes: 1 addition & 1 deletion test/int.jl
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ end
@test mod(123, UInt8) === 0x7b

primitive type MyBitsType <: Signed 8 end
@test_throws ErrorException ~reinterpret(MyBitsType, 0x7b)
@test_throws MethodError ~reinterpret(MyBitsType, 0x7b)
@test signed(MyBitsType) === MyBitsType

UItypes = Base.BitUnsigned_types
Expand Down

0 comments on commit 783a6aa

Please sign in to comment.