libpappsomspp
Library for mass spectrometry
Loading...
Searching...
No Matches
cardano.h File Reference

cubic solver adapted from https://www.codeproject.com/articles/798474/to-solve-a-cubic-equation thanks to "Sergey Bochkanov" serge.nosp@m.y.bo.nosp@m.chkan.nosp@m.ov@a.nosp@m.lglib.nosp@m..net for his advise More...

#include <vector>
#include <cstdint>

Go to the source code of this file.

Classes

struct  InHousePolynomialSolverResult
 

Enumerations

enum class  CardanoResultCase : std::int8_t {
  notvalid , zerod , negatived , positived ,
  quadratic , line
}
 

Functions

InHousePolynomialSolverResult inHousePolynomialSolve (const std::vector< double > &polynome)
 

Detailed Description

cubic solver adapted from https://www.codeproject.com/articles/798474/to-solve-a-cubic-equation thanks to "Sergey Bochkanov" serge.nosp@m.y.bo.nosp@m.chkan.nosp@m.ov@a.nosp@m.lglib.nosp@m..net for his advise

Date
17/12/2022

Definition in file cardano.h.

Enumeration Type Documentation

◆ CardanoResultCase

enum class CardanoResultCase : std::int8_t
strong
Enumerator
notvalid 
zerod 
negatived 
positived 
quadratic 
line 

Definition at line 35 of file cardano.h.

Function Documentation

◆ inHousePolynomialSolve()

InHousePolynomialSolverResult inHousePolynomialSolve ( const std::vector< double > & polynome)

Definition at line 118 of file cardano.cpp.

119{
122 std::size_t polynome_size = polynome.size();
123
124 double a, b, c, delta;
125
126 switch(polynome_size)
127 {
128 case 0:
129 break;
130 case 1:
131 break;
132 case 2: // linear// equation ax + b = 0
133 a = polynome[1];
134 b = polynome[0];
135 if(a != 0)
136 {
137 res.x1 = -b / a;
139 }
140 break;
141 case 3:
142 // quadratic equation ax**2 + bx + c = 0
143 a = polynome[2];
144 if(a == 0)
145 return res;
146 b = polynome[1];
147 c = polynome[0];
148
149 if(a == 0)
150 return res;
151 // calculate the discriminant
152 delta = (b * b) - (a * c * 4);
153 // qDebug() << delta;
154 if(delta < 0)
155 {
156 }
157 else if(std::abs(delta) <= 1e-20)
158 {
159 res.x1 = -b / (a * 2);
160 // qDebug() << x1.real() << " " << x2.real();
162 }
163 else
164 {
165 // find two solutions
166 delta = std::sqrt(delta);
167 res.x1 = (-b + delta) / (a * 2);
168 res.x2 = (-b - delta) / (a * 2);
169
170 // qDebug() << x1.real() << " " << x2.real();
172 }
173 break;
174 case 4:
175 cubic_solver(res, polynome[3], polynome[2], polynome[1], polynome[0]);
176 }
177 return res;
178}
void cubic_solver(InHousePolynomialSolverResult &res, double a1, double b, double c, double d)
Definition cardano.cpp:42

References cubic_solver(), line, notvalid, and quadratic.

Referenced by pappso::MzCalibrationModel1::getMzFromTofIndex().