# Determine whether some expression contains a symbol doing nothing?

I have expression like this:

expr = xuyz;


then

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

xu*y


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?

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


True

• I think you should write Max[...] > 1 for getting True because of xu and gh. – Davit Shahnazaryan May 8 '17 at 6:42
ClearAll[testVars]

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_] :=
AllTrue[
StringLength /@ SymbolName /@
Cases[expr, Except[_?NumericQ, _?AtomQ], ∞], # == 1 &]

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


False

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


True