[PATCH] fix index/lookup for 0 dim Finite DirectProduct

6 views
Skip to first unread message

Qian Yun

unread,
Apr 19, 2026, 6:52:16 PM (3 days ago) Apr 19
to fricas-devel
(1) -> T ==> DirectProduct(0, ZMOD 3)

(2) -> 0$T -- there is only one element in this domain, the empty one

(2) []
Type: DirectProduct(0,IntegerMod(3))
(3) -> index(1)$T -- give wrong result

(3) [1]
Type: DirectProduct(0,IntegerMod(3))
(4) -> lookup(0$T)

>> Error detected within library code:
index out of range


This patch fixes the corner case.

- Qian

(Disclaimer: this issue is found by LLM, but the analysis and patch
is thoroughly reviewed by me.)

diff --git a/src/algebra/vector.spad b/src/algebra/vector.spad
index b105dbe8..6014e036 100644
--- a/src/algebra/vector.spad
+++ b/src/algebra/vector.spad
@@ -412,6 +412,7 @@

if R has Finite then
index(n : PositiveInteger) : % ==
+ dim = 0 => empty()
N : Integer := size()$R
p : Integer := n-1
L : List R := [index(((p rem N) + 1)::PositiveInteger)$R]
@@ -421,6 +422,7 @@
vector(L)
random() == [random()$R for i in 1..dim]
lookup(x) ==
+ dim = 0 => 1
N : Integer := size()$R
L : Integer := lookup(x.1)-1
for i in 2..dim repeat

Waldek Hebisch

unread,
Apr 20, 2026, 2:01:42 PM (2 days ago) Apr 20
to fricas...@googlegroups.com
On Mon, Apr 20, 2026 at 06:52:12AM +0800, Qian Yun wrote:
> (1) -> T ==> DirectProduct(0, ZMOD 3)
>
> (2) -> 0$T -- there is only one element in this domain, the empty one
>
> (2) []
> Type: DirectProduct(0,IntegerMod(3))
> (3) -> index(1)$T -- give wrong result
>
> (3) [1]
> Type: DirectProduct(0,IntegerMod(3))
> (4) -> lookup(0$T)
>
> >> Error detected within library code:
> index out of range
>
>
> This patch fixes the corner case.

Thanks, please commit.

> - Qian
>
> (Disclaimer: this issue is found by LLM, but the analysis and patch
> is thoroughly reviewed by me.)
>
> diff --git a/src/algebra/vector.spad b/src/algebra/vector.spad
> index b105dbe8..6014e036 100644
> --- a/src/algebra/vector.spad
> +++ b/src/algebra/vector.spad
> @@ -412,6 +412,7 @@
>
> if R has Finite then
> index(n : PositiveInteger) : % ==
> + dim = 0 => empty()
> N : Integer := size()$R
> p : Integer := n-1
> L : List R := [index(((p rem N) + 1)::PositiveInteger)$R]
> @@ -421,6 +422,7 @@
> vector(L)
> random() == [random()$R for i in 1..dim]
> lookup(x) ==
> + dim = 0 => 1
> N : Integer := size()$R
> L : Integer := lookup(x.1)-1
> for i in 2..dim repeat
>
> --
> You received this message because you are subscribed to the Google Groups "FriCAS - computer algebra system" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to fricas-devel...@googlegroups.com.
> To view this discussion visit https://groups.google.com/d/msgid/fricas-devel/9f59b168-dadc-49ab-a8f1-3337b867dc54%40gmail.com.

--
Waldek Hebisch
Reply all
Reply to author
Forward
0 new messages