24 #ifndef COM_DAFER45_TBTK_POLYNOMIAL
25 #define COM_DAFER45_TBTK_POLYNOMIAL
36 typename FieldType = std::complex<double>,
37 typename VariableType = std::complex<double>,
38 typename PowerType =
int
45 Polynomial(
unsigned int numVariables);
54 const FieldType &coefficient,
55 const std::vector<PowerType> &powers
65 const FieldType &coefficient,
66 const std::initializer_list<PowerType> &powers
68 addTerm(coefficient, std::vector<PowerType>(powers));
75 void addTerm(
const Polynomial &term, PowerType power);
81 void addTerm(
const Polynomial &lhs,
const Polynomial &rhs);
90 VariableType operator()(
const std::vector<VariableType> &variable)
const;
93 unsigned int numVariables;
96 std::vector<std::tuple<FieldType, std::vector<PowerType>>> terms;
99 std::vector<std::tuple<Polynomial, PowerType>> polynomialTerms;
102 std::vector<std::tuple<Polynomial, Polynomial>> productTerms;
105 template<
typename FieldType,
typename VariableType,
typename PowerType>
106 Polynomial<FieldType, VariableType, PowerType>::Polynomial(
107 unsigned int numVariables
109 this->numVariables = numVariables;
112 template<
typename FieldType,
typename VariableType,
typename PowerType>
113 void Polynomial<FieldType, VariableType, PowerType>::addTerm(
114 const FieldType &coefficient,
115 const std::vector<PowerType> &powers
118 powers.size() == numVariables,
119 "Polynomial::addTerm()",
120 "The number of powers '" << powers.size() <<
"' must be equal"
121 <<
" to the number of variables '" << numVariables <<
"' in"
122 <<
" the polynomial.",
126 terms.push_back(std::make_tuple(coefficient, powers));
129 template<
typename FieldType,
typename VariableType,
typename PowerType>
130 void Polynomial<FieldType, VariableType, PowerType>::addTerm(
131 const Polynomial &term,
135 term.numVariables == numVariables,
136 "Polynomial::addTerm()",
137 "The term has to have the same number of variables as the"
138 <<
" total polynomial. The term has '" << term.numVariables
139 <<
"' variables, while the Polynomial has '" << numVariables
144 polynomialTerms.push_back(std::make_tuple(term, power));
147 template<
typename FieldType,
typename VariableType,
typename PowerType>
148 void Polynomial<FieldType, VariableType, PowerType>::addTerm(
149 const Polynomial &lhs,
150 const Polynomial &rhs
153 lhs.numVariables == numVariables,
154 "Polynomial::addTerm()",
155 "Incompatible number of variables. The left hand side is a"
156 <<
" polynomial with '" << lhs.numVariables <<
"', while the"
157 <<
" polynomial that the product is added to has '"
158 << numVariables <<
"' variables. The number of variables must"
163 rhs.numVariables == numVariables,
164 "Polynomial::addTerm()",
165 "Incompatible number of variables. The right hand side is a"
166 <<
" polynomial with '" << rhs.numVariables <<
"', while the"
167 <<
" polynomial that the product is added to has '"
168 << numVariables <<
"' variables. The number of variables must"
173 productTerms.push_back(std::make_tuple(lhs, rhs));
176 template<
typename FieldType,
typename VariableType,
typename PowerType>
177 VariableType Polynomial<FieldType, VariableType, PowerType>::operator()(
178 const std::vector<VariableType> &variables
181 variables.size() == numVariables,
182 "Polynomial::operator()",
183 "The number of given variables are '" << variables.size()
184 <<
"', but the polynomial has '" << numVariables <<
"'"
189 VariableType value = 0.;
190 for(
unsigned int n = 0; n < terms.size(); n++){
191 VariableType term = std::get<0>(terms[n]);
192 for(
unsigned int c = 0; c < numVariables; c++){
193 PowerType power = std::get<1>(terms[n])[c];
195 term *=
pow(variables[c], power);
201 for(
unsigned int n = 0; n < polynomialTerms.size(); n++){
202 VariableType term =
pow(
203 std::get<0>(polynomialTerms[n])(variables),
204 std::get<1>(polynomialTerms[n])
210 for(
unsigned int n = 0; n < productTerms.size(); n++){
211 const Polynomial &lhs = std::get<0>(productTerms[n]);
212 const Polynomial &rhs = std::get<1>(productTerms[n]);
213 VariableType term = lhs(variables)*rhs(variables);