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
1answer
239 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? ...
9
votes
4answers
752 views

Passing large list by reference

I have the following problem: I would like to control evaluation of a variable that points to a list. For example, frequently in the code I have functions of the form that are supposed to work on ...
4
votes
2answers
268 views

How and when to use Evaluate?

A month ago, I used Mathematica to solve my experiment of the Test Technology of Mechanics Engineering. The exeriment is mainly about the Fourier Transform and verify the validity of the theory of ...
8
votes
3answers
303 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 ...
8
votes
2answers
232 views

Cases[data,Colon[key,_]] vs. Cases[data,key:_] toward XPath, XQuery

Exploring methods to index and search tree-structured key:value pairs via named-entity index (key paths) as opposed to Position-based indexing. Using Rule as ...
9
votes
7answers
378 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: ...
7
votes
3answers
474 views

NDSolve and {C, K, Slot} and other built-ins as a variable name

The following problem is an exploration of what causes the error "Input is not an ordinary differential equation" in Mathematica as it seems to have changed from version 8 to version 9. Specifically ...
5
votes
3answers
1k views

Basic questions about running Mathematica

Is there a command window and an editor in Mathematica 8 similar to those in Matlab? In Mathematica GUI mode, all I saw is write code and run it in the same notebook. In my Ubuntu terminal, I ...
4
votes
2answers
183 views

why set values in this way doesn't work?

question is as follows define a list ttt={1,2}; and if I set values in this way {ttt[[1]],ttt[[2]]}={3,4} then the value ...
0
votes
1answer
316 views

How can I make many runs of a simulation and plot all the results with one expression evaluation?

I have many cells, starting with this first cell: ...
44
votes
2answers
1k views

What are the best practices / most common / idiomatic ways to report errors in Mathematica?

In particular, what are the best ways to report errors about incorrect arguments to a function? Checking for the correct arguments is relatively easy using patterns, but how to report an informative ...
24
votes
3answers
915 views

Getting serious about Mathematica programming

I'm at a point in my Mathematica journey where I need to be a little bit more strategic about my coding. Presently, I have a bunch of notebook files organized using the filesystem of the OS and ...
29
votes
4answers
2k views

Can Mathematica be regarded as a software prototyping environment?

I am an undergraduate student, starting to use Mathematica to model simple physical chemistry problems, as a prelude to a summer internship in a computational/theoretical chemistry group. The solution ...
34
votes
1answer
618 views

Export Code to Pseudocode?

I'm writing a paper and all my code is in Mathematica. The snippets are mostly simple, and consist of loops, maps, variables, and conditionals. I'd like add it to my paper, however, not many academic ...
25
votes
4answers
979 views

Question about collections of custom GUI controls for Mathematica

Background: I have only recently begun programming GUIs in Mathematica, so I have a continuous need for custom controls. I needed a scrollable list control and with the help of Google I found a ...
29
votes
4answers
2k views

Finding a percolation path

I would like to examine percolation on a random lattice. To be exact, I wish to find the minimum length of a 'bond' needed such that the leftmost site can be connected to the rightmost site. Here is ...
26
votes
5answers
967 views

Voronoi diagrams for generators other than points

Any suggestions how to determine Voronoi diagram for sites other than points, as e.g. in the picture below? Input is a raster image.
18
votes
4answers
561 views

Continuous evaluation of complex calculations

When having a first look at some system, it's often very useful to just play with the parameters and see how it develops. Therefore, I would like to let a certain evaluation run "until I cancel it". ...
13
votes
0answers
285 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 ...
20
votes
4answers
710 views

Efficient circular buffer?

I wish to create an efficient circular buffer. That is, I wish to keep a fixed length list while appending a new value and dropping the oldest, repeatedly. As we know lists in Mathematica are ...
19
votes
2answers
1k views

Code Readability and Object-Oriented Code

The basic problem I have been running into is making readable code, where in other languages I am more familiar with I might have been using class or struct like objects. For example, you can imagine ...
9
votes
3answers
394 views

Dealing with nested options

OptionValue allows for recent versions of Mathematica (I think as from 7) to easily access optional parameters if they are explicitely given to a function or their default values otherwise. For big ...
22
votes
4answers
1k views

