diff --git a/NEWS.md b/NEWS.md index af67ba38b212d..886e24b706f1a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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]) diff --git a/base/operators.jl b/base/operators.jl index 8f11e3b574706..7d68761eace3f 100644 --- a/base/operators.jl +++ b/base/operators.jl @@ -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 @@ -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 diff --git a/stdlib/Dates/docs/src/index.md b/stdlib/Dates/docs/src/index.md index 4a7456b72a801..4975f175bbf16 100644 --- a/stdlib/Dates/docs/src/index.md +++ b/stdlib/Dates/docs/src/index.md @@ -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 diff --git a/stdlib/LinearAlgebra/src/adjtrans.jl b/stdlib/LinearAlgebra/src/adjtrans.jl index a8574743cb933..cd0e433bf4fb2 100644 --- a/stdlib/LinearAlgebra/src/adjtrans.jl +++ b/stdlib/LinearAlgebra/src/adjtrans.jl @@ -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 / diff --git a/stdlib/LinearAlgebra/test/bidiag.jl b/stdlib/LinearAlgebra/test/bidiag.jl index ba2dd4274851a..8e589ffed31b6 100644 --- a/stdlib/LinearAlgebra/test/bidiag.jl +++ b/stdlib/LinearAlgebra/test/bidiag.jl @@ -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 @@ -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 diff --git a/stdlib/LinearAlgebra/test/dense.jl b/stdlib/LinearAlgebra/test/dense.jl index a7b31dcc50611..9bdc732d1f67a 100644 --- a/stdlib/LinearAlgebra/test/dense.jl +++ b/stdlib/LinearAlgebra/test/dense.jl @@ -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 diff --git a/stdlib/LinearAlgebra/test/diagonal.jl b/stdlib/LinearAlgebra/test/diagonal.jl index b8186f3b33150..2801332e840e6 100644 --- a/stdlib/LinearAlgebra/test/diagonal.jl +++ b/stdlib/LinearAlgebra/test/diagonal.jl @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/stdlib/LinearAlgebra/test/generic.jl b/stdlib/LinearAlgebra/test/generic.jl index 7a51228efc725..805d9eddef8c8 100644 --- a/stdlib/LinearAlgebra/test/generic.jl +++ b/stdlib/LinearAlgebra/test/generic.jl @@ -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 @@ -441,6 +441,7 @@ 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) @@ -448,6 +449,10 @@ 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)] diff --git a/test/errorshow.jl b/test/errorshow.jl index a05a86a797234..442b5478cda24 100644 --- a/test/errorshow.jl +++ b/test/errorshow.jl @@ -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 diff --git a/test/int.jl b/test/int.jl index caabc7c343073..8b77a59e0c5e2 100644 --- a/test/int.jl +++ b/test/int.jl @@ -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