Commit ba70cb25 authored by Philipp Schäfer's avatar Philipp Schäfer
Browse files

- cleaned up SplineTest a bit

parent 56c3d48b
...@@ -83,8 +83,8 @@ bool SpeedTest() { ...@@ -83,8 +83,8 @@ bool SpeedTest() {
} }
std::cout << myWatch << std::endl; std::cout << myWatch << std::endl;
std::cout << std::endl;
return true; return true;
} }
//!creates one .txt file with the Points a spline function shall be constructed for and one with the cooeficients, this programm has computes. A third contains the coefficenc of the derivate. All can be read by the SplineTest.m into MATLAB. //!creates one .txt file with the Points a spline function shall be constructed for and one with the cooeficients, this programm has computes. A third contains the coefficenc of the derivate. All can be read by the SplineTest.m into MATLAB.
...@@ -100,26 +100,20 @@ void Compare2MATLAB(){ ...@@ -100,26 +100,20 @@ void Compare2MATLAB(){
std::vector<double> vdCoeff = myPoly.Coefficients(); std::vector<double> vdCoeff = myPoly.Coefficients();
std::ofstream myFile; std::ofstream myFile;
myFile.open("points.txt"); myFile.open("points.txt");
for (int k = 0; k < vdDataPoints.size(); k++) { for (int k = 0; k < vdDataPoints.size(); k++)
myFile << vdSupportingPoints[k] << ", " << vdDataPoints[k] << std::endl; myFile << vdSupportingPoints[k] << ", " << vdDataPoints[k] << std::endl;
}
myFile.close(); myFile.close();
myFile.open("coeff.txt"); myFile.open("coeff.txt");
for (int k = 0; k < vdDataPoints.size() - 1; k++) { for (int k = 0; k < vdDataPoints.size() - 1; k++)
myFile << vdCoeff[4 * k] << ", " << vdCoeff[4 * k + 1] << ", " << vdCoeff[4 * k + 2] << ", " << vdCoeff[4 * k + 3] << std::endl; myFile << vdCoeff[4 * k] << ", " << vdCoeff[4 * k + 1] << ", " << vdCoeff[4 * k + 2] << ", " << vdCoeff[4 * k + 3] << std::endl;
}
myFile.close(); myFile.close();
vdCoeff = myDerivate.Coefficients(); vdCoeff = myDerivate.Coefficients();
myFile.open("derivate.txt"); myFile.open("derivate.txt");
for (int k = 0; k < vdDataPoints.size() - 1; k++) { for (int k = 0; k < vdDataPoints.size() - 1; k++)
myFile << vdCoeff[3 * k] << ", " << vdCoeff[3 * k + 1] << ", " << vdCoeff[3 * k + 2] << std::endl; myFile << vdCoeff[3 * k] << ", " << vdCoeff[3 * k + 1] << ", " << vdCoeff[3 * k + 2] << std::endl;
}
myFile.close(); myFile.close();
return;
} }
//!Checks the general behavior of the code //!Checks the general behavior of the code
bool GeneralTest(){ bool GeneralTest(){
...@@ -130,59 +124,91 @@ bool GeneralTest(){ ...@@ -130,59 +124,91 @@ bool GeneralTest(){
const std::vector<float> vdDataPoints{ 77, 80, 19, 49, 45, 65, 71, 76, 28, 68, 66, 17,12, 50, 96, 35, 59, 23, 76, 26, 51 }; const std::vector<float> vdDataPoints{ 77, 80, 19, 49, 45, 65, 71, 76, 28, 68, 66, 17,12, 50, 96, 35, 59, 23, 76, 26, 51 };
const std::vector<float> vdX = {-10, 1.5f, -7.898f,3.3f, 3.3f,9.9f,-2.5f,8.9f,0.01f,-4.3f }; const std::vector<float> vdX = {-10, 1.5f, -7.898f,3.3f, 3.3f,9.9f,-2.5f,8.9f,0.01f,-4.3f };
CPiecewisePolynomial myPoly = CubicSpline(vdSupportingPoints, vdDataPoints); CPiecewisePolynomial myPoly = CubicSpline(vdSupportingPoints, vdDataPoints);
std::cout << "-------Starting with FLOAT values-------" << std::endl << std::endl; std::cout << "-------Starting with FLOAT values-------" << std::endl << std::endl;
std::cout << "At supportion Point 0, the calculated value should be 66: " << myPoly.Value(float(0)) << std::endl; std::cout << "At supportion Point 0, the calculated value should be 66: " << myPoly.Value(float(0)) << std::endl;
//should get specific Value //should get specific Value
std::cout << "At random Point x=0.5 the spline should interpolate ~40.9: " << myPoly.Value(float(0.5)) << std::endl; std::cout << "At random Point x=0.5 the spline should interpolate ~40.9: " << myPoly.Value(float(0.5)) << std::endl;
std::vector<float> vdBreakPoints = { 0, 1 }; std::vector<float> vfBreakPoints = { 0, 1 };
std::vector<float> vdCoeff = { 1,1,1 }; std::vector<float> vfCoeffs = { 1,1,1 };
int iOrder = 2; int iOrder = 2;
CPiecewisePolynomial myPoly2 = CPiecewisePolynomial(vdBreakPoints, vdCoeff, iOrder); CPiecewisePolynomial myPoly2 = CPiecewisePolynomial(vfBreakPoints, vfCoeffs, iOrder);
//deviation test //deviation test
CPiecewisePolynomial myPoly3 = myPoly2.Derivation(); CPiecewisePolynomial myPoly3 = myPoly2.Derivation();
std::cout << "The deviation of x^2+x+1 should be: " << myPoly3.Coefficients()[0] << " * x + " << myPoly3.Coefficients()[1] << std::endl; std::cout << "Deviation test:" << std::endl;
std::cout << "so the new polynoial only got " << myPoly3.NumCoefficients() << " coefficients." << std::endl; if (myPoly3.NumCoefficients() == 2 && myPoly3.Coefficients()[0] == 2 && myPoly3.Coefficients()[1] == 1)
std::cout << "OK";
else
std::cout << "ERROR: The deviation of x^2+x+1 should be 2x + 1 But the new polyonom has " << myPoly3.NumCoefficients() << " coefficients: " << myPoly3.Coefficients()[0] << " * x + " << myPoly3.Coefficients()[1];
std::cout << std::endl << std::endl;
//exceptions for Piecewise Polynoms //exceptions for Piecewise Polynoms
std::cout << "Testing the Value function with Argument out of it's range, without extrapolation:" << std::endl; std::cout << "Testing the Value function with Argument out of it's range, without extrapolation:" << std::endl;
bool ok = false;
try { myPoly2.Value(float(-2)); } try { myPoly2.Value(float(-2)); }
catch (ITAException& err) { catch (ITAException& err) {
std::cout << err << std::endl; ok = true;
std::cout << err;
} }
if (!ok)
std::cout << "ERROR: Expected throw of exception";
std::cout << std::endl << std::endl;
std::cout << "Testing the Value function with Arguments out of it's range, with extrapolation:" << std::endl; std::cout << "Testing the Value function with Arguments out of it's range, with extrapolation:" << std::endl;
std::cout << "At -2, the calculated value should be -1: " << myPoly2.Value(float(-2),true) << std::endl; std::cout << "At -2, the calculated value should be -1: " << myPoly2.Value(float(-2),true) << std::endl;
std::cout << "At 2, the calculated value should be 6: " << myPoly2.Value(float(2), true) << std::endl; std::cout << "At 2, the calculated value should be 6: " << myPoly2.Value(float(2), true) << std::endl;
std::cout << std::endl;
//exceptions for spline //exceptions for spline
std::cout<<"Testing spline with to few Points:" << std::endl; std::cout << "Testing spline with too few Points:" << std::endl;
ok = false;
try { try {
std::vector<float> vdSupportingPoints{ -10, -9 }; std::vector<float> vfSupportingPoints{ -10, -9 };
std::vector<float> vdDataPoints{ 77, 80 }; std::vector<float> vfDataPoints{ 77, 80 };
myPoly = CubicSpline(vdSupportingPoints, vdDataPoints); myPoly = CubicSpline(vfSupportingPoints, vfDataPoints);
} }
catch (ITAException& err) { catch (ITAException& err) {
std::cout << err << std::endl; ok = true;
std::cout << err;
} }
if (!ok)
std::cout << "ERROR: Expected throw of exception";
std::cout << std::endl << std::endl;
std::cout << "Testing non matching dimensions:"<<std::endl; std::cout << "Testing non matching dimensions:"<<std::endl;
ok = false;
try { try {
std::vector<float> vdSupportingPoints{ -10, -5 ,-1,0,3 };//5points std::vector<float> vdSupportingPoints{ -10, -5 ,-1,0,3 };//5points
std::vector<float> vdDataPoints{ 77, 80,4,5,-4,-1 };//6 Points std::vector<float> vdDataPoints{ 77, 80,4,5,-4,-1 };//6 Points
myPoly = CubicSpline(vdSupportingPoints, vdDataPoints); myPoly = CubicSpline(vdSupportingPoints, vdDataPoints);
} }
catch(ITAException& err){ catch(ITAException& err){
std::cout << err << std::endl; ok = true;
std::cout << err;
} }
if (!ok)
std::cout << "ERROR: Expected throw of exception";
std::cout << std::endl << std::endl;
std::cout << "Testing with non ascending supportingPoints" << std::endl; std::cout << "Testing with non ascending supportingPoints" << std::endl;
ok = false;
try { try {
std::vector<float> vdSupportingPoints{ -10, 5 ,-1,0,3}; std::vector<float> vfSupportingPoints{ -10, 5 ,-1,0,3};
std::vector<float> vdDataPoints{ 77, 80,4,5,-4 }; std::vector<float> vfDataPoints{ 77, 80,4,5,-4 };
myPoly = CubicSpline(vdSupportingPoints, vdDataPoints); myPoly = CubicSpline(vfSupportingPoints, vfDataPoints);
} }
catch (ITAException& err) { catch (ITAException& err) {
ok = true;
std::cout << err << std::endl; std::cout << err << std::endl;
} }
if (!ok)
std::cout << "ERROR: Expected throw of exception";
std::cout << std::endl << std::endl;
// //
// ALLES MIT DOUBLES // ALLES MIT DOUBLES
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment