Questions tagged [numpy]
NumPy is the fundamental package for scientific computing with Python.
176 questions
1
vote
1
answer
82
views
Logistic Regression: Gradient Descent and Accelerated Version Thereof not acting as intended
Consider the regularized logistic regression problem:
$$\min_{\theta \in \mathbb{R}^n}\sum_{i=1}^m \ln \left(1+e^{-y_i\theta^T x_i}\right)+\frac{\lambda}{2}\big\lVert\theta\big\rVert^2_2.$$
Here, $y_i ...
2
votes
1
answer
156
views
algorithmic complexity in numpy
I have learnt scientific computing back in the time when C and C++ were the thing, and I'm relatively new to ...
1
vote
1
answer
245
views
How can we compute the exact area under f(x) = e^x from 0 to 10 using fixed step size Δx = 0.5, without taking a limit?
Let’s suppose we are working in a discrete setting with fixed intervals and want to compute the area under a known continuous function without taking the limit as Δx → 0.
Example:
Consider the ...
4
votes
1
answer
249
views
Chebyshev Series Derivative in terms of Coefficients
We all learned in Calc 1 how to take derivatives of polynomials. What we were probably not told, is that this is but one possible expression of a function, in terms of a "power series", and ...
1
vote
0
answers
44
views
An efficient method to compute the results of co-integration test on a rolling window for two time series
I have two time series X and Y of length ~2 million. I have a parameter window_size which is ...
1
vote
0
answers
115
views
Harmonic Quantum Oscillator ODE Python
So, we have to calculate eigenvalues and eigenfunctions for this one assignment. In short, we are using a Quantum Harmonic Oscillator.
If you want to read the full thing , Here:
The probability ...
0
votes
0
answers
138
views
Rayleigh iterate function in Python
I should write a Rayleigh quotient iteration function x, k = rayleigh iterate(A,x0, eig),
which takes a matrix A, an initial vector x0, and an approximate eigenvalue eig as arguments and returns an ...
4
votes
1
answer
2k
views
Overflows and underflows in Python
I’m writing some Python code using NumPy. Since I got an overflow warning, I decided to check for underflows as well at all places in the code, using ...
4
votes
1
answer
153
views
How can I efficiently find an anti-symmetric generator of a special orthogonal matrix?
Given a special orthogonal matrix $O$ (i.e: $OO^T = 1$ and $\det(O) = 1$), I am trying to efficiently find a matrix $X$ such that $O = e^X$ and $X = -X^T$ using Python (NumPy & SciPy).
One obvious ...
1
vote
0
answers
68
views
Converting an expression into an einsum
I have the following expression that I need to calculate for some matrices:
$$
\sum_{k}c_{t,i,k}\sigma^\prime\left(w_tX_t+b_t\right)_k\left(\sum_\ell w_{t,k,\ell}\tilde{X}_t^{w,\ell}\right)
$$
I could,...
0
votes
0
answers
112
views
First and second component of fft for circle approximation to periodic curve
I wanted to understand how the fast fourier transform work in numpy and for this I tried apply it on $n$ points of an ellipse $t_k = \frac{2\pi}{n-1}k$ with $k=1...n$ $$f_k = f(t_k) = (acos(t_k), bsin(...
0
votes
1
answer
341
views
Solving a polynomial with NumPy
I'm trying to do something that I thought would be very straightforward but somehow I'm struggling.
I have a time series and I want to extrapolate it, assuming a linear trend, to forecast when will it ...
12
votes
1
answer
425
views
Is it possible to express an arbitrary tensor contraction in terms of BLAS routines?
I noticed that libraries like numpy and pytorch are able to perform arbitrary tensor contractions at speeds similar to comparably sized matrix multiplications. This leads me to believe that underneath ...
5
votes
2
answers
2k
views
What algorithm(s) do numpy and scipy use to calculate matrix inverses?
I am solving differential equations that require inverting dense square matrices, and I wanted to know what algorithm(s) do numpy and scipy use to calculate matrix inverses?
1
vote
1
answer
179
views
Improvement to naive gradient descent implementation for the Thomson problem
I have a Python program (available on github) that uses naive gradient descent to find approximate solutions to the Thomson Problem. It works surprisingly well, but I've been wondering if there's a ...
3
votes
0
answers
360
views
Helmholtz decomposition of a vector field in Fourier space with Python
I have a 3D vector field and I want to extract its divergence-free part (also called transverse component), using the Helmholtz decomposition.
In principle, this can be done in the Fourier space, as ...
1
vote
1
answer
1k
views
Float equality tolerance for single and half precision
Suppose the metric is
abs(a-b) <= rtol * max(abs(a), abs(b))
i.e. math.isclose with ...
0
votes
1
answer
99
views
Compute a series of matrix multiplications and matrix norms quickly in Python
I need to compute a series of matrix multiplications involving 3x3 matrices and a series of matrix norms also involving 3x3 matrices and I wonder how I can set these computations up with numpy such ...
0
votes
0
answers
125
views
3
votes
1
answer
2k
views
Time and memory required to diagonalize a 18000 by 18000 matrix using numpy in python
Can someone give an estimate of the Time and memory required to diagonalize a 20000 by 20000 complex hermitian matrix using numpy in python ?
1
vote
0
answers
163
views
Convolution/weighted average of two arrays in Python
I have an equation that I need to calculate numerically, but I am having doubts about my approach. I am cross-posting this question from Stack Exchange, because I am not getting any responses.
This is ...
0
votes
0
answers
69
views
Eigenvalues of same operator expressed in two different orthonormal basis are coming out different
I have an operator $H$. I express $H$ as a matrix in the orthonormalized $\{ |e > \}$ basis. Then I diagonalize it to obtain eigenvalues, let's say for example $H$ is $6 \times 6$ and the ...
2
votes
1
answer
1k
views
Beta function and integral value
I have two values $a$ and $b$ where $a \ge 0$ and $b \ge 0$ and I have to calculate the formula below.
$$
\frac{1}{2}\int_0^1\text{abs}\left[\left( \frac{p_i^{(a - 1)} \times (1 - p_i)^{(b - 1)}}{\...
0
votes
1
answer
492
views
Find two lines around which points were randomly generated
Given a list of points that were randomly generated around two lines, find two new lines that match the original lines as closely as possible. Here's the function definition:
...
0
votes
1
answer
141
views
How to convert from a non traditional CSV-style content (file with JSON data but its extension is .CSV) to a traditional CSV-style with Python?
CSV (even though once opened it does not contain a traditional CSV format I think it contains JSON data)[![1], when I try to open it as a TXT file I got this as shown in the first screenshot. When I ...
0
votes
1
answer
165
views
Planes in n-dimensional space
This is not a homework, but a hobby project, and maybe not all terms I use are correct - please help to fix.
Imagine there are K vectors in n-dimensional space. I would like to:
validate whether they ...
2
votes
0
answers
149
views
How to save multiplication computation time between a dense vector and a not that sparse matrix?
I am trying to compute $\mathbf{X}\mathbf{u}$ for many times in my algorithm, where $\mathbf{X}\in \mathbb{R}^{n\times m}$ and $\mathbf{u} \in \mathbb{R}^{m}$. The problem is that, during the ...
0
votes
0
answers
115
views
Advance a Interpolation
Note; No special knowledge of Pykrige is needed to answer the question, as I already mention examples in the question!
Hi I would like to use Universal Kriging in my code. For this I have data that ...
3
votes
0
answers
562
views
How can I reduce the artifact in "Thin Plate Spline" interpolation?
At the Top "right", there is the 2D-density plot of the recorded data
(actual), fewer in number. Recorded data has been sampled a on the 8
arms of a regular octagon. These 8 arms are placed ...
-1
votes
1
answer
236
views
Fitting gauss-hermite-parametrization to data?
I want to fit this data.
I have the following model functions. Classic gaussian:
def gauss_model(x, mu, sigma):
return np.exp(-0.5*((x-mu)/sigma)**2)
And ...
1
vote
1
answer
114
views
Why does this implementation for Eisenstein integer pairs of Euclid's method for finding greatest common denominators get stuck for this one point?
My Math SE question determining if a coincident point in a pair of rotated hexagonal lattices is closest to the origin? explains the problem I have. I won't reproduce the whole thing in detail here, ...
5
votes
1
answer
1k
views
Stochastic SIR using SDEint python package
I want to use the SDEint package to give a numerical solution (plot) of the following stochastic SIR model. Namely, a system of SDEs.
$$\begin{cases}
dS = -\beta SIdt - \sigma SIdW \\
dI = (\beta SI -...
4
votes
0
answers
220
views
How amenable is this 2D Frenkel–Kontorova-like energy minimization problem in Python to the use of a modest PC + GPU? (Heavy reliance on indexing)
@Richard's answer to Going to try to move some of my scipy/numpy calculation to a new GPU, how to avoid disappointing results? is quite helpful, and as promised I've added a simple running example ...
1
vote
0
answers
599
views
Trouble inverting complex matrix with numpy and scipy
I have some matrix-valued, complex data $Z(f)$ with $f\in\{f_0,f_1,\dots\}$ and $Z(f_i)$ being a 3x3 matrix. I require the inverse $Z^{-1}(f)$ in my workflow. After encountering some problems with my ...
4
votes
1
answer
829
views
Going to try to move some of my scipy/numpy calculation to a new GPU, how to avoid disappointing results?
update: I've refactored the question based on helpful advice in the linked meta.
I'm a heavy user of Python's NumPy and SciPy (and not much else) and for years I could run anything I need on my laptop....
5
votes
0
answers
182
views
How to troubleshoot numerical instability using finite difference for steady-state non-linear heat conduction equation
I have a problem which I believe is numerical instability when trying to solve a heat conduction equation using finite difference. The short version is that when the parameter $I=80.3$ I get the blue ...
2
votes
2
answers
374
views
Calculate determinant of unitary matrices based on SVD implementation
I have a real square matrix $X$ which I need to perform a Singular Value Decomposition on. Now, performing the operation
$$
X = USV^T
$$
as $U$ and $V$ are orthogonal, we know that $\det(X)=\pm\det(S)$...
2
votes
1
answer
5k
views
solve_ivp from scipy does not integrate the whole range of tspan
I'm trying to use solve_ivp from scipy in Python to solve an IVP. I specified the tspan ...
0
votes
2
answers
163
views
Implementation of $[X, \cdot]$ as an $n^2 \times n^2$ matrix, where $X$ is an $n \times n$ matrix
Let $M_n(\mathbb{R})$ denote the set of $n\times n$ matrices with real entries. I have an $n\times n$ matrix $X\in M_n(\mathbb{R})$, and I would like to implement the linear operator $[X, \cdot] : M_n(...
0
votes
0
answers
140
views
Plotting the motion of a positive charge in a cylindrically symmetric magnetic field
I want to plot the motion of a positive charge in a cylindrically symmetric magnetic field.
I am assuming a cylinder around the z-axis, with the magnetic field going in clockwise direction. The B-...
0
votes
0
answers
158
views
Getting euclidean distance between vector A and C without anyway of retrieving them when their distances with a common vector B is known
Motivation:
My plan is to get the overall euclidean distance matrix for all the vectors in N number of dataset. Each dataset is basically an array of n-dimensional points. For e.g: A dataset can be ...
5
votes
0
answers
297
views
Solving multiple linear regression in parallel
I am working on a problem where I need to solve approximately 500 Million Linear Regressions (OLS).
What would be the most efficient way to do this (e.g. using GPU or a some framework that can do this ...
-1
votes
1
answer
866
views
Using ODE to plot particle-motion with scipy.integrate.solve_ivp
My Problem:
A positively charged particle (mass = 2 * 10-27 kg) is moving along the x-axis. It is travelling in a homogenous magnetic field such that the field axis in z-direction. The energy of the ...
-2
votes
2
answers
263
views
numerical solution for differential equation
I have these 3 equations and i want to solve them with numerical methods. so I am using scipy library but I don't know how to solve 3 equations together.
R, g, sigma and density are constants.
\begin{...
3
votes
1
answer
277
views
Forming a particular (averaged) block matrix with numpy
Say I have a set of $n \times n$ matrices $A_1, ..., A_m$ as numpy arrays. I'd like to create the block matrix defined below.
I'm looking for a clean, elegant, and easy-to-interpret way of doing this ...
3
votes
1
answer
444
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*[email protected], where $L$ ...
0
votes
2
answers
132
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
1
answer
394
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 ...
1
vote
1
answer
2k
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
0
answers
163
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:
...