NumPy is the fundamental package for scientific computing with Python.

learn more… | top users | synonyms

1
vote
1answer
73 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
184 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 ...
2
votes
0answers
23 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. ...
0
votes
0answers
35 views

How to vectorize the GCD algorithm in number theory

In Python there numerous ways to compute the GCD (Greatest Common Divisor) in Python. Here is one: ...
0
votes
0answers
139 views

how to solve a 2D non-linear Poisson equation?

I am trying to solve the following equation for $P(x,y)$: $$I = P \nabla \cdot \frac{1}{P^2} \nabla{P}$$ where $P$ and $I$ are functions of x and y. $I(x,y)$ and $P(x,y)$ I want to understand ...
3
votes
1answer
88 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 ...
6
votes
2answers
461 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 ...
3
votes
1answer
185 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
99 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 ...
0
votes
0answers
118 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: ...
1
vote
3answers
704 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
307 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%. ...
3
votes
1answer
386 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
73 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 ...
1
vote
0answers
106 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
237 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....
10
votes
1answer
197 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
269 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}}...
0
votes
1answer
64 views

Scipy min warning meanings?

What does "Desired error not necessarily achieved due to precision loss" mean in the context of the scipy_fmin methods? I can't ...
0
votes
1answer
64 views

Mathematical error when attempting to represent step function using fourier series

I am attempting to work through a very simple problem. Determine the Fourier series expansion for the following heat PDE problem with ICS and BCS: $$ u_{t} = \alpha^2u_{xx}$$ $$ u(0, t) = u(L, t) =...
5
votes
1answer
162 views

roots of polynomials with small coefficients

I would like to compute the roots of a polynomial with exponentially small coefficients. $$ \sum_{n=0}^N a_n \frac{z^n}{\sqrt{n!}} \tag{$\ast$}$$ where $a_n$ are Normal random variables with mean $0$...
0
votes
1answer
214 views

roots of polynomials of high degree: LinAlgError: Eigenvalues did not converge

I wrote a simple script to generate random polynoimals $\displaystyle f(z)= \sum_{k=0}^N a_k \frac{z^k}{\sqrt{k!}} $ of high degree and find their roots. For more discussion on random polyomials see ...
3
votes
1answer
75 views

Riemann surfaces: computing $f(z) = \int_0^z \frac{dx}{\sqrt{P(x)}}$

I am trying to validate that the Schwartz-Christoffel mapping does indeed take the upper-half plane $\mathbb{H} = \{ z: \mathrm{Re}(z) > 0\}$ to a polygon. This involves integrals of functions ...
5
votes
1answer
770 views

Python - calculation time derivative and laplacien by finite differences

I would like to determine a temporal derivitive and a Laplacian by the finite differences method to solve the heat equation in a 1-dimensional case. My aim is to get the sources term that is why I ...
4
votes
2answers
1k views

Using numpy arrays in Paraview programmable filter

How can I access a field in Paraview's programmable filter as a numpy array? I want to: Import an existing field as a numpy array Create a similar array for output Register it as a new field for ...
1
vote
0answers
373 views

PageRank computation as the left principal eigenvector

I'm following the book here to compute PageRank: http://nlp.stanford.edu/IR-book/html/htmledition/markov-chains-1.html I built the transition matrix P as indicated in the book. Then I computed the ...
1
vote
1answer
748 views

WRL and VRML to Matplotlib/Numpy

I would like to import (or parse ??) a WRL/VRML file format in python in order to work the data with Matplotlib. Is there any way of converting this file format into plottable numpy arrays ? I ...
6
votes
3answers
1k views

plotting discontinuous functions

I need help plotting the Heaviside function: Real analysis often involves constructing bizarre functions which are intuitively correct, but ultimately wrong. See the great book Counterexamples in ...
3
votes
1answer
182 views

High frequency noise at solving diffusion equation

I'm trying to simulate a simple diffusion based on Fick's 2nd law. ...
3
votes
1answer
593 views

spectral decomposition in Numpy, sign difference

I am trying to follow along with an example from a book, but I get seemingly different answers depending on which spectral decomposition function I use in Numpy. I am trying to transform the Matrix G, ...
1
vote
3answers
2k views

Can numpy.linalg.solve use back substitution when possible?

The question is if Python Numpy library can use back subsitution to solve Ax=b if possible, that is, if A is lower triangular? Do numerical linear algebra packages do this? I would think Numpy would ...
1
vote
1answer
45 views

detecting special $2 \times 2$ matrices in a large array of zeros and ones

I have a large array of zeros and ones and I need to count instances of 0 1 1 0 0 0 1 1 0 1, 1 0, 1 1, 0 0 And I would like to exclude all ...
2
votes
1answer
374 views

