40 votes
Accepted

How to implement the sample-point process like the built-ins of Mathematica?

Plot uses two different algorithms depending on whether PerformanceGoal is set to Quality or ...
user avatar
  • 68.8k
38 votes
Accepted

3D tree in Mathematica?

First, an idomatic, but slow version. ...
user avatar
37 votes

How to implement the sample-point process like the built-ins of Mathematica?

Wanna listen to a story? :) It was around 2002 when I finally became fed up with ParametricPlot3D[] and its inability to adaptively plot space curves. Recall that ...
user avatar
22 votes
Accepted

How to generate a blank crossword sheet?

One can use CellularAutomaton and apply only one rule: do not allow 4 white cells together! ...
user avatar
  • 42.8k
21 votes
Accepted

Machine learning. SVM algorithm

As of Version 10 , Mathematica has a built in function Classify, which implements support vector machines and some other common machine learning algorithms. ...
user avatar
21 votes

Replace interior of matrix with zeros

You can do: x[[2 ;; -2, 2 ;; -2]] = 0; x or ...
user avatar
  • 132k
21 votes
Accepted

Faster position list construction from a "take instructions" list

My answer is based on a modification of a binary heap. Basically the construction looks something like this. We start with a binary tree: Notice that if we label the nodes breadth-first, the labels ...
user avatar
  • 7,624
20 votes
Accepted

How to fill a grid make its total be largest

Below is given a solution derived with ILP combinatorial optimization: The total of the assigned values to the $5 \times 5$ table is $61$. I called in the comments this approach to be "brute force" ...
user avatar
19 votes
Accepted

Solving "Resistance between two nodes on a grid" problem in Mathematica

In addition to Carl Woll's post: Computing the pseudoinverse of a the graph Laplacian matrix (a.k.a. the KirchhoffMatrix) is very expensive and in general leads to ...
user avatar
18 votes
Accepted

Efficient way to build a certain square matrix

Here's my take using NestList cm[n_] := NestList[# + 1 &, Join[Range[n/2 + 1], Reverse@Range[n/2]], n - 1] Then ...
user avatar
  • 32.5k
18 votes
Accepted

Implement the Bisection algorithm elegantly and easily

1. Bisection algorithm The algorithm itself is fairly straightforward and "fast" in some sense: the number of iterations is roughly Log2 of the ratio of the ...
user avatar
  • 216k
18 votes

How to implement the sample-point process like the built-ins of Mathematica?

Earlier in the summer I had written the following for How to obtain adaptive sampling as in Plot function?. It is something like J. M.'s technique. But instead of a new version of Mathematica coming ...
user avatar
  • 216k
18 votes
Accepted

Image segmentation by pixel value

Update: If you want 4-neighborhood, you can use MorphologicalComponents to do most of the work, which is fast and easy to implement (that was my original attempt, ...
user avatar
  • 35.5k
18 votes
Accepted

Quick QuickSort implementation

I also got angry about those randomly picked and ill-implemented benchmarks by the Julia team. I appreciate their efforts (jit compilers are useful), but the Fibonacci example was straight away ...
user avatar
17 votes

How to deal with recursion formula in Mathematica?

If you can rewrite the recursion to be tail recursive, you will not run into recursion limits. Here is an example of a tail-recursive implementation of factorial. ...
user avatar
  • 106k
17 votes

Faster position list construction from a "take instructions" list

Preface Below, you will find two different solutions. For understanding the problem itself, the first, iterative solution is better suited since it gives insight in how the solution can be found ...
user avatar
  • 111k
16 votes
Accepted

Random Partitions

...
user avatar
16 votes

Solving "Resistance between two nodes on a grid" problem in Mathematica

Based on rcampion2012's answer to Efficient Implementation of Resistance Distance for graphs?, you could use: ...
user avatar
  • 124k
15 votes
Accepted

Detecting patterns of black and white stones on a 2D board

One function comes to mind that already implements matching of multidimensonal rules: CellularAutomaton. Allow me to represent your board data like this: ...
user avatar
  • 264k
15 votes
Accepted

Splitting strings into letter keys and integers

You asked for shortened, improved, so here it is using RegularExpressions: ...
user avatar
  • 32.5k
15 votes

An inverted pyramid

A simple solution with And, Xor and Mod: ...
user avatar
  • 42.8k
15 votes

Upper bound "Vicious nearest neighbour" algorithm suggestions

(Notebook: https://github.com/arnoudbuzing/rifleproblem) OK, this is not a full blown solution (I can delete it when there is a real answer), but perhaps a useful tool to help others get a feel for ...
user avatar
15 votes
Accepted

The most efficient algorithm to find all possible integer pairs which sum to a given integer

I think IntegerPartitions[m, {2}, listOfIntegers] does exactly what you want, and seems pretty efficient.
user avatar
  • 36.2k
14 votes
Accepted

Replace interior of matrix with zeros

Just another alternative. x - ArrayPad[ArrayPad[x, -1], 1] // MatrixForm
user avatar
  • 68.8k
14 votes
Accepted

How to deal with recursion formula in Mathematica?

A number of points: First, if you need to compute several values of your sequence, your intial memo-ized implementation will NOT run into recursion limit problems. Second, if you need to compute ...
user avatar
  • 4,994
14 votes

Match opening and closing parentheses?

This one returns the pairs in the order they get closed: ...
user avatar
  • 83.4k
13 votes

Implementing an algorithm for finding the largest circle that contains a single point in a set (and no other point)

Here's a direct implementation of Rahul Narain's algorithm using Version 10 functions (no attempt has been made to optimize): ...
user avatar
  • 32.5k
13 votes

Faster position list construction from a "take instructions" list

I have a tree-based method that has the right asymptotics but a very high coefficient. The upshot being, it will not compete with other methods until we get past 10^6 or so in list size. With ...
user avatar
12 votes

Alternative ways to implement a triangular recursion

In this answer, I will use the Functional Paradigm to deal with triangular recursive formula in a uniform manner. For the triangular recursive formula $$T_k^{(n)}=f(T_{k-1}^{(n)},T_{k-1}^{(n+1)})$$ ...
user avatar
  • 435
12 votes

Efficient way to build a certain square matrix

Edit: See end of post for latest performance enhancement. ...
user avatar
  • 25.1k

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