DMUG-Archiv 2011

Frühere   Chronologischer Index   Spätere
Vorherige   Thematischer Index   Nächste

Re: NotebookGetExpression

Hallo Robert,

es kommt darauf an, welche Art Funktionen benötigt werden:

(i) symbolische Funktionen, dann sagt die Hilfe:

Although := is probably used more often than = in defining functions, there is one important case in which you must use = to define a function. If you do a calculation, and get an answer in terms of a symbolic parameter x, you often want to go on and find results for various specific values of x. One way to do this is to use the /. operator to apply appropriate rules for x in each case. It is usually more convenient, however, to use = to define a function whose argument is x.

(ii) numerische Funktionen, dann kann man Funktionen verwenden, die sich ihrer Werte entsinnen:

fib[1] = fib[2] = 1;
fib[n_] := fib[n] = fib[n - 1] + fib[n - 2]

(iii) könnte man ein Package schreiben, das die Funktion myNowak[x_, ..., manQ_:False] enthält. Falls manQ nicht spezifiziert wird, müsste die Lösung bereits im Package drinstehen und von daher reproduziert werden. Falls myNowak[x, ..., True] gerufen wird, darf das package selber rechnen.

(iv) A cell that cannot be evaluated:

CellPrint[ExpressionCell[Defer[1 + 1], "Input", Evaluatable -> False]]

Man könnte die bewusste Zelle taggen, sie mit NotebookLocate[] anhand des Tags wiederfinden und dann allenfalls mit FrontEndExecute[] die Option Evaluatable der Zelle auf False setzen.

Warum kann es nicht die build-in Methode (ii) sein?
Habe ich die Aufgabe überhaupt verstanden?

Gruss
Udo.



On Tue, 17 May 2011 12:09:28 +0200, Nowak Robert <Robert.Nowak@XXXXXXX.at> wrote:

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  ]]
}





Antworten:
Verweise:
Frühere   Chronologischer Index   Spätere
Vorherige   Thematischer Index   Nächste

DMUG DMUG-Archiv, http://www.mathematica.ch/archiv.html