/
static_poly_io.hpp
195 lines (167 loc) · 5.61 KB
/
static_poly_io.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* Stream inserter for static_poly, with helpers.
* (C) Copyright Nick Matteo 2016.
* Use, modification and distribution are subject to the
* Boost Software License, Version 1.0. (See accompanying file
* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
#ifndef NAM_STATIC_POLYNOMIAL_IO_HPP
#define NAM_STATIC_POLYNOMIAL_IO_HPP
#include <ostream>
#include <cmath> //isnormal, fabs
#include <boost/range/algorithm/find_if.hpp>
#include <boost/range/algorithm/count_if.hpp>
#include <boost/math/special_functions/relative_difference.hpp>
#include "static_poly.hpp"
/** Forward declarations for ostream inserter helpers **/
namespace smath {
template <typename T>
struct complex;
}
namespace boost { namespace math {
template <class T>
class quaternion;
template <class T>
class octonion;
}}
/** Helpers for the stream inserter **/
namespace detail {
struct xpow {
int i;
};
inline std::ostream& operator << (std::ostream& os, xpow x) {
if (x.i == 1)
os << 'x';
else if (x.i > 1)
os << "x^" << x.i;
return os;
}
template <typename T>
std::enable_if_t<std::is_floating_point<T>::value, bool>
is_zero(T n) {
if (!std::isnormal(n))
return true; // zero or denorm, or NaN or ∞
return std::fabs(n) < 1e-11;
}
template <typename T>
std::enable_if_t<!std::is_floating_point<T>::value, bool>
is_zero(T n) {
return n == T{0};
}
template <typename T>
bool is_zero(smath::complex<T> ct) {
return is_zero(ct.real) && is_zero(ct.imag.value);
}
template <typename T>
struct notone {
T val;
};
template <typename T>
notone<T> ifnotone(T val) {
return {val};
}
template <typename T>
std::enable_if_t<std::is_floating_point<T>::value, bool>
isone(T n) {
using boost::math::relative_difference;
return relative_difference(1.0, n) < 1e-11;
}
template <typename T>
std::enable_if_t<!std::is_floating_point<T>::value, bool>
isone(T n) {
return n == T{1};
}
template <typename T>
bool isone(smath::complex<T> n) {
return isone(n.real) && is_zero(n.imag.value);
}
template <typename T>
std::ostream& operator << (std::ostream& os, notone<T> io) {
if (isone(-io.val))
return os << '-';
if (isone(io.val))
return os;
return os << io.val;
}
template <typename T>
bool is_negative(T t) {
return t < T{0} && !is_zero(t);
}
template <typename T>
bool is_negative(smath::complex<T> ct) {
return (ct.real < T{0} && !is_zero(ct.real)) ||
(is_zero(ct.real) && ct.imag.value < T{0} && !is_zero(ct.imag.value));
}
template <typename T>
bool is_negative(boost::math::quaternion<T> q) {
// only if the first nonzero element is negative, and the majority of
// nonzero terms are negative.
using boost::range::find_if;
using boost::range::count_if;
T vals[] = {q.R_component_1(), q.R_component_2(),
q.R_component_3(), q.R_component_4()};
auto nonzero = find_if(vals, [](T val){ return !is_zero(val); });
if (nonzero == boost::end(vals)) return false;
return *nonzero < T{0} &&
count_if(vals, [](T val){ return val < T{0} && !is_zero(val); }) >=
count_if(vals, [](T val){ return val > T{0} && !is_zero(val); });
}
template <typename T>
bool is_negative(boost::math::octonion<T> q) {
// only if the first nonzero element is negative, and the majority of
// nonzero terms are negative.
using boost::range::find_if;
using boost::range::count_if;
T vals[] = {q.R_component_1(), q.R_component_2(),
q.R_component_3(), q.R_component_4(),
q.R_component_5(), q.R_component_6(),
q.R_component_7(), q.R_component_8()};
auto nonzero = find_if(vals, [](T val){ return !is_zero(val); });
if (nonzero == boost::end(vals)) return false;
return *nonzero < T{0} &&
count_if(vals, [](T val){ return val < T{0} && !is_zero(val); }) >=
count_if(vals, [](T val){ return val > T{0} && !is_zero(val); });
}
} // close namespace detail
/** Output for smath::complex **/
namespace smath { // so it can be found
template <typename T>
std::ostream& operator << (std::ostream& os, const smath::complex<T>& cd) {
using ::detail::is_zero;
using ::detail::ifnotone;
if (is_zero(cd.real)) {// real part is zero or denorm (or ∞, NaN)
if (is_zero(cd.imag.value))
return os << '0';
return os << ifnotone(cd.imag.value) << 'i';
}
if (is_zero(cd.imag.value)) // imag part is zero or denorm (or ∞, NaN)
return os << cd.real;
os << '(' << cd.real;
if (cd.imag.value < 0.)
os << " - " << ifnotone(-cd.imag.value);
else
os << " + " << ifnotone(cd.imag.value);
return os << "i)";
}
}
template <class T, int N>
inline std::ostream& operator << (std::ostream& os, const static_poly<T, N>& poly) {
using namespace detail;
int i = poly.degree();
if (i == -1)
return os << '0';
if (i == 0)
return os << poly[0];
os << ifnotone(poly[i]) << xpow{i};
for (--i; i > 0; --i) {
if (is_negative(poly[i]))
os << " - " << ifnotone(-poly[i]) << xpow{i};
else if (!is_zero(poly[i]))
os << " + " << ifnotone(poly[i]) << xpow{i};
}
if (is_negative(poly[0]))
os << " - " << -poly[0];
else if (poly[0] != T{0})
os << " + " << poly[0];
return os;
}
#endif // NAM_STATIC_POLYNOMIAL_IO_HPP