Skip to content

Commit

Permalink
Improve polynomials
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiaisgro committed Apr 2, 2024
1 parent 363668a commit b104221
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/calculus/derivation.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@ namespace theoretica {
template<typename T = real>
inline polynomial<T> deriv_polynomial(const polynomial<T>& p) {

if(p.coeff.size() == 0) {
TH_MATH_ERROR("deriv_polynomial", p.coeff.size(), INVALID_ARGUMENT);
return polynomial<T>({T(nan())});
}

polynomial<T> Dp;
Dp.coeff.resize(p.coeff.size() - 1);

for (unsigned int i = 1; i < p.size(); ++i)
Dp.coeff.push_back(p.get(i) * i);
Dp.coeff[i - 1] = p[i] * i;

return Dp;
}
Expand Down
5 changes: 3 additions & 2 deletions src/calculus/integration.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ namespace theoretica {
inline polynomial<T> integrate_polynomial(const polynomial<T>& p) {

polynomial<T> P;
P.coeff.push_back(0);
P.coeff.resize(p.size() + 1);
P[0] = 0;

for (unsigned int i = 0; i < p.size(); ++i)
P.coeff.push_back(p.get(i) / T(i + 1));
P[i + 1] = p[i] / T(i + 1);

return P;
}
Expand Down
6 changes: 6 additions & 0 deletions src/polynomial/polynomial.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ namespace theoretica {
}


/// Return the nth order coefficient
inline const T& operator[](unsigned int i) const {
return coeff[i];
}


/// Return the nth order coefficient
inline T& operator[](unsigned int i) {
return coeff[i];
Expand Down
2 changes: 1 addition & 1 deletion test/test_autodiff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ int main(int argc, char const *argv[]) {
[=](interval k, Real tol, unsigned int n) {
return test_operator(
[=](vec<> v) {
return gradient(f, v) * J * gradient(H, v);
return gradient(f, v) * (J * gradient(H, v));
},
PRNG::xoshiro(time(nullptr)),
k, 1E-8, n, 4
Expand Down

0 comments on commit b104221

Please sign in to comment.