I want to check if a user input the function with all the specified variables or not. For that I choose the replace variables with some values and check for if the result is a number or not via a doloop. I am thinking there might be more elegant way of doing it such as ReplaceList
but it is not working the way I want it.
Lets assume
u = z^2 Sin[π x] + z^4 Cos[π y] + I y^6 Cos[2 π y] + w;
(*and user give variables as *)
vas = {x, y, z, w};
(* I need to check if all the variables are in the function *)
Do[
u = u /. vas[[i]] -> 1.1;
(* 1.1 is within where the function is going to get \
evaluated *)
If[i == 4, numc9 = NumericQ[u]; Print[numc9];];
(* if numc9 False either there infinity or one of \
the variables in the list is not present in the function or function \
has extra variable(s) *)
Print[u];
, {i, 4}]
Is there more elegant way doing it?
EDIT I
After @Mr.Wizard 's answer I realized that my question was not covering everything I wanted. @Mr. Wizard answer is working, if I was checking all the variables are present in u. However, at the same time I want to check if there is no extra variables in u. Because at the end I want to evaluate u using vars and if u has an extra variable I won't get a value at the end.
For example:
u = z^2 Sin[π x] + z^4 Cos[π y] + I y^6 Cos[2 π y] + w + z^p;
vas = {z, x, y, p};
Level and FreeQ commands give all the variables in function u. After that you check if all the variables in vas are present in this list of variables coming from Level or FreeQ and in the example above its.
In this situation @J.M. undocumented command does what I need. Or I will need to stick with my DoLoop.
u = y z^2 w^3
? Would that be valid or invalid if the variables are specified as{x, y, z, w}
? Or does the user have to specify exactly the variables that literally occur? $\endgroup$vas = {}
? (Your Do-loop allows constant functions, BTW, but the accepted answer does not. It also allows some variables to be missing. Changeu
tou = 1
, for example.) $\endgroup$u= y z^2 w^3
is an acceptable function and user can enter vas={x,y,z,w} andSort[vas] === Variables @ Level[u, {-1}]
result in False so I won't do the calculation (Because there is one extra variable.) I checked u=1; with the accepted answer and it does work. The rhs produce {} and is it not equivalent with Sort[vas]. About vas, the user will actually enter set of variables with set of limits like {x,-1,1},{w,0,2} for the u function. I will subtract variables from there. $\endgroup$NIntegrate[]
in that case. I'll try to work up an answer. $\endgroup$