5
$\begingroup$

I have a list of functions and their derivatives.

How can I select the function and its derivative with the smallest or largest index, provided that both the function and its derivative are in the list?

For example, given the list

vars = {U[1][t], U[2][t], U[3][t], U[5][t], U[6][t], U[7][t], U[3]'[t], U[5]'[t], U[6]'[t]},

I want to select the function and its derivative with the smallest index, which would be {U[3][t], U[3]'[t]}.

$\endgroup$

3 Answers 3

5
$\begingroup$

Use Cases to retrieve the indices of functions and derivatives, then Intersection to get only those that are present in both lists. Afterward, use Min or Max or MinMax to retrieve the smallest or the largest.

MinMax@Intersection[Cases[vars, U[x_][t] :> x], Cases[vars, U[x_]'[t] :> x]]
(* {3, 6} *)
$\endgroup$
0
4
$\begingroup$
vars = {U[1][t], U[2][t], U[3][t], U[5][t], U[6][t], U[7][t], 
  U[3]'[t], U[5]'[t], U[6]'[t]}

min = First@Intersection[D[vars, t], vars]

{Integrate[min, t], min}

{U[3][t], Derivative[1][U[3]][t]}

$\endgroup$
2
$\begingroup$
MinMax @ Cases[{a_, __} :> a] @ Split @ Sort @
    Cases[vars, U[a_][_] | _[_][U[a_]][_] :> a]

{3, 6}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.