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 (2)

0
votes
1answer
108 views

Simulation code performance tuning

The following simulation takes over 20 seconds for evaluation. I have difficulty re-coding it with Nest and Map or Compile functions to make it more efficient. Can some one help with this? The ...
1
vote
0answers
35 views

Linear interpolation over 2 variables

I'm tuning a function to data over 4-5 variables using Differential Evolution. The objective function calls frequently interpolation function. I came to a point, where compiling the objective function ...
9
votes
1answer
177 views

Mathematica9: NDSolve slows down after repeated calls

I have noted that in Mathematica 9 my code, which involves a lot of calls to NDSolve, slows down considerably after some time. Apparently, the problem is NDSolve itself and it seems to be related to ...
8
votes
1answer
164 views

How to create internally optimized expression for computing with high WorkingPrecision?

I have large dataset and need to fit rather complicated function on it with different values of one of its parameters (this parameter must be fixed in every fit). I use the ...
3
votes
0answers
90 views

What can I do to improve the performance of my calculations?

I'm trying to express a simple probability problem in Mathematica, but am having trouble getting my calculations to execute at a reasonable speed. I have an object whose unknown location is modeled ...
8
votes
3answers
363 views

What is the most efficient way to map a linked list?

In order to work with lists effectively using common recursive patterns from functional programming, it's often most convenient to worked with the "linked list" representation, that looks like this: ...
24
votes
4answers
1k views

How to efficiently find positions of duplicates?

Is there an efficient way to find the positions of the duplicates in a list? I would like the positions grouped according to duplicated elements. For instance, given ...
6
votes
4answers
196 views

Efficient way of identifying the indices of first occurrences

