Questions tagged [numpy]

NumPy is the fundamental package for scientific computing with Python.

Filter by
Sorted by
Tagged with
3
votes
1answer
75 views

Lanczos algorithm for finding top eigenvalues of a matrix sum

I am trying to find the top k leading eigenvalues of a NumPy matrix (using python dot product notation) L@L + a*X@X.T, where $L$ ...
0
votes
2answers
81 views

Validating that a code is a good spherical code

Apologies if this is a trivial question. If that is the case I imagine I would benefit from someone explaining where my understanding is lacking. I am having some trouble interpreting the (putatively ...
-1
votes
1answer
55 views

illegal use of ODEINT

given the following system: $$\frac{dP}{dt} = \alpha P(1-\frac{P}{K}) - \beta P I$$ $$\frac{dI}{dt} = \beta P I - \rho I$$ how do I solve the system numerically. as when I attempt to solve this is the ...
0
votes
1answer
71 views

Reason behind different outputs for Fast Fourier Transform in Numpy and Matlab

Here is the output of Numpy np.fft.ifft([0, 4, 0, 0]) array([ 1.+0.j, 0.+1.j, -1.+0.j, 0.-1.j]) # may vary Here is the output of Matlab ...
3
votes
0answers
45 views

Difference between wave vector and density matrix in numerical calculation of Schrödinger equation

I solved Schrödinger equation for a following tow-level time-dependent Hamiltonian numerically in two ways: ...
0
votes
1answer
173 views

Saving Data in Multiple Columns with np.savetxt

I have managed to write the following code for the following problem: Projectile's horizontal and vertical displacement are given by: $$ x = v_0 \, t \cos(\theta) $$ $$ y = v_0 \, t \sin(\theta) - \...
1
vote
0answers
31 views

Interpolate location based on triangular sensor array

There are 3 sensors (A, B, C) on a plane, located in the corners of a (known) equilateral triangle. I want to calculate the (2D) location of an object (X) inside that triangle. One sensor returns one ...
-1
votes
1answer
69 views

plotting $F=K \frac{q_1 q_2}{r^3}$ in python

I tryin to plot the position of electron in the hidrogen atom by Coulomb's law, $F=K \frac{q_1 q_2}{r^3}$ This is mi code ...
-1
votes
1answer
85 views

Solve integral $ \int_{-\infty}^{\infty} e^{-x^2}dx$

i trying to solve this integral $$\int_{-\infty}^{\infty} e^{-x^2}dx$$ I'm using this CODE ...
2
votes
1answer
62 views

How to Invert a Poorly Conditioned Matrix

In my research, I need to invert a Fisher matrix in order to get a covariance matrix for me to do parameter estimation. Unfortunately, the values of Fisher matrix vary by many orders of magnitude, and ...
4
votes
1answer
78 views

Backwards Difference Implicit Method for Nonlinear Parabolic PDE in Python

Original Stack Overflow Question: https://stackoverflow.com/questions/65683788/indexerror-index-31-is-out-of-bounds-for-axis-1-with-size-31?noredirect=1#comment116218335_65683788 PDE: u_t = u_xx + u(...
0
votes
1answer
162 views

How to compare 2D vector fields and minimize the difference?

I want to compare the field of two electrical currents and compare the resulting field to a magnetic dipol field and find magnetic momentum that minimizes the difference of the two fields. My current ...
1
vote
0answers
58 views

Help with vectorized computation where vector multiplication would need to create an additional dimension

I want to use vectorized computation (with python and numpy) to do some calculation involving bessel functions. As is often the case, I need to calculate the sums of bessel functions of (infinit) ...
1
vote
2answers
80 views

The derivative of a gauss function via FFT and IFFT in Python

I have a problem with computing a derivative of a Gauss function using FFT and IFFT from NumPy library. I use the fact that $$ \begin{equation} \frac{d}{dx}f(x) = \frac{1}{\sqrt{2\pi}}\int{ike^{ikx}\...
1
vote
1answer
102 views

Help with modified bessel functions

I'm trying to parse the following expression (Eq. 35 in The Magnetic Field in the Vicinity of Parallel and Twisted Three-Wire Cable Carrying Balanced Three-Phased Current) in Python and calculate its ...
1
vote
0answers
19 views

Problem with recursive implementation of Subspace Iteration method in Numpy

I am having trouble with implementing the method of subspace iteration to find the eigenvalues and vectors of a random, symmetric matrix, A that is mxm with m = 10. ...
-1
votes
1answer
75 views

Converting for loop from matlab to python

I am converting some MATLAB code in to python and have the encountered the error "ValueError Traceback (most recent call last) in 1 for ig in range(nbas): ---->...
0
votes
0answers
55 views

how can I plot specific iterations?

I have made this code for an explicit Euler method but I want to plot only the 0,1,25,50 iterations not all of them.How can I do it? ...
0
votes
1answer
71 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
188 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}\{\...
3
votes
1answer
147 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
81 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
121 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
87 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
88 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
93 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 ...
4
votes
2answers
230 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
94 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
187 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
79 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)=\...
7
votes
0answers
168 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
1k 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
99 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
263 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
448 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
770 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
3k 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
288 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
69 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
365 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
413 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
111 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
76 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
118 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
1k 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
75 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
6k 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
105 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
3k 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
377 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 ...