473,394 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

how to compute roots of a cubic function with minimal truncationerrors?

Hi,

I have the following program which computes roots of a cubic function.
The solution is sensitive to the type, which is due to the truncation
error. 'long double T' gives three solutions, and 'typedef double T'
gives one solutions. The correct number of solutions should be two, 1
and 2.

I know there is some trick to reduce the chance of under or overflow.
For example, std::abs(z) shall be implemented as
|x| * sqrt(1 + (y/x)*(y/x)) if |x|>|y|
and
|y| * sqrt(1 + (x/y)*(x/y)) if |y|>|x|,
where z is a complex number, and x and y are its real and complex
parts.

I'm wondering what trick can be played to reduce the truncation error
when solving the cubic polynomial equations.

BTW, I use the algorithm is shown at http://www.hawaii.edu/suremath/jrootsCubic.html

Thanks,
Peng

#include <vector>
#include <complex>
#include <cmath>
#include <iostream>

template <typename T>
std::vector<Troots_of_cubic_function(const T &a2, const T &a1, const
T &a0) {

const T one = static_cast<T>(1);
const T three = static_cast<T>(3);

T q = one / 3 * a1 - one / 9 * a2 * a2;
T r = one / 6 * (a1 * a2 - three * a0) - one / 27 * std::pow(a2, 3);

T Delta = std::pow(q, 3) + r * r;
std::cout << "Delta = " << Delta << std::endl;

std::vector<Tv;
if(Delta >= T()) {
T s1 = std::pow(r + sqrt(Delta), one/3);
T s2 = std::pow(r - sqrt(Delta), one/3);
v.push_back(s1 + s2 - a2 / 3);
if(Delta == T()) {
v.push_back(-.5 * (s1 + s2) - a2 / 3);
}
}
else {
std::complex<Ttemp = sqrt(std::complex<T>(Delta));
std::complex<Ts1 = std::pow(r + temp, one/3);
std::complex<Ts2 = std::pow(r - temp, one/3);
const T minus_half = - static_cast<T>(1)/2;
v.push_back((s1 + s2 - a2 / 3).real());
v.push_back((minus_half * (s1 + s2) - a2 / 3 + std::complex<T>(0,
sqrt(three)/2) * (s1 - s2)).real());
v.push_back((minus_half * (s1 + s2) - a2 / 3 - std::complex<T>(0,
sqrt(three)/2) * (s1 - s2)).real());
}
return v;
}

int main () {
//typedef long double T;
typedef double T;
const T a2 = -4;
const T a1 = 5;
const T a0 = -2;

std::vector<Tv = roots_of_cubic_function<T>(a2, a1, a0);

std::cout << "Solutions:" << std::endl;
for(std::vector<T>::const_iterator it = v.begin(); it != v.end(); ++
it) {
T x = *it;
T f = ((x + a2) * x + a1) * x + a0;
std::cout << x << " " << f << std::endl;
}
}
Sep 10 '08 #1
2 3110
[Hi Peng, followup to topical group sci.math.num-analysis]

From: Peng Yu <Pe*******@gmail.com>
Date: Tue, 9 Sep 2008 21:24:10 -0700 (PDT)

Hi,

I have the following program which computes roots of a cubic
function.
The solution is sensitive to the type, which is due to the truncation
error. 'long double T' gives three solutions, and 'typedef double T'
gives one solutions. The correct number of solutions should be two, 1
and 2.

I know there is some trick to reduce the chance of under or overflow.
For example, std::abs(z) shall be implemented as
|x| * sqrt(1 + (y/x)*(y/x)) if |x|>|y|
and
|y| * sqrt(1 + (x/y)*(x/y)) if |y|>|x|,
where z is a complex number, and x and y are its real and complex
parts.

I'm wondering what trick can be played to reduce the truncation error
when solving the cubic polynomial equations.

BTW, I use the algorithm is shown at
http://www.hawaii.edu/suremath/jrootsCubic.html

Thanks,
Peng

#include <vector>
#include <complex>
#include <cmath>
#include <iostream>

