7

To localize r, I would use Module. Otherwise, the only changes needed are inserting a missing semicolon and getting rid of the unnecessary Return calls. ClearAll[randomHop]; randomHop[{x_,y_}]:=Module[{ r }, r=RandomInteger[{1,2}]; If[r==1, {{0.5,0.5},{0.5,0.5}}.{x,y}, {{-0.5,-0.5},{0.5,-0.5}}.{x,y}+{1,0} ] ]; SeedRandom[1]; ...


6

The problem is that sort doesn't evaluate when the list is ordered. See what happens when you increase the nesting: NestList[sort, {5, 4, 3, 2, 1}, 12] {{5, 4, 3, 2, 1}, {4, 5, 3, 2, 1}, {4, 3, 5, 2, 1}, {3, 4, 5, 2, 1}, {3, 4, 2, 5, 1}, {3, 2, 4, 5, 1}, {2, 3, 4, 5, 1}, {2, 3, 4, 1, 5}, {2, 3, 1, 4, 5}, {2, 1, 3, 4, 5}, {1, 2, 3, 4, 5}, sort[{...


5

One way to do it is exactly as you basically already wrote aLogb[a_, b_] := If[a == 0, 0, a Log[b]] since when a is symbolic, Mathematica doesn't know if it's a number or zero, or anything, so it just leaves the If statement unevaluated. Another way using pattern specifications to determine the output aLogb[0, b_] = 0; aLogb[a_?NumericQ, b_] := a Log[b] ...


4

Another approach. One that I think is not only more concise, but more efficient and more elegant than using For. f[x_] := 1/(1 + 25 x^2) The built-in function Subdivide can be used to do the subdivision you perform with xk and Table, so those functions can be dispensed with. So all that is needed to generated the points you want to plot is: fpts[n_] := ...


3

Not entirely sure why you want to do this (beware Subscript), but: d = 5; f = Apply[g, Table[Subscript[x, n], {n, 1, d}]]; f (* g[Subscript[x, 1], Subscript[x, 2], Subscript[x, 3], Subscript[x, 4], Subscript[x, 5]] *)


3

ClearAll[fpm]; fpm[g_, a__][step_, pos_: 1] := Module[{arg = MapAt[# + step &, {a}, {pos}]}, g @@ arg] Examples: ClearAll[f,t, v, a, b, c]; f[a_, b_] := (t[a, b] + c[a, b])/v[a, b] fpm[f, a, b][step] (c[a + step, b] + t[a + step, b])/v[a + step, b] fpm[f, x, y][-step] (c[-step + x, y] + t[-step + x, y])/v[-step + x, y] fpm[f, u, t][-step, 2] ...


3

Interpreting your $a^+$ and $a^-$ as limits from above and below, (and in one dimension), this function takes the limit from above and from below and then takes the difference: jump[q_, x_, val_] := Limit[q[x], x -> val, Direction -> "FromAbove"] - Limit[q[x], x -> val, Direction -> "FromBelow"] So for example, say we ...


3

Does this do what you want? Are you looking for additional formatting of input or output? Attributes[jump] = HoldFirst; jump[fn_[args__]] := fn[args] - fn @@ -{args} jump[f[a, b, c]] -f[-a, -b, -c] + f[a, b, c] Following your update please try this and report its utility. Parameters f and g can be changed to whatever symbol modifier you please. ...


3

ClearAll[step, iterate] step[initialvector_, indicesdecay_, rangedecay_, indicesgrow_, rangegrow_] := Module[{ca = Normal[ SparseArray[Join[Thread[indicesdecay -> -1], Thread[indicesgrow -> 1]], Length@initialvector]]}, initialvector (1 + (ca /. {1 :> RandomReal[rangegrow], -1 :> RandomReal[rangedecay]}))] iterate[...


2

Starting as you do, let's write: SeedRandom[123]; list1 = RandomReal[{0.3, 0.8}, 4] which produces {0.52786, 0.788913, 0.771607, 0.781108} Next, we want to increase each of these a small amount. You could do this: smallRange = {0.05, 0.1}; list2 = list1 + RandomReal[smallRange, 4] My session produces {0.592977, 0.862248, 0.824689, 0.85039} Next, ...


1

randomHop[{x_, y_}] := If[RandomInteger[{1, 2}] == 1, {{0.5, 0.5}, {0.5, 0.5}}.{x, y}, {{-0.5, -0.5}, {0.5, -0.5}}.{x, y} + {1.0, 0}] also works.


1

Here's a few pieces of the puzzle. To generate monotonic sequences of "random" numbers: Accumulate[RandomReal[{0, 0.1}, 10] {0.0669849, 0.0935961, 0.141986, 0.216063, 0.238291, 0.255048, 0.269631, 0.276363, 0.284295, 0.329844} Now let's say you have a vector x and you want the first few to go up a tiny bit and the rest to go down. Specify the ...


1

You should be able to use SameQ (===). Volume[RegionIntersection[Circle[{0, 0}, 1], Circle[{2, 2}, 1]]] === Undefined (* True *)


1

You may use NearestTo with Composition. With SeedRandom[123] {ln125, ln126, ln127} = RandomReal[{500, 600}, {3, 10}]; Then First@*NearestTo[551.748] /@ {ln125, ln126, ln127} {546.671, 562.827, 549.18} or without First if you want the nested list. NearestTo[551.748] /@ {ln125, ln126, ln127} {{546.671}, {562.827}, {549.18}} Hope this helps.


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