Skip to main content
78 votes

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 ...
Szabolcs's user avatar
  • 236k
37 votes
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 ...
Martin Ender's user avatar
  • 8,774
33 votes
Accepted

How can I plot this spirally surface?

A manual way of doing it is: ...
Thies Heidecke's user avatar
29 votes

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 ...
WReach's user avatar
  • 69.1k
29 votes
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 ...
N0va's user avatar
  • 3,410
27 votes

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

Executing Trace on an expression reveals what is actually happening: ...
dan7geo's user avatar
  • 1,410
24 votes

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. <...
Mr.Wizard's user avatar
  • 272k
24 votes

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] - ...
Kuba's user avatar
  • 137k
23 votes

How to make mathematica give 1/0 as infinity

Use the two-arg version of ArcTan instead: ArcTan[0, 4] π/2
Carl Woll's user avatar
  • 131k
23 votes
Accepted

Mapping function across its arguments

Tuples[expr, 1] (* {f[a], f[b], f[c]} *)
Karl's user avatar
  • 941
21 votes
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 ...
Szabolcs's user avatar
  • 236k
21 votes
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 ...
b3m2a1's user avatar
  • 46.9k
20 votes
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 ...
Joo-Haeng Lee's user avatar
19 votes
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 ...
Carl Woll's user avatar
  • 131k
18 votes

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 ...
Anton Antonov's user avatar
18 votes

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 ...
halirutan's user avatar
  • 113k
18 votes
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 <...
Szabolcs's user avatar
  • 236k
17 votes

How can I plot this spirally surface?

You may want to plot z(r, fi) = sin (r + fi) ...
Kuba's user avatar
  • 137k
17 votes
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 ...
Mr.Wizard's user avatar
  • 272k
17 votes

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 ...
Mr.Wizard's user avatar
  • 272k
17 votes
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 ...
b3m2a1's user avatar
  • 46.9k
17 votes

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}] ...
kglr's user avatar
  • 398k
17 votes

Number of sign flips in list?

list = {1, 1, -1, 1, -1, -1, 1, 1, 1, -1}; Total[Unitize[Subtract[list, RotateRight @ list]]] 6 ...
kglr's user avatar
  • 398k
17 votes

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 ...
Carl Woll's user avatar
  • 131k
17 votes

How to straighten a curve?

Here is a "homotopy style" transformation between functions $h(x, q) = (1-q) f(x) + q g(x)$. ...
I.M.'s user avatar
  • 3,051
16 votes
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 ...
John Doty's user avatar
  • 13.8k
16 votes

Flatten not working

f[...] := Module[{...}, ... /; condition] is a very special syntax (documented in the Details section of Module). It only works ...
Szabolcs's user avatar
  • 236k
16 votes
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 ...
Mr.Wizard's user avatar
  • 272k
16 votes

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 ...
m_goldberg's user avatar
  • 108k
16 votes
Accepted

Formula for calculating equivalent parallel resistance

Sum is really for symbolic sums. It's clumsy here. I suggest: Rp[r_List] := 1/Total[1/r]
John Doty's user avatar
  • 13.8k

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