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

8
votes
4answers
392 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 ...
2
votes
0answers
381 views

Why is Mathematica limiting the execution time of my calculation? [duplicate]

Is there a Front End option (or programming option) in Mathematica 9 that limits the amount of time that functions take to execute? I have a function (as part of a Mathematica Application) that shows ...
9
votes
4answers
700 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 ...
0
votes
1answer
487 views

Find Roots in Do loop

Task: Finding roots in loop t = List[1, 2, 3, 4, 5] fx[x_] := a*x^2 - 5 List[Do[Print[FindRoot[fx[k] == 1, {a, 1}]], {k, 0, 5}]] Output: Currently the output is ...
4
votes
1answer
123 views

Determining End of Notebook with Mathematica 9

The application package I had working in Mathematica 8 no longer works in Mathematica 9. I believe the problem lies in how I cycle through cells and test for reaching the last cell of the notebook. ...
3
votes
3answers
484 views

Is Table the only functional way to construct nested loops in Mathematica?

Suppose you have a cubic box containing identical particles and you want to compute the distances between each particle and every other particle in the box without double-counting (i.e., if you've ...
1
vote
3answers
166 views

Using Outer with Compiled functions that accept more than 2 arguments

How does one use Outer with a compiled function that accepts 3 or more arguments. Alternatively, how does one create a compiled function with 3 or more arguments ...
2
votes
3answers
174 views

How can I find the points of B Spline functions for a specific range?

Based on this question: I have a list of bspline functions (nearly 1000) provided by applying BSplineFunction to a list of points. I want to get the points each of ...
1
vote
2answers
151 views

Checking equality with ComplexInfinity and testing whether a function is (mathematically) undefined

This seems ridiculously basic, but I cannot find anywhere explaining how to do it. If you evaluate certain undefined functions like 1/0 or ...
2
votes
1answer
142 views

Discard functions with wrong symmetry?

I have functions of x, where x is the polar angle of spherical coordinates, i.e. ...
10
votes
1answer
140 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 ...
6
votes
3answers
314 views

Delete elements from a list really fast

I have this bit of code that works, but it's very slow when there are 600k elements in the list: ...
1
vote
1answer
511 views

pointer like operations in mathematica and evaluation control

I would like to create a data structure of the type "header[pointer]" where pointer would point to a list. I will shown on an example how I'd like this to work, and where the problems are. ...
2
votes
0answers
109 views

Programming Mathematica - Introduction by Paul Wellin [duplicate]

Possible Duplicate: Where can I find examples of good Mathematica programming practice? Can anyone recommend a decent book on programming mathematica ? I came across this which looks to be ...
9
votes
1answer
849 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 ...
7
votes
2answers
1k views

Easier program for period of Fibonacci sequence modulo p

For a little project I need to calculate the period of a Fibonacci sequence modulo p, for which p is a prime number. For example, the Fibonacci sequence modulo 19 would be: $$0, 1, 1, 2, 3, 5, 8, 13, ...
1
vote
1answer
144 views

What is the new command for AffineMap?

AffineMap was present in Mathematica version 4. What is the equivalent command in the current version of Mathematica?
149
votes
19answers
13k views

What are the most common pitfalls awaiting new users?

As you may already know, Mathematica is a wonderful piece of software. However, it has a few characteristics that tend to confuse new (and sometimes not-so-new) users. That can be clearly seen from ...
7
votes
1answer
77 views

Aborting from inside a Dialog

Suppose I got in a Dialog and wanted to not Return but Abort the computation. How could I do ...
5
votes
1answer
272 views

Simplification of double symbolic sums containing a DiscreteDelta without explicit summation range

I am trying to get Mathematica to automatically do simplifications like the following: $$\sum\limits_{q}^{q\in qV}\sum\limits_{q'}^{q'\in q'V}{f(q)g(q')\delta(q-q')}=\sum_{q}^{q\in qV}{f(q)g(q)}.$$ ...
14
votes
3answers
593 views

1D Random Walk variant

I am making a notebook that is a variation to the traditional 1d random walk problem. The normal 1D random walk can be simulated easily by ...
2
votes
1answer
322 views

Nested Sums to multiple sum

I would like to automatically "move nested sums to the left". I mean, just take out of an expression all the summations and go from a nested Sum to a multiple sum. Something like starting with: ...
20
votes
4answers
694 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 ...
5
votes
3answers
196 views

How could I implement the equivalent of NextPrime

I would like to know what an implementation of the function NextPrime would look like if it were implemented in Mathematica's core language.
2
votes
1answer
147 views

Rookie mistake in defining a function with Modules?

I can't figure out what's wrong with this piece of code. I'm trying to define a function that counts the occurrences of x, y and ...
0
votes
1answer
229 views

Rayleigh Criterion

I want to use the Rayleigh criterion to find the resolution of an optical system. I have two functions f1 = Sinc[x]^2 and ...
1
vote
2answers
246 views

How can I find minimum and maximum of a region?

I have a regionplot like the following: ...
4
votes
2answers
202 views

How can I compare the previous value and the current value implemented during NestWhileList?

I am trying to write a part of my code. I am using NestWhileList. However, I couldn't find out the right syntax for testing. It should compare previous result and current result and continue if it is ...
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
541 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 ...
4
votes
1answer
132 views

NumberForm and units

NumberForm can be used to print Quantities to a specified precision. For example, x=Quantity[5.123456789,"Meters"]; 5.12346 m Print[NumberForm[x,{4,2}]] 5.12 m ...
11
votes
4answers
787 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 : ...
1
vote
1answer
100 views

How can I find the row differences of a matrix?

How can I compute the row differences of an m x n matrix to obtain an m-1 x n matrix; that is, given how do I obtain
0
votes
1answer
249 views

How can I improve my code with efficient mathematica built-in functions?

I have written a module for my assignment like below. V[n + 1] = rk[x[n], y[n] ,z[n], V[n]]; vs = V[n].V[n+1]; V[n + 1] = I want to modify it with efficient ...
2
votes
2answers
559 views

defining recursively a function with multiple if conditions

I am trying to recursively define a function which satisfies the following system of equations and which depends on two parameters $n$ and $l$, $$ \begin{align} A(x, n, l) &= F[ A(x,n-1,l) ]\\ ...
2
votes
1answer
117 views

Import a file only if it contains a certain pattern

Let's say I have three files containing: File 1: x y z 1 4 6 File 2: x y z 2 5 9 File 3: x y z 5 4 8 My goal is to import the data from a file only if in ...
6
votes
1answer
601 views

Coding mistake? [closed]

I have just started using Mathematica with v9.0. I am trying to follow a computation from a book on Fourier series with the function $f(x)=x$ on the interval $-\pi < x < \pi$. Here is the code ...
8
votes
4answers
486 views

Efficient code for the Ten True Sentences puzzle

I am trying to solve Ten True Sentences Puzzle. Take a look at the following sentences: The number of times the digit 0 appears in this puzzle is _. The number of times the digit 1 ...
0
votes
3answers
369 views

How can I fill in a matrix at every iteration of a Do loop?

I have a matrix whose dimensions are 26x26x3. For my assignment I have written a program which is using a Do loop (maxx=maxy=10,maxz=2): ...
0
votes
2answers
120 views

How to obtain a smaller-sized output from Solve

I am solving a system of 15 simultaneous linear equations using Solve. On running the code, I get a message saying "A very large output was generated" with options ...
4
votes
2answers
270 views

How can I find the lines that go through?

I have a 80 x 80 x 50 dataset. From this dataset, I get some curves. These curves are randomly seeded, by which I mean that their starting points have been chosen randomly. Below, you can find an ...
5
votes
1answer
672 views

How can I view the code implementing a built-in Mathematica function? [duplicate]

Possible Duplicate: What is the most convenient way to read definitions of in-memory symbols when we don’t have the source files? I would like to look at the code that implements ...
2
votes
3answers
267 views

How do I construct a pure function to extract data fields from records that are strings?

How can I get from data segment format to the final segment format with a pure function? The data fields are fixed length. There are no separators between the fields. a) list ...
12
votes
4answers
481 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 ...
1
vote
1answer
178 views

How do I insert a piece of code in Mathematica?

In Fortran, I can use include "file" to insert a piece of code. Is there a similar method in Mathematica?
5
votes
1answer
181 views

Should Row be used in place of StringForm?

I've been using Mathematica for a long time, so I have a habit of using some of the older functions even when newer, better ones have been added to replace them. ...
8
votes
3answers
243 views

Grid without lines, but with dots

I want to build a grid to look like the top-left table shown below, but without an outside frame. Here is what I've tried to do: ...
34
votes
1answer
1k views

Once more on object orientation in Mathematica: does it have to be so hard?

Recently I came across a set of problems which would be solved most easily within an object-oriented approach. I first attempted to solve them by other means, but found the complexity of the code ...
5
votes
4answers
1k views

How to find the sum all even numbers of this sequence?

I have a sequence $(u_{n})$ $$u_1= 1, \quad u_2 = 2, \quad u_3 = 3, \quad u_{n}= -u_{n-3} + 3u _{n-2} +2 u_{n-1}, \quad \forall n \geqslant 4.$$ I want to list the first $20$ terms of this sequence ...
14
votes
3answers
537 views

How can I improve my code for drawing a tree?

This code draws a tree, but it's not in the functional style. ...