Questions tagged [numpy]
NumPy is the fundamental package for scientific computing with Python.
1
vote
1answer
30 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
73 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
17 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
135 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 ...
3
votes
2answers
127 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 ...
-1
votes
1answer
46 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 ...
6
votes
0answers
177 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
74 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
56 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
871 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
72 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
153 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
74 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
603 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
320 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
366 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
324 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
47 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,...
2
votes
2answers
1k 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
2k 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
128 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
76 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 ...
6
votes
2answers
200 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
107 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
270 views
How to broadcast my matrix exponentiation
I'm trying -to no avail- to broadcast the following piece of code in Python:
...
2
votes
1answer
168 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 ...
1
vote
0answers
635 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 ...
3
votes
1answer
411 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
540 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
57 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
772 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
88 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
147 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
95 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
97 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 ...
8
votes
3answers
3k 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
865 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. ...
3
votes
1answer
309 views
(numpy/scipy) Build a random vector given mean vector and covariance matrix
After running several calculations with numpy, I end with the mean vector and covariance matrix for a state vector. Is there a way with numpy or scipy to sample a random vector around this mean and ...
11
votes
2answers
5k views
Complexity of matrix inversion in numpy
I am solving differential equations that require to invert dense square matrices. This matrix inversion consumes the most of my computation time, so I was wondering if I am using the fastest algorithm ...
4
votes
1answer
4k views
Python Vectorizing a Function Returning an Array
I have the following function that has been vectorized so that for every element in input array t, an array is output:
...
5
votes
2answers
103 views
How to read the number of periods of this complicated graph?
I have two data sets that are quasi-periodic. They have the same period and can be seen clearly by eye. For example when $x\in(100,200)$, both of them have about 32 periods.
Below is a zoom-in of the ...
1
vote
0answers
171 views
Inverse quadratic method only makes one iteration - python
I'm tryin' to implement this method with python3 but i don't solve the problem. This is my code:
...
4
votes
4answers
8k views
LCM builtin in Python / Numpy
I can write a function to find LCM (lowest common multiple) of an array of integers, but I thought it must have been implemented in numpy or scipy and was expecting something like ...
2
votes
3answers
2k views
Runge-Kutta Simulation For Projectile Motion With Drag
I am attempting to simulate projectile flight with drag. However, with a timestep of 0.1 seconds, I am consistently getting an error of ~0.1-1%.
...
5
votes
1answer
1k views
Alternatives to numpy.einsum
Given an $n_1 \times \cdots \times n_k \times g \times g$ tensor $A$ (i.e. a collection of $g \times g$ matrices) and an $n_1 \times \cdots \times n_k \times g$ tensor $b$ (i.e. a collection of ...
3
votes
0answers
146 views
matplotlib contourplot for $\log z$ in the Complex Plane $\mathbb{C}$
I tried using Python's matplotlib on the logarithm and here is what I got, a kind of starburst pattern. Since the angle jumps between $\theta = 0$ and $\theta = 2\pi$, contour assumes there is a ...
2
votes
0answers
313 views
My calculated laser pulse duration is too large. Where am I wrong?
I am currently writing a small Python script to estimate the pulse duration from the optical spectrum.
At the end, the idea is to observe the effects of the spectral phase on the pulse duration and ...
0
votes
1answer
1k views
How do I correctly multiply vectors and matrices in Python and MATLAB?
I have been trying for 2-3 days now to get L2 regularized logistric regression to work in Matlab (CVX) and Python(CVXPY) but no success. I am fairly new to convex optimization so I am quite frustrated....
12
votes
1answer
848 views
Can an approximated Jacobian with finite differences cause instability in the Newton method?
I have implemented a backward-Euler solver in python 3 (using numpy). For my own convenience and as an exercise, I also wrote a small function that computes a finite difference approximation of the ...
1
vote
2answers
561 views
Python, numpy and complex functions (PDE's)
Update 4
I have almost given up on getting this right. This is the solution to the time-independent Schrodinger's equation, so the analytical solution is:
$\psi(x,t) = \psi(x,0)e^{\frac{-iE t}{\hbar}}...