3
$\begingroup$

I would like to figure out how to construct an array using subscripts and superscripts based on the following info:

Cs = {1, 3, 5};
Ss = {1, -1, 1};
Rules = {1 -> "+", -1 -> "-"};
Ss1 = Ss /. Rules;

If I try, for example:

{SubsuperscriptBox["c", Cs[[1]], Ss1[[1]]], 
  SubsuperscriptBox["c", Cs[[2]], Ss1[[2]]], 
  SubsuperscriptBox["c", Cs[[3]], Ss1[[3]]]} // DisplayForm

I get an output of:

$\{c^{+}_1,c^{-}_3,c^{+}_5\}$

This is what I want to get. However, I want to do this more automatically, rather than hard-coding it like I just did. I mean, I should be able to vary the arrays Cs and Ss to something arbitrary. For example, say I have instead:

 Cs = {1, 2, 4, 5};
 Ss = {1, -1, 1, -1};
 Rules = {1 -> "+", -1 -> "-"};
 Ss1 = Ss /. Rules;

I should then get:

$\{c^{+}_1,c^{-}_2,c^{+}_4,c^{-}_5\}$

Of course, I could just change the second block of code, but I want to see if there is a more clean way of doing this, without me needing to manually change it.

$\endgroup$
4
$\begingroup$

You may use, e.g.,

MapThread[
 SubsuperscriptBox["c", ##] &,
 {Cs, Ss1}
 ]
| improve this answer | |
$\endgroup$
4
$\begingroup$
Cs = {1, 2, 4, 5};
Ss = {1, -1, 1, -1};
Ss1 = Ss /. {1 -> "+", -1 -> "-"};

Thread[Subsuperscript["c", Cs, Ss1]]

enter image description here

You can also Apply (@@@) the function Subsuperscript["c", ##]& to pairs of values from Cs and Ss1 (that is, to Transpose[{Cs, Ss1}}]):

Subsuperscript["c", ##] & @@@ Transpose[{Cs, Ss1}]

enter image description here

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.