Follow-up to : trying to use "get" inside a Maxima function - problem using hashed arrays (FYI)
Subject: Follow-up to : trying to use "get" inside a Maxima function - problem using hashed arrays (FYI)
From: Leo Butler
Date: Fri, 18 Mar 2011 10:32:36 +0000 (GMT)
On Thu, 17 Mar 2011, Stavros Macrakis wrote:
< Bruce,
< I think there is a confusion here. ?You said you wanted to attach a quality to a *variable*. ?But I don't think that's what you wanted to do. ?You want a *value* which includes both the matrix and some additional properties.
< ?After all, if you attach a quality to a variable, that quality won't be assigned or bound to a different variable when you assign or bind the *value* of that variable to *another* variable.
<
< Back to the container vs. the contents. ?If you put a green apple in a red box, it doesn't become red, and if you transfer the contents of the red box to a blue box, it is no longer in a red box (and of course it remains
< green).
<
< There is yet another possibility. ?If the qualities you're assigning are not *additional* annotations which vary from case to case, but in fact properties of the value itself, you *can* use hashed arrays, but instead of
< something like variable['property_name]: property_value, you'd use property_name[object_value]: property_value. ?For example, you could have prime[3]:true or factors[12]:[2,3,4,6]. ?In the matrix case, this could be useful for
< (e.g.) annotating a matrix as upper-diagonal or not. ?But it would *not* be useful to attach the basis, since the same matrix value can be used with different bases.
<
< ?? ? ? ? ? ?-s
<
<
<
< On Wed, Mar 16, 2011 at 11:44, Bruce Linnell <brlinnell at verizon.net> wrote:
< Recap : my original goal was to attach a "quality" to a variable?that?has?a value, so that when?the variable?was passed to a function, the function could take the appropriate steps based on the quality.? In
< particular, I'm trying to add to the CTensor package functionality, which requires knowing whether a tensor has upper and/or lower indices.? My goal is to be able to manipulate vectors, matrices, and arrays
< containing equations in order to?multiply tensors, take the covariant derivative of them, raise/lower indices, etc.
Bruce,
Here is one way to achieve what you want above (and as Stavros says,
this is not your original stated goal).
mt(T):=block([TensorFactory],
TensorFactory:buildq([T:T],
lambda([],T[val]:matrix([5,5],[5,5]),
T[rank]:'Trank2UU,T)),apply(TensorFactory,[]))$
The key here is that buildq lets you build a TensorFactory each
time you call mt with a distinct symbol. Buildq substitutes in
the symbol in place of T and voila, you have a custom-built
anonymous function (that's the lambda) that takes no arguments.
The final step applies that function to do the work.
mt(X);
==> X
X[val];
==> matrix([5,5],[5,5])
If you want to see what TensorFactory produces, just remove the last
step and have mt return TensorFactory.
This business could be hidden from the user (you) by writing a custom
macro, since the definition of mt can be mechanically constructed from
the contents of lambda([],...).
I imagine this is one of the motivations to use structures.
Leo
--
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.