Function Repository Resource:

AllDependentVariables

Source Notebook

Retrieve a list of all dependent variables for a given expression

Contributed by: E. Chan-López, Jaime Manuel Cabrera & Jorge Mauricio Paulin Fuentes

ResourceFunction["AllDependentVariables"][expr,ivar]

gives all variables dependent on ivar within a given expr.

Details and Options

ResourceFunction["AllDependentVariables"] threads over lists in the first argument.
Using ResourceFunction["AllDependentVariables"][expr,ivar,"StrictMode"True] checks that the expression depends exclusively on ivar and that the order of the variables matches exactly. If the order or the dependencies are different, it returns an empty list. The default setting of "StrictMode" is False.
When using ResourceFunction["AllDependentVariables"][expr,ivar,"IncludeDerivates"True], derivatives of dependent variables are included in the list of dependencies. The default setting of "IncludeDerivates" is False.
Similar to Variables, ResourceFunction["AllDependentVariables"] supports the Modulus option.

Examples

Basic Examples (1) 

Use AllDependentVariables to get the dependent variables:

In[1]:=
ResourceFunction["AllDependentVariables"][x1[t], t]
Out[1]=

Scope (6) 

Use AllDependentVariables to get the expressions that match dependent variable:

In[2]:=
ResourceFunction["AllDependentVariables"][
 a x1[t] + b x2[t] + x1[t]*x2[t], t]
Out[2]=

Use AllDependentVariables with a list of complicated expressions:

