diff --git a/src/Matrix.jl b/src/Matrix.jl index 51f1d885b..fad37b389 100644 --- a/src/Matrix.jl +++ b/src/Matrix.jl @@ -1207,6 +1207,8 @@ function Base.promote(x::MatElem{S}, y::T) where {S <: NCRingElement, T <: NCRin U = promote_rule_sym(S, T) if U === S return x, base_ring(x)(y) + elseif U === T + return change_base_ring(parent(y), x), y else error("Cannot promote to common type") end diff --git a/test/Matrix-test.jl b/test/Matrix-test.jl index 03077145c..95aecf611 100644 --- a/test/Matrix-test.jl +++ b/test/Matrix-test.jl @@ -161,3 +161,39 @@ end @test r1 == r2 @test S1 == S end + +@testset "Promotion" begin + M = matrix(ZZ, 1, 1, [1]) + N = matrix(QQ, 1, 1, [2]) + + L = @inferred M + N + @test base_ring(L) === QQ + @test L == change_base_ring(QQ, M) + N + L = @inferred M - N + @test base_ring(L) === QQ + @test L == change_base_ring(QQ, M) - N + L = @inferred M * N + @test base_ring(L) === QQ + @test L == change_base_ring(QQ, M) * N + L = @inferred N + M + @test base_ring(L) === QQ + @test L == N + change_base_ring(QQ, M) + L = @inferred N - M + @test base_ring(L) === QQ + @test L == N - change_base_ring(QQ, M) + L = @inferred N * M + @test base_ring(L) === QQ + @test L == N * change_base_ring(QQ, M) + + @test M * QQ[1;] == QQ[1;] + @test M * ZZ[1;] == ZZ[1;] + @test N * QQ[1;] == QQ[2;] + @test N * ZZ[1;] == QQ[2;] + @test QQ[1;] * M == QQ[1;] + @test ZZ[1;] * M == ZZ[1;] + @test QQ[1;] * N == QQ[2;] + @test ZZ[1;] * N == QQ[2;] + + @test M * QQ(1) == QQ(1) * M == QQ.(M) + @test N * ZZ(1) == ZZ(1) * N == QQ.(N) +end diff --git a/test/generic/Matrix-test.jl b/test/generic/Matrix-test.jl index e84b10cf3..6c4c5d602 100644 --- a/test/generic/Matrix-test.jl +++ b/test/generic/Matrix-test.jl @@ -4300,30 +4300,6 @@ end @test collect(M) == [ M() ] end -@testset "Generic.Mat.promotion" begin - M = matrix(ZZ, 1, 1, [1]) - N = matrix(QQ, 1, 1, [2]) - - L = @inferred M + N - @test base_ring(L) === QQ - @test L == change_base_ring(QQ, M) + N - L = @inferred M - N - @test base_ring(L) === QQ - @test L == change_base_ring(QQ, M) - N - L = @inferred M * N - @test base_ring(L) === QQ - @test L == change_base_ring(QQ, M) * N - L = @inferred N + M - @test base_ring(L) === QQ - @test L == N + change_base_ring(QQ, M) - L = @inferred N - M - @test base_ring(L) === QQ - @test L == N - change_base_ring(QQ, M) - L = @inferred N * M - @test base_ring(L) === QQ - @test L == N * change_base_ring(QQ, M) -end - @testset "Generic.Mat.InjProjMat" begin # Construction @test matrix(Generic.inj_proj_mat(QQ, 3, 2, 1)) == QQ[1 0; 0 1; 0 0]