Axiom: Converting Polynomials to Lists and vice-versa

(1) -> P := SparseUnivariatePolynomial(Integer)
(1)  SparseUnivariatePolynomial(Integer)
                                                               Type: Type
(2) -> p: P := x^5+4*x^4+2*x^3+3*x^2+3*x^1+5
      5     4     3     2
(2)  ?  + 4?  + 2?  + 3?  + 3? + 5
                                 Type: SparseUnivariatePolynomial(Integer)
(3) -> v := vectorise(p, 7)
(3)  [5,3,3,2,4,1,0]
                                                     Type: Vector(Integer)
(4) -> l := [v.i for i in 1..#v]
(4)  [5,3,3,2,4,1,0]
                                                       Type: List(Integer)
(5) -> lp := [monomial(c,i)$P for c in l for i in 0..#l]
             2   3   4  5
(5)  [5,3?,3? ,2? ,4? ,? ,0]
                           Type: List(SparseUnivariatePolynomial(Integer))
(7) -> q := reduce(+,lp,0)
      5     4     3     2
(7)  ?  + 4?  + 2?  + 3?  + 3? + 5
                                 Type: SparseUnivariatePolynomial(Integer)
(8) -> p-q
(8)  0

The problem starts when you try to use

q := reduce(+,lp,0)

in a .spad file and compile it. The compiler will return something like:

Compiling FriCAS source code from file
        /path/to/file.spad using old system
        compiler.
    PKGABBREV abbreviates package PackageName
******** Boot Syntax Error detected ********
The prior line was:

 47> lp:List(P):=[monomial(c,i)$P for c in l for i in 0..#l];

The current line is:

 48> q:=reduce(+,lp,0); ^

First currently preparsed lines are:

...

The number of valid tokens is 2.
The prior token was #S(TOKEN :SYMBOL |(| :TYPE KEYWORD :NONBLANK T)
The current token is #S(TOKEN :SYMBOL + :TYPE KEYWORD :NONBLANK T)
The next token is #S(TOKEN :SYMBOL |,| :TYPE KEYWORD :NONBLANK T)

The only thing I could decode from that was that there is probably something wrong with the “+” sign parameter in the reduce function.

… and indeed. As the reply from Dr. Hemmecke (RISC) says:

The problem is that + is used as the name of a function, but it is not actually an identifier. It must be treated in a special way otherwise the parser thinks that + is used without the left and right argument.

Actually, I hope some day that usage of + will work, but currently you have to help the compiler by writing

vq:=reduce(_+,lp,0)

i.e., escaping the + by an underscore. This turns + into an identifier and thus everything will work as expected.

Of course, that escaping also applies in other places and for all the other operators like +,*,-,^, …

Leave a Reply

Your email address will not be published. Required fields are marked *