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

4
votes
1answer
157 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)}.$$ ...
10
votes
3answers
377 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
146 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: ...
18
votes
4answers
500 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 ...
3
votes
0answers
179 views

Running a Mathematica program without Mathematica

I want to create a program in Mathematica that can run without using Mathematica. Is it possible and how should I do it?
5
votes
3answers
143 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
114 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
169 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
125 views

How can I find minimum and maximum of a region?

I have a regionplot like the following: ...
4
votes
2answers
118 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 ...
0
votes
0answers
116 views

Diffusion equation with concentration dependent diffusivity, NDSolve has problem at C0> 10^20

I am using NDSolve to solve the diffusion equation: D[Cp[x, t], t] == D[Dc D[Cp[x, t], x], x] where Dc is a function of ...
10
votes
3answers
344 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? ...
10
votes
4answers
490 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
84 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 ...
6
votes
4answers
348 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
80 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
190 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
242 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
89 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
288 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 ...
7
votes
3answers
308 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
179 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
92 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
223 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
288 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
234 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 ...
11
votes
4answers
414 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
156 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
117 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
202 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: ...
30
votes
1answer
610 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
522 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
425 views

How can I improve my code for drawing a tree?

This code draws a tree, but it's not in the functional style. ...
4
votes
3answers
189 views

More structure in Source Code/Notebooks

Mathematica is great for small and quick projects and has a great syntax. However as soon as a project grows I run into trouble. How do you scale and maintain projects in Mathematica? What is beyond ...
11
votes
8answers
380 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, ...
4
votes
2answers
195 views

About extension fibonacci

I have been trying to solve this problem, it worked slow. I know there is a efficiently way. How do I get it? The following is my code: ...
3
votes
1answer
109 views

How to make a pattern to change x^(2/y) to (x^2)^(1/y) for any x and y?

This might have been asked before. Please let me know if it is. I was looking at an interesting Mathematica question in students forum and trying to solve it, and a chance to learn more about ...
0
votes
1answer
280 views

Loops and subroutines [duplicate]

Possible Duplicate: Alternatives to procedural loops and iterating over lists in Mathematica I am new to Mathematica and not familiar with functional programming. In particular, I have no ...
0
votes
1answer
70 views

Draw from HistogramDistribution with ParallelTable

I wanted to check something, but ran into troubles using HistogramDistribution in combination with ParallelTable. The code does the following: Compute a HistogramDistribution of some sample and use ...
1
vote
3answers
222 views

How to create functions of arbitrary number of variables?

In the following code what would be the simplest way to generalize it to say some $N_f$ number of $z$ instead of just $z_1$ and $z_2$? ...
11
votes
1answer
142 views

How to implement FittedModel like objects

In the course of making some RLink wrappers I want to have some richer containers like Mathematica does with its FittedModel ...
4
votes
4answers
232 views

How to compile the code for generate Pythagorean_triple?

I am finding Pythagorean_triple, it worked slowly. I tried to compile, but it gives some warnings. I also use "Case" or "Do" ,both of them failed.I'm sure my CCompiler has been set correctly. How can ...
6
votes
3answers
201 views

Filtering elements from a list

Suppose that I have a list {{{2, 1}, {4, 3}, {2, 4}}, {{2, 1}, {4, 3}, {3, 1}}, {{2, 1}, {2, 4}, {3, 1}}, {{4, 3}, {2, 4}, {1, 2}}} I want to make a new list ...
7
votes
3answers
297 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 ...
1
vote
2answers
412 views

How to define a differential operator?

I have a linear differential operator, for instance, $L\left (\partial _{t} \right )=\partial _{tt} - 3\partial _{t} + 2$. I use it in 2 different ways: apply the operator to a function: $L\left ...
19
votes
3answers
1k views

Mathematica as a normal programming language

I'm interested in Mathematica's core language for both practical development and as an object of computer science study. Actually, the former is more of a means to the latter. I would like to create ...
2
votes
1answer
113 views

Computing closest set of points to each point in large set — running out of memory (arrays unpacking)

I have a large set of points in 3D and I'm trying to identify all the points that lie within a certain distance of each point. Then using this data store the vector between the pairs of points. I ...
2
votes
1answer
131 views

Progress indicator with shared variables

I'm trying to create a custom progress indicator for a parallelized computation, using Dynamic and SetSharedVariables to display ...
4
votes
1answer
194 views

speed up iteration with conditionals plus optimize memory usage

Given list1 and list2 whose elements are vectors of a certain (fixed) dimension, I am interested in the behaviour of a scalar ...
16
votes
3answers
618 views

Reading periodic elements from a large file

I have a large binary data file (big endian) with 100+ million "rows" of 11 elements, combination of floats and integers. This is the format: ...

1 2 3 4 5 8