Re: How to construct pure expressions
- To: mathgroup at christensen.cybernetics.net
- Subject: [mg733] Re: [mg631] How to construct pure expressions
- From: villegas (Robert Villegas)
- Date: Wed, 12 Apr 1995 01:53:17 -0500
- Keywords: MapIndexed, index, atom
- Organization: Wolfram Research, Inc.
> x[
> x1[
> x11[
> x111[ x1111, x1112, x1113],
> x112[ x1121, x1122, x1123],
> x113[ x1131, x1132, x1133]
> ],
..
> In general, the list will have following properties:
> * All expressions has a head that starts with the letter x.
>
> * The number after x correspond to the position list of that element.
For
> example, x211 is an element with position {2,1,1}.
>
> * The number of digits following x correspond to the level of that
element.
> For example, x111, x211 are elements in level 3, x1 or x2 is in level
1,
> x is in level 0 because it has zero digits following x.
Hello Xah,
If you first make a dummy array that has the right structure, and then
replace all the entries and the heads with "x" of the indices, you
can do it with a short command. Note that all the places where your
example wants x... are atomic, in that they're either bottom-level
entries or they are heads. In Mathematica, this means we want to target
level {-1} and we want Heads->True.
Example for a 2 x 2 matrix, but slightly modifying the form of the x's
from your example, just as an illustration:
In[1]:= MapIndexed[x @@ DeleteCases[#2, 0] &,
Table[Null, {3}, {3}], {-1}, Heads->True]
Out[1]= x[][x[1][x[1, 1], x[1, 2], x[1, 3]], x[2][x[2, 1], x[2, 2], x[2,
3]],
> x[3][x[3, 1], x[3, 2], x[3, 3]]]
That makes it clear what MapIndexed is doing, and we can then just
embellish it by replacing the "x @@ DeleteCases" business with a
pre-defined function that takes care of creating the "x" symbols:
MakeSymbol[base_String, {indices___, 0} | {indices___}] :=
ToExpression @ StringJoin[base, ToString /@ {indices}]
IndexedExpression[base_String, dims:{___Integer?Positive}] :=
MapIndexed[MakeSymbol["x", #2]&, Array[Null &, dims], {-1}, Heads->True]
(* I switched to using Array to make the code simpler given the way
your example inputs dimensions *)
Here's a test:
In[4]:= IndexedExpression["x", {3, 3, 3}]
Out[4]= x[x1[x11[x111, x112, x113], x12[x121, x122, x123],
> x13[x131, x132, x133]], x2[x21[x211, x212, x213],
> x22[x221, x222, x223], x23[x231, x232, x233]],
> x3[x31[x311, x312, x313], x32[x321, x322, x323], x33[x331, x332,
x333]]]
Robby Villegas