Questions tagged [compile]
Questions about the generation and use of compiled functions using Compile.
635
questions
0
votes
0
answers
96
views
Numerical errors in compiled function involving real and complex numbers as output
I am trying to use the Compile command in Mathematica to reduce the computation time because of its fast execution within Mathematica and ability to use the ...
5
votes
1
answer
288
views
Why does NIntegrate become much faster if I evaluate a function, then compile it, then make it numerical?
I am using Mathematica for numerical integrals in optics. By accident, I found out that NIntegrate becomes faster by an order of magnitude for a function I'm integrating, if I take the following steps:...
1
vote
0
answers
43
views
Problem with the return type of Compile [closed]
Can I return anything other than a real number with a compiled function ?
In the example below, I use Compile on a toy function to illustrate my problem, the real ...
3
votes
2
answers
309
views
Using compile to speed up evaluation of a While loop
I want to calculate a loop. For speed, I compiled my code. Even after the compilation, it is still very slow in getting the results.
Are there any further improvements that can be made to my code?
<...
2
votes
0
answers
116
views
Repeated calling of a ParametricNDSolve result in a faster way?
I recently learned of ParametricNDSolve and found that I need to compute it once and then call its result sol at different ...
1
vote
1
answer
220
views
How to speed up by Compile?
I need to NDSolve a system many times by scanning some of its parameters and do some matrix calculation with the (discretized) solutions. The example is the ...
1
vote
2
answers
61
views
Compile function with only some variables [closed]
I made the simple example below for explaining the problem. How can I compile only some variables?
For example, in the example only a and ...
6
votes
1
answer
85
views
Compile - Optimization of function calls - does it matter?
I was surprised to see that, in the results of CompilePrint for a function made with Compile, calls to ...
2
votes
1
answer
61
views
Downvalues for compiled functions
I'm a noob when it comes to DownValues, UpValues, OwnValues, etc. but I figured I would give ...
4
votes
2
answers
76
views
Functions within modules within Compile
I've noticed that if I try to define a Function within a Module within Compile, I get errors ...
5
votes
1
answer
171
views
'Block' with 'Compile' gives an error
Here is a minimal example to the problem I face when using Compile with Block:
...
23
votes
4
answers
3k
views
What does Compile[] do to make code run so much faster?
I am looking for an ELI5 explanation of what Compile[] in the Wolfram Language context does and why it works so much faster than uncompiled code in certain cases. I am trying to get more insight to ...
4
votes
1
answer
127
views
What exactly prevents this code from compiling?
I am trying to compile a function that operates on an initial array (doing some sequential operation) to produce a matrix (which consists of N iterations of the initial array).
Below is a minimal ...
7
votes
2
answers
133
views
Can I improve performance of compiled deleteDuplicates?
I'm working on a project to compute pairs of complex numbers and need to delete the duplicates (within some tolerance, 10^-5 below). I was wondering if the performance of my compiled version can be ...
2
votes
1
answer
118
views
k-subsets of several sets?
Problem: Let A and B be lists of lists of integers. I wish to find every k-element set, ...
1
vote
1
answer
51
views
How to collect results from compiled function in ParallelEvaluate?
I am using Mathematica 12.x and have a compiled function, returning many things. I would like to run several instances of this compiled function, one for each core of my CPU, and then collect all of ...
1
vote
1
answer
151
views
How to inline a variable in function definition
I need to compile a function g that calls an external function f. f and ...
5
votes
2
answers
90
views
Are arbitrary expressions allowed as input arguments in Compile?
I made a typo when entering code for Compile. I thought it should have resulted in an error when the code was compiled, but it didn't. It did give an error when ...
2
votes
2
answers
267
views
Speed improvement for root finding
I'm wondering whether it is possible to speed up the following code that I'm using to find points on the intersection between a line and the zero locus of an equation.
The idea is the following. First,...
1
vote
0
answers
76
views
Create Compiled Version of Function
I am trying to speed up the following function by compilation:
...
5
votes
0
answers
93
views
Effectivelly using Compile for calculate a Unitary transformation
I am new to Mathematica, and this is my first post, so if my question is not clear enough, I would be glad to read the comments and edit my question to add more information.
The problem
I need to ...
2
votes
0
answers
104
views
LTemplate and external library Armadillo
As a great fan of the simplicity of the LTemplate library for the end user I am now faced with a problem when apparently everything compiles correctly but the functions are not loaded from the library....
0
votes
1
answer
66
views
Compiling RiccatiSolve LQRegulator gains
Given some specific matricies, numA, numB, numP, q and r to calculate LQR gains, one can use simply LQRegulatorGains or solve the same via ...
2
votes
3
answers
89
views
Strange Behavior of Compile[] inside With[]
Consider, in the examples below, the difference between defining $n$ as a global variable or as an argument to function comp[], which includes a compiled function ...
6
votes
1
answer
200
views
Mathematica does not recognize c compiler
I have Mathematica 11.1 and Windows 10 OS. I need to use c based compilation inside Mathematica, like ...
3
votes
1
answer
117
views
Compiled Function is slower than non-compiled version?
I need to maximize a function numerically, thus the speed of this function is very important. Here is the most time consuming part of this function.
...
1
vote
1
answer
39
views
How to have a common input for different nb file
I have a set of five files. Each file takes a common input. But I need to change the input and run the files. The output of some file are used in other files. So how to do this. And also is there any ...
1
vote
1
answer
86
views
Error on using Compile[]
My code is below.
...
2
votes
0
answers
122
views
Convert huge (10-100MB of plain text) Mathematica function for external use
I have a huge Mathematica function (plain text ranging 10-100MB) that I am trying to convert to Fortran for external use. I tried using FortranForm, which does most of the work, and cleaned the code ...
3
votes
1
answer
103
views
How to do meta-programming inside Compile?
I'd like to do this: solve some linear systems, compiling the task of getting the building up the matrices from parameter values, and then I have a compiled implementation of LinearSolve. The problem ...
1
vote
2
answers
68
views
How to compile a function that takes a list of arguments using InlineExternalDefinitions?
I can do this right now, but I'd love to make the argument a list. (I have a compiled implementation of Linear Solve from here https://mathematica.stackexchange.com/a/91881/73342).
...
3
votes
1
answer
144
views
How to use a list inside Compile—it says it should be a machine-size real number?
Super simple question, but I'm lost here.
f = Compile[{{a, _Real}, {b, _Real}, {c, _Real}},
x /. NSolve[a x^2 + b x + c == 0, {x}]]
f[1., -4., -5.]
gives the ...
10
votes
1
answer
697
views
Writing compiled functions as fast as Python's Numba
I want to write some code to simulate a damped oscillator that is just as fast as code written using Numba's @njit decorator. I've written the mathematica code and ...
0
votes
0
answers
52
views
Basic question of "good" habits during the evaluation of a notebook in mathematica
Suppose that I have notebook, call it Nb1. And I want to evaluate the notebook Nb1, next suppose that the process is slow, but I have to evaluate other notebooks, suppose Nb2, and Nb3. I see that ...
1
vote
0
answers
65
views
Optimal code generation for evaluation of a fixed set of multivariate rational polynomials
Given a set of multivariate rational polynomials, is it possible to find the optimal (shortest or least expensive in some other sense) sequence of arithmetical operations, such that it results in ...
16
votes
1
answer
235
views
How to use CompilerOptions?
The refpage for FunctionCompile's option CompilerOptions is empty of examples:
Does anyone know what keys and values can be ...
2
votes
1
answer
67
views
why this simple code is not giving the right answer under Compile
I have two sets of inputs that are to be fed to a simple function. The inputs are essentially a set of triangles and the normal for each triangle. The function will compute the gradient about the ...
4
votes
1
answer
188
views
Add Compile function in a code to speed up the execution
I have a code
...
3
votes
1
answer
73
views
Compile a function with undetermined number of variables
I have been wrapping my head about a question it may look simple to you. I want compile a function that depends on a number of undetermined variables as,
...
3
votes
1
answer
57
views
How to handle numerical exception within a compiled function
I'm trying to evaluate a series of expressions, which, given a set of parameters, may be complex. Instead of throwing a CompiledFunction::cfn, I'm trying to catch ...
1
vote
1
answer
59
views
Issue with Compile for Complex Input/Output
I'm writing a Runge-Kutta algorithm for solving a system of coupled differential equations. My code works fine when defined as a module, but when I try to compile it, I get the following error:
...
1
vote
0
answers
91
views
Faster methods for performing repeated list manipulations?
I've done my best to pore over the other posts on this site, and to the best of my ability I can't seem to find a good solution to my problem, so I apologize in advance if elements of my question are ...
6
votes
1
answer
139
views
Compiling with Listable option and Parallelization
I have a bunch of triangles (200k) I need to calculate the normals of. Here are five of them below, each one with its vertices in CCW order.
...
7
votes
1
answer
298
views
Table vs. ConstantArray efficiency with compilation
I've been trying to optimize a bit of code the past couple of days, but think I might be misunderstanding how compilation on certain target functions works. As a most barebones example, I've attached ...
16
votes
2
answers
499
views
How to use DataStructure in FunctionCompile?
The official document said
compile a function that can work with data structures
but how to use it?
There is no doubt that it is possible to use DataStructure as a function parameter:
...
5
votes
1
answer
224
views
Compiling a function depending on an external function
Consider the simple case:
g[x] := x^3;
f[x] := x^2 + g[x];
cf = Compile[{{x, _Real}}, x^2 + g[x]]
Evaluating the two functions, Mathematica complains that the ...
3
votes
1
answer
176
views
How to use IntegerQ in FunctionCompile?
Seems like an easy request, but I've somehow gotten myself stuck again:
...
11
votes
0
answers
218
views
How to use Bags in FunctionCompile?
I was reading this question and it wanted to know how to FunctionCompile this:
...
6
votes
5
answers
285
views
Maximizing efficiency of a power-tower (tetration) calculation
I'm working on a power tower $\displaystyle z^{z^{.^{.^{.}}}}$ and wish to maximize the efficiency of my algorithm to compute a cycle map. This is a color-coded diagram of the cycle period of each ...
2
votes
1
answer
86
views
Compilation: How to get rid of MainEvaluate[Hold[myFunction][arg1, arg2, arg3]]
I am trying the following code:
...