New answers tagged

0

May be, this code does what you are trying to get: g = Compile[{{x, _Real}}, x + 1] g[1] g2[x_] := 1 + g[x] g2[1] Please, note that NumericQ[2 + I] gives True. For examples about compiling, don't take them for shining gold, but here are some snippets I have retrieved from an old notebook: Quiet[Remove[compileFunction]]; compileFunction = Compile[{{x, ...


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 ...


0

Plenty of folks use workbench- I think v2 is in development, but v1 is in production. An alternative could be IntelliJIDEA which may not be a full IDE, but is third-party. Also 5184 Wolfram has a paper on just this: Building large software systems in Mathematica - however, this seems fairly generalized. (didn't link to Wolfram because they make you fill ...


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 = ...


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 ...


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[]}, ...


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.


0

The general method that generating a cylindrical surface is using NURBS theory. See here or here. However, the critical step is solving the contorl-point of the extruded section. Here, I think the tangent-point $A,B,C,D$ should be calculated firstly, and then sampling many midpoints between tangent-point $A, B$ and $C, D$, respectively. Lastly, the ...


0

Local variables will be put in the first {} in Module[], for example: f[x_] := Module[{}, t = x^2]; f[2] (*--> 4*) t (*--> 4*) You will obtain t=4 after you evaluate the codes above, and if you f[x_] := Module[{t}, t = x^2]; f[2] (*--> 4*) t you will obtain t itself. Namely, t is just a global symbol(don't forget clear value ...


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 ...


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 /@ ...


2

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


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}, ...


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 ...


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]


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[ ...


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 ...


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, ...


0

You shouldn't do this. This sounds like the kind of feature that smart people who don't have experience with software engineering dream up. But, there are ways to do this. I think maybe the most reasonable (to me) would to make a decorator (most popularly known thru python) of sorts. We can make a higher order function that does arbitrary things to a ...


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 ...



Top 50 recent answers are included