Vector operations in ARIBAS
ARIBAS
provides various support for vector operations.
For example, vectors can be added, subtracted
and multiplied or divided by scalars.
Example:
==> vec1 := (2,3,4,-1).
-: (2, 3, 4, -1)
==> vec2 := (pi,exp(1),0,0.25).
-: (3.14159265, 2.71828183, 0, 0.250000000)
==> vec1 + vec2.
-: (5.14159265, 5.71828183, 4.00000000, -0.750000000)
==> vec1 - vec2.
-: (-1.14159265, 0.281718172, 4.00000000, -1.25000000)
==> 3*vec1.
-: (6, 9, 12, -3)
==> vec1 * 17.
-: (34, 51, 68, -17)
==> vec2 / 3.
-: (-1.04719755, 0.906093942, 0.000000000, 0.0833333333)
For integer vectors, we have also the operations
div and mod, which are applied
simultaneously to the components of the vector. Example:
==> vec := (100, -200, 300, -400, 500).
-: (100, -200, 300, -400, 500)
==> v1 := vec div 17.
-: (5, -12, 17, -24, 29)
==> v2 := vec mod 17.
-: (15, 4, 11, 8, 7)
==> v1*17 + v2.
-: (100, -200, 300, -400, 500)
A sometimes useful feature is the simultaneous assignment.
Example
==> (x,y) := (2**10, 2**100).
-: (1024, 1_26765_06002_28229_40149_67032_05376)
==> x.
-: 1024
==> y.
-: 1_26765_06002_28229_40149_67032_05376
This can be used to swap variables:
==> (x,y) := (y,x).
-: (1_26765_06002_28229_40149_67032_05376, 1024)
==> x.
-: 1_26765_06002_28229_40149_67032_05376
==> y.
-: 1024
As another example, consider the following compact code to calculate the Fibonacci numbers.
==> function fibo(N: integer): integer;
var
u,v,k: integer;
begin
(u,v) := (1,0);
for k := 1 to N do
(u,v) := (v,u+v);
end;
return v;
end.
-: fibo
==> fibo(100).
-: 3_54224_84817_92619_15075
A new function in ARIBAS is
stack_arraypush(st: stack; vec: array of Type
[; direction: integer]): integer;
Pushes the components of the array vec onto the
stack st. If the optional argument direction
is positive or omitted, the order is from beginning to
the end of vec.
If direction is negative, the pushing occurs
in reverse order. Return value is the number of elements
pushed on st (= the length of vec).
Together with the function stack2array
this can be used to reverse the order of elements of a vector.
Example:
==> var st: stack; end.
-: var
==> vec := (1,2,3,4,5).
-: (1, 2, 3, 4, 5)
==> stack_arraypush(st,vec,-1).
-: 5
==> vec1 := stack2array(st).
-: (5, 4, 3, 2, 1)
Homepage of ARIBAS
Otto Forster 2004-08-16