New answers tagged

13

General The definitions get reordered at definition-time by a part of the pattern matcher, that takes care of automatic rule reordering. It does so, based on relative generality of rules, as far as it is able to determine that. This is not always possible, so when it can't determine which of the two rules is more general, it appends the rules to DownValues ...


14

Actually we have direct control over this via a System Option. Set: SetSystemOptions["DefinitionsReordering" -> "None"]; Then: Clear[f]; f[x_] := Sin[x]; f[x_?EvenQ] := x; f[x_?OddQ] := x^2; {f[1], f[2], f[3], f[4], f[3/2], f[Newton]} {Sin[1], Sin[2], Sin[3], Sin[4], Sin[3/2], Sin[Newton]} Restore the default behavior with: ...


2

Thanks to LLlAMnYP's comment (and Jason B's example of it) I've determined the best solution to my situation would be: In[1]:= Clear[f]; In[2]:= f[x_?EvenQ] := x; In[3]:= f[x_?OddQ] := x^2; In[4]:= DownValues[f]=Prepend[DownValues@f, HoldPattern[f[x_]] :> Sin[x] ]; In[5]:= DownValues[f] Out[5]:= {HoldPattern[f[x_]] :> Sin[x], HoldPattern[f[x_?EvenQ]] ...


6

This is more of an extended comment in response to @LLlAMnYP is there a way to do this without knowing the existing definitions in advance? or maybe use Prepend? You can roll a function like so: SetAttributes[makeDef, HoldAllComplete] makeDef[f_Symbol, expr_, n_Integer] := Module[{dVal}, Block[{f}, Evaluate[expr]; dVal = First[DownValues[f]]]; ...


3

To put LLlAMnYP's comment into an answer, you need to look at the function's DownValues: DownValues[f] gives a list of transformation rules corresponding to all downvalues defined for the symbol f. Clear[f]; f[x_] := Sin[x]; f[x_?EvenQ] := x; f[x_?OddQ] := x^2; DownValues[f] (* {HoldPattern[f[x_?EvenQ]] :> x, HoldPattern[f[x_?OddQ]] :> x^2, ...


2

Quitting and restarting may not always be the best way to handle shadowing, as it destroys the results of any computations performed. One can also use Remove. From the tutorial Contexts If you once introduce a symbol that shadows existing symbols, it will continue to do so until you either rearrange $ContextPath, or explicitly remove the symbol. You ...


0

Note - The code below lets you access your data from the file name but it does not exactly do what you want. Lets say you have some data files named {file1.dat,file2.dat,file3.dat}. Keep the notebook with the code below in the same directory. SetDirectory[NotebookDirectory[]]; aLLfiles = FileNames[NotebookDirectory[] <> "*.dat"]; Table[ ...


2

To see how to extract information from filenames see this question. Notice that indexed variables, as described in the answers to this question may be more advisable than customized names created as in my answer. All that said, You can get the filenames (JPG in this example) fn = FileNames["IMG*.jpg"] {"IMG_20150417_103421814.jpg", ...


2

You can use Symbol and Formal Symbols. With[{\[FormalS] = Symbol["x"]}, \[FormalS] = 4]; x (* 4 *) For your case: filename = "file1"; With[{\[FormalS] = Symbol[filename]}, \[FormalS] = Import[filename <> ".dat"];] A symbol file1 is created by Symbol and formal s is used to hold it. Then this symbol (file1) is assigned the import. You can then ...


5

Documentation and Details ans Options section for CloudDeploy are saying: CloudDeploy[expr,...] automatically deploys all definitions needed to evaluate expr, much like CloudSave. and as we can see, it's not the case here. Or, it's a feature of Manipulate which boxes definitions are got by FrontEnd so maybe evaluation doesn't apply here. At the end, ...


2

If you are expecting Clear[x] to clear definitions of x, don't expect anything more from Clear[s]. So you can go with: X = 5; s = "X"; Clear[#] &@s X X Or, as pointed by Albert Retey, with Clear[Evaluate@s]. Maybe you don't want to use strings, then: X = 5; s = ToExpression["X", StandardForm, Unevaluated]; Clear[#] &@s X works too. Some ...


1

I like march's idea of automation but I think at the moment his updated code is not working. Perhaps this will serve the purpose: SetAttributes[heldDistribute, HoldFirst] heldDistribute[expr_] := Unevaluated[expr] /. Cases[Unevaluated[expr], x_Symbol :> (HoldPattern[x] :> Defer[x]), {-1}, Heads -> False] // Distribute Now with the ...



Top 50 recent answers are included