Questions tagged [numpy]

NumPy is the fundamental package for scientific computing with Python.

Filter by
Sorted by
Tagged with
2
votes
0answers
42 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
42 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 optimization but I wanted to know are they using strassen or copper smith algorithms and if they ...
4
votes
1answer
61 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
65 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: ...
5
votes
2answers
129 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
41 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
34 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
1answer
119 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
353 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
56 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
34 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
61 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
124 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
52 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
2k 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
52 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
892 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
190 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
63 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
328 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
84 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
58 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
2k 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
88 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
232 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
162 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
995 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
545 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
756 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
448 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,...
3
votes
2answers
2k 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
3k 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
159 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 ...
1
vote
1answer
92 views

Fast table interpolation on regular time data for ODE

I am using scipy.integrate.odeint to simulate the reaction of a system with known input signals via integration. The simplified code below illustrates what I'm ...
5
votes
2answers
228 views

Speeding up a linear transform using Python

In an optical wavefront propagation problem, I need to do excessive Fourier-type computations: ...
0
votes
2answers
128 views

Trying to compute the error from comparing two arrays

Some context: I am working with the Black-Scholes model.. I have an explicit (Black-Scholes) formula which is the exact solution to my problem. I have written code which implements a finite-difference ...
2
votes
1answer
322 views

How to broadcast my matrix exponentiation

I'm trying -to no avail- to broadcast the following piece of code in Python: ...
1
vote
1answer
215 views

Power spectrum incorrectly yielding negative values

I have a real signal in time given by: And I am simply trying to compute its power spectrum, which is the Fourier transform of the autocorrelation of the signal, and is also a purely real and ...
2
votes
1answer
857 views

Wrong amplitude of convolution using numpy fft

I try to convolve a rectangle function in [-1/2, 1/2] with itself using fft. The convolution should be a tent shaped function, see figure below. The code is below. In the 3rd to last line I add /50 ...
4
votes
1answer
469 views

Chebyshev approximation by projection vs interpolation

Suppose we want to approximate a function $f: [a, b] \rightarrow \Re$ with a Chebyshev series: $$ f(x) \approx \sum_{k=0}^n c_k \, T_k\left( \frac{2x-b-a}{b-a} \right) $$ where $T_k(x) = \cos(k\, \...
3
votes
1answer
869 views

integral including a vector

I need to calculate the integral of this function def f(z): return ((1-2*z)*np.exp(-d/z))/(((1-z)**(2+d))*(z**(2-d))) Here d is a constant. I am using this ...
1
vote
1answer
62 views

nodal lines of wave-function $\psi(x,y) = \sin 12x \sin y + (1 + \epsilon) \sin x \sin 12y$

I am trying to reproduce this figure of nodal lines of a wavefunction from this work of Berry $$\psi = \sin 2r\,x \sin y + (1 + \epsilon) \sin x \sin 2r\,y$$ Here the image. The first is $\epsilon = ...
1
vote
0answers
961 views

Finding eigenvectors and eigenvalues of large matrices in Python's numpy

I am running a PCA analysis on a data set using Python's (v2.7.10) NumPy. I validated that my program works by running the PCA analysis on a smaller dataset and then confirming that I get similar ...
1
vote
1answer
91 views

Binary tensor operations in Nutils [closed]

How does one write general tensor contractions in the Python-based finite element package Nutils? For example, how does one write the contraction of a fourth-order elasticity tensor $\boldsymbol{C}$ ...
0
votes
2answers
164 views

Fast Python implementation of short-range interacting particles under Metroplis algorithm

Can anyone write a Python implementation of a set of particles interacting in 2D according to a short-range particle-particle force and evolving in time under a Metropolis algorithm, which randomly ...
2
votes
0answers
96 views

Is the numerical resolution of this huge sparse polynomial system tractable?

I'd like to find numerically a solution to a sparse system of 2000000 polynomial equations of degree 3 with 50000 variables and integer coefficients (or at least to decide whether or not a solution ...
1
vote
1answer
98 views

Vectorizing list of different functions for Gradient Descent

I am new to machine learning and statistical analysis and am having trouble figuring how I should go about a problem I have. I believe that I understand the gradient descent algorithm and how it ...
7
votes
3answers
4k views

What does Python offer for distributed/parallel/GPU computing?

Using the SciPy/NumPy libraries, Python is a pretty cool and performing platform for scientific computing. I just wonder: When I have to go parallel (multi-thread, multi-core, multi-node, gpu), what ...
3
votes
0answers
1k views

Efficiently creating an adjacency matrix from a lattice in numpy

I have an $n$ by $m$ numpy array representing a rectangular lattice $L$, where each site contains a one or a zero, representing two different materials. I'm modelling heat flow across this lattice. ...