Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: promotion for matrix-scalar operations #1949

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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

Check warning on line 1211 in src/Matrix.jl

View check run for this annotation

Codecov / codecov/patch

src/Matrix.jl#L1210-L1211

Added lines #L1210 - L1211 were not covered by tests
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
Loading