In[3]:=
ResourceFunction[
 "AllDependentVariables"][{Exp[x[t, s]] + 2 w[t, s], 1/2 m2 (x2'[t, s]^2 + y2'[t, s]^2 + z2'[t, s]^2)}, {t, s}]
Out[3]=

Use AllDependentVariables with an inhomogeneous first-order ordinary differential equation:

In[4]:=
ResourceFunction["AllDependentVariables"][y'[x] + x y[x] == Cos[x], x]
Out[4]=

Use AllDependentVariables with a differential equation with a piecewise coefficient:

In[5]:=
ResourceFunction["AllDependentVariables"][y'[x] + Clip[x]^2 y[x] == 0,
  x]
Out[5]=

Use AllDependentVariables with a Caputo fractional differential equation of order 1/2:

In[6]:=
ResourceFunction["AllDependentVariables"][
 CaputoD[y[x], {x, 1/2}] - 3 y[x] == 0, x]
Out[6]=

Use AllDependentVariables with a singular Abel integral equation:

In[7]:=
ResourceFunction["AllDependentVariables"][Sin[a x] == \!\(
\*SubsuperscriptBox[\(\[Integral]\), \(0\), \(x\)]\(
\*FractionBox[\(y[t]\), 
SqrtBox[\(x - t\)]] \[DifferentialD]t\)\), t]
Out[7]=

Options (7) 

Modulus (2) 

Find dependent variables present after reducing coefficients modulo 2:

In[8]:=
ResourceFunction["AllDependentVariables"][x[t] + 2 y[t] + 3 z[t], t, Modulus -> 2]
Out[8]=

For polynomials in dependent variables with numeric coefficients, AllDependentVariables and Variables gives the same results:

In[9]:=
ResourceFunction["AllDependentVariables"][x[t] + 2 y[t] + 3 z[t], t, Modulus -> 2] === Variables[x[t] + 2 y[t] + 3 z[t], Modulus -> 2]
Out[9]=

StrictMode (1) 

By default, AllDependentVariables extracts all variables that depend on at least one of the supplied variables, regardless of whether they have additional dependencies or the order in which the variables appear. This means that the function does not distinguish between exclusive or non-exclusive dependencies, nor does it enforce a strict order among the variables. For example, x, w, and R are all considered dependent on t:

In[10]:=
ResourceFunction[
 "AllDependentVariables"][{Exp[x[t, s]] + 2 w[t, s], Cos[\[Theta]], R[t], x[t, s]}, t]
Out[10]=

When the "StrictMode"True option is enabled, AllDependentVariables extracts only those variables that depend exclusively on the provided variables, ensuring that no additional dependencies are present. Additionally, it verifies that the order of the variables matches exactly. This is useful when strict control over dependencies and their order is required. Now, only R depends strictly on t.

In[11]:=
ResourceFunction[
 "AllDependentVariables"][{Exp[x[t, s]] + 2 w[t, s], Cos[\[Theta]], R[t], x[t, s]}, t, "StrictMode" -> True]
Out[11]=

IncludeDerivatives (4) 

By default, AllDependentVariables does not include derivatives when identifying dependent variables. This means that only explicitly present variables are extracted, while derivatives of dependent variables are ignored. For example:

In[12]:=
ResourceFunction["AllDependentVariables"][a*x'[t]*y[t] + b*y'[t]*x[t],
  t]
Out[12]=

When the option "IncludeDerivatives"True is enabled, the function also includes derivatives of dependent variables in the output. For example:

In[13]:=
ResourceFunction["AllDependentVariables"][
 a*x'[t]*y[t] + b*y'[t]*x[t], t, "IncludeDerivatives" -> True]
Out[13]=

Use AllDependentVariables with a system of delay differential equations:

In[14]:=
ddeSystem = {x'[t] == a y[t - 1] + y[t - 3], y'[t] == x[t - 1], x[t /; t <= 0] == t, y[t /; t <= 0] == t^2};
In[15]:=
ResourceFunction["AllDependentVariables"][ddeSystem, t, "IncludeDerivatives" -> True]
Out[15]=

Use AllDependentVariables with a linear first-order partial differential equation:

In[16]:=
ResourceFunction["AllDependentVariables"][
 3 D[u[x, y], x] + 5 D[u[x, y], y] == x, {x, y}, "IncludeDerivatives" -> True]
Out[16]=

Use AllDependentVariables to extract the dependent variables from the ImplicitD derivative that involves the symbolic functions f and g:

In[17]:=
implD = ImplicitD[f[x, y], g[x, y] == 0, y, x];
In[18]:=
ResourceFunction["AllDependentVariables"][implD, {x, y}, "IncludeDerivatives" -> True]
Out[18]=

Applications (3) 

Use AllDependentVariables to define two functions for computing the equations of motion derived from a given Hamiltonian:

In[19]:=
PhaseSpaceVariables[ham_, time_Symbol, momentaLabel_String : "p"] := Module[
   	{momenta, coords},
   	momenta = Cases[ResourceFunction["AllDependentVariables"][ham, time],
     		var_ /; StringStartsQ[ToString[var], momentaLabel]
     	];
   	coords = Map[Function[ToExpression[StringDrop[#, 1]]] @* ToString][momenta];
   	{momenta, coords}
   ];

HamiltonianEquations[ham_, vars_List, time_Symbol, momentaLabel_String : "p"] := Module[
   	{momenta, coords, velsEqns, momentaEqns, HamEqns},
   	{momenta, coords} = vars;
   	velsEqns = Thread @ Equal[D[coords, time], Simplify @ Grad[ham, momenta]];
   	momentaEqns = Thread[Equal[D[momenta, time], Simplify[-Grad[ham, coords]]]];
   	HamEqns = ReplaceAll[Flatten @ Transpose @ {velsEqns, momentaEqns},
     		{
      			Power[Csc[
Pattern[any, 
Blank[]]], 2] :> (1 / HoldForm[Sin][any] ^ 2),
      			Cot[
Pattern[any, 
Blank[]]] :> (HoldForm[Cos][any] / HoldForm[Sin][any])
      		}
     	];
   	HamEqns
   ];

Use HamiltonianEqns with the Hamiltonian for the spherical pendulum:

In[20]:=
H1 = -g l m Cos[\[Theta][t]] + p\[Theta][t]^2/(2 l^2 m) + p\[Phi][t]^2/(2 l^2 m Sin[\[Theta][t]]^2);
In[21]:=
phasespace = PhaseSpaceVariables[H1, t]
Out[21]=
In[22]:=
HamiltonianEquations[H1, phasespace, t]
Out[22]=

Use HamiltonianEqns with the Hamiltonian for the PUMA-Like Robot:

In[23]:=
H2 = g (m2 z2 + m3 z3) + px2[t]^2/(2 m2) + px3[t]^2/(2 m3) + py2[t]^2/(2 m2) + py3[t]^2/(2 m3) + pz2[t]^2/(2 m2) + pz3[t]^2/(
   2 m3) + p\[Theta]2[t]^2/(2 A2) + p\[Theta]3[t]^2/(2 A3) + p\[Psi]1[
     t]^2/(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));
In[24]:=
phasespace = PhaseSpaceVariables[H2, t]
Out[24]=
In[25]:=
HamiltonianEquations[H2, phasespace, t]
Out[25]=

Properties and Relations (3) 

Use AllDependentVariables with the resource function SymbolToSubscript:

In[26]:=
H2 = g (m2 z2 + m3 z3) + px2[t]^2/(2 m2) + px3[t]^2/(2 m3) + py2[t]^2/(2 m2) + py3[t]^2/(2 m3) + pz2[t]^2/(2 m2) + pz3[t]^2/(
   2 m3) + p\[Theta]2[t]^2/(2 A2) + p\[Theta]3[t]^2/(2 A3) + p\[Psi]1[
     t]^2/(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));
In[27]:=
ResourceFunction["SymbolToSubscript"][
 ResourceFunction["AllDependentVariables"][H2, t], "NestedIndices"]
Out[27]=

Use AllDependentVariables with the FormalizeSymbols and SolutionRulesToFunctions resource functions with the "IncludeDerivatives" option to extract dependent variables from expressions that include derivatives of them:

In[28]:=
L = 1/2 m l^2 (\[Theta]'[t]^2 + Sin[\[Theta][t]]^2*\[Phi]'[t]^2) + m g l Cos[\[Theta][t]];
In[29]:=
formula = L /. ResourceFunction["SolutionRulesToFunctions"][
     Thread[# -> Through[ReleaseHold@
         HoldForm[ResourceFunction]["FormalizeSymbols"][Head /@ #][
          t]]]] &@ResourceFunction["AllDependentVariables"][L, t, "IncludeDerivatives" -> True]
Out[29]=
In[30]:=
ResourceFunction["AllDependentVariables"][formula, t, "IncludeDerivatives" -> True]
Out[30]=

Use AllDependentVariables with the resource function EulerEquations to compute the corresponding Euler-Lagrange equations of motion for the double pendulum:

In[31]:=
Ldp = 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[32]:=
vars = ResourceFunction["AllDependentVariables"][Ldp, t]
Out[32]=
In[33]:=
ResourceFunction["EulerEquations"][#, vars, t] &@Ldp
Out[33]=

Possible Issues (1) 

AllDependentVariables don't recognize curried functions as dependent variables:

In[34]:=
ResourceFunction[
 "AllDependentVariables"][{x[t][a], Cos[x[t][a]], m*f[t][b]}, t]
Out[34]=

Neat Examples (2) 

AllDependentVariables looks inside nested functions:

In[35]:=
nested = Nest[f, x[t], 10]
Out[35]=
In[36]:=
ResourceFunction["AllDependentVariables"][nested, t]
Out[36]=

AllDependentVariables threads composite functions to obtain the dependent variables:

In[37]:=
formula = Composition[Sec, Tan, Sin][\[Theta][t] + \[Phi][t]]
Out[37]=
In[38]:=
ResourceFunction["AllDependentVariables"][formula, t]
Out[38]=

Publisher

Ramón Eduardo Chan López

Requirements

Wolfram Language 13.0 (December 2021) or above

Version History

  • 1.2.3 – 19 March 2025
  • 1.2.2 – 20 September 2023
  • 1.2.1 – 28 July 2023
  • 1.2.0 – 21 July 2023
  • 1.1.0 – 05 July 2023
  • 1.0.0 – 21 April 2023

Source Metadata

Related Resources

Author Notes

The current implementation has been enriched with valuable recommendations from the Wolfram Team.

License Information