Skip to content
Snippets Groups Projects
Commit 5164c429 authored by Elias Pipping's avatar Elias Pipping Committed by Elias Pipping
Browse files

Add and use two_distance

parent a0251343
No related branches found
No related tags found
No related merge requests found
...@@ -24,4 +24,13 @@ double functionTester(Dune::SampleFunctional<dim> J, ...@@ -24,4 +24,13 @@ double functionTester(Dune::SampleFunctional<dim> J,
std::cout << boost::format("%8g -> %e") % original % final << std::endl; std::cout << boost::format("%8g -> %e") % original % final << std::endl;
return final; return final;
} }
template <int dim>
double two_distance(
typename Dune::SampleFunctional<dim>::SmallVector const &x,
typename Dune::SampleFunctional<dim>::SmallVector const &y) {
typename Dune::SampleFunctional<dim>::SmallVector tmp = x;
tmp -= y;
return tmp.two_norm();
}
#endif #endif
...@@ -31,7 +31,7 @@ int main() { ...@@ -31,7 +31,7 @@ int main() {
Functional J(A, b, phi); Functional J(A, b, phi);
Functional::SmallVector start; Functional::SmallVector start;
Functional::SmallVector error; Functional::SmallVector analytic_descent;
/* /*
for x = b/|b|: for x = b/|b|:
...@@ -50,12 +50,11 @@ int main() { ...@@ -50,12 +50,11 @@ int main() {
start /= (b.two_norm() + 1e-12); start /= (b.two_norm() + 1e-12);
assert(start.two_norm() < 1); assert(start.two_norm() < 1);
Functional::SmallVector returned; Functional::SmallVector numerical_descent;
J.descentDirection(start, returned); J.descentDirection(start, numerical_descent);
error[0] = -(7 / sqrt(5) - 1); analytic_descent[0] = -(7 / sqrt(5) - 1);
error[1] = -(11.5 / sqrt(5) - 2); analytic_descent[1] = -(11.5 / sqrt(5) - 2);
error -= returned; assert(two_distance<dim>(analytic_descent, numerical_descent) < 1e-10);
assert(error.two_norm() < 1e-10);
functionTester(J, start, 6); functionTester(J, start, 6);
} }
...@@ -64,12 +63,11 @@ int main() { ...@@ -64,12 +63,11 @@ int main() {
start /= (b.two_norm() - 1e-12); // Make sure the norm is above 1; start /= (b.two_norm() - 1e-12); // Make sure the norm is above 1;
assert(start.two_norm() > 1); assert(start.two_norm() > 1);
Functional::SmallVector returned; Functional::SmallVector numerical_descent;
J.descentDirection(start, returned); J.descentDirection(start, numerical_descent);
error[0] = -(8 / sqrt(5) - 1); analytic_descent[0] = -(8 / sqrt(5) - 1);
error[1] = -(13.5 / sqrt(5) - 2); analytic_descent[1] = -(13.5 / sqrt(5) - 2);
error -= returned; assert(two_distance<dim>(analytic_descent, numerical_descent) < 1e-10);
assert(error.two_norm() < 1e-10);
functionTester(J, start, 6); functionTester(J, start, 6);
} }
......
...@@ -40,13 +40,12 @@ int main() { ...@@ -40,13 +40,12 @@ int main() {
= 17*(6, 9.5) - (1, 2) + 2/sqrt(5) (1, 2) = 17*(6, 9.5) - (1, 2) + 2/sqrt(5) (1, 2)
= (102 - 1 + 2/sqrt(5), 161.5 - 2 + 4/sqrt(5)) = (102 - 1 + 2/sqrt(5), 161.5 - 2 + 4/sqrt(5))
*/ */
Functional::SmallVector error; Functional::SmallVector analytic_descent;
error[0] = -(102 - 1 + 2 / sqrt(5)); analytic_descent[0] = -(102 - 1 + 2 / sqrt(5));
error[1] = -(161.5 - 2 + 4 / sqrt(5)); analytic_descent[1] = -(161.5 - 2 + 4 / sqrt(5));
Functional::SmallVector returned; Functional::SmallVector numerical_descent;
J.descentDirection(start, returned); J.descentDirection(start, numerical_descent);
error -= returned; assert(two_distance<dim>(analytic_descent, numerical_descent) < 1e-10);
assert(error.two_norm() < 1e-10);
double const ret1 = functionTester(J, start, 6); double const ret1 = functionTester(J, start, 6);
......
...@@ -44,27 +44,22 @@ int main() { ...@@ -44,27 +44,22 @@ int main() {
= 17*(6, 9.5) - (1, 2) = 17*(6, 9.5) - (1, 2)
= (102 - 1, 161.5 - 2) = (102 - 1, 161.5 - 2)
*/ */
Functional::SmallVector error; Functional::SmallVector analytic_descent;
error[0] = -101; analytic_descent[0] = -101;
error[1] = -159.5; analytic_descent[1] = -159.5;
Functional::SmallVector returned; Functional::SmallVector numerical_descent;
J.descentDirection(start, returned); J.descentDirection(start, numerical_descent);
error -= returned; assert(two_distance<dim>(numerical_descent, analytic_descent) < 1e-10);
assert(error.two_norm() < 1e-10);
double const ret1 = functionTester(J, start, 6); double const ret1 = functionTester(J, start, 6);
error = solution; assert(two_distance<dim>(start, solution) < 1e-5);
error -= start;
assert(error.two_norm() < 1e-5);
// Something random // Something random
start[0] = 279; start[0] = 279;
start[1] = -96; start[1] = -96;
double const ret2 = functionTester(J, start, 25); double const ret2 = functionTester(J, start, 25);
error = solution; assert(two_distance<dim>(start, solution) < 1e-6);
error -= start;
assert(error.two_norm() < 1e-6);
assert(std::abs(ret1 - ret2) < 1e-5); assert(std::abs(ret1 - ret2) < 1e-5);
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment