numerical

Efficient way to compute geometric mean of many numbers

走远了吗. 提交于 2019-11-28 20:16:25
I need to compute the geometric mean of a large set of numbers, whose values are not a priori limited. The naive way would be double geometric_mean(std::vector<double> const&data) // failure { auto product = 1.0; for(auto x:data) product *= x; return std::pow(product,1.0/data.size()); } However, this may well fail because of underflow or overflow in the accumulated product (note: long double doesn't really avoid this problem). So, the next option is to sum-up the logarithms: double geometric_mean(std::vector<double> const&data) { auto sumlog = 0.0; for(auto x:data) sum_log += std::log(x);

How do I type a floating point infinity literal in python

三世轮回 提交于 2019-11-28 19:03:26
How do I type a floating point infinity literal in python? I have heard inf = float('inf') is non portable. Thus, I have had the following recommended: inf = 1e400 Is either of these standard, or portable? What is best practice? In python 2.6 it is portable if the CPU supports it The float() function will now turn the string nan into an IEEE 754 Not A Number value, and +inf and -inf into positive or negative infinity. This works on any platform with IEEE 754 semantics. float('inf') is non portable as in not portable back to Python 2.5 when the string output varies between platforms. From 2.6

Extract contours from ContourPlot in Mathematica

早过忘川 提交于 2019-11-28 18:26:21
I have a function f(x,y) of two variables, of which I need to know the location of the curves at which it crosses zero. ContourPlot does that very efficiently (that is: it uses clever multi-grid methods, not just a brute force fine-grained scan) but just gives me a plot. I would like to have a set of values {x,y} (with some specified resolution) or perhaps some interpolating function which allows me to get access to the location of these contours. Have thought of extracting this from the FullForm of ContourPlot but this seems to be a bit of a hack. Any better way to do this? If you end up

How do I convert certain columns of a data frame to become factors? [duplicate]

北慕城南 提交于 2019-11-28 17:05:14
问题 This question already has an answer here : Closed 7 years ago . Possible Duplicate: identifying or coding unique factors using R I'm having some trouble with R. I have a data set similar to the following, but much longer. A B Pulse 1 2 23 2 2 24 2 2 12 2 3 25 1 1 65 1 3 45 Basically, the first 2 columns are coded. A has 1, 2 which represent 2 different weights. B has 1, 2, 3 which represent 3 different times. As they are coded numerical values, R will treat them as numerical variables. I need

How to detect significant change / trend in a time series data? [closed]

本秂侑毒 提交于 2019-11-28 16:14:06
So I have an array of say 25 samples and I would want to be able to note the trends of whether it's decreasing n or increasing from those 25 sample time interval(basically 25 samples array is my buffer that is being filled by every say 1 ms). Note that it is general trend that I am looking for, not the individual derivative(as I would have obtained using finite difference or other numerical differentiation techniques). Basically I expect my data to be noisy so there might be ups and downs even after doing filtering and so on. But it's the general trend of increasing or decreasing behaviour

Javascript to convert string to number?

為{幸葍}努か 提交于 2019-11-28 12:10:31
var str = '0.25'; How to convert the above to 0.25? There are several ways to achieve it: Using the unary plus operator: var n = +str; The Number constructor: var n = Number(str); The parseFloat function: var n = parseFloat(str); var num = Number(str); lincolnk var f = parseFloat(str); For your case, just use: var str = '0.25'; var num = +str; There are some ways to convert string to number in javascript. The best way: var num = +str; It's simple enough and work with both int and float num will be NaN if the str cannot be parsed to a valid number You also can: var num = Number(str); //without

Least Squares C# library [closed]

佐手、 提交于 2019-11-28 10:50:08
I am looking to perform a polynomial least squares regression and am looking for a C# library to do the calculations for me. I pass in the data points and the degree of polynomal (2nd order, 3rd order, etc) and it returns either the C0, C1, C2 etc. constant values or the calculated values "predictions". Note: I am using Least Squares to create some forecasting reports for disk usage, database size and table size. Here is a link for C# code on to do exactly this: http://www.trentfguidry.net/post/2009/08/01/Linear-Regression-of-Polynomial-Coefficients.aspx Good luck! Edit: Apparently the above

Fastest 128 bit integer library [closed]

北城以北 提交于 2019-11-28 07:38:07
I am working on a CPU-heavy numerical computation app. Without going into many details, it's a computational math research project that involves computing a certain function f(x) for large integer x. Right now everything is implemented in C++ in x64 mode, using native 64-bit ints. That limits me to x<2^64~1.8*10^19. I want to go further, to do that, I need a library that does 128-bit arithmetic. And it has to be very fast. In particular, integer divisions should be fast. Otherwise I'll be sitting here waiting for the results till Thanksgiving. And I'd rather not reinvent the wheel. I found a

SQL ORDER chars numerically

霸气de小男生 提交于 2019-11-28 07:11:55
I have a column of numbers stored as chars. When I do a ORDER BY for this column I get the following: 100 131 200 21 30 31000 etc. How can I order these chars numerically? Do I need to convert something or is there already an SQL command or function for this? Thank You. Try this: ORDER BY CAST(thecolumn AS int) This Worked for me: ORDER BY ABS(column_name) This is an issue with ordering numeric strings in a "natural sort" (if you lookup "natural sorting" on Google you'll find tons of stuff). Essentially casting the string as int and sorting on the resulting value should fix it. John The reason

Jacobi iteration doesn't end

╄→尐↘猪︶ㄣ 提交于 2019-11-28 01:43:02
问题 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);