From 9c650c786e15f47bdfa5d3ba2e6320f04a815106 Mon Sep 17 00:00:00 2001
From: Tommy Hofmann <thofma@gmail.com>
Date: Wed, 25 Dec 2024 19:04:14 +0100
Subject: [PATCH] fix: promotion for matrix-scalar operaions

---
 src/Matrix.jl               |  2 ++
 test/Matrix-test.jl         | 36 ++++++++++++++++++++++++++++++++++++
 test/generic/Matrix-test.jl | 24 ------------------------
 3 files changed, 38 insertions(+), 24 deletions(-)

diff --git a/src/Matrix.jl b/src/Matrix.jl
index 51f1d885b2..fad37b389f 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 03077145c4..95aecf6119 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 e84b10cf37..6c4c5d602e 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]