Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [python]

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

1
vote
0answers
51 views

Object-oriented non-linear solving in python

I'd like to build a system in Python, consisting of (broadly speaking) objects which are internally described with (not necessarily just linear) equations, that I can connect with each other - similar ...
3
votes
2answers
2k 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 ...
0
votes
1answer
96 views

How much in depth should I cover Python? [closed]

I'm an undergrad who's studying electronics and fields like data sciences and mostly computing seem most interesting to me but have in-depth knowledge in neither. I learnt basic python last winter ...
2
votes
0answers
146 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 ...
0
votes
1answer
467 views

How can this multidimensional integral be efficiently implemented in python using Gauss-Hermite quadrature

I'm playing around with dynamic programming and need to calculate a multidimensional integral $E[V(W)]$ where we assume $W$ has a log normal distribution. I was looking at the following example in ...
-1
votes
1answer
1k views

Using scipy.odeint to solve coupled equations [closed]

I have a set of three coupled autonomous equations: ${y_{1}}\prime = y_{1}(\frac{\Omega_{m}}{y_{1}^3} + \frac{y_{3}^2}{6.0} + \frac{V(y_{2})}{2.H_{0}^2})$ $y_{2}\prime = y_{3}$ $y_{3}\prime = -3\frac{...
3
votes
1answer
226 views

Imposing special structure on Positive Semi-Definite matrix

I am trying to implement the algorithm described in reference 1 using cvxpy. However I am struggling to constrain the matrix $Z_j$ as described in equations (33-35)....
0
votes
1answer
117 views

ODE System doesn't work when step size (h) is bigger than 1

I am a beginner to Python. Currently I'm writing a code for developing a simple solver for non-linear ODE systems with initial value. The equations of the system are The function of myu is evaluated ...
6
votes
1answer
335 views

Correct eigenfunctions of Laplace operator by Finite Differences

I am trying to compute the eigenfunctions of the Laplace operator, i.e. finding $u$ in $$ -\nabla^2 u = \lambda u .$$ For now I am trying to do this in 1D, so $$ \nabla^2 = \partial_{xx} .$$ I am ...
0
votes
1answer
1k views

Computing spherical harmonic coefficients using Scipy

The scipy.special.sph_harm function evaluates a spherical harmonic function at a point. Does Scipy provide any functions to compute the spherical harmonic ...
1
vote
0answers
364 views

Polynomial approximation - Vandermonde matrix creation - precision

I am trying to fit a polynomial through 340 points in a 3D space, i.e. $$f(x,y,z) = k$$ I asked previously about the theory behind polynomial interpolation here -> Polynomial interpolation Few ...
1
vote
1answer
494 views

Confusion about determining the jacobian in a rootfinding algorithm

I have written some Python code to determine the numerical roots of the following non-linear equation: $$f_m=\tan\lambda_m - \frac{\lambda_m}{1+a}$$ where $\lambda_m\gt0$ and $a\geq0$. The code is: <...
1
vote
1answer
95 views

triple cross prouct of tensor

Im trying to compute a triple cross product of vectors a,b, and c in real space and integrate over the entire space. The result is a term in the hamiltonian for an electronic system so there are ...
1
vote
0answers
781 views

Python program to simulate the trajectory of a cannonball

I am new into programming. Here, I am trying to simulate the motion of a cannonball until it hits the ground using python programming. I have used both analytical and numerical(Euler method) to plot ...
0
votes
1answer
63 views

Speeding up group apply in python

In my code it often happens that I need to calculate values for a group. For example, suppose there is the following data: ...
1
vote
0answers
46 views

Implementing Frechet Distance on pandas data frame [duplicate]

I am working on a trajectory analysis project using python and its data science related libraries. I am planning to implement Frechet Distance algorithm for trajectory analysis, each trajectory has ...
1
vote
0answers
27 views

Simulation of a lens, insufficient points

I am simulating the propagation of a light pulse using the equation $$\frac{\partial}{\partial z}A=\frac{1}{2\cdot k_0}\nabla^2_rA$$ with $$k_0=\frac{2\pi}{\lambda_0}$$ The propagation with a step ...
0
votes
2answers
2k views

Python implementation for Frechet Distance

I am working on a trajectory analysis project using python and its data science related libraries. I am planning to implement Frechet Distance algorithm for trajectory analysis, each trajectory has ...
2
votes
0answers
183 views

minimalistic library for classical molecular mechanics force-field in C/C++/Python

I would like some classical molecular mechanics forcefield library or super-simple program which does not have any external dependences, so that I can plug it into my software without worry of "...
2
votes
0answers
779 views

Using RK2 Method to solve the simple harmonic oscillator of a horizontal mass on a spring (1D)

Being new to numerical analysis techniques, in particular RK2, I decided the best way to jump in is by using python to solve the well known mass-spring oscillator using RK2 techniques. My problem is ...
0
votes
1answer
299 views

Python implementation of Quadtree for trajectory analysis

I am working on a trajectory analysis project using python and its data science related libraries. I am planning to implement Quadtrees for for trajectory analysis, each trajectory has sequence of (x,...
2
votes
2answers
1k views

Plotting Voigt Function in Python

I've been trying to plot the following function in Python: $H(a,u) = \frac{a}{\pi} \int_{-\infty}^{\infty}\frac{exp(-y^2)}{a^2 + (u - y)^2}dy $ But I keep receiving the following error: ...
10
votes
2answers
392 views

Eigenvectors of a small norm adjustment

I have a dataset that is slowly changing, and I need to keep track of eigenvectors/eigenvalues of its covariance matrix. I've been using scipy.linalg.eigh, but it'...
3
votes
1answer
246 views

Translating a nuclear norm constraint to an LMI constraint

I'm attempting to solve a convex optimization problem where one of the constraints is $$\|M\|_* \leq a$$ where $\|M\|_*$ denotes the nuclear norm of matrix $M$. I'm using CVXOPT in Python to solve ...
3
votes
2answers
1k views

Small errors accumulate while solving ODE of motion

I'm trying to solve the ODE of motion: $$ \begin{align} x''=&\ -myu \frac{x}{r^3} \left(1+\frac{3}{2} {J_2} \left(\frac{{r_{\text{eq}}}}{r}\right)^2 \left(1-\frac{5z^2}{r^2}\right)\right) \\ ...
5
votes
2answers
215 views

Speeding up a linear transform using Python

In an optical wavefront propagation problem, I need to do excessive Fourier-type computations: ...
0
votes
0answers
572 views

Tridiagonal Solver in Python

I have a code I'm working on that involves solving a 1D Schrodinger equation using a Crank-Nicolson time step. The code is written in NumPy/SciPy, and I was doing a bit of profiling and discovered ...
2
votes
1answer
115 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 ...
0
votes
2answers
118 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 ...
1
vote
0answers
277 views

How to use “fill_factor” in spilu (scipy) [closed]

I am using spilu (incomplete LU decomposition for sparse matrix) in scipy: https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.sparse.linalg.spilu.html However there is an option call "...
2
votes
1answer
305 views

How to broadcast my matrix exponentiation

I'm trying -to no avail- to broadcast the following piece of code in Python: ...
3
votes
4answers
1k views

Practical coding resources for new CFD practicioners

There seem to be many books and papers that explain various CFD methods in great detail, but unfortunately I have not been able to find many good resources of such methods implemented in codes such as ...
1
vote
1answer
59 views

Plotting the same function multiple times on the same set of axes but with different parameters [Python]

I am currently trying to plot a function which describes linear perturbation growth in cosmology for different world models. I would like to be able to have all the curves on the same set of axes, but ...
1
vote
0answers
147 views

How to test for convergence (smoothness of Pareto front) in DEAP

In the DEAP algorithms (see documentation here), I notice that we need to specify the the number of generations (NGEN). I was advised that convergence has been achieved if the Pareto curve is smooth. ...
3
votes
1answer
566 views

1-D incompressible unsteady Couette Flow Explicit finite differece CFD

I am currently following J.Anderson Jr.'s CFD with basic application and I came into some troubles while coding for my very first CFD problem. As the title suggests I am solving an incompressible ...
0
votes
1answer
466 views

What is wrong with this Euler method code in python?

I am testing the ability of my integration method to reproduce results obtained analytically (for various values of the parameters), but the results turn out to be completely wrong and I can't find ...
2
votes
2answers
930 views

stop condition in scipy.integrate.ode for stiff system

I'm using Python scipy.integrate.ode, and I want to stop my integration at a certain condition. So I use the integrator "dopri5" and use the method "set_solout" to specify a function for my stop ...
1
vote
0answers
731 views

Solving an integral equation in Python

I have to solve the following equation for $x(i), 0 \leq i \leq 1$: $$ y(i) = x(i)^{-a} \int_0^1 y(j)x(j) dj \left(\int_0^1 \mathcal A(j) x(j)^{1-a} dj\right)^{-1} \int_i^1 \left( \int_0^x x(j)^{1-a} ...
1
vote
0answers
716 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 ...
1
vote
1answer
188 views

Efficiently generate a random subgraph (Gs) with maximum degree K, using only edges from an existing graph G

I am looking find a way of efficiently generating a random, undirected subgraph $G_s$ with $N$ vertices, using a subset of edges from an exisiting undirected graph $G$, also of size $N$, where the ...
1
vote
1answer
167 views

Generate paraview plot of an ensight .case without launching the paraview gui

Disclaimer : This question is related to computational science, but it is a purely a post-processing question so if this is on the wrong board, please tell me I would like to postprocess simulation ...
1
vote
0answers
98 views

Integral over reference element in $1$D FEM: how to map the quadrature points?

The following is related to a question a asked a few days back 1, but now I would like to focus on just one part of the problem. I have problems computing the integral over the reference element: $$ ...
3
votes
2answers
172 views

Optimization of known function with respect to two unknown function arguments

I have a data set, composed of points $(x_i, y_i)$ for $i=1,N$. I also have a known function $F$, which maps these points $x_i$ to $y_i$ as such $F(x_i, a(x_i),b(x_i)) = y_i$, where $a(x_i)$ and $b(...
1
vote
0answers
77 views

vectorizing optimization or root finding [closed]

I need to find the roots of a function. I am currently using scipy.optimize.fsolve ...
1
vote
0answers
160 views

Lambdify error when sympy array contains number

This example is a dummy example, but it describes exactly whats goes on in my problem. As a result from some sympy calculation and manipulation i got two arrays ...
3
votes
1answer
705 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
0answers
95 views

vectorizating function integration scipy python

I need to implement the following in python: For a given discrete time series Zt (t=0 to T), find smallest t such that: $c\sum_{s=0}^t e^{[k(Zt-Zs)+m(t-s)]} >= \frac{p*}{1-p*} $ where c,k,m are ...
0
votes
1answer
584 views

Python - Differentiating Cubic Spline numerically or analytically

I'm trying to take data in numpy arrays, interpolate them and retrieve this function to differentiate/integrate at different stages. Here is how I got here, data included: ...