Martin Ender
  • Member for 9 years, 4 months
  • Last seen this week
How to make the digits of π go around in a spiral like this?
Accepted answer
73 votes

My original code was crashing when you used too many digits because apparently Mathematica can handle only so many different font sizes. To fix it, I had to borrow george2079's PDF trick to turn each ...

View answer
Writing a word with straight lines
67 votes

Here is a start. I'm sure others will come up with better solutions, but I think from here it's mostly down to finding a better algorithm to pick the random lines. First, we get ourselves a Region ...

View answer
Modeling the spread of an infection in networked computers
Accepted answer
37 votes

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

View answer
Character to replace _ in symbol names?
32 votes

This has been discussed on comp.soft-sys.math.mathematica. The gist is that there are lots of Unicode characters you could use, e.g. \[LetterSpace] or \[UnderBracket] (you could consult https://...

View answer
Creating 3D dice
22 votes

Image3D isn't what you're looking for. That actually generates a "3-dimensional image" with voxels instead of pixels (by using the given images as layers in the voxel grid). That's what you're seeing ...

View answer
Faster derangements?
18 votes

Here is one way to generate them directly: it is based on a way to generate all permutations but discards invalid ones early: derangements[{}, ___] = {{}}; derangements[list_List, orig_List] := ...

View answer
Notation for 2D, semitotalistic, 9-neighbor Moore cellular automata
Accepted answer
15 votes

Simulating only a single generation is trivial. Just omit the Dynamic and the tspec. gameOfLife = {224, {2, {{2, 2, 2}, {2, 1, 2}, {2, 2, 2}}}, {1, 1}}; board = RandomInteger[1, {64, 64}]; ArrayPlot@...

View answer
How do I split a list of numbers to monotonic sequences?
Accepted answer
15 votes

Starting at 10.1, there's a fairly neat solution using SequenceCases: list = {1, 2, 4, 5, 7, 11, 8, 7, 3, 1, -3, -2, 6, 7, 80}; SequenceCases[list, x_ /; Less @@ x || Greater @@ x] (* {{1, 2, 4, 5, 7,...

View answer
Permutations of lists of fixed even numbers
Accepted answer
14 votes

Permutations where no element remains in its original place are called derangements. Counting them is easy enough: the number of derangements of a set of size $n$ is $!n$, or the subfactorial of $n$. ...

View answer
Finding all sublists or substrings of a given list/string
Accepted answer
14 votes

TMTOWTDI applies to both of these problems. Below I present an overview of various approaches I've come across, followed by timing data obtained in 10.4 on Windows 10 (the timing code is available as ...

View answer
How to calculate extremely large exponential
Accepted answer
14 votes

I think you underestimate how many digits this number has. Even if you used scientific notation, the exponent would overflow your PC's memory, so you'd need scientific notation for the scientific ...

View answer
Constructing a graph from a distance matrix
Accepted answer
12 votes

I think you're looking for RelationGraph. It takes a list of objects to treat as vertices and a test function which determines whether two given vertices should be connected by an edge: pts = {{0, 0},...

View answer
Tuples with one "joker" digit?
12 votes

Permutations can do this. You just need to give it enough copies of 1 and 2 such that they can appear arbitrarily often (i.e. n times, where n is the length of your tuples), but only one 3: With[{n = ...

View answer
Is there a numerical method/built-in to calculate the boundary of a set of graphs?
11 votes

Here is one way to do it, although it gives you a rasterised result. graph = Table[ ParametricPlot[ RotationMatrix[m].{2 + 5 Cos[x], 3 + 6 Sin[x]}, {x, 0, 2 Pi}, PlotRange -> {{-10, ...

View answer
Manipulate 2D random walk
Accepted answer
10 votes

The problem is that RandomInteger is reevaluated each time the manipulate is rendered (i.e. when the slider is dragged). You can fix that quite easily, by generating the random numbers outside of the ...

View answer
StringPattern to remove markers around text
Accepted answer
9 votes

With both StringPattern and RegularExpression the problem is greediness: wildcards will try to match as much as possible. With StringPattern this can be fixed using Shortest: StringReplace[buf, "\\...

View answer
Insert a symbol include first and last position
Accepted answer
9 votes

StringRiffle can also take strings to prepend and append to the result if you supply three delimiters instead of one. It's not that much terser though: StringRiffle[list, {"*", "*", "*"}] Of course ...

View answer
Delete only when there are two (or more) 1s
Accepted answer
9 votes

There are a lot of ways to do this. Using Select: Select[list, Count[#, 1] < 2 &] Using Cases: Cases[list, l_ /; Count[l, 1] < 2] Also if the threshold is at 2, then this DeleteCases ...

View answer
How to generate this structure of a list without `While` and the like?
8 votes

Whenever you're building a list with While or For, there's a good chance Table or Array can help. In this case, the solution with Table is quite simple: just use two iterators and make the bounds of ...

View answer
Is a bug about the FindFundamentalCycles?
Accepted answer
8 votes

I believe what you're seeing is a valid solution. Fundamental cycles are defined with respect to a spanning tree. In fact, the documentation says about FindFundamentalCycles: FindFundamentalCycles ...

View answer
How to sort a list by another list?
Accepted answer
8 votes

If I'm understanding your question correctly, something like this would work: list = {{{357, 120}, {271, 78}}, {{239, 90}, {259, 77}}, {{259, 71}, {165, 25}}, {{271, 70}, {337, 30}}}; order = {{...

View answer
Fast creation of state transitions and probabilities?
Accepted answer
7 votes

Yes, it's possible to compute these explicitly. Let's look at exactly what's happening. Only the individual number of occurrences of $0$, $1$ and $2$ matter, which we'll call $c_0$, $c_1$, $c_2$. Let'...

View answer
A function that accepts a list of pairs and adds new pairs that are the averages of the neighbouring pair values
Accepted answer
7 votes

You can get the lists of consecutive pairs with Partition: list1 = {{x1, y1}, {x2, y2}, {x3, y3}}; pairs = Partition[list1, 2, 1] The easiest way to construct your desired output from here is to ...

View answer
Duplicating and arranging a list
Accepted answer
7 votes

You can use Map (or its shorthand /@) together with Sequence over the list: list = {a, b, c} Sequence[#, #, #] & /@ list (* {a, a, a, b, b, b, c, c, c} *) For larger amounts of repetition, that ...

View answer
list of all permuations of sublists of a nested list
Accepted answer
7 votes

What you're looking for is the outer product of all the lists returned by Permutations /@ .... You can use Outer for that. The only issue is that Outer returns the result as a nested list (basically ...

View answer
Coefficient list of an expression with indices
7 votes

Turning my comment into an answer. If you know that this is exactly the form of the expression, you can just pick out the coefficients manually with First. It's important though that you first turn ...

View answer
How to built such list
Accepted answer
6 votes

As Carl Woll hinted at in the comment, for input {x, y} you're looking for the x-subsets of the list {1, 2, ..., x + y}. As you might expect, Mathematica has a built-in for that: fun[{x_, y_}] := ...

View answer
Calculating the angle between the line joining two points and the x-axis
6 votes

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

View answer
How to define a set of two random orthogonal unit vectors?
Accepted answer
6 votes

As nikie pointed out, the issue is that each time you call one of your functions (regardless of k) new random vectors will be calculated. A quick fix for this is to memoise the functions. That means ...

View answer
Joining table of tuples with table of scalar values
Accepted answer
6 votes

J. M.'s suggestions should work fine, but a third option is MapThread, which I find a bit more intuitive and where the level specification is a bit less awkward: MapThread[List, {tup, ot}, 3] ...

View answer