TransWikia.com

Creating an Array with Subscripts and Superscripts

Mathematica Asked by Vegeta the Prince of Saiyans on April 19, 2021

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.

2 Answers

You may use, e.g.,

MapThread[
 SubsuperscriptBox["c", ##] &,
 {Cs, Ss1}
 ]

Correct answer by Henrik Schumacher on April 19, 2021

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

Answered by kglr on April 19, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP