Function Repository Resource:

DependentVariableQ

Source Notebook

Determine whether an expression is a dependent variable

Contributed by: E. Chan-López & Jorge Luis Ramos Castellano

ResourceFunction["DependentVariableQ"][expr,vars]

gives True if expr depends on one or more variables vars, regardless of their order, and gives False otherwise. Does not enforce strict dependency or order; it only checks that at least one variable in vars is present in expr.

Details and Options

ResourceFunction["DependentVariableQ"] threads over lists.
ResourceFunction["DependentVariableQ"][expr,vars,"StrictMode" False] when set to True, specifies whether a strict dependency and order check should be applied. The default is False.

Examples

Basic Examples (1) 

Use DependentVariableQ to identify a variable that depends on a single variable:

In[1]:=
ResourceFunction["DependentVariableQ"][x[t], t]
Out[1]=

Scope (3) 

Use DependentVariableQ with a list of variables that depend on a single variable:

In[2]:=
ResourceFunction["DependentVariableQ"][{x[t], y[t]}, t]
Out[2]=

Use DependentVariableQ with a variable that depends on two variables:

In[3]:=
ResourceFunction["DependentVariableQ"][x[t, s], {t, s}]
Out[3]=

These are not dependent variables:

In[4]:=
ResourceFunction[
 "DependentVariableQ"][{Tan[\[Theta]], Cos[\[Theta]]}, \[Theta]]
Out[4]=

Options (2) 

StrictMode (2) 

By default, DependentVariableQ checks whether an expression depends on at least one of the supplied variables, regardless of the order or whether there are additional dependencies. This means that the function does not distinguish between exclusive or non-exclusive dependencies, nor does it consider the order of the variables. For example:

