Questions tagged [numpy]

NumPy is the fundamental package for scientific computing with Python.

Filter by
Sorted by
Tagged with
0
votes
1answer
28 views

Odeint error for nonlineal differential equations

I receive the following error when I run the code. ODEintWarning: Excess work done on this call (perhaps wrong Dfun type). Run with full_output = 1 to get quantitative information. warnings.warn(...
0
votes
0answers
47 views

How to take convolution of two arrays in Python by using NumPy?

Generally, we know that if we have this relation between Fourier transforms of three functions in frequency domain as: $$\mathfrak{F}\{\mathsf{P}(t)\} = \mathfrak{F}\{\mathsf{Z}(t)\}\mathfrak{F}\{\...
2
votes
0answers
56 views

Determine the anti-derivative of 2D vector field

I have data on a (x,y) grid with scalar values (time) at each point. I have used the numpy gradient function and linear interpolation to determine the gradient vector Velocity (Vx,Vy) at each point (...
3
votes
1answer
59 views

Fast nonzero indices per row/column for (sparse) 2D numpy array

I am looking for the fastest way to obtain a list of the nonzero indices of a 2D array per row and per column. The following is a working piece of code: ...
0
votes
0answers
22 views

How to plot a function for multiple values of a parameter in the same set of axes in Python?

I'm currently trying to plot a graph wich describes a photoionization cross section as a function of incident photon energy for optical transition in a semiconductor for different values of the $\...
1
vote
1answer
100 views

Composite matrices in Numpy

Lets say I have four matrices A, B, C and D, and I want to combine them together into one new matrix for computation: $$ \left( \begin{matrix} A & B\\ C & D \end{matrix}\right) $$ How can I ...
2
votes
1answer
49 views

Best way to convert a sparse (containing zeros) covariance matrix into a correlation matrix?

I have a $100$x$100$ covariance matrix that looks like this. Some rows/cols are all-zero because those corresponding elements are not present in the sample from which covariance is calculated. I'm ...
1
vote
1answer
60 views

How do I find the portion of a cell/voxel lying within a defined surface?

We have a 3-dimensional grid of voxels (or cells), with individual voxels being of volume $dx\,dy\,dz$ where $dx=dy=dz=1$. A cone-like surface is defined by some function, $z = f(x, y)$, which in ...
-1
votes
1answer
41 views

Numpys `tensordot` and what is happening mathematically

I've encountered a program where np.tensordot was used, so I tried looking it up but I can't really understand what this function is doing... I feel rather ...
0
votes
0answers
47 views

Why am I not getting the flat phase when Fourier-transform a Fourier-limited Gaussian pulse?

I have been trying to obtain a spectrum and a spectral phase of a Gaussian pulse using the Fast Fourier Transform provided with numpy library in Python. Here are ...
4
votes
2answers
208 views

Fastest Way to Mutiply $10^4$ 2x2 Matrices

In a code that I work with (written in python, but also tagging as matlab because numpy is so close and I could use it if need be), we use a transfer matrix method to compute the properties of a ...
5
votes
1answer
87 views

How do I globally change the precision of a piece of code in Python to debug it?

I am solving a system of non-linear equations using the Newton-Raphson method in Python. This involves using the solve(Ax,b) function (...
2
votes
1answer
132 views

Numerical stability in the product of many matrices

I have to calculate in numpy the matrix-product of many matrices (~400). Are there common practices to increase numerical stability? If this is relevant, the matrices are $300\times 300$ orthogonal ...
1
vote
0answers
74 views

Calculate integrals using numpy.fft

Good evening, I would like to understand why I do not get the correct result: I assume that I know my function on discrete data points and expand it as a discrete Fourier transform: $\text{sin}(x)=\...
6
votes
0answers
114 views

fastest way to compute many small dot products

I have two n-by-3 blocks contiguous in memory ("n vectors of length 3") and I'd like to compute the dot product between each of the rows as fast as possible. In numpy, using ...
3
votes
1answer
418 views

Numerical derivative in python

I am trying to take the numerical derivative of a dataset. My first attempt was to use the gradient function from numpy but in that case the graph of the derivative ...
0
votes
0answers
83 views

Smoothing FFT result

I am trying to calculate the spectrum of Bremmstrahlung, which involves calculating the Fourier transformed acceleration. I am solving a non-linear ODE to numerically calculate the acceleration in the ...
0
votes
0answers
178 views

What algorithm do BLAS and ATLAS use for matrix multiplication?

I have searched and what I understood was that they use the naive one with several memory and cache optimizations. However, I wanted to know whether they are using the Strassen or the Coppersmith-...
4
votes
1answer
252 views

Diagonalize a unitary matrix with orthogonal matrices using numpy

An important component of the Cartan KAK decomposition for 2 qubit operations is to diagonalize a 4x4 unitary matrix using orthogonal (not unitary, purely real orthogonal) matrices. That is to say, ...
0
votes
1answer
324 views

Implementing structured grid boundary conditions using NumPy arrays?

I am making a toy code in Python to solve the advection equation $$u_t + cu_x = 0$$ with, for example, periodic boundary conditions. Background information The numerical grid is specified like this: ...
6
votes
2answers
1k views

Is there an efficient way to form this block matrix with numpy or scipy?

Is there an efficient way to form this block matrix with numpy or scipy? $$ \left[ \begin{array}{cccc} \mathbf{B} & \mathbf{0} & \cdots & \mathbf{0}\\ \mathbf{AB} & \mathbf{B} & \...
1
vote
1answer
127 views

Doing computations on a very large numpy array: streaming the calculation vs out-of-core memory

I am trying to perform a calculation in numpy that depends on several parameters, and involved the creation of many intermediate arrays. These intermediate steps involve integrals over more parameters....
2
votes
1answer
55 views

numpy.outer without flatten

$x$ is an $N \times M$ matrix. $y$ is a $1 \times L$ vector. I want to return "outer product" between $x$ and $y$, let's call it $z$. z[n,m,l] = x[n,m] * y[l] ...
1
vote
2answers
264 views

Numpy FFT gives me a pulse shorter than it should be. Not sure what I am doing wrong

I've created a code (Python, numpy) that defines an ultrashort laser pulse in the frequency domain (pulse duration should be 4 fs), but when I perform the Fourier Transform using DFT, my pulse in the ...
5
votes
2answers
388 views

Python: vectorizing a structured linear system solve

Overview I am looking for a way to solve a structured linear system in Python without using a for loop (preferably using vectorization, if possible). Background Consider the following linear system:...
2
votes
1answer
74 views

Problem of multiplication of big (sparse) matrix with numpy (python)

I wanted to multiply two simple (big and sparse) matrix with numpy. And I saw that the calculation fails when matrices are too big. If i take $X$ a random vector (size $n$). With pandas, I ...
0
votes
1answer
60 views

Specifying mesh spacing for DFT in numpy

I was testing the .fft package of numpy 1.16.1 in Python 3.7.2. In particular I was trying to verify that the transform resembles the analytical one for: $$f(x) = \mathrm{exp}\left[-\left(\frac{x-5}{2}...
0
votes
1answer
93 views

Creating an Interpolation of a w = f(x,y,z) function

I am trying to finish a series of interpolation functions. The problem is more related with organizing the data than how to do the interpolations. Using the RegularGridInterpolator, I created this ...
0
votes
1answer
815 views

Problems with python's interp 2D

I am writing some functions to interpolate data. While using interp2D, somehow, a sample matrix works but when I change the size of the matrix, it returns an error. ...
1
vote
1answer
68 views

Minimizing the used memory in diffusion simulation using Python

I am recently dealing with a diffusion simulation project and I have come up with the following code: ...
0
votes
1answer
5k views

Python Finite Difference Schemes for 1D Heat Equation: How to express for loop using numpy expression

Hello all, I've recently been introduced to Python and Numpy, and am still a beginner in applying it for numerical methods. I've been performing simple 1D diffusion computations. I suppose my ...
0
votes
1answer
91 views

Numpy: How to permute array into indices of larger array? [closed]

I have an array of length L with N zeros, and L-N non-zero values. I have another array of length N. I would like to put the values of the shorter array into the positions of the longer array which ...
0
votes
1answer
2k views

Applying neumann boundary conditions to diffusion equation solution in python [duplicate]

For the diffusion equation $$ \frac{\partial u(x,t)}{\partial t} = D \frac{\partial ^2 u(x,t)}{\partial x^2} + Cu(x,t) $$ with the boundary conditions $u(-\frac{L}{2},t)=u(\frac{L}{2},t)=0$ I've ...
4
votes
2answers
312 views

Moore-Penrose pseudoinverse of singular rank degenerate matrix

I am trying to attain the Moore-Penrose pseudoinverse of a very large, very sparse, rank-degenerate, singular, and square matrix. ($75000 \times 75000$, near rank). The matrix is a graph Laplacian and ...
0
votes
1answer
100 views

Is it possible to partition 2D data into bins such that each bin contains the same number of samples?

I am trying to sort data following a bivariate distribution into a numpy histogramdd, where each bin should contain the same number of data points (to the nearest whole sample). I expect that some ...
8
votes
0answers
525 views

Fast Automatic Differentiation for numpy?

I would like to use automatic differentiation to calculate gradients to function written in numpy. I've come across a number of packages, including autograd tangent chainer But none of them seem ...
3
votes
0answers
88 views

Inconsistency in optimize.minimize

I am trying to fit a time-dependent curve at each time step. I do so in minimizing along $x_c$ the quadratic error between the curve and a reference solution $ 1/(1 + \exp\left(\sqrt{S}(x-x_c)\right) $...
3
votes
1answer
59 views

why am I not getting a staircase for the rotation number?

I'm trying to understand the staircase map. Look at this map from the circle to itself: $$ x \stackrel{F}{\mapsto} \big[\omega + x + \tfrac{\epsilon}{2\pi} \sin (2\pi x) \big] \pmod 1 $$ Such a map ...
1
vote
1answer
3k views

Why `import numpy as np` for scientific computing? [closed]

Why do scientists bother with the np for numpy? (Note this is more a "philosophical"/reasoning question - let us assume that ...
1
vote
0answers
104 views

computing dual matrix trace norm and tensor gradient in python

I'm trying to write the following function in python: $$ f_\mu(\mathcal X) = f_0(\mathcal X) + \sum_{i = 1}^n \max_{||\mathcal Y_{i(i)}|| \leq1} \alpha_i\langle \mathcal X_{(i)},\mathcal Y_{i(i)} \...
1
vote
1answer
307 views

Performing a random walk on a lattice that traps the particles

I am trying to solve this problem where we have a 1D-lattice of size 100 and the particle can start from any position in the lattice and moves randomly on it(with equal probability of moving to either ...
1
vote
0answers
222 views

Rank filter on an nXm array using python

I would like to apply a rank filter on an nXm numpy array. Let's say I have this array: ...
0
votes
1answer
2k views

Numpy.polyfit with regularization

I am trying to use the numpy polyfit method to add regularization to my solution. My non-regularized solution is ...
0
votes
1answer
825 views

Numpy attributes not recognized in Numba [closed]

Numba offers JIT for Python. In its documentation it says "One objective of Numba is having a seamless integration with NumPy." So why including some of the simplest features from numpy isn't ...
1
vote
1answer
1k views

Why doesn't multiplying two matrices with NumPy work sometimes? [closed]

Using NumPy and attempting to multiply matrices together sometimes doesn't work. For example ...
4
votes
1answer
599 views

Poor SVD reconstruction of singular matrix

I am trying to calculate the singular value decomposition of this matrix using numpy.linalg.svd . However, reconstructing the matrix from the SVD gives a poor ...
1
vote
0answers
49 views

Update model parameter with new data, discarding old data

I have this dataset, and I am using y = (a * x^n) / (b + x^n) Hill function as the model, where a is the limit of the Hill curve,...
5
votes
3answers
4k views

Calculating partial trace of array in NumPy

A simulation I'm doing requires me to calculate the partial trace of a large density matrix. I am trying to calculate it using tools from numpy, but my code seems to be having some problems. For ...
1
vote
1answer
4k views

Python: Comparing 2D arrays and Returning independent values from those arrays [closed]

I'm sure this would be a common question, however yet to find an answer for. Suppose I have two, 2D vectors in Python ...
2
votes
0answers
180 views

Optimizing for loops by using Einstein summations instead

I have some Python code containing a couple of for loops, which I would like to optimize by using low-level functions. My first approach is to use ...