Is it necessary to have a prior computational background or is it possible to learn Mathematica as a first programming language?

I have a background in economics and I'm very interested in learning Mathematica but I'm afraid of starting out because I have seen that most programmers already have a good background in some kind of ...
17
votes
1answer
183 views

How to get Cases to wrap results in an arbitrary head?

A number of functions provide for results to be wrapped in an arbitrary head. This is very important in cases where the results should not be evaluated. Take ...
10
votes
5answers
490 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 ...
21
votes
3answers
837 views

Finding a “not-shortest” path between two vertices

In designing a routine for making a simple three dimensional (5x5x5) labyrinth, I realized that my solutions (a solution is a labyrinth includes a single path from {1, 1, 1} to {5, 5, 5} in a 5 x5x5 ...
14
votes
4answers
1k views

How to implement the Observer design pattern?

As a programmer with a history with procedural languages, the Observer design pattern inmediately springs to my mind when attacking certain class of problems, mostly UI/notifications related. A ...
13
votes
3answers
740 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 ...
12
votes
10answers
608 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 ...
11
votes
1answer
322 views

Triggering actions when a variable is set

Some built-in variables trigger actions when their values are changed: ...
10
votes
2answers
426 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 ...
14
votes
3answers
543 views

How can I improve my code for drawing a tree?

This code draws a tree, but it's not in the functional style. ...
12
votes
2answers
430 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 ...
7
votes
1answer
846 views

Efficient backtracking with Mathematica

Backtracking is a general algorithm for finding all (or some) solutions to some computational problem, that incrementally builds candidates to the solutions, and abandons each partial candidate c ...
17
votes
4answers
379 views

Function that caches when it returns unevaluated

How would you go about implementing a function that caches not the result but the fact that it didn't match a pattern? So that it doesn't waste time recomputing a complex time-consuming pattern ...
14
votes
1answer
300 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 ...
8
votes
3answers
196 views
8
votes
1answer
173 views

How can I make CurrentValue for font characteristics pick up the font of the output not the input cell?

The command CurrentValue (documentation) allows you to access various properties of fonts, such as x-height, line height, and various measures of width such as ...
2
votes
1answer
278 views

Do “objects” in Mathematica have to be declared?

A while ago I learned a trick which allows one to imitate object-oriented programming in MMA using SubValues: ...
18
votes
2answers
419 views

When to use Get vs Needs

When should I use << vs Needs? I just always use Needs as ...
15
votes
1answer
368 views

Importing Zip files

Mathematica can import zipped files automatically, but when the content of the zip file it's another zipped file, I just get the list of files in the second zip: ...
12
votes
2answers
316 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 ...
10
votes
1answer
549 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 ...
9
votes
4answers
466 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$, ...
8
votes
7answers
558 views

Populate an upper triangular matrix from a vector of elements

What is the best way to populate an upper triangular (or alternatively: lower triangular or symmetric) matrix from a vector of elements? This example input: ...
8
votes
1answer
307 views

What is a good coding style for setting and changing application level constants?

Background: Every application has user-definable constants. The Java language has the Properties class to handle them. Question: What is a good and re-usable coding style for setting and changing ...
8
votes
5answers
358 views

How to distinguish between lists and values?

I have a (hopefully small) problem with some numerical integration algorithm, more specifically I want to integrate the imaginary part of a complex valued function, e.g. ...
7
votes
2answers
328 views

find subsequences of constant increase

A list like l = {0, 1, 2, 3, 4, 5, 7, 9, 12, 13, 18, 19} may have subsequences of constant increase, $a_{n+1} = a_n + k$. For example: ...
7
votes
1answer
751 views

Handling failed FindRoot calls

I want to handle FindRoot calls which did not converge (e.g "thrown" error message FindRoot::cvmit) ...
6
votes
3answers
453 views

How to ensure that Polygon[list] plots a simple polygon?

Consider the following code which plots a triangle. p = {{0, 0}, {.2, 0}, {0, .2}}; {Cyan, Polygon[Dynamic[p]]} // Graphics Then adding (for example) ...