Linear output representation of polynomials

4 views
Skip to first unread message

Grégory Vanuxem

unread,
Dec 26, 2022, 10:53:19 AM12/26/22
to fricas...@googlegroups.com
Hello,

Is there already a way to obtain a linear representation of a
polynomial, i.e. 1D and not 2D like
7*x^3+4*z^9
instead of
9 3
4 z + 7 x
ideally as a String? And vice versa? Let me specify, in Spad and not
via the interpreter.

Cheers,
__
Greg

Ralf Hemmecke

unread,
Dec 26, 2022, 11:43:08 AM12/26/22
to fricas...@googlegroups.com
print1D x ==> display((x::OutputForm)::Formatter(Format1D))
print1D(7*x^3+4*z^9)

Or you can set 1D output format.

setFormat!(Format1D)$JFriCASSupport
)set output algebra off

See

https://github.com/fricas/fricas/blob/master/src/algebra/fmtjfricas.spad

You can also get a string directly.

first lines formatExpression(7*x^3+4*z^9)$Format1D

Ralf

Grégory Vanuxem

unread,
Dec 26, 2022, 5:55:23 PM12/26/22
to fricas...@googlegroups.com
Excellent! Thank you very much! This is exactly what I need.

My next step is to define the polynomial ring in the form:
R, (x,z) = PolynomialRing(ZZ, ["x","z"])
=> (Multivariate Polynomial Ring in x, z over Integer Ring, fmpz_mpoly[x, z])

So I only need to know the inderminates, probably via 'listOfTerms'.
Since FriCAS is strongly typed, the coefficient Ring will help for the
different algorithms to be used.
And after I just have to evaluate what I want via the Format1D.
Last step reevaluate in FriCAS the result. That's not computationally
expensive here.

I only plan, for Algebra, to implement expensive computation functions
in different packages.
That will use the FLINT 2 library via Julia if success is with me :)

As a matter of fact :
The arithmetic Fateman bench, here with nested Univariate Polynomials:

FriCAS
===================================================
(34) -> x : UP(x,INT)
(35) -> y : UP(y,UP(x,INT))
(36) -> z : UP(z,UP(y,UP(x,INT)))
(37) -> t : UP(t,UP(z,UP(y,UP(x,INT))))
(38) -> f := x + y + z + t + 1
(38) t + z + y + x + 1
(39) -> p := f^30;
(40) -> )set message time on
(40) -> q := p*(p+1);
Time: 0 (IN) + 148.97 (EV) + 0.00 (OT) = 148.97 sec
(41) -> q := p*(p+1);
Time: 0.00 (IN) + 151.28 (EV) + 0.00 (OT) = 151.29 sec
===================================================
FLINT 2 via the Nemo library in Julia
===================================================
julia> R, x = PolynomialRing(ZZ, "x");
julia> S, y = PolynomialRing(R, "y");
julia> T, z = PolynomialRing(S, "z");
julia> U, t = PolynomialRing(T, "t");
julia> f = x + y + z + t + 1
t + z + y + x + 1
julia> p = f^30;
julia> @time q = p*(p+1);
32.041742 seconds (8.72 M allocations: 337.483 MiB, 3.31% gc time,
0.02% compilation time)
julia> @time q = p*(p+1);
30.781938 seconds (5.67 M allocations: 277.926 MiB, 0.29% gc time)
> --
> 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 on the web visit https://groups.google.com/d/msgid/fricas-devel/02fab499-32ae-bd38-764f-1196817cdc02%40hemmecke.org.

Ralf Hemmecke

unread,
Dec 26, 2022, 6:15:06 PM12/26/22
to fricas...@googlegroups.com
Greg,

how can I reproduce what you are doing with julia and flint?

Ralf

Grégory Vanuxem

unread,
Dec 26, 2022, 7:02:45 PM12/26/22
to fricas...@googlegroups.com
Install the binary Julia (www.julialang.org) and after install Nemo
via the 'julia' interpreter..
In the Julia interpreter type ']', you will enter in Pkg'
management mode and type "add Nemo'. Afte,r a backspace will revert
you to Julia shell.
Or in Julia shell 'Pkg.add("Nemo") should do the trick.

See Nemo CAS : https://nemocas.github.io/Nemo.jl/latest/
You'lll find needed documentation and examples.

For info, in my code, %time is a macro to display information about
computations (time elapsed, GC % etc.).

Sorry for the response delay, I needed a pause.

Grégory Vanuxem

unread,
Dec 26, 2022, 7:10:04 PM12/26/22
to fricas...@googlegroups.com
If you want more there is also Oscar which bundles Nemo and other
packages but to use Nemo directly you need to install Nemo separately,
or use the Oscar interface. It bundles also GAP and Singular for
instance. Julia interfaces very easily C/C++ libraries. Python too.

Grégory Vanuxem

unread,
Dec 26, 2022, 7:27:36 PM12/26/22
to fricas...@googlegroups.com
I forgot, in the Julia interpreter 'using Nemo' will give you access
to this library.

