2

I have several product states, represented by a string of variables - for example, "ABC". I'd like to multiply this state by a constant, call it J, to which I don't want to pre-assign a value. Using J * "ABC" as my input, the output returns: ABC J. How do I get Mathematica to display the output as J ABC instead?

Thanks a lot! :)

user38762
  • 121
  • 1

3 Answers3

3

You can use NonCommutativeMultiply instead, since it does not have the Orderless attribute:

J ** ABC
Karsten7
  • 27,448
  • 5
  • 73
  • 134
Jokeur
  • 59
  • 2
2

Input

sample = "ABC";
data = "J";
data <> " " <> sample

Output

"J ABC"

Reference

StringJoin (<>)
String Manipulation

e.doroskevic
  • 5,959
  • 1
  • 13
  • 32
0

I don't recommend using Strings that you append to to represent objects. Use instead a sequence of the names wrapped inside a custom Head, something like

state[a, b, c]

and can add to the state via Prepend, Append, or most generally Insert depending on where you want to add the new state:

Append[state[a, b, c], j]
(* state[a, b, c, j] *)
Prepend[state[a, b, c], j]
(* state[j, a, b, c] *)
Insert[state[a, b, c], j, 2]
(* state[a, j, b, c] *)

Alternatively,

SetAttributes[state, Flat]
state[state[a, b, c], d]
state[d, state[a, b, c]]
(* state[a, b, c, d] *)
(* state[d, a, b, c] *)
march
  • 23,399
  • 2
  • 44
  • 100