template <typename T>
std::vector<Troots_of_cubic_function(const T &a2, const T &a1,
const
T &a0) {

const T one = static_cast<T>(1);
const T three = static_cast<T>(3);

T q = one / 3 * a1 - one / 9 * a2 * a2;
T r = one / 6 * (a1 * a2 - three * a0) - one / 27 * std::pow(a2,
3);

T Delta = std::pow(q, 3) + r * r;
std::cout << "Delta = " << Delta << std::endl;

std::vector<Tv;
if(Delta >= T()) {
T s1 = std::pow(r + sqrt(Delta), one/3);
T s2 = std::pow(r - sqrt(Delta), one/3);
v.push_back(s1 + s2 - a2 / 3);
if(Delta == T()) {
v.push_back(-.5 * (s1 + s2) - a2 / 3);
}
}
else {
std::complex<Ttemp = sqrt(std::complex<T>(Delta));
std::complex<Ts1 = std::pow(r + temp, one/3);
std::complex<Ts2 = std::pow(r - temp, one/3);
const T minus_half = - static_cast<T>(1)/2;
v.push_back((s1 + s2 - a2 / 3).real());
v.push_back((minus_half * (s1 + s2) - a2 / 3 + std::complex<T>(0,
sqrt(three)/2) * (s1 - s2)).real());
v.push_back((minus_half * (s1 + s2) - a2 / 3 - std::complex<T>(0,
sqrt(three)/2) * (s1 - s2)).real());
}
return v;
}

int main () {
//typedef long double T;
typedef double T;
const T a2 = -4;
const T a1 = 5;
const T a0 = -2;

std::vector<Tv = roots_of_cubic_function<T>(a2, a1, a0);

std::cout << "Solutions:" << std::endl;
for(std::vector<T>::const_iterator it = v.begin(); it != v.end();
++
it) {
T x = *it;
T f = ((x + a2) * x + a1) * x + a0;
std::cout << x << " " << f << std::endl;
}
}
--
Quidquid latine scriptum est, altum videtur.
Sep 10 '08 #2
>>>>"Peng" == Peng Yu <Pe*******@gmail.comwrites:

PengHi,
PengI have the following program which computes roots of a cubic function.
PengThe solution is sensitive to the type, which is due to the truncation
Pengerror. 'long double T' gives three solutions, and 'typedef double T'
Penggives one solutions. The correct number of solutions should be two, 1
Pengand 2.

PengI know there is some trick to reduce the chance of under or overflow.
PengFor example, std::abs(z) shall be implemented as
Peng|x| * sqrt(1 + (y/x)*(y/x)) if |x|>|y|
Pengand
Peng|y| * sqrt(1 + (x/y)*(x/y)) if |y|>|x|,
Pengwhere z is a complex number, and x and y are its real and complex
Pengparts.

PengI'm wondering what trick can be played to reduce the truncation error
Pengwhen solving the cubic polynomial equations.

PengBTW, I use the algorithm is shown at http://www.hawaii.edu/suremath/jrootsCubic.html

Look at the formula for s1 and s2. You might want to factor the r out
from sqrt(q^3+r^2) to abs(r)*sqrt(1+q^3/r^2). This might give you
better accuracy for s1 and s2.

Or choose one of the real roots, use Newton iteration to refine it,
and then solve the resulting quadratic separately. This requires a
bit of care too.

Ray
Sep 12 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Philip KOCH | last post by:
I'm trying to calculate the cubic root of a fonction, what syntax should I use? Thanx PHIL
5
by: Justin Caldicott | last post by:
Hi There are n solutions to the nth root of any number, eg: (-8)^(1/3) = 1 + sqrt(3)i or -2 or 1 - sqrt(3)i The following code:
8
by: JKop | last post by:
Could someone please point me to somewhere where I can get a full complete reference of the C++ Standard Libaries, how to use all their functions and global varibles. Is there any function in...
1
by: wildbill | last post by:
I'm looking for an example of how to compute an age of a record in a query and then display that on a form. For example: I have record that shows it was entered on 8/2/2005. I would like to be...
3
by: Carl Johansen | last post by:
I have a big ASP website (used by several thousand car dealers) that is a collection of lots of small and medium-sized applications. Now I want to start adding ASP.NET applications to it. I have...
2
by: =?ISO-8859-1?Q?Sch=FCle_Daniel?= | last post by:
Hello NG, given this call to roots funtion from pylab In : roots() Out: array() as far as I understand it stands for a0+a1*x+a2*x^2 in the above case it yields 2x^2+2x = 2x(1+x) and the...
1
by: candacefaye1 | last post by:
1. write a C++ program to decide if the coefficients of a quadratic equation have real roots. The three choices will be to write the message “zero divide” when A is zero, write the message “no real...
4
by: Peng Yu | last post by:
Hi, I'm wondering how to get the cubic root for a complex number? It seems that cbrt does not work complex numbers. Thanks, Peng #include <complex> #include <iostream>
10
by: Matthias | last post by:
Dear newsgroup. I want to write a template function which accepts either integer or floating point numbers. If a certain result is not a whole number and if the template parameter is an...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.