numerical

How to find coefficients of polynomial equation?

烈酒焚心 提交于 2021-01-28 07:54:54
问题 Given two points in the x, y plane: x, f(x) 1, 3 2, 5 I can interpolate them using Lagrange and find f(1.5) , which result in 4 . Thinking a little I managed to find a way to discover the coefficients of the equation: void l1Coefficients(const vector<double> &x, const vector<double> &y) { double a0 = y[0]/(x[0]-x[1]); double a1 = y[1]/(x[1]-x[0]); double b0 = (-x[1]*y[0])/(x[0]-x[1]); double b1 = (-x[0]*y[1])/(x[1]-x[0]); double a = a0 + a1; double b = b0 + b1; cout << "P1(x) = " << a << "x +

Convert numbers into corresponding letter using Python

本小妞迷上赌 提交于 2020-04-08 09:41:53
问题 I was wondering if it is possible to convert numbers into their corresponding alphabetical value. So 1 -> a 2 -> b I was planning to make a program which lists all the alphabetical combinations possible for a length specified by a user. See I know how to build the rest of the program except this! Any help would be wonderful. 回答1: import string for x, y in zip(range(1, 27), string.ascii_lowercase): print(x, y) or import string for x, y in enumerate(string.ascii_lowercase, 1): print(x, y) or

Perl: Numerical sort of arrays in a hash

♀尐吖头ヾ 提交于 2020-02-04 04:14:25
问题 I have a hash of arrays, and I need to sort it first on the keys, and then on the values in the array. Here's my simple code: my %myhash; $line1 = "col1 0.999"; $line2 = "col2 0.899"; $line3 = "col2 -0.52"; $line4 = "col2 1.52"; #insert into hash @cols = split(" ", $line1); push @{ $myhash{$cols[0]} }, $line1; @cols = split(" ", $line2); push @{ $myhash{$cols[0]} }, $line2; @cols = split(" ", $line3); push @{ $myhash{$cols[0]} }, $line3; @cols = split(" ", $line4); push @{ $myhash{$cols[0]} }

Approximation error when using sqrt and floor

荒凉一梦 提交于 2020-01-24 10:56:05
问题 I have to do an enumeration of solutions of an equation and I know that y < x *( sqrt(n) - 1 ) , where x , y and n are integers. My naive approach would be to look for y less or equal than floor( x * ( sqrt( (float)n ) - 1 ) ) . Should I be worried about approximation error? For example, if my expression is a little be greater than an integer m , should I be worried to get m-1 at the end? How could I detect such errors? 回答1: You should definitely be worried about approximation error, but how

Dealing with floating point errors in .NET

假装没事ソ 提交于 2020-01-20 04:56:29
问题 I'm working on a scientific computation & visualization project in C#/.NET, and we use double s to represent all the physical quantities. Since floating-point numbers always include a bit of rounding, we have simple methods to do equality comparisons, such as: static double EPSILON = 1e-6; bool ApproxEquals(double d1, double d2) { return Math.Abs(d1 - d2) < EPSILON; } Pretty standard. However, we constantly find ourselves having to adjust the magnitude of EPSILON as we encounter situations in

How do I calculate radius of curvature from discrete samples?

久未见 提交于 2020-01-16 14:33:30
问题 I've got a series of (x,y) samples from a plane curve, taken from real measurements, so presumably a bit noisy and not evenly spaced in time. x = -2.51509 -2.38485 -1.88485 -1.38485 -0.88485 -0.38485 0.11515 0.61515 1.11515 1.61515 ... y = -48.902 -48.917 -48.955 -48.981 -49.001 -49.014 -49.015 -49.010 -49.001 -48.974 ... If I plot the whole series, it looks like a nice oval, but if I look closely, the line looks a bit wiggly, which is presumably the noise. How would I go about extracting an

MATLAB's “SIGMA must be symmetric and positive definite” error sometimes not making sense

大憨熊 提交于 2020-01-13 04:33:30
问题 I am not sure if this is a programming or statistics question, but I am %99 sure that there should be a numerical problem. So maybe a programmatic solution can be proposed. I am using MATLAB's mvnpdf function to calculate multi-variate Gaussian PDF of some observations. Frequently I get "SIGMA must be symmetric and positive definite" errors. However, I am obtaining the covarince matrix from the data, so the data should be legal. A code to regenerate the problem is: err_cnt = 0; for i = 1:1000

Best way to check if double equals negative infinity in C++

廉价感情. 提交于 2020-01-11 06:42:26
问题 I found this: http://en.cppreference.com/w/cpp/numeric/math/isinf but it appears to check for either positive or negative infinity. I just want to check if a value is equal to exactly negative infinity, or in otherwords is log(0) Thanks for answer! Based on response below, here is some code that shows what works. #include <iostream> #include <cmath> #include <math.h> using namespace std; int main() { double c = std::log(0.0); auto result = c == - INFINITY; cout << result << endl; return 0; }

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

二次信任 提交于 2020-01-09 12:18:53
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 2 years ago . 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

Sort numbers with letters numerically and alphabetically

让人想犯罪 __ 提交于 2020-01-02 05:30:08
问题 I have a Database on my website with a Long List of #'s (Product #'s) all containing letters (Exp. TC-345, TC-234 or HC-236W 123-234-PWD...) Can we numerically and alphabetically sort the #'s on the website? Currently we store it alphabetically, so the order is (10-PDW, 100-PDW, 110-PDW 2-PDW) We would like to change it into (2-PDW, 10-PDW, 100-PDW, 110-PDW) My developers say "The colorway number can never be sorted numerically. We would need to add another numeric field to the database for