Skip to content

Commit

Permalink
Added generic function to calculate interpolating function in a speci…
Browse files Browse the repository at this point in the history
…fic point.

Modified test unit.

Signed-off-by: Massimo Gengarelli <gengarel@cs.unibo.it>
  • Loading branch information
Massimo Gengarelli committed Jul 28, 2011
1 parent 33675a5 commit c3871ff
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
15 changes: 15 additions & 0 deletions Renal/NextCalculator.cpp
Expand Up @@ -23,6 +23,7 @@
#include "NextException.h"

#include <vector>
#include <cmath>

namespace Renal {

Expand Down Expand Up @@ -63,4 +64,18 @@ namespace Renal {
coords->clear();
coeffs->clear();
}

double NextCalculator::CalculateInPoint(double x) {
if (coeffs->size() < 1)
throw NextException("I have no coefficients.");

double result = 0;
int exponent = coeffs->size()-1;
for (std::vector<double>::iterator ite = coeffs->begin(); ite != coeffs->end()-1; ++ite, --exponent)
result += (*ite * pow(x, exponent));

result += coeffs->data()[coeffs->size()-1];

return result;
}
}
1 change: 1 addition & 0 deletions Renal/NextCalculator.h
Expand Up @@ -38,6 +38,7 @@ namespace Renal {
size_t CountCoords();
std::vector<double> GetPolynom();
void Clear();
double CalculateInPoint(double x);

protected:
std::vector<std::pair<double, double> > *coords;
Expand Down
40 changes: 35 additions & 5 deletions Sensitive.cpp
Expand Up @@ -29,8 +29,7 @@

Renal::NextCalculator *generic_calculator;

void TestWithCoords(std::vector<std::pair<double, double> > coords_vector)
{
void TestWithCoords(std::vector<std::pair<double, double> > coords_vector) {
generic_calculator->Clear();

for (std::vector<std::pair<double, double> >::iterator ite = coords_vector.begin(); ite != coords_vector.end(); ++ite)
Expand All @@ -43,7 +42,7 @@ void TestWithCoords(std::vector<std::pair<double, double> > coords_vector)
printf("\t\tf(x) = ");
for (std::vector<double>::iterator ite = polynom.begin(); ite != polynom.end(); ++ite)
printf("%.2f ", *ite);
printf("\n\n\n\n"); /* 0 1 1 = x + 1 */
printf("\n");
}
}

Expand All @@ -53,9 +52,14 @@ void TestWithCoords(std::vector<std::pair<double, double> > coords_vector)

}

void PrintOut(double x) {
double result = generic_calculator->CalculateInPoint(x);

int main()
{
printf("\t\tf(%.f) = %.f\n\n\n\n", x, result);
}


int main() {
generic_calculator = new Renal::LaGrangeCalculator();

std::vector<std::pair<double, double> > coords;
Expand All @@ -65,37 +69,63 @@ int main()
coords.push_back(std::pair<double, double>(2., 3.));
coords.push_back(std::pair<double, double>(3., 4.));
TestWithCoords(coords); /* 0 1 1 = x + 1 */
PrintOut(8); /* 9 */

coords.clear();
coords.push_back(std::pair<double, double>(1., 7.));
coords.push_back(std::pair<double, double>(3., 31.));
coords.push_back(std::pair<double, double>(5., 71.));
TestWithCoords(coords); /* 2 4 1 = 2x^2 + 4x + 1 */
PrintOut(7); /* 127 */

coords.clear();
coords.push_back(std::pair<double, double>(2., 1.));
coords.push_back(std::pair<double, double>(4., 2.));
coords.push_back(std::pair<double, double>(8., 4.));
TestWithCoords(coords); /* 0 0.50 0 = x/2 */
PrintOut(14); /* 7 */

coords.clear();
coords.push_back(std::pair<double, double>(1., 3.));
coords.push_back(std::pair<double, double>(2., 7.));
coords.push_back(std::pair<double, double>(3., 13.));
TestWithCoords(coords); /* 1 1 1 = x^2 + x + 1 */
PrintOut(5); /* 31 */

coords.clear();
coords.push_back(std::pair<double, double>(1., 30.));
coords.push_back(std::pair<double, double>(3., 296.));
coords.push_back(std::pair<double, double>(4., 525.));
TestWithCoords(coords); /* 32 5 -7 = 32x^2 + 5x - 7 */
PrintOut(2); /* 131 */

coords.clear();
coords.push_back(std::pair<double, double>(1., 2.));
coords.push_back(std::pair<double, double>(2., 4.));
coords.push_back(std::pair<double, double>(3., 6.));
coords.push_back(std::pair<double, double>(4., 8.));
TestWithCoords(coords); /* 0 0 2 0 = 2x */
PrintOut(16); /* 32 */

coords.clear();
coords.push_back(std::pair<double, double>(1., 1.));
coords.push_back(std::pair<double, double>(2., 2.));
TestWithCoords(coords); /* 1 0 = x */
PrintOut(9); /* 9 */

coords.clear();
coords.push_back(std::pair<double, double>(2., 4.));
coords.push_back(std::pair<double, double>(4., 8.));
coords.push_back(std::pair<double, double>(8., 16.));
coords.push_back(std::pair<double, double>(3., 6.));
coords.push_back(std::pair<double, double>(5., 10.));
coords.push_back(std::pair<double, double>(25., 50.));
coords.push_back(std::pair<double, double>(9., 18.));
coords.push_back(std::pair<double, double>(11., 22.));
coords.push_back(std::pair<double, double>(1., 2.));
coords.push_back(std::pair<double, double>(7., 14.));
TestWithCoords(coords); /* 0 .. 0 2 0 = 2x */
PrintOut(15); /* 30 */

delete(generic_calculator);

Expand Down

0 comments on commit c3871ff

Please sign in to comment.