Questions tagged [python]
A general purpose high-level programming language that emphasizes ease of code syntax and readability.
225
questions with no upvoted or accepted answers
7
votes
0
answers
137
views
Can we sparse solve a few eigenvalues specified by index range?
I need to solve a few eigenvalues of a large sparse matrix specified by their index range. These indices are according to the whole eigenspectrum sorted in algebraic (not absolute value) ascending ...
7
votes
0
answers
439
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 ...
7
votes
0
answers
341
views
Implementation of Lanczos method that returns tridiagonal matrix
The Lanczos method can be used to obtain extremal eigenpairs of sparse symmetric or hermitian matrices. I know there are several implementations of the Lanczos method (as well as Arnoldi, Davidson, ...
5
votes
0
answers
122
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 ...
5
votes
0
answers
188
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 ...
5
votes
0
answers
1k
views
Symmetric sparse direct solvers in scipy
scipy.linalg.solve, in its newer versions, has a parameter assume_a that can be used to specify that the matrix $A$ is symmetric ...
5
votes
0
answers
562
views
Avoiding divergent solutions with `odeint`? shooting method
I am trying to solve an equation in Python. Basically what I want to do is to solve the equation:
$$
\frac{1}{x^2}\frac{d}{dx}\left(Gam \frac{dL}{dx}\right)+L\left(\frac{a^2x^2}{Gam}-m^2\right)=0
$$
...
5
votes
0
answers
548
views
Iteratively finding both left and right eigenvectors for non-symmetric complex matrix
I have a complex, non-Hermitian matrix $\mathbf{A}$, for which I need to find a few eigenvalues and eigenvectors in the generalised eigenvalue problem:
$$\mathbf{A}\cdot \mathbf{x} = \lambda \mathbf{...
5
votes
0
answers
1k
views
Python - calculation time derivative and laplacien by finite differences
I would like to determine a temporal derivative 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 ...
5
votes
0
answers
258
views
Negative viscosity stabilized by fourth order terms
I am trying to solve a "Navier-Stokes"-type problem where the viscosity is negative. Of course this renders the equation unstable and thus I add a fourth order term, so the entire equation becomes:
$$...
4
votes
0
answers
237
views
Spurious oscillations in solving diffusion problems using finite elements
I've been struggling with this problem for a while so I hope someone can help me here.
I'm trying to solve the McNabb-Foster equations for hydrogen diffusion in metals using a simple 1D finite element ...
4
votes
0
answers
176
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 ...
4
votes
0
answers
51
views
Pass forward intermediate results during iterative optimization
To investigate a counter-current flow heat exchanger while considering temperature dependent physical properties (such as specific heat $c_\textrm{p,i}$, heat conductivity $\lambda_\textrm{i}$, ...
4
votes
0
answers
292
views
How to numerically evaluate this double Integral?
I want to evaluate the following integral:
$$\int_{0}^{60} \ \left(\int_{0}^{2z} 0.5\cdot t \left(\mathrm{erf}(t-a) -1 \right)J_{0}(qt)\mathrm{d}t \right)^2 \mathrm{exp}\left(-\frac{(z-a)^2}{2s^2}\...
4
votes
0
answers
474
views
Open source implementation of Multiscale Combinatorial Grouping
I would like to use Multiscale Combinatorial Grouping for my PhD research. However, I am restricted to use open-source implementations and this one runs on Matlab.
Does anyone know of an equivalent ...
3
votes
0
answers
195
views
Python code of explicit method of a nonlinear a BVP
I am trying to have a Python code for the following nonlinear BVP:
$$\frac{\partial N}{\partial t}=\frac{\partial^2 N}{\partial x^2}+N(1-N)-\sigma N$$ $$N(0,x)=\sin(2\pi x)$$
$$N(t,0)=0 \hspace{3mm}N(...
3
votes
0
answers
143
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:
...
3
votes
0
answers
157
views
Python routine to calculate shape resonances of H2
I am currently doing a project in which my aim is to write a program that can be used to calculate single and multi-channel shape resonances. So I'm looking at bound states and quasi-bound states.
...
3
votes
0
answers
460
views
Computing Small Eigenvalues with Sparse Symmetric Indefinite Mass Matrix
I want the eigenvalues of the following generalized eigenvalue problem:
$$ Av = \lambda M v $$
where
$A\in\mathbb{R}^{n\times n}$ is sparse, symmetric, and positive semi-definite
$M\in\mathbb{R}^{n\...
3
votes
0
answers
103
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
0
answers
125
views
How to optimize for decay constant in exponential-like function?
I've got a data set of points $M_O .. M_N$ for time points $t_0 .. t_N$, where $N$ is approximately 10-20, and the spacing of time is not uniform (i.e., $t_{i+1}-t_i$ is not constant for all i). It is ...
3
votes
0
answers
210
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 ...
3
votes
0
answers
2k
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
0
answers
1k
views
Polynomial Fitting with Least Squares using Numpy and Scipy
I am trying to fit data to a polynomial using Python - Numpy. The points, with lines sketched above them are as in the picture.
I am trying to fit those points to a polynomial of 4. or 5. degree. ...
3
votes
0
answers
2k
views
BFGS Fails to Converge
The model I'm working on is a multinomial logit choice model. It's a very specific dataset so other existing MNLogit libraries don't fit with my data.
So basically, it's a very complex function ...
3
votes
0
answers
170
views
Is there an easy way to read a PetscBag into a python dict?
I'm using a PetscBag to store the input parameters of my program. At some point, I'm going to need to use python to plot these parameters against some output parameters, and ...
2
votes
0
answers
53
views
Solving a system of non-linear equations to find relationship between arguments
I have a program that implements a multivariate function, call it $f = \mathcal{Q}(Z,v)$ that I can compute given $Z,v$. The $v$ variable is related to the $f$ variable by another relation, call it $v ...
2
votes
0
answers
90
views
Numerical solution for inviscid Burgers' equation seems to have no breaking time?
So I'm trying to use the Lax-Friedrichs method to solve the inviscid burgers' equation with initial condition $$u(x,0) = \sin(x)$$, using
$$u_m^{n+1} = \frac{1}{2}(u_{m+1}^n + u_{m-1}^n) - \frac{\...
2
votes
0
answers
79
views
Numerical integration in Fourier space over 3D grid
I am attempting to implement a model outlined in this paper:
General magnetostatic shape–shape interactions
Background
This model allows the calculation of magnetostatic interaction energies between ...
2
votes
0
answers
108
views
Newton-Raphson with Zeroth-Order Continuation is not Converging
I am trying to solve this system of nonlinear equations using Newton-Raphson method with continuation (zeroth-order continuation).
$F_1(x, y, \xi, \nu) = \left(1 - \frac{1}{\nu}\right) \left(1 - x\...
2
votes
0
answers
99
views
How can we symbolically working out $\phi^4$ theory green's function/propagator and consequences in python?
I am having some difficulty calculating Green's function symbolically in Python for $\phi^4$ theory.
The specific rendition of the $\phi^4$ theory I have in mind can be written as follows.
$\mathcal{L}...
2
votes
0
answers
137
views
How can I incorporate angular momentum in this code?
I'm currently working on the 3-body problem, and I was writing a code to plot the trajectories of all 3 bodies while also manipulating the angular momentum of the system. I found a code online and ...
2
votes
0
answers
294
views
Solving 2D Poisson equation with Dirichlet boundary conditions in Python
I am trying to solve the following PDE:
$$
\begin{align*}
u_{xx} + u_{yy}
=
\begin{cases}
- \cos(x) \quad -\pi/2 \leq x \leq \pi/2, \\
0 \quad \text{otherwise}
\end{cases}
...
2
votes
0
answers
128
views
Scipy.root not converging even when provided with initial guesses very close to solution
I've made a previous question here and also in SO wondering why only the fsolve solver converges for the simple one dimensional unsteady conduction problem
$$ \frac{\partial T}{\partial t} = \alpha \...
2
votes
0
answers
169
views
Error in implementation of Crank-Nicolson method applied to 1D TDSE?
Some context, I've posted this question on physics SE and stack overflow. The former had nothing to offer, the latter had a great commenter that agreed with the phase looking off being one of the ...
2
votes
0
answers
147
views
Numerical solution to integro-differential equation
The time dynamics of an atom interacting with a reservoir of spectral density $J(\omega)$ are obtained by solving the following integro-differential equation:
$$ \frac{\mathrm{d}c(t)}{\mathrm{d}t} = - ...
2
votes
0
answers
180
views
Numerical solution to the Tolman-Oppenheimer-Volkoff equations for any equation of state (numerical or analytical)
I've been working on a code to solve the Tolman-Oppenheimer-Volkoff (TOV) equations for a while and recently I've got it right but only for one specific equation of state, the bag model, which is not ...
2
votes
0
answers
314
views
How to solve the following SDP with cvxpy in Python?
The SDP problem is
$$
\min_{Z \in S^{n},Y \in S^{m}} {\rm trace}(Z) +{\rm trace}(Y)\\
{\rm s.t.} \begin{bmatrix}
Y & X\\
X^T & Z
\end{bmatrix} \succeq 0\\
X \in C
$$
Where $C$ is a convex set....
2
votes
0
answers
454
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 ...
2
votes
0
answers
223
views
Numerical instability in the inverse Laplace transform
I have a problem with Laplace inversion and my function is not numerically stable for the Laplace inverse, but I do not understand the cause of this problem.
Here is my code and graph of this problem. ...
2
votes
0
answers
213
views
solve Ax=b for outrigger A matrix python
I implement Crank-Nicolson 2D finite-difference method.
I get a matrix A which is banded with 1 band above and below the main diagonal, but also contains 2 additional bands , further apart from the ...
2
votes
0
answers
104
views
Cyipopt fails to converge for NLP problem which fmincon() can solve
I'm currently trying to implement a python script for solving a constrained nonlinear optimization problem with ~800 variables and 2 constraints, one linear and one nonlinear. There already exists a ...
2
votes
0
answers
376
views
Efficient way of calculating a cumulative integral with prefactor
I have a grid of points $x_i$ and corresponding function values $y_i=f(x_i)$. I'm interesting in something like the cumulant of $f$, but it has an awkward prefactor. The desired quantity we'll call
$$...
2
votes
0
answers
809
views
How to write a simple finite element solver in python in order to solve Poisson equation in 2D
I would like to write a simple finite element solver in python in order to solve 2D Poisson equation and then visualize it.
$$
-\nabla^{2} u(x,y)=f(x,y), \quad x,y \quad in \quad \Omega\\
u(x,y) = u_D ...
2
votes
0
answers
131
views
Split Operator 2D-->3D
I am trying to modify my simulations on population dynamics using the split-operator method from 2D two-level real Hamiltonain $$H_{2D} =T(x,y)\otimes1_2 +\begin{pmatrix} -z & y\\
y & z\\
\...
2
votes
1
answer
419
views
Adding a "cost term" to a linear regression, so solution values are minimized
I'm using Python's optimize.lsq_linear method to run a linear regression with the bounds set between 0% and 100% power usage.
...
2
votes
0
answers
652
views
Errors in Integral Estimate of Gaussian using Trapezoidal Rule
I'm trying to estimate the percentage error in computing the integral of a Gaussian via composite trapezoidal rule versus via an exact formula. To do this I've generated a gaussian with mean 0, ...
2
votes
0
answers
151
views
Divergence on wave equation simulation
I'm currenly working on my own PDE solver for non-linear simulations in python. I've done succesfully simulations for KdV and Fisher's equation, but now I'm playing with second order derivatives in ...
2
votes
0
answers
91
views
Finding the extrema of a transition probability function for a quantum walker on a graph
The goal
Implement some Python code to find the extrema points of a function that is strongly oscillating.
The background
Let $G$ be a connected graph with $n$ points with Laplacian matrix $L(G)$. We ...
2
votes
0
answers
376
views
Numerical simulation of magnetic dipole in a inhomogeneous magnetic field
Goal:
I want to use Python to illustrate how a magnetic dipole with magnetic moment m2 moves in a non-homogeneous magnetic field in a 2D-Plane. This field is generated by another magnetic dipole with ...