Skip to content

Commit

Permalink
refactor: initialize size for minor improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
FaaizHaikal committed Jan 5, 2025
1 parent 2033394 commit de046c3
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions include/keisan/interpolation/polynom.impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,51 +56,51 @@ bool Polynom::operator!=(const Polynom & other) const { return !(*this == other)

Polynom Polynom::operator+(const Polynom & other) const
{
std::vector<double> new_coefficients;
std::vector<double> new_coefficients(std::max(coefficients.size(), other.coefficients.size()));

for (size_t i = 0; i < std::max(coefficients.size(), other.coefficients.size()); ++i) {
double a = (i < coefficients.size()) ? coefficients[i] : 0.0;
double b = (i < other.coefficients.size()) ? other.coefficients[i] : 0.0;

new_coefficients.push_back(a + b);
new_coefficients[i] = a + b;
}

return Polynom(new_coefficients, domain_min, domain_max);
}

Polynom Polynom::operator-(const Polynom & other) const
{
std::vector<double> new_coefficients;
std::vector<double> new_coefficients(std::max(coefficients.size(), other.coefficients.size()));

for (size_t i = 0; i < std::max(coefficients.size(), other.coefficients.size()); ++i) {
double a = (i < coefficients.size()) ? coefficients[i] : 0.0;
double b = (i < other.coefficients.size()) ? other.coefficients[i] : 0.0;

new_coefficients.push_back(a - b);
new_coefficients[i] = a - b;
}

return Polynom(new_coefficients, domain_min, domain_max);
}

Polynom Polynom::derivative() const
{
std::vector<double> new_coefficients;
std::vector<double> new_coefficients(coefficients.size() > 1 ? coefficients.size() - 1 : 0);

for (size_t i = 1; i < coefficients.size(); ++i) {
new_coefficients.push_back(i * coefficients[i]);
new_coefficients[i - 1] = i * coefficients[i];
}

return Polynom(new_coefficients, domain_min, domain_max);
}

Polynom Polynom::integral() const
{
std::vector<double> new_coefficients;
std::vector<double> new_coefficients(coefficients.size() + 1);

new_coefficients.push_back(0.0);
new_coefficients[0] = 0.0;

for (size_t i = 0; i < coefficients.size(); ++i) {
new_coefficients.push_back(coefficients[i] / (i + 1));
new_coefficients[i + 1] = coefficients[i] / (i + 1);
}

return Polynom(new_coefficients, domain_min, domain_max);
Expand Down

0 comments on commit de046c3

Please sign in to comment.