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)

4
votes
1answer
102 views

Find random $n$ combinations of values with a given sum

This problem is described in the related StackOverflow question: Find all combinations of coins when given some dollar value. I would like generate a list of $n$ combinations of values that sum up to ...
15
votes
2answers
300 views

Which elements of a large array are part of a set?

I have a large integer packed array arr. My array is multidimensional, but for simplicity let's consider 1D arrays for now. ...
3
votes
2answers
175 views

Tracing 2d coordinates by using Nearest function

In 5 digital images I have determined the 2d coordinates of detected objects. The resulting coordinate list can be downloaded from here: http://pastebin.com/7bE0CEJ5 ...
6
votes
1answer
83 views

Faster square test for integers

This question was asked already in Jan '12 and the most recent answer is from Oct '12, so it's several Mathematica versions out of date. What is a faster test for whether an integer is a perfect ...
3
votes
3answers
165 views

Optimizing a series of basic matrix operations—scaling doesn't seem right with matrix size

I have a simple operation that I'd like to apply to an n×n matrix. My problem (well, one of them...) is that it runs in, say, 4 seconds for a 100×100 matrix, but it took five and a half minutes to run ...
1
vote
1answer
75 views

How to make this code involving Hypergeometric functions to run faster?

This question is followed up from this Question. I would like to thank Dr. Hintze and I_Mariusz for the comments and help. I am pretty new to mathematica ( I just learned it 4 days ago) so I would ...
3
votes
1answer
104 views

Generate a 3D grid data table

I would like to generate a 3D Grid, not for displaying, but to do computations on it, so no draw functions I guess. I have a simple version of what I am thinking, however, it is terribly slow: ...
0
votes
1answer
53 views

Timing and memory use is critical:fast partitioning of binary sparse array

I do experiments were I measure a signal, and I need to know at what time points it is above a threshold. If at a given time point, the signal is above the threshold, then a '1' gets put into the ...
7
votes
1answer
134 views

Numerically evaluating an integral related to Cantor's staircase

Cantor's staircase $F_C(x)$ is a well-known "pathological" function: Plot[CantorStaircase[x], {x, 0, 1}] The MathWorld link given above claims that $$\int_0^1 ...
6
votes
3answers
129 views

How to remove duplicate elements from two separated lists efficiently?

I have two lists of elements: indices = {Prime /@ Range[5000], Prime /@ Range[100, 300]} // Flatten; values = RandomReal[100, 3] & /@ Range[Length[indices]]; ...
3
votes
1answer
87 views

Fast evaluation of a function in many points

