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

16
votes
1answer
449 views

List manipulation to build a functional expression

Since Mathematica deals with Head[a, b] as an expression in this way The first Part of Head[a, b] is ...
1
vote
1answer
95 views

Avoiding unnecessary computations within loops

Consider the following data: test = RandomReal[{1, 2}, {10, 5, 3, 3}]; How can I do the following without the For loops, e.g. ...
6
votes
2answers
402 views

CorrelationFunction for vectors

Mathematica 9 has a new CorrelationFunction. Sadly the joy does not last long, as I can't get it to work with vectors. I would like to make a velocity correlation ...
2
votes
1answer
582 views

Executing a .m file from the Linux command line

I have some code in a file named Demo-3.m, It can be evaluated in Mathematica 9. The final expression in the file is ...
4
votes
2answers
388 views

Finding all/most roots of a discontinuous function more consistently?

I have the equation: f[x_]:=α Tan[α*a] - Sqrt[c - α^2] and ideally I want to find all of its positive zeros, given a and c, with variable alpha. The problem is ...
4
votes
2answers
155 views

How to achieve Set+Part like behaviour in custom Set function?

So I've been toiling away on my DataFrame package. I've been trying to get Set to work with it, but it seems it either can't be done (ie via ...
15
votes
4answers
1k views

Find the minima and maxima of a list

I have a list, such as: ...
0
votes
1answer
223 views

Why does memory run out when adding same-sized matrices in a loop?

I have written a module that it generates a 101x101x51 zero matrix at first. At every iteration, it generates a list by funct1 and based on this list, if list is empty it gives the previous result. ...
14
votes
1answer
274 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 ...
7
votes
4answers
383 views

How to set Block local variables by code?

I need to create a user defined Block function where the Block variables values are defined by code. For example, imagine I have: ...
0
votes
1answer
116 views

Where do those nulls come from? [closed]

I have seen discussions of unwanted nulls in the output in the context of building lists with conditions on the elements, but that is not involved here. I would like to know where the nulls come from ...
8
votes
3answers
301 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 ...
112
votes
1answer
21k views

Mathematica Minecraft

Some time ago I asked myself: with all these great graphics and interactive capabilities of Mathematica, what kinds of 3D games can be implemented in it? And the answer which came to mind is ...
1
vote
3answers
833 views

Finding the square root of a random number with Newton's method, using While/Do/For loops?

I am trying to construct a program that will find the square root of a number, using Newton's method, which is $$x(n+1) = x_n- f(x) / f'(x_n)$$ The number, will be a random number, generated by: ...
2
votes
2answers
1k views

Product of Fibonacci numbers using For/Do/While loops

I want to find the product of the first 20 Fibonacci numbers (the fibonorial). I want implementations using While, Do, and ...
4
votes
2answers
321 views

Improve performance and style of solution to Project Euler #32

Since I'm fairly new to Mathematica, I'm trying to learn better ways to improve my coding skills so I've turned to Project Euler and this site to speed up my learning pace. Anyways, I was trying to ...
8
votes
4answers
400 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
400 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
725 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
502 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
126 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
494 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
168 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
177 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
156 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
145 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
143 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
318 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
541 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 ...
10
votes
1answer
905 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
146 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?
157
votes
19answers
14k 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
79 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
277 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
601 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
335 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
704 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
197 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
232 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
253 views

How can I find minimum and maximum of a region?

I have a regionplot like the following: ...
4
votes
2answers
205 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
543 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
134 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
825 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
101 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
253 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 ...