15

You can get the behavior you ask for if you define your function to work with an association. The key-value paradigm provides syntax close but not exactly as you describe. Simple approach f[a_Association] := a[x]^2 + a[y]^3 f[<|x -> 5, y -> 7|>] 368 Argument keys are not affected by global assignments. Changing the order of the key-value pairs ...


10

If this is just to help entering values in the correct order (and not to be permanently visible), you can possibly use a Placeholder. Reevaluating phf below will produce the template again: just type phf, select it, right-click and choose Evaluate in Place. f[x_, y_] := x^2 + y^2 phf = Defer[f[Placeholder[x], Placeholder[y]]]


7

Clear["Global`*"] Use Module to keep the temporary variable names (and values) out of the Global context. g[t_] := Module[{res = 0, i = 1}, While[i <= t, res = res + i; i = i + 1]; res]; However, note that g evaluates to 0 for symbolic arguments. g[t] (* 0 *) Consequently, restrict its arguments to being NumericQ or preferably, Positive. ...


6

A general approach using Graphics3D[] and surf[] (below, built with NDSolve): rr[t_] := {Cos[t], Sin[t]}; ht[t_] := 2 + Cos[t]; Manipulate[ Graphics3D[{EdgeForm[], surf[traj[rr, {0 &, ht}, {t, 0, 2 Pi}, 2 Pi - t0]]}, BoxRatios -> Automatic, PlotRange -> {{-1.55 Pi, 2.05 Pi}, {-1.55 Pi, 2.05 Pi}, {-0.1, 3.5}}], {t0, 0., 2 Pi}] A fancier ...


5

With numerical functions, you have to be careful that NIntegrate will always get a valid integrand. The easiest way to do this is with pattern matching: ClearAll[pintN]; pintN[ip_][t_?NumericQ] := Module[{x}, NIntegrate[(x + t x - 2 t) ip[x], {x, 0, t}] ] This will now work, but it's very slow: Plot[pintN[pintN[Exp[-#] &]][u], {u, 0, 5}, PlotPoints -&...


5