In[5]:=
ResourceFunction["DependentVariableQ"][x[t, s], #] & /@ {t, s, {t, s}, {s, t}}
Out[5]=

When the "StrictMode"True option is enabled, DependentVariableQ verifies that the expression depends exclusively on the provided variables and that the order of the variables matches exactly. This is useful when strict control over dependencies and their order is required. For example:

In[6]:=
ResourceFunction["DependentVariableQ"][x[t, s], #, "StrictMode" -> True] & /@ {t, s, {t, s}, {s, t}}
Out[6]=

Applications (8) 

Define a simple function to identify all dependent variables of a single variable for a given Lagrangian and Euler-Lagrange equations:

In[7]:=
GeneralizedCoordenates[fun_, idpvar_] := With[{d = Depth[fun]},
  Union@ReplaceAll[Derivative[_][x_Symbol][y_Symbol] :> x[y]][
    Cases[CurryApplied[Level[##, Heads -> True] &, {2, 1}][d]@fun, Derivative[_][s1_Symbol][s2_Symbol] | s1_Symbol[s2_Symbol] /; ResourceFunction["DependentVariableQ"][s1[s2], s2], {0, \[Infinity]}]]]

Lagrangian for the double pendulum:

In[8]:=
L1 = 1/6 m l^2 (\[Theta]2'[t]^2 + 4 \[Theta]1'[t]^2 + 3 \[Theta]1'[t]*\[Theta]2'[t]*
       Cos[\[Theta]1[t] - \[Theta]2[t]]) + 1/2 m g l (3 Cos[\[Theta]1[t]] + Cos[\[Theta]2[t]]);
In[9]:=
GeneralizedCoordenates[L1, t]
Out[9]=

Lagrangian for the spherical pendulum:

In[10]:=
L2 = 1/2 m l^2 (\[Theta]'[t]^2 + Sin[\[Theta][t]]^2*\[Phi]'[t]^2) + m g l Cos[\[Theta][t]];
In[11]:=
GeneralizedCoordenates[L2, t]
Out[11]=

Lagrangian for the PUMA-Like Robot:

In[12]:=
L3 = 1/2 m2 (x2'[t]^2 + y2'[t]^2 + z2'[t]^2) + 1/2 m3 (x3'[t]^2 + y3'[t]^2 + z3'[t]^2) + 1/2 (C1 + C2 Cos[\[Theta]2[t]]^2 + C3 Cos[\[Theta]3[t]]^2 + B2 Sin[\[Theta]2[t]]^2 + B3 Sin[\[Theta]3[t]]^2) \[Psi]1'[
     t]^2 + 1/2 A2 \[Theta]2'[t]^2 + 1/2 A3 \[Theta]3'[t]^2 - g (m2 z2 + m3 z3);
In[13]:=
GeneralizedCoordenates[L3, t]
Out[13]=

Use GeneralizedCoordenates with the resource function EulerEquations to compute the corresponding Euler-Lagrange equations of motion for the previously defined Lagrangians:

Euler-Lagrange equations for the double pendulum:

In[14]:=
ResourceFunction["EulerEquations"][L1, GeneralizedCoordenates[L1, t],
  t]
Out[14]=

Euler-Lagrange equations for the spherical pendulum:

In[15]:=
ResourceFunction["EulerEquations"][L2, GeneralizedCoordenates[L2, t],
  t]
Out[15]=

Euler-Lagrange equations for the PUMA-Like Robot:

In[16]:=
ResourceFunction["EulerEquations"][L3, GeneralizedCoordenates[L3, t],
  t]
Out[16]=

Properties and Relations (4) 

Use DependentVariableQ with the resource function SolutionRulesToFunctions to convert solution rules to function rules in a given list containing rules whose left-hand side don't match with a variable that depends on other variables:

In[17]:=
Cases[{m, s, q, y[t] -> a x[t], z[t] -> c b w[t]}, Rule[a_ /; ResourceFunction["DependentVariableQ"][a, t], b_] :> ResourceFunction["SolutionRulesToFunctions"][Rule[a, b]]]
Out[17]=

Use DependentVariableQ with the resource function SolutionRulesToFunctions on a more complicated list:

In[18]:=
Cases[{1, Cos[t], m, s, q, y[t] -> a x[t], sol[x, t] -> Sinc[x - t]}, Rule[a_ /; ResourceFunction["DependentVariableQ"][a, t], b_] :> ResourceFunction["SolutionRulesToFunctions"][Rule[a, b]]]
Out[18]=

Use DependentVariableQ with the resource function SymbolToSubscript:

In[19]:=
Cases[{m, s, q, y1[t] -> a x1[t], z2[t] -> c b w2[t]}, Rule[a_ /; ResourceFunction["DependentVariableQ"][a, t], b_] :> ResourceFunction["SymbolToSubscript"]@
   ResourceFunction["SolutionRulesToFunctions"][Rule[a, b]]]
Out[19]=

Use DependentVariableQ with the resource function FormalizeSymbols:

In[20]:=
Through[ReleaseHold@
  HoldForm[ResourceFunction]["FormalizeSymbols"][
    ToExpression@CharacterRange["\[Alpha]", "\[Kappa]"]][t]]
Out[20]=
In[21]:=
ResourceFunction["DependentVariableQ"][Out[23], t]
Out[21]=

Possible Issues (1) 

DependentVariableQ only identifies dependent variables that match the pattern head_Symbol[args___Symbol]:

In[22]:=
ResourceFunction["DependentVariableQ"][y[1][t], t]
Out[22]=

Publisher

Ramón Eduardo Chan López

Requirements

Wolfram Language 13.0 (December 2021) or above

Version History

  • 1.2.0 – 10 March 2025
  • 1.1.1 – 21 July 2023
  • 1.1.0 – 26 June 2023
  • 1.0.1 – 01 May 2023
  • 1.0.0 – 17 April 2023

Source Metadata

Related Resources

Author Notes

The present implementation has been enhanced with invaluable suggestions from kindly reviewers within the Wolfram Team.

License Information