axiom-math
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Axiom-math] Very simple calculus on matrix and vector.


From: Ralf Hemmecke
Subject: Re: [Axiom-math] Very simple calculus on matrix and vector.
Date: Sat, 25 Feb 2006 22:23:08 +0100
User-agent: Thunderbird 1.5 (X11/20051201)

Hello Francois

But can I concat 3 or 4 or more vectors with only ONE command ?

I don't know if I can define in axiom a function with 1 or 2 or 3 or 4 ... arguments.
Sequences of maple or mupad allow this.
With axiom must I define a function with a list of arguments ?

Well, I don't know whether I should call this a trick or not, but you
could achieve this easily in Aldor. There is something similar in Aldor.

In Aldor and Axiom you can write.

(4) -> (a, b) := (3, 5)

   (4)  5
        Type: PositiveInteger

Interestingly the type is shown as PositiveInteger.

In Aldor you would get

aldor -gloop
#include "aldor"
#include "aldorinterp"
import from Integer
(a, b) := (3, 5)
  () @ AldorInteger, AldorInteger

which (interestingly) omits the parens around the type
  AldorInteger, AldorInteger

(Note that in Aldor we have Integer==>AldorInteger.)

What you have seen is a "parallel assignment". And it also works in Axiom.

(5) -> a

   (5)  3
        Type: PositiveInteger
(6) -> b

   (6)  5

It is interesting to analyse the type. It is something of the form
(X, X). That looks like a tuple. And in fact "Tuple" would be one
solution to your problem. Of course you can write a function that takes
a List as input as you suggested above.

However, if Axiom/Aldor sees an expression of the form

  f x

it tries to find "f" and "x" with appropriate types. Now if x is of the
form (r,s,t) or (u,v) where all of the r,s,t,u,v are of the same type X.
Then the type for f must be "(X, X, X) -> Y" or "(X, X) -> Y".
But that is not all. What about "Tuple X -> Y"?
(r,s,t) and (u,v) are perfect elements of "Tuple X".

Basically, that says, instead of writing a function that takes arbitrary
elements, we are going to write a function that takes just one element.
However, the elements are (mathematically) elements of

  \bigcup_{n\in N}X^n ((the union of any power of X))

Here is an example in Aldor... (compile it on the command line, no Axiom
is needed)
---- mysumaldor.as -------
-- aldor -grun -laldor mysumaldor.as
--empty sum  = 0
--sum(2)     = 2
--sum(2,3)   = 5
--sum(2,3,5) = 10
#include "aldor"

macro {
  Z == Integer;
  P == PrimitiveArray Z;
}
MySum: with {
  mysum: Tuple Z -> Z;
} == add {
  mysum(t: Tuple Z): Z == {
    import from MachineInteger;
    local n: Z := 0;
    for i in 1..(length t) repeat n := n + element(t, i);
    n;
  }
}

main(): () == {
  import from MySum, Z, TextWriter, String, Character;
  stdout << "empty sum  = " << mysum()      << newline;
  stdout << "sum(2)     = " << mysum(2)     << newline;
  stdout << "sum(2,3)   = " << mysum(2,3)   << newline;
  stdout << "sum(2,3,5) = " << mysum(2,3,5) << newline;
}

main();
------ end mysumaldor.as


For Axiom the domain constructor "Tuple" has different exports. :-(
And there is a bug somewhere.

------ mysumaxiom.as
#include "axiom"

macro {
  Z == Integer;
  P == PrimitiveArray Z;
  T == Tuple Z;
}
MySum: with {
  mysum: T -> Z;
} == add {
  mysum(t: T): Z == {
    import from Z;
    local n: Z := 0;
-- cannot use "select" because it is an Aldor keyword.
--  for i in 1..length t repeat n := n + select(t, n);
--  p: P := (coerce$T)(t)@P; -- strange that `t::P' does not work

-- Thirty trick due to a bug of Axiom/Aldor not finding "coerce".
-- Take representation of Tuple from src/algebra/array1.spad
    R ==> Record(len: NonNegativeInteger, elts: P);
    import from R;
    p: P := (t pretend R).elts;
    for i in 1..(length t)::Integer repeat n := n + p.i;
    n;
  }
}
----- end mysumaxiom.as

Well, it does not quite work in Axiom, but that seems only because I am
missing some "runtime.o". That is strange, because I have a runtime.lsp
in that directory and that should be enough for Axiom, shouldn't it?

Can someone explain the error message for mysumaxiom.as?

--(2) -> )co mysumaxiom.as
--(2) -> mysum()
--
--   >> System error:
--   Cannot open the file
--   /home/hemmecke/software/Axiom/mnt/linux/aldor/lib/runtime.o.
--
--(2) -> mysum(2)
--
--   >> System error:
--   Cannot open the file
--   /home/hemmecke/software/Axiom/mnt/linux/aldor/lib/runtime.o.

It is certainly easier if you write a function that works on List.
Anyway, if the compiler sees a "mysum(3,4,5)" in your code it will
internally create an array [3,4,5] and then hide this array by claiming
it to be a Tuple. But it means that the compiler has to use some extra
memory in order to store the tuple/array.

I hope, that helps. I'm sure you can translate the above code into
something that concatenates vectors.

Ralf






reply via email to

[Prev in Thread] Current Thread [Next in Thread]