Hi Neven,
thank you. You hit another problem that is not clearly enough described
in the book. When I started with AXIOM, it took some time until I
understood, that not everything that works in the interpreter also works
for the compiler. You have hit such a case here. Another thing that
should go to the FAQs.
Maybe someone corrects me if I am wrong, but I am not aware of a mehtod
to define a recursive function in a .spad file in the way you did (and
which would probably work in a .input file. (BTW, do not use TABs in
your files, not forbidden, but ugly. Convince your editor to replace
them by spaces.)
r : NonNegativeInteger -> Float
r(0) == a.1
r(1) == a.2
r(2) == a.3
r(m) == (x.1+x.2+x.3)*r(m-1) - (x.1*x.2+x.2*x.3+x.3*x.1)*r(m-2) +
x.1*x.2*x.3*r(m-3)
r(n)
> I don't know how to proceed. It seems as if the ^ operation from
> Domain Float, the one that takes Float and Integer as input, is not
> available.
The error message is indeed not very helpful. But you also have quite
some errors in it.
Note that in contrast to the interpreter, you have to be quite explicit
for the compiler. The site http://fricas.github.io/api should help you.
Just type your function/type into the search field.
As you see in the attachment, I have introduced other "tricks" like
using macros to abbreviate types or even some functions like "asN".
This "asN" is special, since subtraction is not generally defined in
NonNegativeInteger. When I look at
http://fricas.github.io/api/NonNegativeInteger.html, I cannot even find
it. The compiler knows that NonNegativeInteger is a subdomain of Integer
and thus computes that "m-1" inside Integer. The "qcoerce(m-1)@N" then
simply says: "Compiler, believe me, that integer is a NonNegativeInteger."
I have also added types. They are not always necessary, but when you
read your program in a year, then you will find it helpful to recognize
the types from the program text. A particular problem was
"particularSolution". Contrary to the interpreter, the compiler has
fewer things in scope, so you must add
$LinearSystemMatrixPackage1(Float)
in order to tell the compiler in which package it should look to find a
function with name "particularSolution".
The compiler does not like guessing and that is fine, since otherwise
your program might compute other things than you intended to compute.
Note that in FriCAS there is heavy overloading of names.
For lab1method2 I introduced lab1m2 just to show that a function can
return a function and that one can can define a function inside another
function.
Of course, lab1m2 is not needed and you can directly define r insied
labimethod 2 and then return r(n).
Hope that helps.
Ralf
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/fricas-devel/bef85750-6f45-195a-3ea6-43cea4522b7d%40hemmecke.org.
)abbrev package DMLAB1 DiscreteMathLab1
DiscreteMathLab1() : Exports == Implementation where
D ==> DirectProduct(3, Float)
V ==> Vector Float
M ==> Matrix Float
N ==> NonNegativeInteger
Exports ==> with
lab1method1 : (D, D, N) -> Float
lab1method2 : (D, D, N) -> Float
Implementation ==> add
lab1method1(x : D, a : D, n : N) : Float ==
m: M := matrix [[(x.j^i)@Float for j in 1..3] for i in 0..2]
v: V := a :: V
sol: Union(V, "failed") := particularSolution(m, v)$LinearSystemMatrixPackage1(Float)
dot(directProduct(sol::V), map(z +-> z^n, x))
lab1m2(x : D, a : D): N -> Float ==
asN n ==> qcoerce(n)@N
r(m: N): Float ==
zero? m => a.1
m = 1 => a.2
m = 2 => a.3
(x.1+x.2+x.3) * r(asN(m-1)) _
- (x.1*x.2+x.2*x.3+x.3*x.1) * r(asN(m-2)) _
+ x.1*x.2*x.3 * r(asN(m-3))
r
lab1method2(x : D, a : D, n : N) : Float == lab1m2(x, a)(n)