numerical-methods

log-sum-exp trick why not recursive

蓝咒 提交于 2019-11-30 03:05:26
问题 I have been researching the log-sum-exp problem. I have a list of numbers stored as logarithms which I would like to sum and store in a logarithm. the naive algorithm is def naive(listOfLogs): return math.log10(sum(10**x for x in listOfLogs)) many websites including: logsumexp implementation in C? and http://machineintelligence.tumblr.com/post/4998477107/ recommend using def recommend(listOfLogs): maxLog = max(listOfLogs) return maxLog + math.log10(sum(10**(x-maxLog) for x in listOfLogs)) aka

Solving the Lorentz model using Runge Kutta 4th Order in Python without a package

筅森魡賤 提交于 2019-11-29 12:15:25
I wish to solve the Lorentz model in Python without the help of a package and my codes seems not to work to my expectation. I do not know why I am not getting the expected results and Lorentz attractor. The main problem I guess is related to how to store the various values for the solution of x,y and z respectively.Below are my codes for the Runge-Kutta 45 for the Lorentz model with 3D plot of solutions: import numpy as np import matplotlib.pyplot as plt #from scipy.integrate import odeint #a) Defining the Runge-Kutta45 method def fx(x,y,z,t): dxdt=sigma*(y-z) return dxdt def fy(x,y,z,t): dydt

What's the numerically best way to calculate the average

戏子无情 提交于 2019-11-29 11:49:34
问题 what's the best way to calculate the average? With this question I want to know which algorithm for calculating the average is the best in a numerical sense. It should have the least rounding errors, should not be sensitive to over- or underflows and so on. Thank you. Additional information: incremental approaches preferred since the number of values may not fit into RAM (several parallel calculations on files larger than 4 GB). 回答1: You can have a look at http://citeseer.ist.psu.edu/viewdoc

Power Method in MATLAB

末鹿安然 提交于 2019-11-29 11:21:47
I would like to implement the Power Method for determining the dominant eigenvalue and eigenvector of a matrix in MATLAB. Here's what I wrote so far: %function to implement power method to compute dominant %eigenvalue/eigenevctor function [m,y_final]=power_method(A,x); m=0; n=length(x); y_final=zeros(n,1); y_final=x; tol=1e-3; while(1) mold=m; y_final=A*y_final; m=max(y_final); y_final=y_final/m; if (m-mold)<tol break; end end end With the above code, here is a numerical example: A=[1 1 -2;-1 2 1; 0 1 -1] A = 1 1 -2 -1 2 1 0 1 -1 >> x=[1 1 1]; >> x=x'; >> [m,y_final]=power_method(A,x); >> A*x

How to calculate machine epsilon in MATLAB?

穿精又带淫゛_ 提交于 2019-11-29 11:00:29
I need to find the machine epsilon and I am doing the following: eps = 1; while 1.0 + eps > 1.0 do eps = eps /2; end However, it shows me this: Undefined function or variable 'do'. Error in epsilon (line 3) while 1.0 + eps > 1.0 do What should I do? rayryeng First and foremost, there is no such thing as a do keyword in MATLAB, so eliminate that from your code. Also, don't use eps as an actual variable. This is a pre-defined function in MATLAB that calculates machine epsilon , which is also what you are trying to calculate. By creating a variable called eps , you would shadow over the actual

Jacobi iteration doesn't end

早过忘川 提交于 2019-11-29 08:21:59
I'm trying to implement the Jacobi iteration in MATLAB but am unable to get it to converge. I have looked online and elsewhere for working code for comparison but am unable to find any that is something similar to my code and still works. Here is what I have: function x = Jacobi(A,b,tol,maxiter) n = size(A,1); xp = zeros(n,1); x = zeros(n,1); k=0; % number of steps while(k<=maxiter) k=k+1; for i=1:n xp(i) = 1/A(i,i)*(b(i) - A(i,1:i-1)*x(1:i-1) - A(i,i+1:n)*x(i+1:n)); end err = norm(A*xp-b); if(err<tol) x=xp; break; end x=xp; end This just blows up no matter what A and b I use. It's probably a

Generating digits of square root of 2

穿精又带淫゛_ 提交于 2019-11-29 02:57:06
问题 I want to generate the digits of the square root of two to 3 million digits. I am aware of Newton-Raphson but I don't have much clue how to implement it in C or C++ due to lack of biginteger support. Can somebody point me in the right direction? Also, if anybody knows how to do it in python (I'm a beginner), I would also appreciate it. 回答1: You could try using the mapping: a/b -> (a+2b)/(a+b) starting with a= 1, b= 1 . This converges to sqrt(2) (in fact gives the continued fraction

Numerical ODE solving in Python

假如想象 提交于 2019-11-29 02:47:05
问题 How do I numerically solve an ODE in Python? Consider \ddot{u}(\phi) = -u + \sqrt{u} with the following conditions u(0) = 1.49907 and \dot{u}(0) = 0 with the constraint 0 <= \phi <= 7\pi. Then finally, I want to produce a parametric plot where the x and y coordinates are generated as a function of u. The problem is, I need to run odeint twice since this is a second order differential equation. I tried having it run again after the first time but it comes back with a Jacobian error. There must

sparse matrix library for C++ [closed]

坚强是说给别人听的谎言 提交于 2019-11-29 00:12:39
Is there any sparse matrix library that can do these: solve linear algebraic equations support operations like matrix-matrix/number multiplication/addition/subtraction,matrix transposition, get a row/column of a matrix,and so on matrix size could be 40k*40k or bigger,like 250k*250k fast can be used in Windows Can someone recommend some libraries for me? If you recommend, please tell me the advantages and disadvantages of it, and the reason why you recommend it. By the way,I have searched many sparse matrix libraries on the internet and tested some of them. I found that each of them only

sample random point in triangle [closed]

一笑奈何 提交于 2019-11-28 07:39:33
Suppose you have an arbitrary triangle with vertices A , B , and C . This paper (section 4.2) says that you can generate a random point, P , uniformly from within triangle ABC by the following convex combination of the vertices: P = (1 - sqrt(r1)) * A + (sqrt(r1) * (1 - r2)) * B + (sqrt(r1) * r2) * C where r1 and r2 are uniformly drawn from [0, 1] , and sqrt is the square root function. How do you justify that the sampled points that are uniformly distributed within triangle ABC ? EDIT As pointed out in a comment on the mathoverflow question , Graphical Gems discusses this algorithm . You have