make a function that assign value to variable and is visible later
Subject: make a function that assign value to variable and is visible later
From: Stavros Macrakis
Date: Wed, 18 Jul 2012 14:30:13 -0400
Several comments:
create_list is useful for creating a list, e.g. create_list(i,i,1,10). It
can also be used for side-effects, e.g. (x:2, create_list(x:x*x,i,1,10)),
though at that point you might as well use a loop: (x:2, for i thru 10 do
x:x*x).
There is a very big difference between x=1 and x:1. x=1 is simply a
mathematical expression with no side effects. x:1 assigns the value 1 to
the variable x. Maxima conflates the mathematical sense of a variable with
the programming sense of a variable, but they are really somewhat different
(though with overlaps).
-s
On Tue, Jul 17, 2012 at 3:42 PM, Zbigniew Komarnicki <cblasius at gmail.com>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);
> ^^^
>
> Please see this code:
> (%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.
>
> Thank you in advance,
> Zbigniew
> _______________________________________________
> Maxima mailing list
> Maxima at math.utexas.edu
> http://www.math.utexas.edu/mailman/listinfo/maxima
>