I have expression like this:

expr = xuyz; 


Head[expr] = xuyz

But I wanted the product of four factors, so it should have been written as x*u*y*z or x u y z, because Mathematica understands multiplication of four single-character variables in these two ways only.

So I need to detect whether or not an expression is written in the right way. By "right" I mean all variables should are required to be single-character, all factors to be such variables or function thereof, and all terms to be products of such factors .

I have tried with:

FreeQ[expr, Times].

But it doesn't help, because I can have the situation


which is not right for me, because xu and y are are the factors and I want x, u and y to be the factors.

So is there any way to detect this problem?


4 Answers 4

f1 = Max@StringLength[SymbolName /@ Variables@#] == 1 &;

f1 /@ {xu*y, 3 x + w z^2}

{False, True}


This returns True because of xu and/or gh

Max[StringLength[ToString /@ DeleteCases[Level[xu*y/gh, {-1}], _?NumericQ]]] > 1


  • 1
    $\begingroup$ I think you should write Max[...] > 1 for getting True because of xu and gh. $\endgroup$ May 8, 2017 at 6:42

testVars::usage = 
  "testVars[expr, allowedVars] returns variables appearing in expr that \
are not in the list allowedVars.";

testVars[expr_, allowedVars_?VectorQ] :=
 Complement[Variables[Level[expr, {-1}]], allowedVars]

vars = {x, y, z};

testVars[xyx, vars]

(*  {xyx}  *)

I haven't thoroughly tested the following function, but at least it might be a good starting point for you to solve your problem.

singleCharVarsQ[expr_] := 
    StringLength /@ SymbolName /@ 
      Cases[expr, Except[_?NumericQ, _?AtomQ], ∞], # == 1 &]

singleCharVarsQ[x y z + x Sqrt[x^2 + y^2] - Cos[xy]]


singleCharVarsQ[x y z + x Sqrt[x^2 + y^2] - Cos[x y]]



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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