Questions tagged [python]

A general purpose high-level programming language that emphasizes ease of code syntax and readability.

Filter by
Sorted by
Tagged with
4
votes
2answers
177 views

Storing large amounts of interpolation data

Overview and Prior Research I am looking for a way store a (in principle arbitrarily) large "3D-table" for interpolation/ lookup in combination with python. I have considered CSV files, but,...
0
votes
1answer
22 views

Logistic growth curve using scipy is not quite right

I'm trying to fit a simple logistic growth model to dummy data using Python's Scipy package. The code is shown below, along with the output that I get. The correct ...
6
votes
1answer
125 views

Why Is This Python Code Faster Than Fortran Wrapper with F2PY?

I am writing a proof-of-concept implementation of Newton's method for minimizing the negative log-likelihood term in a logistic regression model. I'm comparing the performance of a native python ...
0
votes
0answers
37 views

Numerically solving schrodinger equation [duplicate]

Consider the potential given above: $$U(r) = \frac{U_0}{\exp[(r - r_0)/\epsilon] + 1}\, .$$ How to solve the Schrodinger equation with this potential numerically and find the eigenvalues?
12
votes
1answer
1k views

How to compute Singular value decomposition of a large matrix with Python

Language: Python3 Problem: I have a matrix Q of shape [51200 rows x 51200 cols] stored in a binary file, each of the element in this matrix has a data type of complex64. To load the data into memory I ...
3
votes
1answer
402 views

Poisson image blending artifacts

I am trying to implement Poisson image blending as in the paper Poisson Image Editing. This is the task of filling in a masked region of an image by minimizing $$\min_f\int_\Omega \left | \nabla f - \...
3
votes
0answers
40 views

Help with restart functionnality in sef-made GMRES solver in python

