Questions on writing non-trivial programs in Mathematica. Do not use this tag for questions on plotting/graphics or for questions on doing mathematics with Mathematica, where the focus is more on the math than the program.

learn more… | top users | synonyms

14
votes
3answers
159 views

What does `: Hold[$IterationLimit]` mean?

In this answer, what does the : Hold[$IterationLimit] part of the following construct do? ...
14
votes
1answer
229 views

How to make a function like Set, but with a Block construct for the pattern names

How can we define a function that works like f[x_]=ComputeSomething[x] and treats x as a variable that does not have a value? ...
14
votes
2answers
463 views

Lexicographic ordering of strings in Mathematica

I recently realized that Mathematica seems incapable of comparing strings in the "normal" expected lexicographic order. Indeed, for some simulations, I need to process text directly, without fiddling ...
14
votes
2answers
346 views

Distributing PlusMinus consistently

I would like to give the built-in function PlusMinus, which is undefined by default, the intuitive meaning: considering all possible combinations of $+$ and $-$. ...
13
votes
4answers
1k views

Find the minima and maxima of a list

I have a list, such as: ...
13
votes
2answers
873 views

Is Mathematica a Turing-complete language?

Is Mathematica a Turing-complete language? If so, how can that be proved? If not, why?
13
votes
6answers
817 views

Splitting up delimited data in lists

One task that I frequently find myself doing in Mathematica is splitting lists into lists of sublists, using specific elements to define the break-points. This is particularly useful with imported ...
13
votes
4answers
952 views

Is there a Mathematica/Lisp link?

I'd like to start learning Lisp as it sometimes leads to interesting answers on this site with concepts borrowed from this language, so I'm curious. As I already spend most of my programming time in ...
13
votes
3answers
260 views

How to keep some of the results of the NestList

I'm using some iterative arithmetics to calculate wave propagation with the help of NestList. I have to use a small step size for iteration to guarantee the accuracy, which lead to too much data (e.g, ...
13
votes
2answers
268 views

How to make use of NumericQ[x] = True (and use it safely)?

Basic Issue: I'm trying to understand the proper use of NumericQ's "magical" capabilities. Please consider the examples below. Actual question and some links are ...
13
votes
2answers
697 views

Suppressing negative roots in Mathematica

Problem Using Mathematica's Solve operator can sometimes lead to an output involving a positive and negative root (say when solving for a variable such as ...
13
votes
2answers
353 views

Efficient priority queues?

I'm trying to figure out how to transfer the concept of a priority queue to the functional world. Searches have turned up some implementations that use Append and other expense list copying ...
13
votes
3answers
626 views

Reading from STDIN, or: how to pipe data into Mathematica

Today I tried using Mathematica's plotting capabilities to display the output of a C++ program. This made me wonder whether it is possible to somehow tell a Mathematica script to read from STDIN and ...
13
votes
2answers
538 views

Question about designing a particular data structure

Background: I store polygons with a color assigned at the face level. In some occasions I want to use vertex colors but I want to store data efficiently, i.e. don't want to duplicate data to the ...
13
votes
1answer
242 views

Is the sharing of variables in Module/Block within Compile documented behavior?

