Math::Pari "factor" is wrong

Math::Pari "factor" is wrong

am 17.03.2006 19:41:55 von gamo

#!/usr/local/bin/perl -w

# use Math::PariInit qw( primes=12000000 stack=1e8 );
# use Math::Pari qw(:all);
use Math::Pari 'factor';

# $i = PARI 0;
# $P = PARI 1;

for $i (0..1_000_000_000) {
$P = 5*$i**4-10*$i**3+20*$i*$i-15*$i+11;
$f = factor($P,0);
# print "$f\n";
# next if ($f =~ /-1/);
@fact= ($f =~ /[\[|\;](\d+)\,/g);
# print "@fact\n";
for $j (@fact){
if ($j%10!=1){
print "$f -> $j factor of P($i)=$P\n";
}
}
# last if $i==1000;
}


__END__


This code gives incorrects results as -1 as factor of a positive
integer or 5 as factor of a number not terminated in 5 or 0.

I do a sample calculation using 'echo "factor(number)" | gp' and this
gives correct results.

TIA, best regards

Re: Math::Pari "factor" is wrong

am 18.03.2006 02:53:29 von gamo

This message is in MIME format. The first part should be readable text,
while the remaining parts are likely unreadable without MIME-aware tools.

--8323328-1598280312-1142646809=:27081
Content-Type: TEXT/PLAIN; charset=ISO-8859-1
Content-Transfer-Encoding: QUOTED-PRINTABLE

On Fri, 17 Mar 2006, gamo wrote:

>=20
> #!/usr/local/bin/perl -w
>=20
> # use Math::PariInit qw( primes=3D12000000 stack=3D1e8 );
> # use Math::Pari qw(:all);
> use Math::Pari 'factor';
>=20
> # $i =3D PARI 0;
> # $P =3D PARI 1;
>=20
> for $i (0..1_000_000_000) {
> $P =3D 5*$i**4-10*$i**3+20*$i*$i-15*$i+11;
> $f =3D factor($P,0);
> # print "$f\n";
> # next if ($f =3D~ /-1/);
> @fact=3D ($f =3D~ /[\[|\;](\d+)\,/g);
> # print "@fact\n";
> for $j (@fact){
> =09if ($j%10!=3D1){
> =09 print "$f -> $j factor of P($i)=3D$P\n";
> =09}
> }
> # last if $i==1000;
> }
>=20
>=20
> __END__
>=20
>=20
> This code gives incorrects results as -1 as factor of a positive
> integer or 5 as factor of a number not terminated in 5 or 0.
>=20
> I do a sample calculation using 'echo "factor(number)" | gp' and this
> gives correct results. =20
>=20
> TIA, best regards=20
>=20

Well I have a workaround, but the speed is so slow that is impractical.

#!/usr/local/bin/perl -w

use Math::BigInt;

for $i (0..1_000_000_000) {
$P =3D 5*$i**4-10*$i**3+20*$i*$i-15*$i+11;
@f =3D `echo "factor($P)" | gp`;
# print "@f\n";
# @f2 =3D grep (/\[\d+\s/, @f);
# print "@f2\n";
@fact=3D map (/\[(\d+)\s/, @f);
# print "@fact\n";
for $j (@fact){
if ($j%10!=3D1){
print "$j factor of P($i)=3D$P\n";
}
}
if ($i % 10_000_000==0) { print "."; }
# last if $i==1000;
}

__END__

PS: use bignum; don't works here.
Best regards,

--=20
http://www.telecable.es/personales/gamo/
S=F3lo hay 10 tipos de personas, las que saben binario y las que no
perl -e 'print 111_111_111**2,"\n";'
--8323328-1598280312-1142646809=:27081--

Re: Math::Pari "factor" is wrong

am 18.03.2006 08:20:29 von Sisyphus

"gamo" wrote in message
..
..
>
>
> This code gives incorrects results as -1 as factor of a positive
> integer or 5 as factor of a number not terminated in 5 or 0.
>
> I do a sample calculation using 'echo "factor(number)" | gp' and this
> gives correct results.
>
I think you could have provided a simpler test case:

use warnings;
use Math::Pari;

$f1 = Math::Pari::factor(72);
print $f1, "\n";

$z = 4225760561;
$f2 = Math::Pari::factor($z);
print $f2, "\n";

$p = PARI $z;
$f3 = Math::Pari::factor($p);
print $f3, "\n";

__END__

(You could, of course, further simplify the above code.) For me that
produces:

[2,3;3,2]
[-1,1;5,1;13,1;743,1;1433,1]
[-1,1;5,1;13,1;743,1;1433,1]

Quite clearly the first line is correct, since (2**3) * (3**2) == 72
Just as clearly, the second 2 lines are not what we expect.

There's obviously a problem when we get to 32-bit numbers. The unsigned long
4225760561, when treated as a signed int, becomes -69206735 - and it's not
hard to work out that the last 2 lines of output are the factorisation
of -69206735.

I'm not surprised that factor($z) in the above code returns what we see, but
I *did* think that factor($p) would have returned what you and I expected. I
think it's a bug - though I'm not an expert on Math::Pari and haven't read
the documentation very thoroughly.

As for what to do about this .... you could file a bug report at
http://rt.cpan.org/Public/Dist/Display.html?Name=Math-Pari , or you could
contact the author directly. I don't think he would mind that. (I think he
peruses this list, so he may yet respond to this thread.)

Cheers,
Rob

Re: Math::Pari "factor" is wrong

am 18.03.2006 08:32:32 von Ilya Zakharevich

[A complimentary Cc of this posting was sent to
gamo
], who wrote in article :

> This code gives incorrects results as -1 as factor of a positive
> integer or 5 as factor of a number not terminated in 5 or 0.

How do you suppose we are going to treat this? Send you our
condolences? What else do you think we can do?

What version of what? What Math::Pari test suite was saying? Can you
shorten your program to 1 line?

Puzzled,
Ilya

Re: Math::Pari "factor" is wrong

am 18.03.2006 08:49:29 von Ilya Zakharevich

[A complimentary Cc of this posting was sent to
gamo
], who wrote in article :
> for $i (0..1_000_000_000) {
> $P = 5*$i**4-10*$i**3+20*$i*$i-15*$i+11;

This does not make sense. Why do approximate calculations, if you
want integer arithmetic?

The correct way is (if $i fits exact Perl types)

for $j (0..1_000_000_000) {
my $i = PARI $j; # Or PARI "$j" to be absolutely sure
$P = ...

Or make a while() loop operating on a Math::Pari integer, not Perl integer.

But anyway, I discovered a bug in unsigned --> PARI conversion.
Sigh...

perl -MMath::Pari -wle "print PARI 3936116531"
-358850765

This is clearly a bug, Perl data contains enough bits of info to
deduce the correct result... >>TODO

Hope this helps,
Ilya

Re: Math::Pari "factor" is wrong

am 18.03.2006 10:47:10 von Ilya Zakharevich

[A complimentary Cc of this posting was NOT [per weedlist] sent to
Ilya Zakharevich
], who wrote in article :
> But anyway, I discovered a bug in unsigned --> PARI conversion.
> Sigh...
>
> perl -MMath::Pari -wle "print PARI 3936116531"
> -358850765

[Note that this bug is not related to the problems the poster had
due to misunderstanding of Perl arithmetic; but it may move the
threshold where problems appear.]

I released 2.010704 which has this bug fixed. (The test suite is
sensitive to details of propagation from INT to FLOAT in Perl, so may
be wrong on 64-bit platforms. Also, if somebody runs a platform where
$Config{longsize} < $Config{ivsize}, I would like to hear from you
even if test suite succeeds. ;-)

Thanks,
Ilya

Re: Math::Pari "factor" is wrong

am 18.03.2006 11:30:30 von gamo

This message is in MIME format. The first part should be readable text,
while the remaining parts are likely unreadable without MIME-aware tools.

--8323328-1710087361-1142677830=:22760
Content-Type: TEXT/PLAIN; charset=ISO-8859-1
Content-Transfer-Encoding: QUOTED-PRINTABLE

On Sat, 18 Mar 2006, Ilya Zakharevich wrote:

> [A complimentary Cc of this posting was sent to
> gamo=20
> ], who wrote in article 3094@jvz.es>:
>=20

Thank you.

> > This code gives incorrects results as -1 as factor of a positive
> > integer or 5 as factor of a number not terminated in 5 or 0.
>=20
> How do you suppose we are going to treat this? Send you our
> condolences? What else do you think we can do?
>=20
> What version of what? What Math::Pari test suite was saying? Can you

Math-Pari-2.010703.tar.gz

> shorten your program to 1 line?
>=20

No, I can't.

> Puzzled,
> Ilya
>=20

--=20
http://www.telecable.es/personales/gamo/
S=F3lo hay 10 tipos de personas, las que saben binario y las que no
perl -e 'print 111_111_111**2,"\n";'
--8323328-1710087361-1142677830=:22760--

Re: Math::Pari "factor" is wrong

am 18.03.2006 11:58:53 von gamo

This message is in MIME format. The first part should be readable text,
while the remaining parts are likely unreadable without MIME-aware tools.

--8323328-1718442122-1142679533=:22760
Content-Type: TEXT/PLAIN; charset=ISO-8859-1
Content-Transfer-Encoding: QUOTED-PRINTABLE

On Sat, 18 Mar 2006, Ilya Zakharevich wrote:

> [A complimentary Cc of this posting was sent to
> gamo=20
> ], who wrote in article 3094@jvz.es>:
> > for $i (0..1_000_000_000) {
> > $P =3D 5*$i**4-10*$i**3+20*$i*$i-15*$i+11;
>=20
> This does not make sense. Why do approximate calculations, if you
> want integer arithmetic?

That's not an approximate calculation. $P It's the value of the polynomial=
=20
in the point $i.=20

>=20
> The correct way is (if $i fits exact Perl types)
>=20
> for $j (0..1_000_000_000) {
> my $i =3D PARI $j; # Or PARI "$j" to be absolutely sure
> $P =3D ...

Ok, I'll revise that. =20

>=20
> Or make a while() loop operating on a Math::Pari integer, not Perl intege=
r.
>=20
> But anyway, I discovered a bug in unsigned --> PARI conversion.
> Sigh...
>=20
> perl -MMath::Pari -wle "print PARI 3936116531"
> -358850765
>=20
> This is clearly a bug, Perl data contains enough bits of info to
> deduce the correct result... >>TODO
>=20
> Hope this helps,
> Ilya
>=20
Thanks, all the best

--=20
http://www.telecable.es/personales/gamo/
S=F3lo hay 10 tipos de personas, las que saben binario y las que no
perl -e 'print 111_111_111**2,"\n";'
--8323328-1718442122-1142679533=:22760--

Re: Math::Pari "factor" is wrong

am 18.03.2006 12:36:11 von Ilya Zakharevich

[A complimentary Cc of this posting was sent to
gamo
], who wrote in article :
> > > for $i (0..1_000_000_000) {
> > > $P =3D 5*$i**4-10*$i**3+20*$i*$i-15*$i+11;

> > This does not make sense. Why do approximate calculations, if you
> > want integer arithmetic?

> That's not an approximate calculation.

Says who?

> $P It's the value of the polynomial=
> =20
> in the point $i.=20

It IS an approximate calculation. Any calculation done with Perl
numeric values is approximate. The loss of precision may happen to be
0, but this is a coincidence only.

Hope this helps,
Ilya