I am new to this forum and to computational science in general. I started to learn numerical liner algebra on my own and would like to code a GMRES solver in python (no preconditioner for the time ...
2
votes
2answers
210 views

(Lack of) Availability of Finite-Difference library for simple 2D PDEs

I would like to solve two types of simple 2D problems, namely the stationary heat equation on an L shaped geometry like this: And also compute the magnetostactic field in an air gap of the following ...
3
votes
2answers
1k views

Generate random smooth 2D closed curves

I would like to know how can I generate a collection of random 2D closed smooth curves. I thought about generating a random 3D surface with random peaks, and then intersecting the Z=0 plane with it, ...
3
votes
1answer
683 views

Calculation of Mean Square Displacement for Brownian dynamics system with Lennard Jones interactions in python3

I have a problem getting a sensible result for the Mean Square Displacement (MSD) for a simulation of $N$ particles under Brownian dynamics with Lennard-Jones interaction between them with or without ...
9
votes
1answer
430 views

How to solve a second order differential equation (diffusion) with boundary conditions using Python

I am having trouble implementing a model from a publication. Huang, K-L.; Holsen, T.M.; Selman, J.R. Ind. Eng. Chem. Res. 2003, 42, 15, 3620–3625 scihub link: https://sci-hub.se/10.1021/ie030109q I ...
4
votes
3answers
5k views

How to get ODE solution at specified time points?

The code below basically illustrates my problem. It is a test code for a pendulum. I solve it using a method suggested on https://stackoverflow.com/questions/12926393/using-adaptive-step-sizes-with-...
2
votes
2answers
165 views

Single hexahedral element stiffness matrix problem, help me find the mistake

Attached below is some code I wrote to solve a basic problem: finding the node displacements of a cube with two vertices constrained (vertices 6 and 7 with coordinates ...
0
votes
1answer
81 views

How to improve the efficiency of periodicity detection for long time based lined and gapped datasets

Our data set has $10^4$ data points, but has a long baseline and many gaps. As the histogram shows, the horizontal-axis is time and most of the time, there are no data. The vertical-axis is data ...
3
votes
1answer
88 views

Parameters estimation with fewer variables than parameters

I am trying to estimate parameters, 4 of them, by fitting a system of 3 ordinary differential equations. I am using a model published that was using 3 parameters and gave a good fit to the data, and I ...
0
votes
2answers
111 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(...
2
votes
1answer
179 views

Double Integral with Gauss- Hermite for one component

I am trying to perform the following integral $$\int_{0}^{2\pi}\int_{0}^{+\infty} \frac{r'\left(e^{-r'^2/2\sigma^2}\right)\left(r-r'\cos(\theta-\theta')\right)}{r^2+r'^2-2rr'\cos(\theta-\theta')}dr'...
3
votes
2answers
2k views

Applying the result of Cuthill-McKee in SciPy

I have applied SciPy's implementation of the Cuthill-McKee algorithm to a $48 \times 48$ sparse non-symmetric matrix in Compressed Sparse Row (CSR) format and the output is an array of length $48$ ...
3
votes
2answers
164 views

How to solve the integral-like energy equation with Sagdeev potential numerically in Python?

I am trying to numerically solve equation (6) of Lakhina 2021 in Python. The equation is $$\frac{1}{2}\left(\frac{d \phi}{d\xi}\right)^2 + S(\phi, M) = 0\, .$$ The Sagdeev potential expression is ...
-1
votes
1answer
220 views

Error in python (jupyter): index 1 is out of bounds for axis 0 with size 1 [closed]

I am an amature in python, I wrote a simple code in jupyter. But it is giving an error. I want to plot a function: ...
2
votes
0answers
37 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. ...
3
votes
3answers
223 views

Convolute a gaussian kernel with a large array of off-grid centroids without looping? (how to make "A Thousand (Gaussian) Points of Light" )

For a finite object size diffraction simulator, I need to generate arrays which are the sum of thousands of instances of a Gaussian (or other) 2D kernel at centroids that will not fall in any ...
2
votes
1answer
736 views

Error in Simpson's 3/8 rule is higher than that of Simpson's 1/3 rule

For a given function $f(x)$, I have tried to find its numerical integral using Simpson's 1/3 and Simpson's 3/8 rules. I then compare the solution from the numerical quadratures to the analytical ...
1
vote
1answer
143 views

How to generate the convolution of f(x, y) with a parametric function g(t), x(t), y(t) in Python? (Something better than this brute-force sum)

The answer to Convolute a gaussian kernel with a large array of off-grid centroids without looping? (how to make "A Thousand (Gaussian) Points of Light" ) involves summing a 3D array over ...
25
votes
3answers
16k views

Recommendation for Finite Difference Method in Scientific Python

For a project I am working on (in hyperbolic PDEs) I would like to get some rough handle on the behavior by looking at some numerics. I am, however, not a very good programmer. Can you recommend ...
1
vote
2answers
33 views

scipy odeint: excess work done on this call depending on initial values even with analytically solvable ODE

I am trying to solve a differential equation in the form: dx/dt = funct(x) using scipy odeint. However, for some initial values, I get a "ODEintWarning: Excess work done on this call", even ...
0
votes
0answers
35 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-...
12
votes
1answer
5k views

Meaning of "-0.0" in Python?

We are finding in Python some occasional errors in our coordinate transforms and other similar computations that produce a result of -0.0. What purpose does this ...
1
vote
1answer
115 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 ...
2
votes
1answer
63 views

Python documentation on creation of an exponential random variable [closed]

I didn't really know if this stack was the right place to post but I was reading the documentation for creating an exponential random variable in numpy. But isn't there a typo. Like shouldn't it be : $...
88
votes
18answers
112k views

Is there a high quality nonlinear programming solver for Python?

I have several challenging non-convex global optimization problems to solve. Currently I use MATLAB's Optimization Toolbox (specifically, fmincon() with algorithm=<...
1
vote
1answer
61 views

Efficiency of scipy.sparse.linalg.expm_multiply with sparse vs unsparse vectors

From the package scipy.sparse.linalg in Python, calling expm_multiply(X, v) allows you to compute the vector ...
0
votes
1answer
96 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}...
2
votes
1answer
619 views

How can I evaluate more accurate energy eigenvalues from Schrodinger equation using shooting method?

I am trying to use the "shooting method" for solving Schrodinger's equation for a reasonably arbitrary potential in 1D. But the eigenvalues so evaluated in the case of potentials that do not have hard ...
4
votes
0answers
55 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 ...
3
votes
1answer
2k views

Applying Neumann boundaries to Crank-Nicolson solution in python

Consider the heat equation $$u_t = \kappa u_{xx}$$ with boundary conditions of $$u(x,0)=0\\ u(0,t)=100\\ u(l,t)=0$$ Numerical analysis by pyton can be done with ...
3
votes
1answer
281 views

Calculating Error for Poisson Equation using Successive Over-Relaxation technique, Python

I am trying to solve the Poisson Equation $\frac{\partial^2 T}{\partial x^2} + \frac{\partial^2 T}{\partial y^2} = 32(x(x-1) + y(y-1))$ for a 61x61 grid using Python3 with boundary conditions being $T=...
1
vote
1answer
71 views
-1
votes
1answer
113 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 ...
0
votes
0answers
134 views

Do the projection (with Jacobian) and marginalisation (inversion of matrix and remove a row/column and reinversion) commute?

I try to check the equality or the inequality between 2 Fisher matrices. The goal is too see if the projection (with Jacobian) and marginalisation (inversion of matrix and remove a row/column and ...
2
votes
1answer
192 views

Another way to evaluate the gravitational force from a uniform cube?

Appendix A of Liu, Baoyin, and Ma (2011) Equilibria, periodic orbits around equilibria, and heteroclinic connections in the gravity field of a rotating homogeneous cube shows an analytic expression ...
-1
votes
1answer
110 views

Fitting using curve_fit of scipy in python gives totally different answer for 1/t and t

I was trying to fit some data to a single degree exponential decay function but a*exp(-x*t) and a*exp(-x/t) gives completely ...
2
votes
1answer
80 views

Determining the voxels between two boundary surfaces

Issue description I am working on human brain tACS simulations where I have the models of the skin, skull, csf, brain and ventricles in STL format. The shape does not matter and there are no ...
0
votes
1answer
54 views

Trying to do a 2D random walk for a particle

I am trying to perform a two-dimensional random walk for a particle, but I am not getting the desired result. The plot produced is a linear plot, which obviously should not have been. Similar code for ...
1
vote
1answer
178 views

Translating the Euler code in scipy's solve_ivp

My code is based on the similarity transformation X=VZ.I simulate the model for transformed equations involving Z by replacing ...
0
votes
1answer
177 views

Pseudospectrum of non square Matrix in Python

I have a rectangular matrix $A \in \mathbb{R}^{m \times n}$ ...
0
votes
2answers
57 views

Passing in user inputs with mpirun

I am building a quantum circuit simulator using mpi4py. The simulator takes user inputs to build the initial system and quantum gates the system passes through. I can get the code to work if I do not ...
-2
votes
2answers
89 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{...
0
votes
1answer
152 views

Error using scipy.integrate.solve_ivp: index error: the index 0 is out of bounds for axis 0 with size 0

I am recently working on the code based on the stick-slip phenomenon in Python. It's the stick-slip oscillator (chapter 3.4) in https://www.sciencedirect.com/science/article/pii/S0888327020301205. And ...
-1
votes
1answer
334 views

Generate 3D "matrix" with Pandas, based on comparing two dataframes [Python]

Good morning everyone. I am working with Python and Pandas. I have two DataFrames, of the following type: ...

1
2 3 4 5
15