Today I noticed something, I think for the first time. When used inside Compile variable values within Module (and ...
13
votes
1answer
272 views

Count number of sublists with a total not greater than a given max

Suppose I have a list of positive integers: data={1, 1, 2, 3, 3, 3, 5, 5, 5, 7, 7, 8, 8, 9, 10, 10, 12, 16, 23} I want to count the number of subsets up to ...
13
votes
2answers
389 views

Most influential and/or disrupting features introduced with new versions of Mathematica

This is something that has been bugging me for ages. With every upgrade, Mathematica has added new functionalities and amendend some of its bugs. In the process, though, some older functionality has ...
13
votes
0answers
248 views

What Mathematica features and idioms have n^2 complexity or worse

Here are a few: AppendTo[data, elem], Sort[data, test], in some cases use of ___ (as ...
12
votes
9answers
441 views

Generating a matrix using sublists A and B n times

I want to write a function that generates a square matrix from sublists. My sublists are a = Range[0, x, 0.5]; b = Range[0.25, x + 0.25, 0.5]; Suppose x=2, then I ...
12
votes
10answers
597 views

How can I generate a nested array like this one?

Well, this is how the array looks like: I came up with this code: n = 10; Nest[ArrayPad[#, 1, 1 - #[[1, 1]]] &, {{1}}, n] /. {1 -> "*"} Any better ...
12
votes
5answers
1k views

Finding all elements within a certain range in a sorted list

Suppose we have a sorted list of values. Let's use list = Sort@RandomReal[1, 1000000]; for this example. I need a fast function ...
12
votes
5answers
620 views

How to correct my code for solving the Josephus problem?

Problem Decription Recently, I have been reading the book Schaum's Outline of Mathematica (2nd Edition), where I encounted the problem: Flavius Josephus was a Jewish historian of the first ...
12
votes
5answers
681 views

How to perform a depth-first preorder traversal of an expression?

Due to confusion of terminology on my part I asked the wrong question before. Since it has already received other answers I shall not edit it, but instead ask the question I meant to in the first ...
12
votes
5answers
663 views

How to improve this code for solving the “Mr.S and Mr.P” puzzle?

Mr.S and Mr. P puzzle — "Formalization of two Puzzles Involving Knowledge", McCarthy, John (1987) We pick two numbers $a$ and $b$, such that $a\geq b$ and both numbers are within the range ...
12
votes
4answers
471 views

Can you recommend an efficient method for finding the least integer satisfying my inequality?

I tried find the minimum make $\frac{1}{2}+\frac{1}{2+\sqrt{2}}+\frac{1}{3 +\sqrt{3}}+...+\frac{1}{n+\sqrt{n}}>15$ Following code is so slow when I use condition ...
12
votes
2answers
373 views

Need help coding/creating a recursive list

I have two lists A and B, each with 99 real numbers. I want to form a list, C, whose ...
12
votes
3answers
270 views

Notebook vs. DocumentNotebook; display a Notebook inline, like a DocumentNotebook

If you evaluate a DocumentNotebook[] expression in the front-end, it nicely displays inline, inside of an output cell in the current notebook: For my purposes, ...
12
votes
1answer
359 views

rule-based implementation of an algorithm

When I first started learning about rule-based programming with Mathematica, I tried to translate this algorithm for computing the convex hull of a set of 2-D points in $O(n \log(n))$ time, to use ...
12
votes
2answers
254 views

Listing subgraphs of G isomorphic to SubG

If I have an undirected graph G, how could I write a function in Mathematica to obtain a list of subgraphs of G that are isomorphic to some other undirected graph SubG? I'd like to learn how to ...
12
votes
3answers
739 views

Big-Oh Notation

Is there a way to have Mathematica understand Big-Oh notation? For example, I want something like: MinBigOh[...] where ...
11
votes
8answers
428 views

Any built-in function to generate successive sublists from a list?

Given lst = {a, b, c, d} I'd like to generate {{a}, {a, b}, {a, b, c}, {a, b, c, d}} but using built-in functions only, ...
11
votes
3answers
299 views

Does AbsoluteTiming slow the evaluation time?

I've started using AbsoluteTiming to discriminate between similar function constructs, but I sometimes feel like it takes substantially more time to evaluate a ...
11
votes
3answers
852 views

What is Mathematica's equivalent to MATLAB's filter function?

The MATLAB code filter(0.5,[1, -0.5], [1:10]) is equivalent to Rest@FoldList[(#1 + #2)/2. &, 0, Range[10]] I don't ...
11
votes
5answers
748 views

How can I get exactly 5 logarithmic divisions of an interval?

I'd like to get exactly 5 divisions from x to y on a log scale. Can FindDivisions do this?
11
votes
4answers
1k views

How to modify function argument?

Usually in programming languages, function arguments are normal local variables, which can be modified. Is this not true in Mathematica? ...
11
votes
4answers
524 views

How to write this without For loop

Suppose I have a few lists of numbers and want to exponentiate element-wise, then sum up everything into a polynomial. For example, if I have ...
11
votes
3answers
321 views

Custom functions by delegating options in a specific way and using core functions

I'd like to create a custom function that does essentially the same as a core function of mathematica but uses different default settings. Example: I want a Plot function that uses Mathematica's core ...
11
votes
5answers
1k views

What is the fastest way to extract a substring from a huge string?

I develop an algorithm to find the most common substrings of a given length n up to a certain distance l in front of a pattern p in a huge sequence S. E.g. I want to find the most common hexamers ...
11
votes
3answers
1k views

Programmatically generate packages from notebook files?

Currently, there are two ways I know of to generate a package (.m) file from a notebook file; one is to set the AutoGeneratedPackage option in the Option Inspector, ...
11
votes
3answers
379 views

How can I type-check the arguments of a Mathematica function?

(For educational purposes) I defined the following functions: Translation (in $\mathbb{R}^2$): trM[vec_] := {{1, 0, vec[[1]]}, {0, 1, vec[[2]]}, {0, 0, 1}} ...
11
votes
2answers
541 views

Google Code Jam: Mountain View code review

After participating in Google Code Jam 2012 Round 2, it occurred to me that the Mountain View problem boiled down to solving a set of linear inequalities, which could be done in Mathematica. I've only ...
11
votes
2answers
278 views

Triggering actions when a variable is set

Some built-in variables trigger actions when their values are changed: ...
11
votes
3answers
161 views

Emulating Sequence with my own function

I just considered if/how one could implement Sequence in Mathematica if it were not predefined. It turned out that the following simple definition has in all my ...
11
votes
1answer
255 views

Modules that initialize themselves on first call

I use a lot of functions that extract a specific data item from a file with many data items. I want these functions to load data (slow) and return the item (fast) on first call, but just return the ...
11
votes
1answer
805 views

Poisson solver using Mathematica

I am looking for some help with a Poisson solver I am writing in Mathematica. The code is quite long with Arrays plugged in, so the full details can be found at http://pastebin.com/uSrSDcW6 I am ...
11
votes
1answer
231 views

Evaluation indicator for a notebook

I have a GUI with a number of TabView and other Manipulate controls. Sometimes clicking from one Tabview to the other can take a while, and I would like to have a global "Please wait" indicator. So ...
11
votes
1answer
310 views

Generating Gelfand-Tsetlin patterns

I am doing some research on some combinatorial object called GT-patterns. They are generated from three parts of data. Two integer, partitions (sequences of weakly decreasing numbers), $\lambda = ...
10
votes
4answers
533 views

Reproducing Image Patterns with Mathematica

Surfing on the net I stared at this pattern. It´s a bamboo steam, and it´s awesome. I'm new to Mathematica so I'm a little lost, I'd like to try to reproduce this pattern, of course in a very ...
10
votes
5answers
500 views

Mathematica quine

A quine is a computer program which takes no input and produces a copy of its own source code as its only output. There are many trivial quines in Mathematica: ...
10
votes
5answers
446 views

How to find first list element that differs from average of N previous elements by more than a given amount?

Given a list of real numbers, I need to write a function that returns the value of the first element in the list that differs from the mean of the previous $N$ elements by greater than $P$ standard ...