It seems to me like identifying the first occurrence of unique elements should take about the same amount of time as it takes to delete duplicates. My data sets are much larger than this (...
4
votes
1answer
131 views

Bresenham's line algorithm

Bresenham's line algorithm is producing discretized line for given two points for purpose of plotting for example. Like that: I have to stress that I'm interested in positions, not a plot. ...
1
vote
2answers
287 views

Research positions of lists of varying size

I'm new to the site and I am French, so I ask your indulgence for my future mistakes in English. I would like to optimise the following code : ...
4
votes
4answers
245 views

Distance between two line segments in 3-space

I need to compute the distance between two line segments in a project. After googling, I found this algorithm and used it to implement a Mathematica version: ...
58
votes
2answers
592 views

Why are numeric division and subtraction not handled better in Mathematica?

There is something that has been troubling me for a while. At least in version 7, the performance of a / b and a - b is not ...
2
votes
0answers
46 views
4
votes
3answers
141 views

How to compile a diagonal array efficiently?

For example, if we use some functions defined prior to Compile, we usually have the main evaluators in the compiled codes, pointing to the definition of the ...
10
votes
1answer
151 views

How to work with Experimental`NumericalFunction?

This question is intimately connected with previous one: "How to create internally optimized expression for computing with high WorkingPrecision?" Oleksandr R. correctly states in the comment: A ...
2
votes
1answer
74 views

Performance on Binomial Deviate is slow

I hope I won't get spanked for reposting. This is related to a previous question discussed here: Performance on Multinomial Deviate is slow. but this version is simpler because it is a binomial ...
0
votes
0answers
62 views

Optimization of vector manipulation

I'm trying to implement an upwind scheme for solving transport equation (at first, I work with no right hand side and the simpliest case possible - velocity is constant, initial data is smooth, etc). ...
5
votes
1answer
101 views

How to speed up this TDSE code?

I'm trying to build a 1D time-dependent Schrodinger equation solver, using the Crank-Nicholson method. And I think the code is kind of working now, but the speed is still slow for me. Here is the ...
1
vote
0answers
44 views

Speed up MinimalPolynomial

My Mathematica code runs slowly MinimalPolynomial[Sqrt[2] + Sqrt[3]+ Sqrt[5]+ Sqrt[7]+ Sqrt[11]+ Sqrt[13], x] runs slowly, but the Maple version ...
3
votes
2answers
165 views

DynamicModule/Dynamic CheckboxBar Slow for Large Lists

I am trying to create a dynamic construct that takes a large list of expressions and allows the user to interactively select any number of entries for further manipulation. This is what I have ...
2
votes
0answers
45 views

LibraryLink parallel performance

I'm trying to use a LibraryLink function in parallel. It is an embarrassingly parallel problem, so that no dependency between those parallel tasks. However I can only get the parallel efficiency about ...
4
votes
2answers
101 views

Values (or positions) of array row elements within a specified number of positions from target value

I have the need to collect values from the rows of rectangular arrays of arbitrary precision integers, given a target element value and allowed "distance" (positions +/- within the row from found ...
3
votes
0answers
97 views

“Off-label use” of Mathematica functions for performance

Assisting an acquaintance with a simulation where a very large number of binomial probability calculations are required, I pointed them to a slightly obscure function to dramatically speed up their ...
11
votes
5answers
1k views

What is the fastest way to extract a substring from a huge string?

I develop an algorithm to find the most common substrings of a given length n up to a certain distance l in front of a pattern p in a huge sequence S. E.g. I want to find the most common hexamers ...
6
votes
3answers
161 views

Enumerate the 1-factors (perfect matchings) of $K_n$

Introduction I would like to enumerate the 1-factors, or (near-)perfect matchings, of the complete graph Kn. The adjacency list representation for Kn is basically { (x, y) | 1 ≤ x < y ≤ n }. For ...
-2
votes
2answers
138 views

How to write efficient code? How to make this example run faster? [closed]

I would like to learn writting more efficiently. I am working on problem of simulating random walks (abnormal diffusion). I believe this is good example of inefficient code. How can I make it faster? ...
1
vote
2answers
76 views

Extract the real components from a very large expression

Suppose I have a very long expression (>500,000 characters) which contains a mixture of real and imaginary terms. e.g ...
1
vote
1answer
114 views

Code Performance Tuning

The following code takes over 4 seconds to run. Any one has suggestions to make the expressions for qf, phi, ...
7
votes
3answers
360 views

Suppress connection to Wolfram|Alpha when making unit conversions

I want to speed up calculations with unit conversions. I suspect primarily that some units evaluate from Wolfram|Alpha. Any way to prevent this automatically, so that Mathematica throws ...
9
votes
5answers
407 views

Fast way of finding position of Interval containing an Integer

I have a large list (about 2.2 million) of pairs of integers. Each pair defines an Interval[{}]. Given some integer x, I need to ...
0
votes
0answers
41 views

Approximate to the speed of Build-In functions

In Mathematica, there is a function PowerMod[a,b,m], which computes a^b mod m. I will concern only this case: ...
10
votes
1answer
161 views

Drop selection of columns from a ragged array

I have an application where I need to drop some arbitrary list of columns from a ragged array (where of course the shortest array rows have at least all the specified columns). E.g., given a ragged ...
9
votes
8answers
311 views

Fast way to get positions of “boxed” array elements?

Given a 2D array (arbitrary numeric / evaluates to numeric elements, simplified for examples, and can be arbitrary dimensions so long as the operation makes sense, I'm working with 2K X 2K in my ...
0
votes
0answers
44 views

Addition of sparse array objects [duplicate]

I have been having some trouble with the addition of large (but very sparse) matrices using SparseArray. Here is the simplest example to illustrate the issue. First, I will define diagonal matrices ...
0
votes
0answers
28 views

trying to speed up sequential use of NDSolve for large system of ODEs

I don't have a basic example just yet but I'll describe the problem for now. I have a large system (>100) of coupled first order ODEs. Where the output from one solution becomes the ICs for the next ...
4
votes
1answer
131 views

Selecting expressions from a sequence that are not subexpressions of earlier elements

I have a While loop that generates and prints an expression on each iteration (there can be thousands of iterations). UPDATE: Some expressions are generated in ...
5
votes
2answers
106 views

Improving performance in the generation of random (directed acyclic) graphs

I need to generate large (directed, acyclic, transitive) graphs. My code in Mathematica gets very slow for large numbers of vertices. I've tried a number of approaches and I can't find any significant ...
38
votes
2answers
2k views

Internal`Bag inside Compile

Since Internal`Bag, Internal`StuffBag and Internal`BagPart can be compiled down, it is a ...
2
votes
3answers
178 views

Re-arranging / selecting of a data table very slow in For loop

My task is quite simple; I have a table of data like M = {{1,2}, {1,3}, {3,4}, {5,1}, {6,9}, {6,8}, {6,1}}; so the table is sorted with respect to the first ...
4
votes
1answer
81 views

Why does a heavy background slow LocatorPane from updating, even if only the locators are Dynamic?

I'm having some trouble getting a dynamic plot to operate efficiently, and I think I've tracked down at least part of the problem to some pretty weird behaviour on the part of ...
7
votes
5answers
1k views

Something faster than Do loops with AppendTo

I am an "old" programmer used to Fortran and Pascal. I can't get rid of For, Do and While ...
9
votes
2answers
211 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
0answers
41 views

Using Table[ ] vs using Do[Append [ ] ] [duplicate]

Is it generally better/faster to use the Table function over using Do[Append []]? For example, I have this piece of code: ...
0
votes
1answer
56 views

Why does initialization slow down manipulate in this case?

I'm trying to manipulate a plot of function with some parameters. The function is a long expression, so I tried to put it inside the Initialization options of ...
7
votes
5answers
310 views

LengthWhile until first duplication

I have the need to trim each of a large number of potentially very long lists (10^6+ elements) to the length where the last occurrence of any distinct element happens, or said another way, where the ...
4
votes
1answer
171 views

How to choose MaxStepFraction for optimal speed of NDSolve

I'm trying to use NDSolve to solve a 1D Schrodinger's equation, and it seems that MaxStepFraction has huge effect on the ...
6
votes
1answer
93 views

Better way to get Fisher Exact?

I want to perform a Pearson's $\chi^2$ test to analyse contingency tables; but because I have small numbers, it is recommended to perform instead what is called a Fisher's Exact Test. This requires ...
40
votes
4answers
2k views

Speeding up this fractal-generating code

I used the code below (which is a sample from this gist containing more similar code) in my answer to my own question about Mandelbrot-like sets for functions other than the simple quadratic on ...
5
votes
1answer
560 views

Hardware Performance Metrics for Mathematica

Dear Mathematica users I am looking into buying a new computer and Mathematica is one of the main applications I run. To help me assess the overwhelming welter of choice out there: I'd like to know ...
11
votes
3answers
435 views

Every second evaluation in notebook front end is slow

I'm having a rather baffling problem that Wolfram was unable to help me diagnose, and I'm hoping one of you geniuses in residence here can figure it out. (I don't get access to the Technical Support ...