Skip to main content
Tweeted twitter.com/StackMma/status/760100802790842368
deleted 3 characters in body
Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k

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  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.

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.

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.

edited tags; edited tags
Link
deleted 17 characters in body
Source Link
Erdem
  • 889
  • 4
  • 11

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 thethat my question was not covering everything I wanted. Both @Mr @Mr. Wizard answers work answer is working, if I was checking only if all all the variables are present in u but. 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 can'twon'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. However, when I evaluate the u using vas the final answer is not going to be a number.
In

In this situation @J.M. undocumented command does what I need. Or I will need to stick with my DoLoop.

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 the my question was not covering everything I wanted. Both @Mr. Wizard answers work if I was checking only if all the variables are present in u but at the same time I want to check if no extra variables in u. Because at the end I want to evaluate u using vars and if u has an extra variable I can't.

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 variables and in the example above its. However, when I evaluate the u using vas the final answer is not going to be a number.
In this situation @J.M. undocumented command does what I need. Or I will need to stick with my DoLoop.

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.

deleted 12 characters in body
Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k
Loading
added 852 characters in body
Source Link
Erdem
  • 889
  • 4
  • 11
Loading
added 52 characters in body
Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k
Loading
Source Link
Erdem
  • 889
  • 4
  • 11
Loading