Questions on optimizing Mathematica code for higher performance. This may mean faster execution, lower memory usage or both. Not to be confused with mathematical optimization.

learn more… | top users | synonyms (4)

9
votes
2answers
218 views

Additive SparseArray Assembly

The goal is to assemble a SparseArray in an additive fashion. Let us assume we have a large List of indices (some will be ...
1
vote
1answer
93 views

Plotting a sum as a function of its upper bound

Apologies if this is easy to find in the documentation, but is there a quick way of doing the following up to any given 'n'? ...
5
votes
2answers
176 views

Reading non-tabular data with NaN-s from file

A NaN stands for an entry that is not a number/undefined/missing/infinite/etc. On my machine the C code printf("%f", 0/0.) produces ...
4
votes
1answer
183 views

Calculate the covariance of a large matrix

I have to compute the covariance of 50 very large integer matrices (2500x2000 elements). However, according to my estimation this will take around 10 days. Do you have any ideas how to speed things ...
2
votes
2answers
125 views

Definite Integral. Perfomance problem

I have a notebook in Mathematica 4. I'm trying to convert it to use in Mathematica 9. One of the problem is the long computation of definite integral. In Mathematica 4 the result of the following code ...
2
votes
3answers
156 views

Efficient matrix multiplication

I have a list of vectors vecs = {vec1, vec2, ..., vecN} where veci is a list with length $N$. Now I have a matrix $N\times N$ ...
3
votes
2answers
215 views

My code for solving n queens problem Compile Failed

I want to make my code for solving n-queens problem faster, but it seems that MapIndexed can't be compiled. Mathematica return CompiledFunction::cfse: Compiled ...
2
votes
1answer
118 views

Putting values into a grid after iteration process

I'm developing a model of thermodynamic cycle in Mathematica. I wrote several functions, applied them to a list containing starting values and iterated, for the model to converge. ...
8
votes
4answers
372 views

Slow plotting of trigonometric functions

I have two sets of solutions for a and b. I'm plotting solution only if it is positive for both ...
3
votes
1answer
223 views

How could I improve this Central Limit Theorem demonstration speed- and content-wise?

My purpose is to demonstrate the Central Limit theorem by assuming two custom distributions, taking some random samples, calculating their means and ploting the results. Ideally, the histograms should ...
6
votes
0answers
112 views

Slow NIntegrate over complicated domains (sometimes)

This is no longer a problem with Mathematica 10.* where I can use regions to do the integrations Overview I have a very large matrix I need to calculate where each element requires the integration ...
4
votes
5answers
267 views

Can I generate a “piecewise” list from a list in a fast and elegant way?

Suppose we have a number list a: n = 1000000; a = RandomReal[{0, 1}, {n}]; We want to generate a list ...
2
votes
0answers
138 views
5
votes
2answers
195 views

how to improve efficiency when I want to classify a list according to certain laws

I have a function that generates lists: f[n_] := Join @@ Permutations /@ PadLeft@IntegerPartitions[n]; For example f[3] ...
2
votes
1answer
582 views

Simulated Annealing InitialPoints option

I have a cost function that depends on 4 parameters: CostFunction[{tx,ty,tz,ox}]. I used NMinimize[] function with ...
6
votes
1answer
309 views

Efficiently searching a list for the position of the closest number to a specified number

Searching by closest and position I wasn't able to find an answer—but found Nearest by guessing. Nearest, however, returns ...
4
votes
1answer
249 views

MapThread Alternatives

I want to add two matrices, the first one containing a 2D vector at each position the other one a list of 2D vectors. The 2D vector from the first matrix should be added to each of the 2D vectors at ...
4
votes
1answer
173 views

MapThread and Compile

After discovering that MapIndexed cannot be used with compile (see MapIndexed and Compile) I am now trying do implement similar functionality using a combination of ...
8
votes
1answer
597 views

Imposing a Periodic Boundary Condition in Nearest Neighbour Search

I am trying to find first nearest neighbour distributions of randomly dispersed point-like objects in an infinite system. To do this I make a finite sized box unit cell with a chosen concentration of ...
5
votes
0answers
86 views

Is there something like MaxProcessorUsed?

For standard procedures it is easy to test if the implementation is better or worse than other by using Timing etc. It does not help us with creating ...
0
votes
0answers
144 views
3
votes
0answers
358 views

Taking real and imaginary parts of indexed functions and speeding up ComplexExpand

I am setting up a large system of ODEs and in order to use the IDA method (which is sig. faster for my system and thus attractive), I must split my equations into real and imaginary parts. I am ...
5
votes
1answer
137 views
10
votes
2answers
438 views

Why is ParallelMap way slower than Map

I recently tried to use ParallelMap instead of Map and to my surprise encountered that ...
0
votes
1answer
154 views

Better way to evaluate integral containing Boolean function

I am trying to compute the integral ...
0
votes
1answer
225 views

How to combine two usages of # into one and speed up the code?

I have a function num with two variables, matf and matg. How can I combine the last two lines of code below into one? The code is aimed to find out the maxiaml count bel when we give each $i3 = ...
4
votes
3answers
423 views

Improve the performance of solutions to Project Euler (#14)

Problem I have following code, when n<=10^5 it's not slow, but n>2*10^5 it's became very slow. I think maybe some temp value greater than 2^31-1, so make compile invalid. Could you give any idea ...
5
votes
2answers
179 views

How to optimize computing a product?

Problem description Write a function to caculate the formula My solutions Solution 1: ...
6
votes
1answer
903 views

Fastest way to do vector / matrix multiplication with constant matrix

Suppose we have a fixed n x n matrix, call it $B$. What is fastest way to evaluate $v^t B\;v$ over many different vectors $v$? Since $B$ is a constant matrix, does this allow the number of operations ...
0
votes
1answer
141 views

Efficiency problem with least square algorithm (Error in both variables)

I was informing myself about the least square algorithms with errors in x and y. I found this post and the top answer wasn't ...
5
votes
1answer
263 views

How to speed up multi-curve plotting?

The following example is a very simplified version of the problem I'm working with, though I hope it still captures the essentials. First, the definitions: ...
3
votes
1answer
163 views

Any way to speed up evaluation in an Animate?

I have a self-defined function which is fairly complicated and gives the analytical form. The result contains two independent variables. I use Animate to plot the ...
25
votes
5answers
652 views

Compiling ColorFunction for faster complex phase-amplitude plots

I have gotten into the habit of plotting complex-valued functions on a plane using a color-function along the lines of ...
7
votes
1answer
110 views

Why does Rescale performance vary significantly based on the real number values in a list?

I have a list of the form: testList = Table[{Flatten[RandomReal[{-200, 200}, {1, 2}], 1], RandomReal[{-200, 200}, {69, 2}]}, {i, 1, 100}]; And I would like to ...
0
votes
1answer
165 views

johansen test error in code

Cross-posted at the Wolfram community forum I have found this code on a forum, apparently it worked in a previous version of mathematica. Now when I run it, it keeps running for ages. I am not able ...
1
vote
1answer
86 views

Insert glyphs into a string using functional code

I asked this question few days ago: Replace For-loop with functional code Suppose I use strings instead of lists; for example a = ToString /@ {123123, 112233};` ...
5
votes
2answers
211 views

Parallelizing or optimizing a script for calculating the size of all pairwise intersections between collections of sets

I need to speed up or parallelize an operation that looks essentially like this: ...
4
votes
2answers
380 views

Quickly reducing the number of decimal digits for a set of real numbers

How can I quickly convert a number with $n$ decimal points to a number of with $m$ decimal points? Round works, however, it is slower than I would like. This ...
2
votes
2answers
224 views

Reducing quality of Graphics3D scene to improve performance?

ProteinData["SERPINA1", "MoleculePlot"] gives the detailed Graphics3D plot below. I'd like to make many small copies of this ...
6
votes
1answer
144 views

Why LinearModelFit is slower than NonlinearModelFit?

Why LinearModelFit is slower than NonlinearModelFit? ...
8
votes
2answers
223 views

How to reduce the number of operations needed to evaluate an expression? [duplicate]

For some numerical calculations in C++ I have, for example, this complicated expression to evaluate. Given as Mathematica input, in its original form it is ...
4
votes
3answers
412 views

SparseArray row operations

Given a large (very) sparse matrix, A, how can I efficiently "operate" on only the nonzeros in a given row? For example: For each row in A, generate a list of column indices that have a magnitude ...
2
votes
1answer
259 views

Manipulate executes expressions multiple times

I am using Manipulate for a rather intricate front end for data visualization. I was getting very sluggish performance and was able to boil the problem down to a ...
5
votes
2answers
466 views

Faster Eigenvalues with lower precision goal

I compute all eigenvalues of a large matrix, and I decide that the speed is more important than the precision. Then the question is, can I speed up Eigenvalues[] by ...
5
votes
1answer
324 views

How to speed up calculations with large number of replacement rules?

In my program I have an expression which looks like: ...
5
votes
1answer
281 views

How to avoid unpacking from Language`ExtendedFullDefinition

Consider this code: LaunchKernels[]; On["Packing"] a = RandomReal[{0., 1.}, {64, 30000}]; ParallelMap[Fourier, a]; // AbsoluteTiming Developer...
9
votes
1answer
294 views

Why I only get 2X speedup when parallelized with 16 kernels

I'm trying to parallelize a function by ParallelTable, and it seems the speedup is not very good. I have 16 parallel kernels on my computer node: ...
5
votes
2answers
218 views

How can I speed up my compiled RBF interpolating function?

Overview I have a function that acts like the Interpolation on sparse $n$-dimensional data using a simple implementation of RBF interpolation method. I want my ...
3
votes
1answer
186 views

Optimization of power tower fractal generator [closed]

I tried to optimize the code for generating power tower fractals from here. As the author suggested, I tried to memorize the points already tested in a list. Here is my code : ...
17
votes
3answers
1k views

How can I efficiently “remap” an image?

Many image processing libraries like OpenCV, Intel Performance Primitives or Octave have a useful function called "remap", that takes an image, an array with X coordinates and an array with Y ...