numerical-methods

Using boost geometry to check if two lines have an intersection

心不动则不痛 提交于 2019-12-07 04:08:31
问题 Is it possible to use boost::geometry to check whether two line segments (each given by two points in 2D) intersect each other? If this is possible, does boost::geometry allow to check also for special cases such as that only one point is (numerically) on the other line, or that both lines are equal? 回答1: If you are talking specifically about Boost.Geometry API to it is, of course, possible. Your code should look roughly like this #include <boost/geometry/geometries/segment.hpp> #include

Problem with arithmetic using logarithms to avoid numerical underflow (take 2)

别等时光非礼了梦想. 提交于 2019-12-07 02:54:49
问题 I have two lists of fractions; say A = [ 1/212, 5/212, 3/212, ... ] and B = [ 4/143, 7/143, 2/143, ... ] . If we define A' = a[0] * a[1] * a[2] * ... and B' = b[0] * b[1] * b[2] * ... I want to calculate a normalised value of A' and B' ie specifically the values of A' / (A'+B') and B' / (A'+B') My trouble is A are B are both quite long and each value is small so calculating the product causes numerical underflow very quickly... I understand turning the product into a sum through logarithms

Algorithm of boost::math::erf

一曲冷凌霜 提交于 2019-12-07 02:17:10
问题 are there any details available for the algorithm behind the erf-function of boost? The documentation of the module is not very precise. All I found out is that several methods are mixed. For me it looks like variations of Abramowitz and Stegun. Which methods are mixed? How are the methods mixed? What is the complexity of the erf-function (constant time)? Sebastian 回答1: The docs for Boost Math Toolkit has a long list of references, among which Abramowitz and Stegun. The erf-function interface

Numerical grouping using matlab / octave and not repeating values found in main array

懵懂的女人 提交于 2019-12-06 21:44:34
I have about 100,000 numbers that I would like to group together based on dividing by two or increments of two. PS: The increment values may change and the values found in the main array "x" can only be used once. I'm not sure how to check and stop the loop if a number in the "array_all" array has been repeated from the "x" array. See example below Example: x=[9,8,7,6,5,4,3,2,1] I'm trying to get the array_all array to look like this: array_all= [ 9.00000 4.50000 2.25000 8.00000 4.00000 2.00000 7.00000 3.50000 1.75000 6.00000 3.00000 1.50000 5.00000 2.50000 1.25000 1.00000 0.50000 0.25000] and

Solve a particular linear system efficiently in julia

痴心易碎 提交于 2019-12-06 09:01:10
I use extensively the julia's linear equation solver res = X\b . I have to use it millions of times in my program because of parameter variation. This was working ok because I was using small dimensions (up to 30 ). Now that I want to analyse bigger systems, up to 1000 , the linear solver is no longer efficient. I think there can be a work around. However I must say that sometimes my X matrix is dense, and sometimes is sparse, so I need something that works fine for both cases. The b vector is a vector with all zeroes, except for one entry which is always 1 (actually it is always the last

Fastest numerical solution of a real cubic polynomial?

懵懂的女人 提交于 2019-12-06 07:59:25
R question: Looking for the fastest way to NUMERICALLY solve a bunch of arbitrary cubics known to have real coeffs and three real roots. The polyroot function in R is reported to use Jenkins-Traub's algorithm 419 for complex polynomials, but for real polynomials the authors refer to their earlier work. What are the faster options for a real cubic, or more generally for a real polynomial? Fleshing out Arietta's answer above: > a <- c(1,3,-4) > m <- matrix(c(0,0,-a[1],1,0,-a[2],0,1,-a[3]), byrow=T, nrow=3) > roots <- eigen(m, symm=F, only.values=T)$values Whether this is faster or slower than

Using scipy.quad with iε trick: Bad results

眉间皱痕 提交于 2019-12-06 07:35:48
In order to circumvent the cauchy principle value, I tried to integrate an integral using a small shift iε into the complex plane to evade the pole. However, as can be inferred from the figure below, the result is pretty bad. The code for this result is shown below. Do you have ideas how to improve this method? Why is it not working? I already tried changing ε or the limit in the integral. Edit: I included the method "cauchy" with the principle value, which seems not to work at all. import matplotlib.pyplot as plt from scipy.integrate import quad import numpy as np def cquad(func, a, b, *

Statsmodels logistic regression convergence problems

谁都会走 提交于 2019-12-06 05:21:55
I'm trying to run a logistic regression in statsmodels on a large design matrix (~200 columns). The features include a number of interactions, categorical features and semi-sparse (70%) integer features. Although my design matrix is not actually ill-conditioned, it seems to be somewhat close (according to numpy.linalg.matrix_rank , it is full-rank with tol=1e-3 but not with tol=1e-2 ). As a result, I'm struggling to get logistic regression to converge with any of the methods in statsmodels. Here's what I've tried so far: method='newton' : Did not converge after 1000 iterations; raised a

Computing a matrix which transforms a quadrangle to another quadrangle in 2D

醉酒当歌 提交于 2019-12-06 02:36:47
问题 In the figure below the goal is to compute the homography matrix H which transforms the points a1 a2 a3 a4 to their counterparts b1 b2 b3 b4. That is: [b1 b2 b3 b4] = H * [a1 a2 a3 a4] What way would you suggest to be the best way to calculate H(3x3). a1...b4 are points in 2D which are represented in homogeneous coordinate systems ( that is [a1_x a1_y 1]', ...). EDIT : For these types of problems we use SVD, So i would like to see how this can be simply done in Matlab. EDIT : Here is how I

Finding complex roots from set of non-linear equations in python

旧街凉风 提交于 2019-12-06 01:30:06
问题 I have been testing an algorithm that has been published in literature that involves solving a set of 'm' non-linear equations in both Matlab and Python. The set of non-linear equations involves input variables that contain complex numbers, and therefore the resulting solutions should also be complex. As of now, I have been able to get pretty good results in Matlab by using the following lines of code: lambdas0 = ones(1,m)*1e-5; options = optimset('Algorithm','levenberg-marquardt',...