Skip to content

Commit

Permalink
fix: promotion for matrix-scalar operations (#1949)
Browse files Browse the repository at this point in the history
  • Loading branch information
thofma authored Jan 7, 2025
1 parent 82c1b6a commit 6be03b7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 24 deletions.
2 changes: 2 additions & 0 deletions src/Matrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions test/Matrix-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 0 additions & 24 deletions test/generic/Matrix-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 6be03b7

Please sign in to comment.