-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolynomial_impl.hh
66 lines (55 loc) · 2.13 KB
/
polynomial_impl.hh
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
// https://stackoverflow.com/questions/10632251/undefined-reference-to-template-function
// A 'specialization' of a template here means a particular value of the
// template parameter.
#ifndef POLYNOMIAL_IMPL_H
#define POLYNOMIAL_IMPL_H
#include "term.hh"
#include "term_vector.hh"
#include <exception>
namespace polynomial {
template <long unsigned int N>
Polynomial::Polynomial(std::array<double, N> coef, std::array<int, N> expon) {
#ifdef DEBUG
log("Polynomial arrays constructor");
#endif
// Following is covered by a DEATH test that gcov does not recognize.
if (coef.empty() || expon.empty()) {
throw std::invalid_argument(
"Cannot create a polynomial from empty coefficients or exponent.");
}
term::SyncSortTwoArrays(&expon, &coef, 0);
// Does not work, as it overwrites the class member of the same name!
// std::unique_ptr<term::Term> h_(new term::Term(expon[0], coef[0]));
h_ = std::unique_ptr<term::Term>(new term::Term(expon[0], coef[0]));
// Other nodes.
for (int i = 1; i < static_cast<int>(N); i++) {
Prepend(std::unique_ptr<term::Term>(new term::Term(expon[i], coef[i])));
}
}
} // namespace polynomial
// I tried placing the following code in a term_vector_impl.hh and then referring
// to it in a term_vector_lib_test.cc, but could not get rid of either linker
// failures or compilation failures (could not find class TermVector) depending
// on the order of header files.
namespace termvector {
template <long unsigned int N>
TermVector::TermVector(std::array<double, N> coeff, std::array<int, N> expon) {
#ifdef DEBUG
std::cerr << "TermVector arrays constructor" << std::endl;
#endif
term::SyncSortTwoArrays(&expon, &coeff, 0);
size_ = N;
// Performs allocation.
// termvec_[0] = std::make_unique<term::Term>(Term(expon[0], coeff[0]));
#ifdef DEBUG
assert(coeff.size() == static_cast<unsigned>(size_));
assert(expon.size() == static_cast<unsigned>(size_));
#endif
termvec_ = std::make_unique<term::Term[]>(3u);
for (unsigned i = 0; i < static_cast<unsigned>(size_); i++) {
termvec_[i].exponent = expon[i];
termvec_[i].coefficient = coeff[i];
}
}
} // namespace termvector
#endif