Grégory Vanuxem

unread,
Dec 27, 2022, 5:38:30 AM12/27/22
to fricas...@googlegroups.com
Le lun. 26 déc. 2022 à 23:54, Grégory Vanuxem <g.va...@gmail.com> a écrit :

[snippet]]

> FLINT 2 via the Nemo library in Julia
> ===================================================
> julia> R, x = PolynomialRing(ZZ, "x");
> julia> S, y = PolynomialRing(R, "y");
> julia> T, z = PolynomialRing(S, "z");
> julia> U, t = PolynomialRing(T, "t");
> julia> f = x + y + z + t + 1
> t + z + y + x + 1
> julia> p = f^30;
> julia> @time q = p*(p+1);
> 32.041742 seconds (8.72 M allocations: 337.483 MiB, 3.31% gc time,
> 0.02% compilation time)
> julia> @time q = p*(p+1);
> 30.781938 seconds (5.67 M allocations: 277.926 MiB, 0.29% gc time)

In fact, I just wrote to flint-devel and nemo-devel google groups
about the regression shown by this bench I ran around march 2020, the
beginning of the 1st World COVID 19 containment. I was highly
surprised yesterday.

March 2020:
R, x = PolynomialRing(ZZ, "x");
S, y = PolynomialRing(R, "y");
T, z = PolynomialRing(S, "z");
U, t = PolynomialRing(T, "t");
f = x + y + z + t + 1
p = f^30;
@time q = p*(p+1);
20.099625 seconds (2.32 M allocations: 102.120 MiB, 0.78% gc time)

Regards,
__
Greg

Grégory Vanuxem

unread,
Dec 27, 2022, 5:59:55 AM12/27/22
to fricas...@googlegroups.com
I forgot, FriCAS in march 2020:
)set mess type off
)set output length 45
x : UP(x,INT)
y : UP(y,UP(x,INT))
z : UP(z,UP(y,UP(x,INT)))
t : UP(t,UP(z,UP(y,UP(x,INT))))
f := x + y + z + t + 1
p := f^30;
)set mess time on
q := p*(p+1);
Type: UnivariatePolynomial(t,UnivariatePolynomial(z,UnivariatePolynomial(y,UnivariatePolynomial(x,Integer))))
Time: 184.01 (EV) = 184.01 sec


f : MPOLY([x,y,z,t],INT) := x + y + z + t + 1
p := f^30;
)set message time on
q := p*(p+1);
(3) -> q := p*(p+1);

Type: MultivariatePolynomial([x,y,z,t],Integer)
Time: 283.59 (EV) = 283.59 sec

So an amelioration for nested univariate polynomials.

Today (multivariate):
(2) -> f : MPOLY([x,y,z,t],INT) := x + y + z + t + 1
p := f^30;

(2) x + y + z + t + 1
Type: MultivariatePolynomial([x,y,z,t],Integer)
(3) -> )set message time on

Type: MultivariatePolynomial([x,y,z,t],Integer)
(4) -> (4) -> q := p*(p+1);
(3) -> q := p*(p+1);

Type: MultivariatePolynomial([x,y,z,t],Integer)
Time: 0 (IN) + 227.10 (EV) + 0 (OT) = 227.10 sec
(5) -> q := p*(p+1);

Type: MultivariatePolynomial([x,y,z,t],Integer)
Time: 0 (IN) + 227.53 (EV) + 0 (OT) = 227.53 sec
Idem.
That's all.

Dima Pasechnik

unread,
Dec 27, 2022, 6:08:08 AM12/27/22
to fricas...@googlegroups.com
As far as I know, an up to date Flint supports multivariate polynomial rings, so this should go much faster if you just create one ring.
(assuming it is already used in Nemo).


--
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.

Grégory Vanuxem

unread,
Dec 27, 2022, 6:33:31 AM12/27/22
to fricas...@googlegroups.com
Yes ;)
Here with FLINT 2 today
julia> R, (x, y, z, t) = PolynomialRing(ZZ, ["x", "y","z","t"])
(Multivariate Polynomial Ring in x, y, z, t over Integer Ring,
fmpz_mpoly[x, y, z, t])
julia> f = x + y + z + t + 1
x + y + z + t + 1
julia> p = f^30;
julia> @time q = p*(p+1);
11.451405 seconds (14.72 M allocations: 3.969 GiB, 2.64% gc time,
0.11% compilation time)
julia> @time q = p*(p+1);
12.174498 seconds (14.72 M allocations: 3.969 GiB, 2.34% gc time)

And my fault, in march 2020 multivariate was already implemented, in
FLINT the bench gave ~13 sec
> To view this discussion on the web visit https://groups.google.com/d/msgid/fricas-devel/CAAWYfq3522qaq_sm4Ypx3fcUmf2LfPsuRW7Hy2rRaj3jRjSe7Q%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages