Clearing numeric values โ€‹โ€‹in Mathematica

I am working on rather large projects of Mathematica, and the problem arises that I have to periodically check the numerical results, but I want to easily return to ensuring that all my constructions are in analytical form.

The code is pretty liquid. I don't want to use scope constructions everywhere, since they add overhead. Is there an easy way to identify and clear all jobs that are numeric?

EDIT: I really know that defining a scope is a way to do it right ;-). However, for my workflow, I'm really just looking for a dirty trick to fix all the numerical tasks after the fact, instead of the forethought to put the block in.

+1
source share
2 answers

If your assignments are at the top level, you can use something like this:

a = 1; b = c; d = 3; e = d + b; Cases[DownValues[In], HoldPattern[lhs_ = rhs_?NumericQ] | HoldPattern[(lhs_ = rhs_?NumericQ;)] :> Unset[lhs], 3] 

This will work if you have enough $HistoryLength history length (infinity by default). However, note that in the above example, e was assigned 3+c , and 3 was not canceled here. Thus, the problem is really ambiguous in the wording, because some numbers can turn it into definitions. One way to avoid this is to use SetDelayed for assignments, not Set .

Another alternative would be to parse the names in the say Global' text (if that is the context in which your characters live), and then say OwnValues and DownValues characters, similar to the above, and remove the definitions with purely numeric rhs

But IMO, none of these approaches is reliable. Anyway, I will use area determination schemes and try to isolate the numerals. One possibility is to wrap the final code in Block and assign numeric values โ€‹โ€‹inside that Block . This seems like a cleaner approach. Overhead is minimal - you just need to remember which characters you want to assign to the values. Block automatically guarantees that outside its characters will not have definitions.

EDIT

Another possibility is to use local rules. For example, one could define rule[a] = a->1; rule[d]=d->3 rule[a] = a->1; rule[d]=d->3 instead of the assignments above. You can then apply these rules by extracting them, as they say DownValues[rule][[All, 2]] , when you want to test with some numerical arguments.

+2
source

Based on Andrew Moylanโ€™s decision, you can build a function of type โ€œBlockโ€ that will accept the rules:

 SetAttributes[BlockRules, HoldRest] BlockRules[rules_, expr_] := Block @@ Append[Apply[Set, Hold@rules , {2}], Unevaluated[expr]] 

Then you can save your numerical rules in a variable and use BlockRules [savedrules, code] or even define a function that would apply a fixed set of rules, like:

 In[76]:= NumericCheck = Function[body, BlockRules[{a -> 3, b -> 2`}, body], HoldAll]; In[78]:= a + b // NumericCheck Out[78]= 5. 

EDIT In response to Timo's comment, it may be possible to use NotebookEvaluate (new in 8) to achieve the requested effect.

 SetAttributes[BlockRules, HoldRest] BlockRules[rules_, expr_] := Block @@ Append[Apply[Set, Hold@rules , {2}], Unevaluated[expr]] nb = CreateDocument[{ExpressionCell[ Defer[Plot[Sin[ax], {x, 0, 2 Pi}]], "Input"], ExpressionCell[Defer[Integrate[Sin[ax^2], {x, 0, 2 Pi}]], "Input"]}]; BlockRules[{a -> 4}, NotebookEvaluate[nb, InsertResults -> "True"];] 

As a result of this assessment, you get a laptop with evaluated commands when it was locally set to 4. To take it further, you will have to take a laptop with a code, open a new notebook, evaluate Notebooks[] to identify the notebook of interest, and then do:

 BlockRules[variablerules, NotebookEvaluate[NotebookPut[NotebookGet[nbobj]], InsertResults -> "True"]] 

Hope you can make this idea work.

enter image description here

+1
source

Source: https://habr.com/ru/post/1348166/


All Articles