numba

jit – "Failed in nopython mode pipeline" error, despite not using nopython in numba

jit – "Failed in nopython mode pipeline" error, despite not using nopython in numba Question: I am using value function iteration to solve a complex dynamic programming problem with many states. I want to use numba/jit to speed up my code (and eventually parallelize the for loops). When I use the @jit decorator in my …

Total answers: 1

when can you use numpy arrays as dict values in numba?

when can you use numpy arrays as dict values in numba? Question: I am confused by the type rules for numba dicts. Here is an MWE that works: import numpy as np import numba as nb @nb.njit def foo(a, b, c): d = {} d[(1,2,3)] = a return d a = np.array([1, 2]) b = …

Total answers: 1

Fastest way to add matrices of different shapes in Python/Numba

Fastest way to add matrices of different shapes in Python/Numba Question: I want to "add" two matrices, a matrix a with shape (K,T) and a matrix b of shape (K,N), to result in a matrix of shape (K,T,N) The following works ok: import numpy as np from numba import njit @njit def add_matrices(a, b): K, …

Total answers: 2

Matplotlib with Numba to try and accelerate code

Matplotlib with Numba to try and accelerate code Question: How could I use numba for the following code to try and accelerate it? When I add @njit before func1 several error returns? Or would there be another way to optimise/accelerate the code to reduce the overall process time as depeding on the number of iterations …

Total answers: 1

Why do these two different ways to sum a 2d array have such different performance?

Why do these two different ways to sum a 2d array have such different performance? Question: Consider the following two ways of summing all the values in a 2d numpy array. import numpy as np from numba import njit a = np.random.rand(2, 5000) @njit(fastmath=True, cache=True) def sum_array_slow(arr): s = 0 for i in range(arr.shape[0]): for …

Total answers: 1

Apply slicing, conditionals to Sparse Arrays with Pallalization in Python

Apply slicing, conditionals to Sparse Arrays with Pallalization in Python Question: Apply slicing, conditionals to Sparse Arrays with Pallalization I want to do something like dynamic programming on sparse array. could you check the following example function,which I would like to implement for Sparse Array (the first example is for numpy.array) First,importing modules from numba …

Total answers: 1

How to speed up this big additive for loop in Python with Numba?

How to speed up this big additive for loop in Python with Numba? Question: I’m trying to speed up this function, which takes an array D of size (M,N,O), an array Pi of size (M,M) and arrays x_i, y_i, x_pi, y_pi of size (M,N,O) as inputs, and returns an array D_new of similar size. @njit …

Total answers: 1

Multiply a (N,N) matrix by a (N,M,O) matrix along the O dimension with Numba

Multiply a (N,N) matrix by a (N,M,O) matrix along the O dimension with Numba Question: I’m trying to multiply a matrix A of size $(N,N)$ by a matrix B of size $(N,M,O)$ matrix along the O dimension (that is, left-multiply all the "pages" of B along the O dimension by A), using a jitted numba …

Total answers: 2

Using Numba njit with np.array

Using Numba njit with np.array Question: I have two Python functions that I am trying to speed up with njit as they are impacting the performance of my program. Below is a MWE that reproduces the following error when we add the @njit(fastmath=True) decorator to f. Otherwise it works. I believe the error is because …

Total answers: 2

Numba @guvectorise returns garbage values

Numba @guvectorise returns garbage values Question: The code below is a test for calculating distance between points in a periodic system. import itertools import time import numpy as np import numba from numba import njit @njit(cache=True) def get_dr(i=np.array([]),j=np.array([]),cellsize=np.array([])): k=np.zeros(3,dtype=np.float64) for idx, _ in enumerate(cellsize): k[idx] = (j[idx]-i[idx])-cellsize[idx]*np.round((j[idx]-i[idx])/cellsize[idx]) return np.linalg.norm(k) @numba.guvectorize(["void(float64[:],float64[:],float64[:],float64)"], "(m),(m),(m)->()",nopython=True,cache=True) def get_dr_vec(i,j,cellsize,dr): dr=0.0 k=np.zeros(3,dtype=np.float64) …

Total answers: 1