Tag Info

11

Here is an observation, not really an answer: Hold@1.5 /. x_Real :> RuleCondition[N[x] + $MachineEpsilon, True] (* -> Hold[1.0000] *) But: Block[{Internal$SameQTolerance = -Infinity}, Hold@1.5 /. x_Real :> RuleCondition[N[x] + $MachineEpsilon, True] ] (* -> Hold[1.] *) It does not help your test case because 1.5 is SameQ 1. But I think ... 9 Without thinking about any consequences, one idea popped into my mind. First, your definitions for f with the DownValues. I made it a bit more interesting: ClearAll[f]; f // Attributes = {HoldAll}; f /: HoldPattern[f[x_] + f[y_]] := upvaluesSeen[f[x], f[y]]; f[x_] := downvalue[x] How about a small wrapper function that temporarily deletes all DownValues ... 8 No, you only need the Internet connection if you are using Wolfram|Alpha to enter your inputs, because this is a web service. If you had typed the Mathematica input Plot[Sin[x]/x, {x, -9.4, 9.4}] rather than graph of sin[x]/x then this would not have happened. W|A's interpretation of inputs is unpredictable at the best of times, so I would suggest ... 6 Adapting Leonid's method for How do you set attributes on SubValues? ClearAll[f, g]; SetAttributes[{f, g}, HoldAll]; g /: g[x_] + g[y_] := upvalue; f[x_] := downvalue f := With[{stack = Stack[_] /. HoldPattern[f] :> g}, With[{foo = Cases[stack, Alternatives @@ _ /@ First /@ UpValues @ g]}, g /; foo =!= {} ] /; stack =!= {} ] Now: f[1] ... 5 You can put Print around the Show data = {{0.18, -0.13}, {0.84, -0.06}, {0.05, 0.88}, {0.24, -0.63}, {0.67, 0.93}, {0.05, 0.88}, {0.65, 0.92}, {0.01, 0.99}, {0.17, -0.04}, {0.23, -0.55}}; model[{a_, k_, w_, p_}][x_] = a Exp[-k x] Sin[w x + p]; lp = ListPlot[data, PlotRange -> All, PlotStyle -> Red]; FindFit[data, model[{a, k, w, p}][x], ... 4 While this may or may not be closed, either as a "mistake" or as "not reproducible," let me just point out the general approach indicated by my comment. When Mathematica is not behaving as expected, especially on such simple input as the OP's session-image shows, it is good to suspect that some invisible input might be causing the mischief. This may be ... 4 The best way is to use bash script! First you need to save your script as .m file and "save as" option, wont work alone! You have to convert it to "code" format, by select all cmd+8 in OS X if I'm not wrong or select all and then fromat->style->code. Second, we tell mathematica to run its kernel through a command. First we give the path to the Mathematica ... 4 Starting from Mathematica version 6 Show by default has no side effects. In your particular case it just returns the Graphics expression to StepMonitor which does not use it because the latter is designed only for working with functions which have side effects like Print (side effect is printing of the expression and nothing is returned as the output) or Set ... 4 This works: Dynamic@f FindFit[data, model[{a, k, w, p}][x], {a, k, w, p}, x, StepMonitor :> CompoundExpression[Pause[.5], f = Show[Plot[model[{a, k, w, p}][y], {y, 0, 1}, PlotRange -> {-2, 2}], lp]]] 4 I added an extra definition for exprSquareError that will only fire when the parameters are numeric. I redefined exprMinimize to call this version of exprSquareError. Thus, exprMinimize's call of exprSquareError does not expand until the parameters are numeric. exprSquareError[expr1_, expr2_, vars_, strategy_: Automatic, maxpnts_: Automatic] := ... 3 Let us consider in parallel two NumberForm expressions: expr1 = NumberForm[N[Pi], 10] expr2 = NumberForm[N[Pi], {3, 2}] and let us look at their TreeForms: Row[{TreeForm[expr1, ImageSize -> 300], Spacer[20], TreeForm[expr2, ImageSize -> 300]}] giving this: One can see now that in the both cases the number itself has the tree ... 3 A simple way to inject the value of expression into the unevaluated LinkWrite argument is to use With: Clear[bar] expression = Hold[bar = 3]; bar (* bar *) With[{x = expression}, LinkWrite[newKernel, Unevaluated@EvaluatePacket[ReleaseHold[x]]] ] LinkRead[newKernel] (* ReturnPacket[3] *) LinkWrite[newKernel, Unevaluated@EvaluatePacket[bar^2]] ... 3 It's quite easy with a little bit of metaprogramming, symbol is replaced unevaluated in the body, instead of evaluating it in the arguments and replacing it in the body with a value, like what would happen if the function didn't have a HoldXXX attribute like in the usual evaluation used by most functions. SetAttributes[InitSingleton,HoldAll]; ... 3 If you really need to do everything in the frontend, then I would Suggest the below procedure, in this process Mathematica would open a new window selects all the cells and runs them then it would go for the second notebook file nb1 = NotebookOpen["/Users/you/Documents/1.nb"]; SelectionMove[nb1, All, Notebook] SelectionEvaluate[nb1] nb1 = ... 3 You can hide your workaround in$Pre: SetAttributes[specialEvaluate, HoldAll] specialEvaluate[expr_] := ReleaseHold[ Hold[expr] /. UpValues[f] ] \$Pre = specialEvaluate; And now: f[1] + f[2] (* Out: upvalue *)

2

If I want foo to be immutable after gets bound to a value, I write foo = 42; Protect[foo]; Now, observe what happens when I attempt to bind foo to a different value. foo = 0; foo Set::wrsym: Symbol foo is Protected. >> 42 or clear it foo =.; foo Set::wrsym: Symbol foo is Protected. >> 42

2

It appears that there may be some indirect dependence on RuleCondition as well as other structures. I found this out while trying to explain the problem to WRI technical support. In all cases, these answers should be a number (1. or 2.) followed by answers with that same number in a list. The spots where the number has extra zeroes indicate a rule that went ...

1

After a couple hours of tinkering around... I think I got it. Here it is... currentStyle = DynamicModule[{label = " "}, Dynamic[With[{sel = Cells[NotebookSelection[InputNotebook[]]]}, If[sel =!= {}, label = CurrentValue[sel[[1]], "CellStyleName"]]]; label, UpdateInterval -> .5]]; ActionMenu[currentStyle, Evaluate[# ...

1

I think the best you can do is to put the test in the function defintion and allow all arguments: MyFunction[x___] := If[Length[{x}] =!= 2, Error, If[IntegerQ[#] && StringQ[#2] &[x], 32, Error]] MyFunction[2, "test"] MyFunction["test", 2]

1

This handling of If is more or less correct behavior. The condition evaluates to True so If exits with its second argument as the result. That second arg is Return[{"Exit Block", i}, If]. At this point the evaluator figures out there is no surrounding If from which the Return can actually return, and it (apparently) bubbles up to the top level. At worst this ...

Only top voted, non community-wiki answers of a minimum length are eligible