[PATCH] bug in diagonalMatrix

3 views
Skip to first unread message

Qian Yun

unread,
Apr 18, 2026, 6:29:34 PM (4 days ago) Apr 18
to fricas-devel
(1) -> diagonalMatrix [matrix [[1,2]], matrix [[3],[4]]]

+1 0 0+
| |
(1) |0 3 0|
| |
+0 4 0+

It should look like:

(2) -> matrix [[1,2,0],[0,0,3],[0,0,4]]

+1 2 0+
| |
(2) |0 0 3|
| |
+0 0 4+

It seems that most call of diagonalMatrix is "List R -> %"
instead of "List % -> %", so this bug does not affect
other parts of fricas.

- Qian

diff --git a/src/algebra/matcat.spad b/src/algebra/matcat.spad
index fa9f3995..a948d5ba 100644
--- a/src/algebra/matcat.spad
+++ b/src/algebra/matcat.spad
@@ -460,7 +460,7 @@
ans := zero(rows, cols)
loR := minr ans; loC := minc ans
for mat in list repeat
- hiR := loR + nrows(mat) - 1; hiC := loC + nrows(mat) - 1
+ hiR := loR + nrows(mat) - 1; hiC := loC + ncols(mat) - 1
for i in loR..hiR for k in minr(mat)..maxr(mat) repeat
for j in loC..hiC for l in minc(mat)..maxc(mat) repeat
qsetelt!(ans, i, j, qelt(mat, k, l))

Waldek Hebisch

unread,
Apr 18, 2026, 6:38:26 PM (4 days ago) Apr 18
to fricas...@googlegroups.com
On Sun, Apr 19, 2026 at 06:29:30AM +0800, Qian Yun wrote:
> (1) -> diagonalMatrix [matrix [[1,2]], matrix [[3],[4]]]
>
> +1 0 0+
> | |
> (1) |0 3 0|
> | |
> +0 4 0+
>
> It should look like:
>
> (2) -> matrix [[1,2,0],[0,0,3],[0,0,4]]
>
> +1 2 0+
> | |
> (2) |0 0 3|
> | |
> +0 0 4+
>
> It seems that most call of diagonalMatrix is "List R -> %"
> instead of "List % -> %", so this bug does not affect
> other parts of fricas.

Thanks, please commit with a test. I think that typical use
is for square matrices. But it is specified for non-suare
ones too, so we should fix it.

> - Qian
>
> diff --git a/src/algebra/matcat.spad b/src/algebra/matcat.spad
> index fa9f3995..a948d5ba 100644
> --- a/src/algebra/matcat.spad
> +++ b/src/algebra/matcat.spad
> @@ -460,7 +460,7 @@
> ans := zero(rows, cols)
> loR := minr ans; loC := minc ans
> for mat in list repeat
> - hiR := loR + nrows(mat) - 1; hiC := loC + nrows(mat) - 1
> + hiR := loR + nrows(mat) - 1; hiC := loC + ncols(mat) - 1
> for i in loR..hiR for k in minr(mat)..maxr(mat) repeat
> for j in loC..hiC for l in minc(mat)..maxc(mat) repeat
> qsetelt!(ans, i, j, qelt(mat, k, l))
--
Waldek Hebisch
Reply all
Reply to author
Forward
0 new messages