Group Abstract Group Abstract

Message Boards Message Boards

0
|
1.5K Views
|
6 Replies
|
1 Total Like
View groups...
Share
Share this post:

Create an association using multiple manner of keys to retrieve the values

Posted 3 years ago

I've the following association:

In[95]:= BasicVectors
BasicVectors[1]==BasicVectors["TricPrim"]

Out[95]= <|"TricPrim" -> {{a, 0, 0}, {b Cos[\[Gamma]], 
    b Sin[\[Gamma]], 0}, {c Cos[\[Beta]], 
    c (-Cos[\[Beta]] Cot[\[Gamma]] + Cos[\[Alpha]] Csc[\[Gamma]]), 
    c Sqrt[-(-Cos[\[Beta]] Cot[\[Gamma]] + 
         Cos[\[Alpha]] Csc[\[Gamma]])^2 + Sin[\[Beta]]^2]}}, 
 "MonoPrim" -> {{0, -b, 0}, {a Sin[\[Gamma]], -a Cos[\[Gamma]], 
    0}, {0, 0, c}}, 
 "MonoBase" -> {{0, -b, 
    0}, {1/2 a Sin[\[Gamma]], -(1/2) a Cos[\[Gamma]], -(c/2)}, {1/
     2 a Sin[\[Gamma]], -(1/2) a Cos[\[Gamma]], c/2}}, 
 "OrthPrim" -> {{0, -b, 0}, {a, 0, 0}, {0, 0, c}}, 
 "OrthBase" -> {{a/2, -(b/2), 0}, {a/2, b/2, 0}, {0, 0, c}}, 
 "OrthBody" -> {{a/2, b/2, c/2}, {-(a/2), -(b/2), c/2}, {a/
    2, -(b/2), -(c/2)}}, 
 "OrthFace" -> {{a/2, 0, c/2}, {0, -(b/2), c/2}, {a/2, -(b/2), 0}}, 
 "TetrPrim" -> {{a, 0, 0}, {0, a, 0}, {0, 0, c}}, 
 "TetrBody" -> {{-(a/2), a/2, c/2}, {a/2, -(a/2), c/2}, {a/2, a/
    2, -(c/2)}}, 
 "TrigPrim" -> {{0, -a, c}, {(Sqrt[3] a)/2, a/2, 
    c}, {-((Sqrt[3] a)/2), a/2, c}}, 
 "HexaPrim" -> {{0, -a, 0}, {(Sqrt[3] a)/2, a/2, 0}, {0, 0, c}}, 
 "CubiPrim" -> {{a, 0, 0}, {0, a, 0}, {0, 0, a}}, 
 "CubiFace" -> {{0, a/2, a/2}, {a/2, 0, a/2}, {a/2, a/2, 0}}, 
 "CubiBody" -> {{-(a/2), a/2, a/2}, {a/2, -(a/2), a/2}, {a/2, a/
    2, -(a/2)}}, 
 1 -> {{a, 0, 0}, {b Cos[\[Gamma]], b Sin[\[Gamma]], 
    0}, {c Cos[\[Beta]], 
    c (-Cos[\[Beta]] Cot[\[Gamma]] + Cos[\[Alpha]] Csc[\[Gamma]]), 
    c Sqrt[-(-Cos[\[Beta]] Cot[\[Gamma]] + 
         Cos[\[Alpha]] Csc[\[Gamma]])^2 + Sin[\[Beta]]^2]}}, 
 2 -> {{0, -b, 0}, {a Sin[\[Gamma]], -a Cos[\[Gamma]], 0}, {0, 0, c}},
  3 -> {{0, -b, 
    0}, {1/2 a Sin[\[Gamma]], -(1/2) a Cos[\[Gamma]], -(c/2)}, {1/
     2 a Sin[\[Gamma]], -(1/2) a Cos[\[Gamma]], c/2}}, 
 4 -> {{0, -b, 0}, {a, 0, 0}, {0, 0, c}}, 
 5 -> {{a/2, -(b/2), 0}, {a/2, b/2, 0}, {0, 0, c}}, 
 6 -> {{a/2, b/2, c/2}, {-(a/2), -(b/2), c/2}, {a/2, -(b/2), -(c/2)}},
  7 -> {{a/2, 0, c/2}, {0, -(b/2), c/2}, {a/2, -(b/2), 0}}, 
 8 -> {{a, 0, 0}, {0, a, 0}, {0, 0, c}}, 
 9 -> {{-(a/2), a/2, c/2}, {a/2, -(a/2), c/2}, {a/2, a/2, -(c/2)}}, 
 10 -> {{0, -a, c}, {(Sqrt[3] a)/2, a/2, c}, {-((Sqrt[3] a)/2), a/2, 
    c}}, 11 -> {{0, -a, 0}, {(Sqrt[3] a)/2, a/2, 0}, {0, 0, c}}, 
 12 -> {{a, 0, 0}, {0, a, 0}, {0, 0, a}}, 
 13 -> {{0, a/2, a/2}, {a/2, 0, a/2}, {a/2, a/2, 0}}, 
 14 -> {{-(a/2), a/2, a/2}, {a/2, -(a/2), a/2}, {a/2, a/2, -(a/2)}}|>

Out[96]= True

As you can see, I want to retrieve the values via both the number index corresponding to their postions and the meaningful keys. But this way makes the association data is long and repetitive.

I want to know if there is a better way to achieve the same purpose.

Regards,
Zhao

POSTED BY: Hongyi Zhao
6 Replies
Posted 3 years ago

But, if you want "uniform access" exactly, maybe you could use Dataset:

Yes. This is exactly what I want to achieve. Thank you again for your wonderful tricks.

Regards, Zhao

POSTED BY: Hongyi Zhao
Posted 3 years ago

I read the goal as "to retrieve the values via both the number index corresponding to their postions and the meaningful keys"

Retrieving a subexpression via index is what Part (the double brackets) does.

But, if you want "uniform access" exactly, maybe you could use Dataset:

BasicVectorsData = Dataset[Take[BasicVectors, 14]];

Now you have:

BasicVectorsData[1] == BasicVectorsData["TricPrim"]
POSTED BY: Eric Rimbey
Posted 3 years ago

Thank you for your comment. This is not a problem, but mainly my personal nitpicking.

Regards, Zhao

POSTED BY: Hongyi Zhao
Posted 3 years ago

But one more layer of square brackets must be used.

POSTED BY: Hongyi Zhao
Posted 3 years ago

Why is that a problem?

POSTED BY: Eric Rimbey
Posted 3 years ago
BasicVectors["TricPrim"] == BasicVectors[[1]]
(*True*)

What this shows is that you can access an Association with Part.

POSTED BY: Eric Rimbey
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard