2

I want to joint symbolic expressions together to create a another symbolic expressions.

one way to do that is as follows:

symb = ToExpression[ToString[#1] <> ToString[#2]] & @@@ {{x, y}, {y, 
    z}, {x, z}}

(*{xy, yz, xz}*)

is there any better way to do this? something like the StringJoin "<>" for strings

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
Basheer Algohi
  • 19,917
  • 1
  • 31
  • 78
  • 3
    You may want to look at SymbolName and Symbol, but I think you are going to have to use string functions as you have done in your example. – mfvonh Jun 25 '14 at 23:14
  • So, what will the result of joining Sin[x] with 1+x+x^2 ? It can not be Sin[x] 1 +x +x^2 ? Is that what you really want? May be there is need to add ( ) around each expression then before joining. However, there is issue where M sometimes removes those ( ) around single expression as not needed. Try typing (x) in the notebook, the result will be x as the () are stripped off. I think the semantics of joining expressions needs more thought. – Nasser Jun 25 '14 at 23:26
  • Symbol[SymbolName@# ~~ SymbolName@#2] & @@@ {{x, y}, {y, z}, {x, z}}? – kglr Jun 25 '14 at 23:27
  • @Nasser that is not what I want. I want to create symbols from symbols in my work. but for your example, will be neat to create Sin from S,i,n? – Basheer Algohi Jun 25 '14 at 23:37
  • It seems we've been down this road before: How to 'merge' a list like FromDigits, but with a mixture of numbers and symbols? -- I shall mark this question as a duplicate unless someone disagrees. – Mr.Wizard Jun 25 '14 at 23:43
  • OK, but you did say joint symbolic expressions together. And Sin[x] and 1 +x +x^2 are symbolic expressions. So that is why I asked. No problem. – Nasser Jun 25 '14 at 23:46
  • @Mr.Wizard yes it is some thing similar. but again, converting to string and back to expression is the trick. not single join function for symbols yet. – Basheer Algohi Jun 25 '14 at 23:49

1 Answers1

1

A bit more terse than your own code:

Symbol /@ ToString /@ Row /@ {{x, y}, {y, z}, {x, z}}
{xy, yz, xz}

Or using SymbolName as suggested by mfvonh in the comments:

Symbol[""<>(SymbolName /@ #)] & /@ {{x, y}, {y, z}, {x, z}}
{xy, yz, xz}

However, both these and yours will fail if a Symbol such as x already has a value.
To get around that you will need to introduce holding somewhere, perhaps like this:

SetAttributes[symbolJoin, HoldAll]

symbolJoin[s__Symbol] := Symbol @ ToString @ HoldForm @ Row[{s}]

Now:

x = 7;

List @@ symbolJoin @@@ Hold[{x, y}, {y, z}, {x, z}]
{xy, yz, xz}

Or:

symbolJoin @@@ Unevaluated[{{x, y}, {y, z}, {x, z}}]
{xy, yz, xz}
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371