Function Repository Resource:

AllDependentVariables (1.0.0) 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.

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 (2) 

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.0.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]=

Options (2) 

Modulus (2) 

Find dependent variables present after reducing coefficients modulo 2:

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

For polynomials, AllDependentVariables and Variables gives the same results:

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

Applications (3) 

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

In[6]:=
HamiltonianEquations[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 HamiltonianEquations with the Hamiltonian for the spherical pendulum:

In[7]:=
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[8]:=
HamiltonianEquations[H1, t]
Out[8]=

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

In[9]:=
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[10]:=
HamiltonianEquations[H2, t]
Out[10]=

Properties and Relations (2) 

Use AllDependentVariables with the resource function SymbolToSubscript:

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

Use AllDependentVariables with the resource functions FormalizeSymbols and SolutionRulesToFunctions:

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

Possible Issues (1) 

AllDependentVariables don't recognize curried functions as dependent variables:

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

Neat Examples (1) 

AllDependentVariables threads composite functions to obtain the dependent variables:

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

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