rational functions in Python

I would like use Python to verify the following identities: $ \frac{1}{1-z} = 1 + z + z^2 + z^3 + \dots $ $ \frac{1}{1-z - z^2} = 1 + z + 2z^2 + 3z^3 + \dots $ $ q \prod_{n \geq 1} (1 - q^n)^{24} =...
1
vote
2answers
1k views

evaluating a function along an axis in numpy

I have a numpy function f that takes arrays as arguments and a 3D array x[a,b,c]. I would ...
5
votes
1answer
412 views

Finite Volume Implementation

I am trying to implement a simple finite volume method solver. I had a class on FVM a while back, but am still aware of the principal concepts. But implementing the FVM for non-cartesian or 1D meshes ...
0
votes
2answers
891 views

Successive over-relaxation not converging (when not done in-place)

I'm trying to find the potential given some boundary conditions using the successive over-relaxation method. I have 2 solutions: -One iterates over all elements and applies the formula ...
3
votes
3answers
528 views

Caculating the mean of vector accurately

I am having trouble with calculating a mean of vector with sufficient accuracy. My current solution which works but it quite slow and has unpredictable performance: mean_sum = mean = math.fsum(...
1
vote
1answer
905 views

Using multiprocessing to assign values to a numpy array [closed]

I have a code similar to this: ...
8
votes
2answers
320 views

Continuity of eigenvectors of parametric matrix

I have $n$-dimensional matrices $\mathrm{\hat{H}}(\vec{k})$ depending on vector parameter $\vec{k}$. Now, eigenvalue routines return eigenvalues in no particular order (they are usually sorted), but ...
4
votes
1answer
1k views

Analytic solution 2D scalar wave equation in cylindrical coordinates numerical implementation

I am trying to compare my finite difference's solution of the scalar (or simple acoustic) wave equation with an analytic solution. For that purpose I am using the following analytic solution ...
10
votes
4answers
2k views

Memory efficient implementations of partial Singular Value Decompositions (SVD)

For model reduction, I want to compute the left singular vectors associated to the - say 20 - largest singular values of a matrix $A \in \mathbb R^{N,k}$, where $N\approx 10^6$ and $k\approx 10^3$. ...
5
votes
1answer
251 views

Calculate large and small frequency separation for the Sun

I want to determine the big and small frequency seperation from timeseries data for the sun. An excerpt of the data (timeseries and power series) is plotted below. The power series is calculated in ...
2
votes
1answer
217 views

Explicit 4th order space wave equation not stable implementation?

The explicit 4th order discretization for the 2D scalar wave equation is given by: \begin{eqnarray} U_{jk}^{n+1} = \left( \frac{\Delta t V_{jk} }{\Delta s} \right) ^2 \left( \sum_{a=-N}^N w_a U_{...
10
votes
1answer
1k views

Why does SciPy eigsh() produce erroneous eigenvalues in case of harmonic oscillator?

I'm developing some larger code to perform eigenvalue computations of huge sparse matrices, in the context of computational physics. I test my routines against the simple harmonic oscillator in one ...
2
votes
1answer
118 views

python numpy print array(x,y,z) as (x by y) by z?

Printing photo matrices (x,y,z) where z is 0:2 colour, I want to see (x in rows, y in columns) 3 times, once for each colour (the way I've been taught by every mathematician). By default, numpy does ...
21
votes
1answer
982 views

How does the performance of Python/Numpy array operations scale with increasing array dimensions?

How do Python/Numpy arrays scale with increasing array dimensions? This is based on some behaviour I noticed while benchmarking Python code for this question: How to express this complicated ...
13
votes
3answers
806 views

How to express this complicated expression using numpy slices

I wish to implement the following expression in Python: $$ x_i = \sum_{j=1}^{i-1}k_{i-j,j}a_{i-j}a_j, $$ where $x$ and $y$ are numpy arrays of size $n$, and $k$ is a numpy array of size $n\times n$. ...
9
votes
1answer
950 views

Numerical integration for modelling curve for superconductors (Python)

I am a physicist who is trying to model the current-voltage characteristics of a superconductor-superconductor junction. The equation for this model is: \begin{align} I(V) = \frac{1}{eR_{\mathrm{n-n}...
2
votes
2answers
2k views

vectorizing a non-trivial function in numpy

I have a 2d numpy array, with shape (100,3) a = np.random((100,3)) and I want to call a function ...
3
votes
2answers
2k views

Consistent handling of division by zero in numpy array

I want to populate a numpy array with values from the smooth bump function f(x) = exp ( - 1 / (1 - x^2) ) if |x| < 1, f(x) = 0 otherwise Currently I ...