[PATCH] fix 'prime?' in UFD

3 views
Skip to first unread message

Qian Yun

unread,
May 19, 2026, 12:33:31 AM (8 days ago) May 19
to fricas-devel
(1) -> prime?(0::SUP INT)

(1) true

(2) -> prime?(((x-1)^2)::SUP INT)

(2) true


prime$SUP INT is implemented in UFD.
The implementation clearly overlooked a few things.


On a related subject, is it intentional that "factor" of
POLY INT does not factor the coefficient?

(3) -> factor((6*(x-1)^2)::POLY INT)

2
(3) 6 (x - 1)

- Qian


diff --git a/src/algebra/catdef.spad b/src/algebra/catdef.spad
index 262ba2ba..109671c8 100644
--- a/src/algebra/catdef.spad
+++ b/src/algebra/catdef.spad
@@ -1532,7 +1532,10 @@
squareFreePart x ==
unit(s := squareFree x) * _*/[f.factor for f in factorList s]

- prime? x == numberOfFactors factor x = 1
+ prime? x ==
+ zero? x => false
+ f := factorList factor x
+ # f = 1 and first(f).exponent = 1

--Copyright (c) 1991-2002, The Numerical ALgorithms Group Ltd.
--All rights reserved.

Waldek Hebisch

unread,
May 19, 2026, 7:58:08 AM (8 days ago) May 19
to fricas...@googlegroups.com
On Tue, May 19, 2026 at 12:33:27PM +0800, Qian Yun wrote:
> (1) -> prime?(0::SUP INT)
>
> (1) true
>
> (2) -> prime?(((x-1)^2)::SUP INT)
>
> (2) true
>
>
> prime$SUP INT is implemented in UFD.
> The implementation clearly overlooked a few things.

Thanks, pleas commit.

> On a related subject, is it intentional that "factor" of
> POLY INT does not factor the coefficient?
>
> (3) -> factor((6*(x-1)^2)::POLY INT)
>
> 2
> (3) 6 (x - 1)
>

Yes. Factoring integers may be extremaly expensive, while
factoring polynomials is relatively cheap and we do this
frequently. Also, most polynomial algorithms assume that
factoring is done over a field and then adjusted to have
coefficients in the ring.

If 'factor' would factor constant factor, then we would need
a separate function 'factor_but_do_not_factor_constant_factor'
and use it in many/most places instead of 'factor'. It would be
more consistent to do so, but it requires work and gain
seem to be small.

> diff --git a/src/algebra/catdef.spad b/src/algebra/catdef.spad
> index 262ba2ba..109671c8 100644
> --- a/src/algebra/catdef.spad
> +++ b/src/algebra/catdef.spad
> @@ -1532,7 +1532,10 @@
> squareFreePart x ==
> unit(s := squareFree x) * _*/[f.factor for f in factorList s]
>
> - prime? x == numberOfFactors factor x = 1
> + prime? x ==
> + zero? x => false
> + f := factorList factor x
> + # f = 1 and first(f).exponent = 1
>
> --Copyright (c) 1991-2002, The Numerical ALgorithms Group Ltd.
> --All rights reserved.

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