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

12
votes
9answers
453 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
3answers
1k 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 ...
12
votes
5answers
2k 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?
12
votes
4answers
2k 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? ...
12
votes
4answers
1k views

Pure Functions with Lists as arguments

Assuming I have two function: example 1: add[{x_, y_, z_}] := x + y - z add[{1, 3, 5}] If use pure function,I know I can write it as : ...
12
votes
5answers
607 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: ...
12
votes
5answers
790 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
508 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
3answers
2k 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, ...
12
votes
2answers
262 views

ShearingTransform and Dynamic

Background: I have a geometric transformation composed of a RotationTransform, ScalingTransform and ...
12
votes
2answers
1k 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 ...
12
votes
2answers
668 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 ...
12
votes
3answers
1k 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
506 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
4answers
630 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 ...
11
votes
4answers
566 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
4answers
574 views

How to implement dual numbers in Mathematica?

I wonder how can I implement dual numbers in Mathematica, so that all functions work well with them (as with complex numbers). Particularly, for each function $f$, ...
11
votes
5answers
2k 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
6answers
396 views

For a given expression: if it appears, remove it, but if it is absent, add it

While reformatting Szabolcs's code from (42660) I noticed this interesting operation: ...
11
votes
1answer
1k views

Use Mathematica as a terminal

I love how notebooks work in Mathematica. You can edit code in real time and hit Ctrl+Shift to run it. Additionally you copy and paste different cells around to organize and test ideas. Search ...
11
votes
2answers
3k views

Pass function or formula as function parameter

I can write either Integrate[x^2, {x,0,3}] or f[x_] = x^2 Integrate[f[x],{x,0,3}] and get the same computation. Suppose I ...
11
votes
3answers
183 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
2answers
638 views

Dynamic Programming with delayed evaluation

By using dynamical programming, we can save intermediate steps for recursive relations, as in f[n_]:= f[n] = f[n-1] + f[n-2] However, this only stores ...
11
votes
1answer
371 views

Triggering actions when a variable is set

Some built-in variables trigger actions when their values are changed: ...
11
votes
1answer
330 views

Using Apply inside Compile

In this great answer a compiled version of the Nelder-Mead algorithm is presented. Since it works on arbitrary dimensions (i.e. arbitrary number of arguments), it has to use apply on the objective ...
11
votes
1answer
435 views

Mathematica implementation of Earth Movers Distance?

Has anyone implemented in Mathematica the Earth Mover's Distance (EMD)? A concept dating back to Monge 1871, used to compare histograms and images, e.g., the CIEDE2000 Color Difference algorithm. ...
11
votes
1answer
174 views

Figuring out how AbsoluteOptions works with Graphs

I am puzzled by how AbsoluteOptions works with Graph objects. I would have expected that one could use it to obtain all the ...
10
votes
4answers
425 views

When should I use Apply (or Function) and when @@ (or &)?

This is a rather general question, which I fail to answer myself. I guess it is mainly due to my insufficient knowledge of the precise terms. If I understand correctly, the following are equivalent: ...
10
votes
4answers
290 views

Searching linked lists that contain lists?

Following the advice I've read here and other sites, I've been trying to use the Mathematica equivalent of a linked lists... ...
10
votes
5answers
528 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 ...
10
votes
5answers
2k views

Select/Delete with Sublist elements?

Probably easy and short question, I still didn't fully figure out how to easily select/delete sublists from a list. Example: tt = {{2, 4}, {4, 8}} I want to delete/select all the elements where ...
10
votes
2answers
722 views

Finding all possible solutions to a math mind game

In the example below the letters shall be replaced by numbers. The numbers are from 1 to 9, and each letter has the same number ...
10
votes
2answers
214 views

Built-in symbols that support operator form

In version 10 some built-in symbols are updated to support operator form like the Derivative. For example, Select, ...
10
votes
3answers
356 views

How can I hold UpValues but evaluate other expressions?

Consider these definitions: own = "OwnValue"; down[_] = "DownValue"; sub[_][_] = "SubValue"; N[n] = 3.14; _[___, up, ___] ^= "UpValue"; The attribute ...
10
votes
5answers
554 views

Best way to modify values in a list of rules?

Recently I had to solve a problem similar to this: Let's say I have a list of list of rules ...
10
votes
3answers
522 views

How can I speed up SQL queries in Mathematica?

I have an application that queries a MySQL database many times for data. At the moment, Mathematica and MySQL are running on the same box and yet it takes over 1.6 seconds for the query to run and ...
10
votes
3answers
358 views

Patternmatching sets

How to match set-patterns against sets? A set (in the mathematical sense) is a list of elements without repetition and order of elements does not matter. For example, we have a pattern set ...
10
votes
3answers
433 views

Generating Linear Extensions of a Partial Order

Given a set $S$ and a partial order $\prec$ over $S$, I'm looking for a way to "efficiently" generate a list of linear extensions of $\prec$. Suppose the partial order is given by a ...
10
votes
2answers
237 views

Why isn't Lookup treated like other symbols?

With Mathematica 10, I wanted to overload Lookup using TagSetDelayed, but it doesn't work. For instance, if I define two objects ...
10
votes
1answer
182 views

Are formal characters like \[FormalA] to be used differently than other symbols?

I noticed that Mathematica has a set of special 'Formal' characters such as \[FormalA], \[FormalB] , etc.. In the front end, it ...
10
votes
1answer
215 views

Is there a method to enumerate the keys/values of System`Utilities`HashTable

Lacking from the simple API of System`Utilities`HashTable is a method to enumerate the keys/values. ...
10
votes
1answer
650 views

Modifying a List in a function in place [duplicate]

An example will be most specific: func[list_, column_] := list[[All, column]] = Map[#*2 &, list[[All, column]]]; This throws errors. I want to avoid doing ...
10
votes
1answer
414 views

Abort not aborting

I have a relatively simple bit of code that looks like the following: ---some code here--- If[statement, Abort[]]; ---more code following--- However, while ...
10
votes
2answers
346 views

Visualizing several long lists of numerical information to see relative frequency

Mathematica has very powerful visualization techniques. However, I'm at a loss at how to best make the following chart readable. ...
10
votes
1answer
171 views

Copy Built-in function to new name

It is know that some built-in functions are system function and changing their properties may cause trouble. Suppose for example I want to change features of Cases: ...
10
votes
0answers
287 views

Create a new infix operator [duplicate]

It is well known that the infix operator @@ is used as a short form of Apply. For example, both ...
9
votes
7answers
403 views

How to efficiently Append a result of an operation on each element of a list to itself

I'm looking for the best function to apply the product of the last two elements of sublist elements to each element: Example: ...
9
votes
6answers
558 views

Best way to find permutation with specific total

Can anyone suggest a good way to find permutations of 12 digits, 0 to 4, totalling 24. I.e. two such permutations:- {2, 1, 4, 1, 2, 2, 2, 2, 2, 2, 1, 3} // Total ...
9
votes
4answers
327 views

Interactively inspecting parts of an object

There have been times when I want to find the structure of an object in order to extract some part of it. For example, extracting the x and y coordinates from a Graphics object generated by Plot. I ...
9
votes
3answers
748 views

Improve speed for calculating a recursive sequence

I want to calculate following recursive sequence: $\alpha_{0}=0,\\ \cos(\alpha_{i})=\cos(\alpha_{i-1})\cdot\cos(\beta_{i})+\sin(\alpha_{i-1})\cdot\sin(\beta_{i})\cdot\cos(\gamma_{i}).$ In ...