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, and gives False otherwise.

Details

ResourceFunction["DependentVariableQ"] threads over lists.

Examples

Basic Examples (1) 

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

In[1]:=
ResourceFunction[
 "DependentVariableQ", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][x[t], t]
Out[1]=

Scope (3) 

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

In[2]:=
ResourceFunction[
 "DependentVariableQ", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][{x[t], y[t]}, t]
Out[2]=

Use DependentVariableQ with a variable that depends on two variables:

In[3]:=
ResourceFunction[
 "DependentVariableQ", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][x[t, s], {t, s}]
Out[3]=

These are not dependent variables:

In[4]:=
ResourceFunction[
 "DependentVariableQ", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][{Tan[\[Theta]], Cos[\[Theta]]}, \[Theta]]
Out[4]=

Applications (8) 

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

In[5]:=
DependentVars[g_, indepvar_] := Map[If[ResourceFunction[
      "DependentVariableQ", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][#, indepvar] === True, #, Nothing] &, Sort[DeleteDuplicates@
    ReplaceAll[Derivative[_][x_][_] :> x[indepvar]][
     DeleteDuplicates[
      Flatten[Which[Depth[#] == 1 \[Or] Depth[#] == 2, #, Depth[#] >= 3, Level[#, {Length[Depth[#]] - 2}]] & /@ Level[g, {1}]]]]]]

Lagrangian for the double pendulum:

In[6]:=
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[7]:=
DependentVars[L1, t]
Out[7]=

Lagrangian for the spherical pendulum:

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

Lagrangian for the PUMA-Like Robot:

In[10]:=
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[11]:=
DependentVars[L3, t]
Out[11]=

Use DependentVars with the resource function EulerEquations to compute the corresponding Euler-Lagrange equations of motion for the Lagrangians L1, L2 and L3:

Euler-Lagrange equations for the double pendulum:

In[12]:=
ResourceFunction["EulerEquations"][L1, DependentVars[L1, t], t]
Out[12]=

Euler-Lagrange equations for the spherical pendulum:

In[13]:=
ResourceFunction["EulerEquations"][L2, DependentVars[L2, t], t]
Out[13]=

Euler-Lagrange equations for the PUMA-Like Robot:

In[14]:=
ResourceFunction["EulerEquations"][L3, DependentVars[L3, t], t]
Out[14]=

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[15]:=
Map[If[Head[#] === Rule && ResourceFunction[
      "DependentVariableQ", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][#[[1]], t] === True, ResourceFunction["SolutionRulesToFunctions"][#], Nothing] &, {m, s,
   q, y[t] -> a x[t], z[t] -> c b w[t]}]
Out[15]=

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

In[16]:=
Map[If[Head[#] === Rule && (ResourceFunction[
        "DependentVariableQ", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][#[[1]], t] === True \[Or] ResourceFunction[
        "DependentVariableQ", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][#[[1]], {x, t}] === True), ResourceFunction["SolutionRulesToFunctions"][#], Nothing] &, {1, Cos[t], m, s, q, y[t] -> a x[t], sol[x, t] -> Sinc[x - t]}]
Out[16]=

Use DependentVariableQ with the resource function SymbolToSubscript:

In[17]:=
Map[If[Head[#] === Rule && ResourceFunction[
      "DependentVariableQ", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][#[[1]], t] === True, ResourceFunction["SymbolToSubscript"]@
    ResourceFunction["SolutionRulesToFunctions"][#], Nothing] &, {m, s, q, y1[t] -> a x1[t], z2[t] -> c b w2[t]}]
Out[17]=

Use DependentVariableQ with the resource function FormalizeSymbols:

In[18]:=
Through[ReleaseHold@
  HoldForm[ResourceFunction]["FormalizeSymbols"][
    ToExpression@CharacterRange["\[Alpha]", "\[Kappa]"]][t]]
Out[18]=
In[19]:=
ResourceFunction[
 "DependentVariableQ", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][%, t]
Out[19]=

Possible Issues (1) 

DependentVariableQ only identifies dependent variables that have the formats x[var] or x[vars]:

In[20]:=
ResourceFunction[
 "DependentVariableQ", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][y[1][t], t]
Out[20]=

Publisher

Ramón Eduardo Chan López

Requirements

Wolfram Language 13.0 (December 2021) or above

Version History

  • 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