PolynomialRoots.h
Go to the documentation of this file.
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2013, SimQuest Solutions Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef SURGSIM_MATH_POLYNOMIALROOTS_H
17 #define SURGSIM_MATH_POLYNOMIALROOTS_H
18 
19 #include <iostream>
20 #include <algorithm>
21 
23 
24 namespace SurgSim
25 {
26 namespace Math
27 {
28 
36 template <typename T, int N> class PolynomialRoots;
37 
43 template <typename T, int N>
45 {
46 public:
48  static const int DEGENERATE = -1;
49 
51  bool isDegenerate() const;
52 
54  int getNumRoots() const;
55 
61  T operator[](int i) const;
62 
63 private:
69 
70 protected:
73 
76 
78  std::array<T, N> m_roots;
79 };
80 
83 template <typename T>
84 class PolynomialRoots<T, 1> : public PolynomialRootsCommon<T, 1>
85 {
86 public:
90  explicit PolynomialRoots(const Polynomial<T, 1>& p, const T& epsilon = 1.0e-09);
91 };
92 
95 template <typename T>
96 class PolynomialRoots<T, 2> : public PolynomialRootsCommon<T, 2>
97 {
98 public:
102  explicit PolynomialRoots(const Polynomial<T, 2>& p, const T& epsilon = 1.0e-09);
103 };
104 
114 template <typename T, int N>
115 void solve(const T& a, const T& b, const T& epsilon, int* numRoots, std::array<T, N>* roots);
116 
127 template <typename T, int N>
128 void solve(const T& a, const T& b, const T& c, const T& epsilon, int* numRoots, std::array<T, N>* roots);
129 
130 }; // Math
131 }; // SurgSim
132 
134 
135 #endif // SURGSIM_MATH_POLYNOMIALROOTS_H
Definition: CompoundShapeToGraphics.cpp:29
int getNumRoots() const
Definition: PolynomialRoots-inl.h:32
std::array< T, N > m_roots
An array of up to N roots for a degree N polynomial ordered ascendingly.
Definition: PolynomialRoots.h:78
PolynomialRootsCommon & operator=(const PolynomialRootsCommon &)
PolynomialRootsCommon()
Constructor. Since the constructor must define the roots, only allow construction from a derived clas...
Definition: PolynomialRoots.h:72
static const int DEGENERATE
Indicator for a degenerate polynomial (infinite number of roots).
Definition: PolynomialRoots.h:48
Polynomial<T, 2> specializes the Polynomial class for degree 2 (quadratic polynomials) ...
Definition: Polynomial.h:183
The (algebraic) roots of a Polynomial<N,T>.
Definition: PolynomialRoots.h:36
T operator[](int i) const
Read only access to the roots of the polynomial.
Definition: PolynomialRoots-inl.h:38
bool isDegenerate() const
Definition: PolynomialRoots-inl.h:26
void solve(const T &a, const T &b, const T &epsilon, int *numRoots, std::array< T, N > *roots)
Specialized solve routine for linear polynomials (2 coefficients)
Definition: PolynomialRoots-inl.h:64
int m_numRoots
The number of roots available for the polynomial, or DEGENERATE if there are infinite roots...
Definition: PolynomialRoots.h:75
Polynomial<T, 1> specializes the Polynomial class for degree 1 (linear polynomials) ...
Definition: Polynomial.h:117
The common base class for PolynomialRoots specializations for various N.
Definition: PolynomialRoots.h:44