I need to feed to an external program a number of points (in Complex128 format) generated from the numerical evaluation of some function, e.g. $e^{i ...
3
votes
1answer
87 views

Extreme Slowdown with Fold

I am doing a finite difference model, and need to create a list recursively. For a small list, Fold is extremely fast, but when the list becomes large enough, Fold becomes extremely slow. Here is the ...
6
votes
1answer
114 views

List consisting of positions of the matrix elements and the elements themselves

Given a 2D matrix I would like to make a list with elements consisting of positions of the matrix elements and the elements themselves. Let the matrix be ...
3
votes
1answer
62 views

How to multiply a row to multiple rows in a matrix

I have the Following Matrixes; ...
5
votes
1answer
59 views

When is RegionMeasure[ImplicitRegion[…]] faster than (N)Integrate[Boole[…]]?

I wanted to use Mathematica to compute areas and volumes of various implicitly defined regions, so I ran a simple test case using the unit circle: ...
1
vote
1answer
100 views

Evaluation time is always 0.23 s [closed]

Following the instructions in this documentation page, I'm setting EvaluationCompletionAction -> "Show Timing" in the option inspector so that Mathematica prints ...
0
votes
0answers
64 views

Speed up the Eliminate

I have problem of eliminating some variables from a set of equations, which contain logarithmic function, in order to obtain a differential equation. These are the parameter and functions in ...
0
votes
1answer
27 views

Efficient calculation of leading order behavior of rational function under a limit?

Considering a rational function, as for example myExpr=(x+3)(2+5x+6x^2)/(x(x-5)(x+20)) I would like to have a function that tells me the leading order behavior ...
5
votes
1answer
115 views

Why are so many built-in numerical function doesn't check its argument is packed array or not? [closed]

This question is brought by this one. Outer[Times, RandomReal[1., 1000000], {1., 2.}]; is much slower than ...
6
votes
1answer
154 views

Strange performance of Outer

The following takes 0.07 seconds on my laptop. list1 = RandomReal[1.,1000000]; (Outer[Times, list1, RandomReal[1., 2]];) // AbsoluteTiming But this takes 1.2 ...
3
votes
2answers
75 views

union several arrays of same flag(date for example) efficiently

Suppose I have several arrays from data files which look like: arrayi = {{datei1, xi11, xi21, ...}, {datei2, xi12, xi22, ...}, ..., {datein, xi1n, xi2n ,...}} ...
0
votes
0answers
76 views

Nestlist inside Compile

My question is a continuation of question1 and question2, but I think it may be considered as a self-contained one. I am interested to make the following code faster ...
1
vote
1answer
58 views

Improving strategy to solve this constrained root-finding problem

I am looking for roots of the function eqs defined below. It has $2k$ unknowns and returns a vector of $2k-1$ components. The sole purpose of the following block ...
12
votes
1answer
278 views

Wiggly and imprecise animated gif output

Trying to make animated gif of zooming into famous Ford circles. With the code ...
7
votes
1answer
201 views

How to terminate early a functional computation

Problem description: Given a list $A=\{1,2,0,7,0,-1,2,6,\ldots\}$ and a list $B=\{0,7,0\}$ write a program that determines whether $B$ appears in $A$ or not. Note: the order of $B$'s items does ...
14
votes
5answers
537 views

A quicker than outer

I need to get a matrix $\{a(x_i-x_j)\}$, where $x_i$ form a partition of an interval, $a(x)$ is a given function. I use ...
4
votes
1answer
67 views

More efficient method to implement knots instertion algorithm

Knots instertion algorithm of B-spline About the definition of B-spline, please see here. Suppose $\overline u \in [u_{k},u_{k+1})$initially has multiplicity $s$, and suppose it is to be inserted ...
2
votes
0answers
86 views

Solving this system of nonlinear equations

Edit Interestingly, I realized solving my problem with $2n$ equations appeared to be more efficient than using mathematical results on the solutions to reduce the problem to $n$ equations (the $n$ ...
3
votes
1answer
135 views

default RGB coloring

I am playing with some basic genetic algorithms to recreate input images by transparent plane figures (similar like here:). Everything would work great but the speed is terrible. I was looking for a ...
4
votes
0answers
144 views

strange timing result of LinearAlgebra`BLAS` in mathematica? [closed]

BLAS is short for "Basic Linear Algebra Subprograms". It is a famous collection of routines for doing linear algebra. I just know from Oleksandr R. that mma can directly call BLAS under the context ...
5
votes
1answer
182 views

My introduction to Compile

I am reading Fractals from the Newton-Raphson method from Peter Young's page [http://young.physics.ucsc.edu/115/]{http://young.physics.ucsc.edu/115/}. I've tried: ...
0
votes
0answers
35 views

Multivariate remainder of polynomial in respect to a set of polynomials

I would like to have a really fast routine that computes the so called Normal Form of a multivariate polynomial f in respect to a set of other multivariate ...
1
vote
2answers
91 views

Finding matrix given null space

Suppose I input a very large $n$ integer vectors and I wish to compute the matrix whose nullspace is spanned uniquely by these vectors. Is there an appropriate command/module to do this using ...
0
votes
1answer
56 views

Minimization of linear combination of vectors

Suppose I have a vectors in $\mathbb{R}^{6}$. For example, take the vector $v = k(-2,2,4,6,2,2)$. I would like to find some $k$ with the constraint that the last two components are necessarily ...
2
votes
0answers
44 views

Performance of different Count patterns

The two pieces of code below are functionally equivalent, and I would have expected similar performance. (The only difference between the two is the form of the pattern used.) But the second piece of ...
9
votes
0answers
84 views

Efficient solution of huge sparse linear system

I'm trying to improve efficiency of my code in which main task is a solution to huge (~$10^4\times 10^4$) but sparse linear system $$Ax=b$$ (In fact my aim is to solve nonlinear equations $F(x)=0$, ...
5
votes
1answer
102 views

How to increase speed of iterations with nonlinear functions

Let me ask about part of a problem which I have in solving of a nonlinear DE (numerically). I am working with vectors of length $xN$. I have an initial vector $v$, a given $xN\times xN$ matrix $A$ and ...
17
votes
2answers
182 views

Speeding up generation of block diagonal matrix

I'm struggling with the following problem. I have $48$ square matrices (full, filled with real machine precision numbers, thus are packed, all different) of size $128$. I wolud like to place them on a ...
2
votes
1answer
73 views

Deleting isomorphic graphs efficiently

I'm trying to delete isomorphic graphs from a large list. My solution is as follows: DeleteIsoG1[gl_List]:= Module[{}, DeleteDuplicates[gl,IsomorphicGraphQ]]; ...
28
votes
1answer
753 views

Terrifying performance decrease for contour/density plots in v10.1 -> 10.3

Bug introduced in 10.3 and fixed in 10.3.1 I setup for download a test dataset at http://www.inrim.it/~magni/cm.dat.gz (a set of 64800 triplets x,y,z). The behavior can be reproduced with the ...
6
votes
1answer
88 views

Count degrees of freedom of a polynomial

I want to count the independent degrees of freedom of a polynomial in three variables $(z_0,z_1,z_2)$. Therefore I take the coefficients of $f$ and compute the rank Jacobian with respect to the ...
2
votes
1answer
99 views

Manipulate gets stuck when ConvexHullMesh is made transparent

Version 10.3 The followinng Manipulate is a bit slow, but quite OK ...
1
vote
1answer
157 views

precision and speed inefficiency issues of Nearest function with sqrt expression [closed]

Recently, Nearest has been attacked for many bugs. Some are already fixed. I am going to show some issues when Nearest applied ...
4
votes
2answers
123 views

How to be more efficient/faster with this MatrixExp-vector multiplication?

I am looking for expertise that helps to improve speed of the code below. First, a little bit of background: There is some system of differential equations $\dot{\vec{m}}(t)=S(t)\vec{m}(t)$. In order ...
5
votes
2answers
314 views

Smallest element distance between two lists

I have a problem, where I generate two lists of points, and I want to see which points are the closest. If I just want to find which ones are exactly the same, I can use ...
2
votes
0answers
65 views

MySQL insert statement over remote server is extremely slow. How can I speed it up?

I need to insert values to a remote MySQL server. However it is extremely slow compared to a local MySQL server. This is my timing for the local MySQL server ...
3
votes
2answers
145 views

Efficient and fastest way to execute 4 nested loops

I have a question regarding the use of ParallelDo. I tried to parallelize the following code involving four nested Do loops by ...
6
votes
2answers
202 views

Speeding up code for lattice generation

Below is a lattice point generation code I write ...
6
votes
1answer
87 views

Expensive MeshRefinementFunction

I want to discretize a region using DiscretizeRegion. Now, I want the mesh to be more refined, when the curvature of a certain function ...
5
votes
1answer
61 views

Can not understand this timing result

I set a=1. tmptmp = Tuples[Range /@ {1000, 100}].{{3 a, 0}, {0, Sqrt[3] a}} And then run ...