# [Maxima] make a function that assign value to variable and is visible later

andre maute andre.maute at gmx.de
Tue Jul 17 17:15:15 CDT 2012

```I would do it like this way

------------------------
display2d : false;

my_assignment(var,n) := block(
for k : 1 thru n do block(
var[k,1] : k^2
)
);

n : 10;

A : zeromatrix(n,1);
my_assignment(A,10);
for k : 1 thru 10 do block(
print(k,A[k,1])
);
------------------------

But I believe there are better variants

Andre

On 07/17/2012 09:42 PM, Zbigniew Komarnicki wrote:
> Hello,
>
> I want to obtain such result
> A[1]: x(l+2, p+1);
> A[2]: x(l+1, p+1);
> A[3]: x(l  , p+1);
>
> but by function. How I can do that? I try in this way but this is not optimal
> way, I suspect. Is maybe better way?
>
> (%i1) fun(var, j):= create_list( var[i+1] = x(l-i+2, p+j) , i, 0, 2);
> (%o1)     fun(var, j) := create_list(var      = x(l - i + 2, p + j), i, 0, 2)
>                                          i + 1
>
> (%i2) a: fun(A,1);
> (%o2)     [A  = x(l + 2, p + 1), A  = x(l + 1, p + 1), A  = x(l, p + 1)]
>              1                     2                     3
>
> (%i3) [aL, aR]: [ map(lhs,a), map(rhs,a) ];
> (%o3)     [[A , A , A ], [x(l + 2, p + 1), x(l + 1, p + 1), x(l, p + 1)]]
>               1   2   3
>
> (%i4) aL;
> (%o4)     [A , A , A ]
>              1   2   3
>
> (%i5) aR;
> (%o5)     [x(l + 2, p + 1), x(l + 1, p + 1), x(l, p + 1)]
>
> (%i6) map(":", aL, aR);
> (%o6)     [x(l + 2, p + 1), x(l + 1, p + 1), x(l, p + 1)]
>
> (%i7) A[1];
> (%o7)     x(l + 2, p + 1)
>
> (%i8) A[2];
> (%o8)     x(l + 1, p + 1)
>
> (%i9) A[3];
> (%o9)     x(l, p + 1)
> (%i10)
>
>
> If I make in this way, this didn't work
> fun(var, j):= create_list( var[i+1] : x(l-i+2, p+j) , i, 0, 2);
>                                     ^^^
>
> (%i1) fun(var, j):= create_list( var[i+1]: x(l-i+2, p+j) , i, 0, 2);
> (%o1)      fun(var, j) := create_list(var      : x(l - i + 2, p + j), i, 0, 2)
>                                           i + 1
>
> (%i2) a: fun(A,1);
> (%o2)      [x(l + 2, p + 1), x(l + 1, p + 1), x(l, p + 1)]
>
> (%i3) A[1];
> (%o3)      A
>              1
>
> (%i4) A[2];
> (%o4)      A
>              2
>
> (%i5) A[3];
> (%o5)      A
>              3
>
> Here are simply symbols A[1], but no assignments is apparent.
>
>
> How to obtain result as I do in (%i6) by function as assignment that is
> visible later e.g.
> A[1];
>      x(l + 2, p + 1)
>
> etc.
>