# Tag Info

### Is there any algorithm that runs faster in Mathematica than in C or Fortran?

High-level languages, like Mathematica, have a high overhead for executing each command/instruction. However, they also typically include commands/instructions that solve a larger and more complex ...
• 236k
Accepted

### Modeling the spread of an infection in networked computers

If it is at all an option to represent the grid as a 2D list instead of a list of infected coordinates, I would model this is a cellular automaton. What you've essentially got is an outer totalistic ...
• 8,774
Accepted

### How can I plot this spirally surface?

A manual way of doing it is: ...
• 8,834

### Should I always be using Module in functions unless I explicitly want global variables?

The short answer is "yes", we should always use Module to localize any intermediate variables within our functions. We might get away with using global variables ...
• 69.1k
Accepted

### Visualizing gravity warping the fabric of spacetime

Here my physicist approach to the problem: I consider a cloud/grid of test-particles which get attracted to a reference point by a force. In the following I assume attraction by gravity -- i.e. the ...
• 3,410

### Why does removing a function and defining it on the same line not work?

Executing Trace on an expression reveals what is actually happening: ...
• 1,410

### Can we intelligently control evaluation in Thread?

Analysis of the problem All functions in Mathematica either hold one or more of their arguments, or per The Standard Evaluation Procedure the arguments are evaluated before the function is applied. <...
• 272k

### Cleanest way to take a[b[c]] to a[b][c]

test = a[b[c[d]]]; Fold[ Construct, (* or Compose, see [1] *) Level[test, {-1}, Heads -> True] ] a[b][c][d] [1] - ...
• 137k

### How to make mathematica give 1/0 as infinity

Use the two-arg version of ArcTan instead: ArcTan[0, 4] π/2
• 131k
Accepted

### Mapping function across its arguments

Tuples[expr, 1] (* {f[a], f[b], f[c]} *)
• 941
Accepted

### Inheriting Options?

Let us assume that you are designing a function which can take its own unique options, but also shares option names with Plot. What is the best way to implement ...
• 236k
Accepted

### Custom atomic expressions - modern tutorial

I have a new package for speeding up the construction of things like these which I discuss at the end Atomic vs Valid These are two related but distinct concepts. The former indicates that we can't ...
• 46.9k
Accepted

### Multiple reflections of a laser beam in a triangle

Based on some geometric operations such as reflection and line-line intersection (LLI), I wrote up a small code. Hope this could be a starting point to build a more compact ...
Accepted

### Why do subsequent evaluations seem to be stopped by a False Condition (/;)?

You need to use Update, e.g. Update[ig] or Update[ih], to tell Mathematica that the ...
• 131k

### Writing functions with "Method" options

Introduction What are good (robust?, simple?, efficient?) patterns for doing this kind of code-switching? This answer outlines a development strategy that can produce robust and extensible ...
• 37.9k

### Can we intelligently control evaluation in Thread?

First, let me tell you that my answer here is by no means a replacement for the trickier, but more capable implementation of Mr. Wizard. What I want to show is that the examples I point out at the end ...
• 113k
Accepted

### How I can define this operator in Mathematica?

g[f_][x_?NumericQ] := If[EvenQ@Floor[x], f@FractionalPart[x], 1/f@FractionalPart[x]] The ?NumericQ part is important because <...
• 236k

### How can I plot this spirally surface?

You may want to plot z(r, fi) = sin (r + fi) ...
• 137k
Accepted

### Duplicate the swap function

Attributes[swap] = HoldAll; swap[x_, y_] := {x, y} = {y, x}; x = 1; y = 2; swap[x, y]; {x, y} {2, 1} There are two ...
• 272k

### Permutations of nested parentheses (Dyck words)

StringReplaceList I just realized that there is a comparatively clean though not highly efficient way to write this using ...
• 272k
Accepted

### Piecewise-constant function with infinitely many pieces

That $F$ should really be something like $F_n$, defining a family of functions and then your total $F$ would really be the union of the $\{F_n\}$ over the domains where they are non-zero. Then ...
• 46.9k

### Piecewise-constant function with infinitely many pieces

"... I need it only as a function of x" ClearAll[f] f[x_] := Min[x - Quotient[x, 2], 1 + Quotient[x, 2]] Plot[f[x], {x, 0, 10}] ...
• 398k

### Number of sign flips in list?

list = {1, 1, -1, 1, -1, -1, 1, 1, 1, -1}; Total[Unitize[Subtract[list, RotateRight @ list]]] 6 ...
• 398k

### Number of sign flips in list?

Here's my version: Total @ Unitize @ BitXor[list, RotateRight[list]] 6 Addendum Assuming the lists consist of only 1 and -1, then the ...
• 131k

### How to straighten a curve?

Here is a "homotopy style" transformation between functions $h(x, q) = (1-q) f(x) + q g(x)$. ...
• 3,051
Accepted

### How to define conditional function with Mathematica?

I think it's easier just to define this straight up, rather than compute something procedurally. f[1, 0] = 77; f[0, 1] = 66; f[_, _] = 0; Mathematica is ...
• 13.8k

### Flatten not working

f[...] := Module[{...}, ... /; condition] is a very special syntax (documented in the Details section of Module). It only works ...
• 236k
Accepted

### return {} when the function is given {}

Actually, I want the output to be {} if there is anything wrong with the argument. For this I recommend one or more definitions with patterns that only match a valid argument, and a fall-through ...
• 272k

### How to define a function reminding of names of the independent variables?

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 ...
• 108k