Liebe MMA user vielleicht fällt euch hierzu etwas ein.
Dear all !
I am looking for a way how to revert to an expensive (processing time)
evaluated expression.
I would like to have a notebook (or package) containing the expression
in question which should be reevaluated only on a manual request and
otherwise the once preevaluated expression should be used.
Any hints on how this could be achieved simpler ?
I found the following solution:
(*---------------------------------------------------------------------------------------------------
*)
(* get the expression of the preceding cell *)
NotebookGetExpression[] := Module[{nb, e},
nb = SelectedNotebook[];
SelectionMove[nb, Before, EvaluationCell];
SelectionMove[nb, Previous, CellContents];
e = ToExpression@NotebookRead[nb];
SelectionMove[nb, After, EvaluationCell];
e
]
(* this stands exemplary for an expression expensive to evaluate *)
(* after once evaluated the cell will be marked by hand as non
evaluatable *)
(* would be nice if the non evaluatable marking could be automated *)
Expand[(x + y)^2]
Out[ ]= x^2 + 2 x y + y^2
(* her we get the preceding expression and define a function for it *)
Ex[x_, y_] = NotebookGetExpression[];
(* test the defined function *)
Ex[a, b]
Out[ ]= a^2 + 2 a b + b^2
(*---------------------------------------------------------------------------------------------------
*)
(* CellExpression starting here : *)
{
Cell[BoxData[
RowBox[{
RowBox[{"(*", " ",
RowBox[{
"gets", " ", "the", " ", "expression", " ", "of", " ", "the", " ",
"preceding", " ", "cell"}], " ", "*)"}], "\n",
RowBox[{
RowBox[{"NotebookGetExpression", "[", "]"}], ":=",
RowBox[{"Module", "[",
RowBox[{
RowBox[{"{",
RowBox[{"nb", ",", "e"}], "}"}], ",", "\n", "\t",
RowBox[{
RowBox[{"nb", "=",
RowBox[{"SelectedNotebook", "[", "]"}]}], ";", "\n", "\t",
RowBox[{"SelectionMove", "[",
RowBox[{"nb", ",", "Before", ",", "EvaluationCell"}], "]"}],
";", "\n", "\t",
RowBox[{"SelectionMove", "[",
RowBox[{"nb", ",", "Previous", ",", "CellContents"}], "]"}],
";", "\n", "\t",
RowBox[{"e", "=",
RowBox[{"ToExpression", "@",
RowBox[{"NotebookRead", "[", "nb", "]"}]}]}], ";", "\n",
"\t",
RowBox[{"SelectionMove", "[",
RowBox[{"nb", ",", "After", ",", "EvaluationCell"}], "]"}],
";", "\n", "\t", "e"}]}], "\n", "]"}]}]}]], "Input",
CellChangeTimes->{{3.514611168095347*^9, 3.514611328704722*^9}, {
3.5146116455745387`*^9, 3.5146116509185286`*^9}, {
3.514611853377117*^9, 3.5146118571428137`*^9}, {
3.5146120519585495`*^9, 3.514612127927786*^9}, {
3.514612364241467*^9, 3.514612367991467*^9}, {
3.5146124428041286`*^9, 3.514612443429145*^9}, {
3.514613427110442*^9, 3.5146134734076815`*^9},
3.514613562174443*^9, {3.5146136409098253`*^9,
3.514613720004588*^9}}],
Cell[CellGroupData[{
Cell[BoxData[
RowBox[{
RowBox[{"(*", " ",
RowBox[{
"this", " ", "stands", " ", "exemplary", " ", "for", " ", "an",
" ", "expression", " ", "expensive", " ", "to", " ", "evaluate"}],
" ", "*)"}], "\n",
RowBox[{"(*", " ",
RowBox[{
"after", " ", "once", " ", "evaluated", " ", "the", " ", "cell",
" ", "will", " ", "be", " ", "marked", " ", "by", " ", "hand",
" ", "as", " ", "non", " ", "evalauatable"}], " ", "*)"}], "\n",
RowBox[{"(*", " ",
RowBox[{
"would", " ", "be", " ", "nice", " ", "if", " ", "the", " ", "non",
" ", "evaluatable", " ", "marking", " ", "could", " ", "be", " ",
"automated"}], " ", "*)"}], "\n",
RowBox[{"Expand", "[",
SuperscriptBox[
RowBox[{"(",
RowBox[{"x", "+", "y"}], ")"}], "2"], "]"}]}]], "Input",
Evaluatable->False,
CellChangeTimes->{{3.514611868752412*^9, 3.5146120165051975`*^9}, {
3.514613501236163*^9, 3.5146135021893*^9}, {3.5146137213483553`*^9,
3.514613723160878*^9}, {3.5146137558330317`*^9,
3.5146137883642817`*^9}}],
Cell[BoxData[
RowBox[{
SuperscriptBox["x", "2"], "+",
RowBox[{"2", " ", "x", " ", "y"}], "+",
SuperscriptBox["y", "2"]}]], "Output",
CellChangeTimes->{3.514611341923472*^9}]
}, Open ]],
Cell[BoxData[
RowBox[{
RowBox[{"(*", " ",
RowBox[{
"her", " ", "we", " ", "get", " ", "the", " ", "expression", " ",
"of", " ", "the", " ", "preceding", " ", "and", " ", "define",
" ", "a", " ", "function", " ", "for", " ", "it"}], " ", "*)"}],
"\n",
RowBox[{
RowBox[{
RowBox[{"Ex", "[",
RowBox[{"x_", ",", "y_"}], "]"}], "=",
RowBox[{"NotebookGetExpression", "[", "]"}]}], ";"}]}]], "Input",
CellChangeTimes->{{3.51461143789336*^9, 3.5146114714729896`*^9}, {
3.51461151100601*^9, 3.514611576665202*^9}, {3.51461169146722*^9,
3.5146117313278985`*^9}, {3.5146135213770456`*^9,
3.5146135825028276`*^9}}],
Cell[CellGroupData[{
Cell[BoxData[
RowBox[{
RowBox[{"(*", " ",
RowBox[{"test", " ", "the", " ", "defined", " ", "function"}], " ",
"*)"}], "\n",
RowBox[{"Ex", "[",
RowBox[{"a", ",", "b"}], "]"}]}]], "Input",
CellChangeTimes->{{3.5146114477219257`*^9, 3.5146114497063894`*^9}, {
3.51461155541425*^9, 3.514611556461172*^9}, {3.514611709639909*^9,
3.5146117106712055`*^9}, {3.514611837439311*^9,
3.51461183870496*^9}, {3.5146138100517817`*^9,
3.5146138104580317`*^9}}],
Cell[BoxData[
RowBox[{
SuperscriptBox["a", "2"], "+",
RowBox[{"2", " ", "a", " ", "b"}], "+",
SuperscriptBox["b", "2"]}]], "Output",
CellChangeTimes->{3.514614846234047*^9}]
}, Open ]]
}