3
$\begingroup$

Consider the following definition

a = D[x,x]

Now, a=1.

Is there a way in which I can recover the information about the input definition of a, ie is it possible to define a function info such that

info[a]
(* D[x,x] *)

To be more clear, I know that the information is stored, in fact I can recover it typing

??In

but I'm asking about a way to recover it without manually searching among all the output of ??In (that seems to me, by the way, a strange output, and I don't know how handle it)

P.S. Perhaps the question has been asked before, but I cannot find an answer to it

$\endgroup$
3
  • $\begingroup$ Have you seen InString[]? $\endgroup$
    – J. M.'s torpor
    Jan 24 '17 at 11:21
  • $\begingroup$ I'm seeing it now, thank you. But I've never used the RowBox; so I still struggle with this. $\endgroup$
    – Giancarlo
    Jan 24 '17 at 11:47
  • $\begingroup$ @Giancarlo You could use MakeExpression on the RowBox-containing results to translate them to an expression in a standard form. $\endgroup$
    – MarcoB
    Jan 24 '17 at 13:44
2
$\begingroup$

Maybe this will work for you. The idea here is to automate a search of the down-values of In for a given variable.

SetAttributes[defFromIn, HoldFirst]
defFromIn[var_Symbol] :=
  With[{dv = Most[DownValues[In]]},
    Extract[
      dv, ReplacePart[Position[dv, Unevaluated[var]][[1]], -1 -> 2], HoldForm]]

a = D[x, x];
b = D[x^2, x];
c = D[x^3, x];

Now

defFromIn[a]

gives

result_a

and

defFromIn[c]

gives

result_c

$\endgroup$
1
  • $\begingroup$ Hi, thanks for your reply; I've found another way that seems more general: in fact, your function doesn't work in cases such as f[x_]=x. Also if I didn't ask for this kind of definition, I think that my solution is more general since it covers also these cases. $\endgroup$
    – Giancarlo
    Jan 25 '17 at 7:02
0
$\begingroup$

I have found these functions

-given a symbol X it returns the input numbers in which the symbol is present

SetAttributes[InNumber, HoldAll]
InNumber[X_] := Union[First/@Position[Table[ToExpression[InString[i]], {i,$Line}],SymbolName[Unevaluated[X]]]]

-the following gives the first input in which X is present, that is, generally, the definition of X

SetAttributes[Def, HoldAll]
Def[X_] := DisplayForm@ToExpression[InString[First[InNumber[X]]]]

-the following give all the inputs in which X is present

SetAttributes[DefComplete, HoldAll]
DefComplete[X_] := TableForm[Drop[DisplayForm/@Flatten[{{"In[" <> ToString[#] <> "]:=",ToExpression[InString[#]], ""}&/@InNumber[X]}], -3]]

They work in all my cases.

For example

a = D[x, x];
b = D[x^2, x];
c[x_] = D[x^3, x];
d[x_] := x
d[{a_, b_}, {c_, d_}] := a d - c b

Def[a] (* a=D[x,x]; *)
Def[b] (* b=D[x^2,x]; *)
Def[c] (* c[x_] = D[x^3, x]; *)
Def[d] (* d[x_]:=x *)
DefComplete[d]
(* In[10]:=
d[x_]:=x

In[11]:=
d[{a_,b_},{c_,d_}]:=a d-c b

In[15]:=
Def[d] *)

DefComplete[x]
(* In[7]:=
a=D[x,x];

In[8]:=
b=D[x^2,x];

In[9]:=
c[x_]=D[x^3,x];

In[10]:=
d[x_]:=x *)
$\endgroup$

This site is temporarily in read only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .