Message Boards Message Boards

0
|
1293 Views
|
5 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Using same module multiple times to write single line output

I have written this small module to write the n(2S+1)LJ symbol

angsym[n1_, s1_, l1_, j1_] := Module[{n = n1, s = s1, l = l1, j = j1},
  lsym = Piecewise[{{"S", l == 0}, {"P", l == 1}, {"D", l == 2}, {"F",
       l == 3}}];
  Print[Superscript[n + 1, 2 s + 1], Subscript[lsym, j1]]]

This code is working fine. But I want this code to write a single output to symbolize a reaction like: n1(2 S1+1)L1 J1 -> n2 (2 S2 +1)L2 J2 by using this module. I have tried

Print[angsym[n1,s1,l1,j1], "->", angsym[n2,s2,l2,j2]]

This does not work.

POSTED BY: Kundan Kumar
5 Replies
Posted 11 months ago

It would help to understand how you actually want to use these structures, but here's my initial suggestion. You don't need Module and you don't need Print. Try something like this:

angsym2[n_, s_, l_, j_] :=
 With[
  {lsym = Switch[l, 0, "S", 1, "P", 2, "D", 3, "F", _, 0]},
  Row[{Superscript[n + 1, 2 s + 1], Subscript[lsym, j]}]]

You also don't need Print to display your rule example:

angsym2[n1, s1, l1, j1] -> angsym2[n2, s2, l2, j2]
POSTED BY: Eric Rimbey

Thanks for the answer. It works fine. I didn't know about the Switch command. Using the Print inside module was not working. Using Row and Return is working. Below is the code

angsym[n1_, s1_, l1_, j1_] := Module[{n = n1, s = s1, l = l1, j = j1},
  lsym = Piecewise[{{"S", l == 0}, {"P", l == 1}, {"D", l == 2}, {"F",
       l == 3}}];
  rowout = Row[{Superscript[n + 1, 2 s + 1], Subscript[lsym, j1]}];
  Return[rowout]]

And the output can be taken in same manner.

POSTED BY: Kundan Kumar
Posted 11 months ago

The Print was actually working, you just didn't understand what was happening. Also, you don't need the Return in your Module. The resulting value of a Module is simply the last expression in the Module.

POSTED BY: Eric Rimbey
Posted 11 months ago

Also, you're free to write your code however you want, of course, but I'm curious... After learning about Switch, and after acknowledging that my simpler code works, why do you revert back to using Piecewise, Module, and Print?

POSTED BY: Eric Rimbey

I was being lazy about changing Piecewise to Switch. But I needed to understand modules better because I have to use Module functions made by others and I wanted to change them according to my needs. So, I am sticking to Module. Print is an odd command to use, but I didn't know about the use of Row like that. Plus I am using Return because in the other Module, I have many output statements and I need different statements at different times as written output.

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

Group Abstract Group Abstract