2
$\begingroup$

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

$\endgroup$
7
  • 3
    $\begingroup$ 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. $\endgroup$
    – mfvonh
    Jun 25, 2014 at 23:14
  • $\begingroup$ 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. $\endgroup$
    – Nasser
    Jun 25, 2014 at 23:26
  • $\begingroup$ Symbol[SymbolName@# ~~ SymbolName@#2] & @@@ {{x, y}, {y, z}, {x, z}}? $\endgroup$
    – kglr
    Jun 25, 2014 at 23:27
  • $\begingroup$ @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? $\endgroup$ Jun 25, 2014 at 23:37
  • $\begingroup$ 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. $\endgroup$
    – Mr.Wizard
    Jun 25, 2014 at 23:43

1 Answer 1

1
$\begingroup$

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}
$\endgroup$
0

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