473,624 Members | 2,026 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to compute roots of a cubic function with minimal truncationerror s?

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<Tro ots_of_cubic_fu nction(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<Tt emp = sqrt(std::compl ex<T>(Delta));
std::complex<Ts 1 = std::pow(r + temp, one/3);
std::complex<Ts 2 = 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((mi nus_half * (s1 + s2) - a2 / 3 + std::complex<T> (0,
sqrt(three)/2) * (s1 - s2)).real());
v.push_back((mi nus_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_iter ator 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 3132
[Hi Peng, followup to topical group sci.math.num-analysis]

From: Peng Yu <Pe*******@gmai l.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<Tro ots_of_cubic_fu nction(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<Tt emp = sqrt(std::compl ex<T>(Delta));
std::complex<Ts 1 = std::pow(r + temp, one/3);
std::complex<Ts 2 = 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((mi nus_half * (s1 + s2) - a2 / 3 + std::complex<T> (0,
sqrt(three)/2) * (s1 - s2)).real());
v.push_back((mi nus_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_iter ator 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*******@gmai l.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
3517
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
3204
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
5594
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 the Stdlib for getting the cubic root of a number? Thanks,
1
5548
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 able to have the query return something like "record is 35 days old". I know I can compute the age via the DateDiff function in VBA but can't seem to figure out how to change the value in an unbound control on the form as I scroll through the...
3
2205
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 read Q307467 (How To Create an ASP.NET Application from Multiple Projects for Team Development) and it seems to work well. I understand that when you create a web project in VS.NET, it creates an IIS application root, and that you can remove this...
2
2028
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 roots are 0 and -1
1
2997
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 roots” if the discriminant is negative and find the two roots when there is no error condition. DO NOT FIND THE ROOT IF THERE IS AN ERROR CONDITION. 2. use a NESTED DECISION to do the three parts of the algorithm above. 3. write a...
4
4448
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
4526
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 integer, it should return false, but it should work normally if the parameter is a float. To illustrate this, I attached a minimal example.
0
8174
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8680
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8336
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7164
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6111
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4082
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2607
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1786
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.