Hot answers tagged

14

This isn't necessarily how these functions are implemented, but MathematicalFunctionData gives a way to access definitions that are equivalent to the ones Mathematica uses. (* There are a total of 348 functions to choose from *) Length[functions = MathematicalFunctionData[]] 348 functions[[1]]["Definition"] {Function[{\[FormalX]}, Inactivate[ ...


13

Usually, when one defines a function that's not too complex (usually a one-liner) it is customary (here we mean Mathematica custom) to define it directly without any scoping constructs (Module, With or Block). For example: myFunction[x_]:= 2 Sin[x] + Exp[-x^2] But as the function definition gets more complex, instead of polluting the Global context with ...


8

There is an example in the documentation which may get you started: pairs = RandomReal[{-1, 1}, {10000, 2}]; 4 Count[Map[Norm, pairs], _?(# <= 1 &)]/10000. (*3.1248*) You can plot see this as: Graphics[{PointSize[Small], Blue, Point@Select[pairs, Norm[#] <= 1 &], Gray, Point@Select[pairs, Norm[#] > 1 &], Red, Thick, Circle[]}, ...


7

Here is an approach using RandomPoint and graphics primitives: pts = RandomPoint[Rectangle[], 10^6]; (* generate random points on the unit square *) rm = RegionMember[Disk[{0.5, 0.5}, 0.5]]; (* RegionMemberFunction for an embedded Disk *) Now we count the number of points that fall in the circle and divide that by the total number of points. That should ...


7

Append and Prepend and possibly ReplacePartare most likely slowing your code down substantially. I could not recode your stuff to work without these constructs withing the limited time I had available, nor do I understand competely why you approach the issue this way. Regardless, there is a good and clear demonstration by Philip Gregory that gives an ...


6

This is not answer, but an extremely long comment. I find this problem very interesting, but haven't been able to solve it. In my attempts, I developed a tool to visualize the the two-walker random walk. I am posting this tool because I think it might be useful to the OP or anyone else looking this problem for exploring what's going on. steps = {{0, 1}, ...


5

Like you said, & has very low precedence. You've written: funct_: (##) & : has a higher precedence than &, so this is actually equivalent to: (funct_: (##) ) & That is, an unnamed function with body funct_: (##). Of course, your function call doesn't match this pattern, since supplying this argument is no longer optional. We can easily ...


5

This looks like a fairly simple mistake: when correcting the angle you add Pi instead of 2Pi. However, I figured I'd make this an answer, because there's a much simpler way to handle this. You can use Mod to map the angles into the $[0, 2\pi)$ range. You can also compute the arctangent by applying ArcTan directly to the difference of the points so you don't ...


4

Here three approachs: 1. Using comments like (* metadata *) written as plain text directly in the notebook's file. Pros: Human readable. Simple to manage. Readable from other application. If in XML format, simply to validate against a DTD. Cons: Unpredictable behaviour (well, ... a behaviour that I can't understand): sometime I have seen comments, all of ...


4

[...] but I'm hoping someone can suggest a sleek and novel implementation that is easy to use. The answer to this is to use Object-Oriented Design Patterns in Mathematica as explained and exemplified in the presentation "Object Oriented Design Patterns" at the Wolfram Technology Conference 2015. (The presentation recording is also uploaded at ...


3

Here is what you requested. The basic idea is to edit the DownValues of a function you defined. Let's define the fibbonacci function as an example: fib[1] = 1; fib[2] = 2; fib[n_] := fib[n - 1] + fib[n - 2] We can see what the definition of fib internally looks like by using DownValues: DownValues[fib] {HoldPattern[fib[1]] :> 1, ...


2

FromLetterNumber@Range@Range[4] {{a},{a,b},{a,b,c},{a,b,c,d}}


2

I assume that you want to obtain following list {0.8,g[0.8 (*with i=1*)],g[g[0.8] (*with i=2*)]...} One way: g[x_, i_] := ((i - 1/i) x + (1/i) (x/2))/2; FoldList[g, 0.8, Range[20]] Another way, if you really want to use NestList g2[{x_, i_}] := {((i - 1/i) x + (1/i) (x/2))/2, i + 1}; First@Transpose@NestList[g2, {0.8, 1}, 20]


2

I think that value injection using With will work for you. Let's first define the sample g value you want: g = 1 + x^2 Now we define myfunc injecting the current definition of g inside its definition: Clear[myfunc] With[{g = g}, myfunc[f_] := Simplify[f/g]] Now let's change the value of g and check whether the definition of myfunc is affected: g = ...


1

Maybe something like this? g = 1 + x^2 Unprotect[saveg] saveg = g; myfunc[f_] := Simplify[(f/saveg)] Protect[saveg]


1

I think one of the most important core functions is ReplaceAll(/.). I find it useful as some functions output results as rules and then you can use replaceall directly with the results. https://reference.wolfram.com/language/ref/ReplaceAll.html.


1

The discrepancy had to do with the way the walkers were subsequently moved. You have to either move synchronously (on separate threads perhaps) or you have to make it check for a specific case where the walkers are exactly one step away and their next step is towards each other. In other words, if they swap positions then there was a collision. Thanks ...


1

Here is my code for a single walker: singleWalker[] := Module[{ stepTypes = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}, pos1 = RandomInteger[{0, 4}, 2] }, Return@Rest@NestWhileList[ Mod[# + RandomChoice[stepTypes], 5] &, pos1, # != {2, 2} & ]; ] Doing walks = (singleWalker[] & /@ Range[1000]); and measuring N@Mean[Length /@ ...



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