Loading...
Searching...
No Matches
Quartic.h
1/***************************************************************************
2 * Copyright (C) 2016 by Саша Миленковић *
3 * sasa.milenkovic.xyz@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * ( http://www.gnu.org/licenses/gpl-3.0.en.html ) *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21/*
22 * Modified by Gaurav Jalan @HiPeRLab Berkeley
23 * Changed function arguments
24 * Added new Quartic namespace
25 */
26
27#ifndef QUARTIC_H_INCLUDED
28#define QUARTIC_H_INCLUDED
29
30#include <complex>
31
32namespace Quartic
33{
34
35 const double PI = 3.141592653589793238463L;
36 const double M_2PI = 2 * PI;
37 const double eps = 1e-12;
38
39 typedef std::complex<double> DComplex;
40
41 //---------------------------------------------------------------------------
42 // useful for testing
43 inline DComplex polinom_2(DComplex x, double a, double b)
44 {
45 // Horner's scheme for x*x + a*x + b
46 return x * (x + a) + b;
47 }
48
49 //---------------------------------------------------------------------------
50 // useful for testing
51 inline DComplex polinom_3(DComplex x, double a, double b, double c)
52 {
53 // Horner's scheme for x*x*x + a*x*x + b*x + c;
54 return x * (x * (x + a) + b) + c;
55 }
56
57 //---------------------------------------------------------------------------
58 // useful for testing
59 inline DComplex polinom_4(DComplex x, double a, double b, double c, double d)
60 {
61 // Horner's scheme for x*x*x*x + a*x*x*x + b*x*x + c*x + d;
62 return x * (x * (x * (x + a) + b) + c) + d;
63 }
64
65 //---------------------------------------------------------------------------
66 // x - array of size 3
67 // In case 3 real roots: => x[0], x[1], x[2], return 3
68 // 2 real roots: x[0], x[1], return 2
69 // 1 real root : x[0], x[1] ± i*x[2], return 1
70 unsigned int solveP3(double a, double b, double c, double *x);
71
72 //---------------------------------------------------------------------------
73 // solve quartic equation x^4 + a*x^3 + b*x^2 + c*x + d
74 // Attention - this function returns dynamically allocated array. It has to be released afterwards.
75 size_t solve_quartic(const double &a, const double &b, const double &c, const double &d, double root[]);
76
77#endif // QUARTIC_H_INCLUDED
78}