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.
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", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][a x1[t], t]
Out[1]=

Scope (8) 

Use AllDependentVariables to get the expressions that match dependent variable:

In[2]:=
ResourceFunction[
 "AllDependentVariables", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][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", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][{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", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][y'[x] + x y[x] == Cos[x], x]
Out[4]=

Use AllDependentVariables with a differential equation with a piecewise coefficient:

In[5]:=
ResourceFunction[
 "AllDependentVariables", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][y'[x] + Clip[x]^2 y[x] == 0, x]
Out[5]=

Use AllDependentVariables with a system of delay differential equations:

In[6]:=
ResourceFunction[
 "AllDependentVariables", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][{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", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][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", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][
 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", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][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", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][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", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][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", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][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]]] /. {Csc[any_]^2 :> 1/HoldForm[Sin][any]^2, Cot[any_] :> HoldForm[Cos][any]/HoldForm[Sin][any]}]

Use HamiltonianEqns with the Hamiltonian for the spherical pendulum:

In[13]:=
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[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 (3) 

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]:=
ResourceFunction["SymbolToSubscript"][
 ResourceFunction[
  "AllDependentVariables", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][H2, t], "NestedIndices"]
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[# -> Through[ReleaseHold@
        HoldForm[ResourceFunction]["FormalizeSymbols"][Head /@ #][
         t]]]] &@ResourceFunction[
  "AllDependentVariables", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][L, t]
Out[20]=
In[21]:=
ResourceFunction[
 "AllDependentVariables", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][%, t]
Out[21]=

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

In[22]:=
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[23]:=
ResourceFunction["EulerEquations"][#, ResourceFunction[
    "AllDependentVariables", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][#, t], t] &@Ldp
Out[23]=

Possible Issues (1) 

AllDependentVariables don't recognize curried functions as dependent variables:

In[24]:=
ResourceFunction[
 "AllDependentVariables", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][{x[t][a], Cos[x[t][a]], m*f[t][b]}, t]
Out[24]=

Neat Examples (2) 

AllDependentVariables looks inside nested functions:

In[25]:=
Nest[f, x[t], 10]
Out[25]=
In[26]:=
ResourceFunction[
 "AllDependentVariables", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][%, t]
Out[26]=

AllDependentVariables threads composite functions to obtain the dependent variables:

In[27]:=
Composition[Sec, Tan, Sin][\[Theta][t] + \[Phi][t]]
Out[27]=
In[28]:=
ResourceFunction[
 "AllDependentVariables", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][%, t]
Out[28]=

Publisher

Ramón Eduardo Chan López

Requirements

Wolfram Language 13.0 (December 2021) or above

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