In Mathematic this sort thing is much more easily done with Function Like so: Funky[n_] := {(n + # + 10) &, (n - # + 10) &} {f, g} = Funky[1] {1 + #1 + 10 &, 1 - #1 + 10 &} Then f[1] 12 g[1] 10 Now let's look at what (1 + # + 10) & means. (n + # + 10) & // FullForm Function[Plus[1, Slot[1], 10]] where Slot[1] is a descriptor ...


5

Simplifying a bit and fixing a typo: Funky[n_] := Module[{LocalFunc1, LocalFunc2, MkLocalFunc}, LocalFunc1[y_] = n + y; LocalFunc2[y_] = n - y; MkLocalFunc[fnToUse_] := Module[{LocalFunc}, LocalFunc[w_] = fnToUse[w] + 10; LocalFunc]; {MkLocalFunc[LocalFunc1], MkLocalFunc[LocalFunc2]}] (Funky[1] // First)[1] (* 12 *) The only typo that ...


4

SetOptions[ParametricPlot3D, Boxed -> False, Axes -> None, ImageSize -> Large, PlotStyle -> Directive[Opacity[0.5], Blue], PlotRange -> {{-8, 8}, {-8, 8}, {0, 5}}, ViewProjection -> "Orthographic"]; r[s_] = {Cos[s], Sin[s]}; f[θ_, s_] := If[0 <= θ <= s, r[θ], r[s] + (θ - s)*Normalize[r'[s]]]; h[θ_] = 2 + Cos[...


4

If you'd like to convert your 2D unrolling a circle process to 3D, you could do the following: Manipulate[ ParametricPlot3D[ If[ϕ < θ, {ϕ + Sin[θ - ϕ], 1 - Cos[θ - ϕ], z (2 + Cos[θ])}, {θ, 0, z (2 + Cos[θ])}], {θ, 0, 2 π}, {z, 0, 1}, PlotRange -> {{-1, 7}, {-1, 2}}, PlotStyle -> Directive[Opacity[0.5], Blue], Mesh -> {101, 2}, ...


3

ClearAll[f, cf] f[x_] := (x^2 + x)/x cf[0. | 0] = 1.; With[{comp = Compile[{x}, Evaluate[f[x]]]}, cf[a_] := comp[a] ] cf[0.] (* 1. *) cf[0] (* 1. *) cf[2] (* 3. *)


3

Please try Norm[{2, 3} - {2, 1}] this results in 2. Please note: Wolfram Language has an enormous amount of prebuild functions which are available. You can also use dist[{2, 3}, {2, 1}] Hope this helps.


3

Maybe like this? randomfun[r_][{x_, y_}] := Piecewise[{ {{x, y}/2, r == 1}, {{x, y}/3 + {2/3, 0}, r == 2}, {RotationMatrix[Pi/3] . {x, y}/3 + {1/3, 0}, r == 3}, {RotationMatrix[-Pi/3] . {x, y}/3 + {1/2, Sqrt[3]/6}, r == 4}}] randomfun[RandomInteger[{1, 4}]][{x, y}] randomfun[#][{x, y}] & /@ RandomChoice[{1, 2, 3, 4}, 2] NestList[randomfun[...


2

The problem is that NonCommutativeMultiply (**) distributes over Times. That means: a[k] ** u[k]*a[q] is interpreted as: (a[k] ** u[k]) * (a[k] ** a[q]). If you write: a[k] ** (u[k]*a[q]) you get: a[k] ** a[q] u[k] Furthermore, Times has the attribute Orderless. Therefore MMA will rearrange the expression according to its own rules (I think ...


2

I decided to get some quantitative results to this question -- through brute-force. I decided to write my own version of BorderDimensions (calculates borders of a solid color around an image) using Compile. It's non-trivial enough to actually demonstrate the point, but (I hope) small enough to post here. Note that I'm aware that my version could be "...


2

Here’s a couple of interesting methods: f1[x_,y_]:=With[ { $x=x/.Equal[$arg_,$val_]:>$val, $y=y/.Equal[$arg_,$val_]:>$val }, $x^2+$y^3 ] f2[X_,Y_]:=With[ { $x=X/.Equal[$arg_,$val_]:>Rule[$arg,$val], $y=Y/.Equal[$arg_,$val_]:>Rule[$arg,$val] }, ClearAll[$assoc]; $a=Association[{$x,$y}]; $a[x]^2+$a[y]^3 ] Both carry the same syntax: f1[x==5,y==3] ...


2

f[x_, y_] := 0 /; Mod[x, π] == 0 && Mod[y, π] == 0 does the trick. Just figured it. Any comments are most welcome.


1

It looks like the definitions for NonCommutativeMultiply don't make it to the parallel kernels. You can test this as follows: CloseKernels[]; Unprotect[NonCommutativeMultiply]; NonCommutativeMultiply[args___] := f[args]; Protect[NonCommutativeMultiply]; Quiet @ LaunchKernels[]; DistributeDefinitions[NonCommutativeMultiply]; ParallelEvaluate[Hold[Evaluate[3 **...


1

NO[Times[x__, y_NonCommutativeMultiply, z___]] := x z NO[y] NO[Times[x___, y_NonCommutativeMultiply, z__]] := x z NO[y] should do the trick in all situations: Depending of the canonical ordering of Times it might not be necessary to include both versions but better save then sorry.


1

You can use a Piecewise definition as well: Clear[f] f[x_, y_] := Piecewise[ {{1/(Sin[x] + Sin[y]), Mod[x, Pi] != 0 || Mod[y, Pi] != 0}}, 0 ] f[2, 3.] (* 0.952002 *) f[Pi, 3 Pi] (* 0 *) f[2, 10 Pi] (* Csc[2] *) f[Pi, 2 Pi] (* 0 *) Here I made the default value $0$ explicit for future code readability, but ...


1

Posting as an answer at the request of the OP There is probably no built-in way to achieve what you want because the result you are after is in general not correct: in order to get it you need to rewrite and reorder the summations, which is only possible under special convergence conditions. That being said, if you are sure beforehand that all your ...


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