Function Repository Resource:

AllDependentVariables (1.2.1) current version: 1.2.2 »

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.
Similar to Variables, ResourceFunction["AllDependentVariables"] also has the Modulus option.

Examples

Basic Examples (1) 

Use AllDependentVariables to get the expression that matches to be a mathematical solution:

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

Scope (8) 

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", ResourceVersion->"1.2.1"][{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 system of delay differential equations:

In[6]:=
ResourceFunction["AllDependentVariables", ResourceVersion->"1.2.1"][{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}, t]
Out[6]=

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

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

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

In[8]:=
ResourceFunction["AllDependentVariables"][
 3 D[u[x, y], x] + 5 D[u[x, y], y] == x, {x, y}]
Out[8]=

Use AllDependentVariables with a singular Abel integral equation:

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

Options (2) 

Modulus (2) 

Find dependent variables present after reducing coefficients modulo 2:

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

For polynomials, AllDependentVariables and Variables gives the same results:

In[11]:=
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[11]=

Applications (3) 

Use AllDependentVariables to define a simple function to compute the equations of motion for a given Hamiltonian:

In[12]:=
HamiltonianEqns[H_, time_Symbol] := Module[{sols, classcoordsprev, classcoords, momentaeqns, velseqns},
  sols = ResourceFunction["AllDependentVariables"][H, time];
  classcoordsprev = GatherBy[sols, FreeQ[Characters[ToString[Head[#]]], "p"] &];
  classcoords = Which[Length[classcoordsprev] == 1, Join @@ List[classcoordsprev, Transpose[
       Map[ToExpression@*StringJoin, Map[DeleteCases[#, "p"] &@*Characters@*ToString, Transpose@classcoordsprev]]]], Length[classcoordsprev] == 2 && Length[classcoordsprev[[1]]] === Length[classcoordsprev[[2]]], classcoordsprev, Length[classcoordsprev] == 2 && Length[classcoordsprev[[1]]] =!= Length[classcoordsprev[[2]]], Join @@ List[{classcoordsprev[[1]]}, {Transpose[
        Map[ToExpression@*StringJoin, Map[DeleteCases[#, "p"] &@*Characters@*ToString, Transpose@classcoordsprev[[1]]]]]}]];
  momentaeqns = Thread[D[classcoords[[1]], time] == Simplify[Map[-D[H, #] &, classcoords[[2]]]]];
  velseqns = Thread[D[classcoords[[2]], time] == Simplify[Map[D[H, #] &, classcoords[[1]]]]];
  Flatten[Transpose[List[velseqns, momentaeqns]]]]

Use HamiltonianEqns with the Hamiltonian for the spherical pendulum:

In[13]:=
H1 = -(1/2) g l^3 m^2 Cos[\[Theta][t]] + p\[Theta][t]^2/(2 l^2 m) + (
   Csc[\[Theta][t]]^2 p\[Phi][t]^2)/(2 l^2 m);
In[14]:=
HamiltonianEqns[H1, t]
Out[14]=

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

In[15]:=
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[16]:=
HamiltonianEqns[H2, t]
Out[16]=

Properties and Relations (2) 

Use AllDependentVariables with the resource function SymbolToSubscript:

In[17]:=
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[18]:=
Through[Map[ToExpression, Map[If[FreeQ[Characters[ToString[#]], "p"] === False, Subscript[#[[1]], Subscript[#[[2]], #[[3]]]], ResourceFunction["SymbolToSubscript"][StringJoin@#]] &, Map[Characters@*ToString@*Head, ResourceFunction["AllDependentVariables"][H2, t]]], {-1}][
  Variables[
    Level[ResourceFunction["AllDependentVariables"][H2, t], {-1}]][[
   1]]]]
Out[18]=

Use AllDependentVariables with the resource functions FormalizeSymbols and SolutionRulesToFunctions:

In[19]:=
L = 1/2 m l^2 (\[Theta]'[t]^2 + Sin[\[Theta][t]]^2*\[Phi]'[t]^2) + m g l Cos[\[Theta][t]];
In[20]:=
L /. ResourceFunction["SolutionRulesToFunctions"][
  Thread[ResourceFunction["AllDependentVariables"][L, t] -> Through[ReleaseHold@
      HoldForm[ResourceFunction]["FormalizeSymbols"][
        Head /@ ResourceFunction["AllDependentVariables"][L, t]][t]]]]
Out[20]=
In[21]:=
ResourceFunction["AllDependentVariables"][%, t]
Out[21]=

Possible Issues (1) 

AllDependentVariables don't recognize curried functions as dependent variables:

In[22]:=
ResourceFunction["AllDependentVariables", ResourceVersion->"1.2.1"][{x[t][a], Cos[x[t][a]], m*f[t][b]}, t]
Out[22]=

Neat Examples (2) 

AllDependentVariables looks inside nested functions:

In[23]:=
Nest[f, x[t], 10]
Out[23]=
In[24]:=
ResourceFunction["AllDependentVariables"][%, t]
Out[24]=

AllDependentVariables threads composite functions to obtain the dependent variables:

In[25]:=
Composition[Sec, Tan, Sin][\[Theta][t] + \[Phi][t]]
Out[25]=
In[26]:=
ResourceFunction["AllDependentVariables"][%, t]
Out[26]=

Publisher

Ramón Eduardo Chan López

Version History

  • 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