# Extracting the body of a function

I have a very simple issue with Mathematica, but I can't seem to find an answer for it.

I solved an equation with DSolve, and I got a result as an array of the following type:

Solution = { A11 -> Function[ {t} , expression1] , A22 ->Function[ {t}, expression 2] , etc }


Now I want to define a matrix with elements {{A11, A12},{A21, A22}} and to give these elements the values for expression1, expression 2, etc, essentially to put those functions in a matrix:

{{ expression 1, expression 2},{ expression 3, expression 4}}


What I tried so far is to use

R11 = {A11} /. Solution[[1]]


, but it gives R11 the value Function [ {t} , expression 1] and not expression 1.

How can I solve this issue?

• g = Function[{t}, expression1 t]; Module[{t}, g[[2]]] Nov 14 '14 at 12:58
• This just gives Function[{t},expression 1]. I want to extract expression 1 out of there. Nov 14 '14 at 13:02
• No, it returns expression1 t, at least in Mathematica v9. Nov 14 '14 at 13:08

There are two syntaxes for DSolve:

In[5]:= DSolve[x''[t] + x[t] == 0, x, t]
Out[5]= {{x -> Function[{t}, C[1] Cos[t] + C[2] Sin[t]]}}

In[6]:= DSolve[x''[t] + x[t] == 0, x[t], t]
Out[6]= {{x[t] -> C[1] Cos[t] + C[2] Sin[t]}}


Notice that one returns the solution for x, the other for x[t]. Both are meant to be substituted into x[t], and will give the same result after the substitution:

In[7]:= x[t] /. %5
Out[7]= {C[1] Cos[t] + C[2] Sin[t]}

In[8]:= x[t] /. %6
Out[8]= {C[1] Cos[t] + C[2] Sin[t]}


So the key is to use {{A11[t], A12[t]},{A21[t], A22[t]}} instead of {{A11, A12},{A21, A22}}.

Generally, the "extract the expression in a Function" just means applying the function: Function[x, x^2][t] gives t^2.

If you use MMA 10 try DSolveValue instead of DSolve. In this case may help this:

(*extracting expressions from Function*)
t = Solution /. func_Function :> func[[2]];
(*extracting expressions from Rule*)
t /. rule_Rule -> rule[[2]]

• WORKS: The way to solve the problem is the following element = Res /. func_Function :> func[[2]] and then combine it with RhoSol = {{\[Rho]11 /. element[[1]], \[Rho]12 /. element[[2]]}, {\[Rho]21 /. element[[3]], \[Rho]22 /. element[[4]]}}; Works great, thanks! Nov 14 '14 at 13:49

Another solution (make a matrix of elements Aij[t] and substitute the functions you have found):

sol = {A11 -> Function[{t}, expression1], A12 -> Function[{t}, expression2],
A21 -> Function[{t}, expression3], A22 -> Function[{t}, expression4]};

Partition[Through[{A11, A12, A21, A22} [t]], 2] /. sol


(* {{expression1, expression2}, {expression3, expression4}} *)

ClearAll[t];
{#[[0]], #[[1]], #[[2]]} &@Function[{t}, expression1 t] //Column

(*
Function
{t}
expression1 t

*)