Viskores  1.0
NewtonsMethod.h
Go to the documentation of this file.
1 //============================================================================
2 // The contents of this file are covered by the Viskores license. See
3 // LICENSE.txt for details.
4 //
5 // By contributing to this file, all contributors agree to the Developer
6 // Certificate of Origin Version 1.1 (DCO 1.1) as stated in DCO.txt.
7 //============================================================================
8 
9 //============================================================================
10 // Copyright (c) Kitware, Inc.
11 // All rights reserved.
12 // See LICENSE.txt for details.
13 //
14 // This software is distributed WITHOUT ANY WARRANTY; without even
15 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 // PURPOSE. See the above copyright notice for more information.
17 //============================================================================
18 #ifndef viskores_NewtonsMethod_h
19 #define viskores_NewtonsMethod_h
20 
21 #include <viskores/Math.h>
22 #include <viskores/Matrix.h>
23 
24 namespace viskores
25 {
26 
29 template <typename ScalarType, viskores::IdComponent Size>
31 {
33  bool Valid;
35  bool Converged;
40 };
41 
70 template <typename ScalarType,
72  typename JacobianFunctor,
73  typename FunctionFunctor>
75  JacobianFunctor jacobianEvaluator,
76  FunctionFunctor functionEvaluator,
77  viskores::Vec<ScalarType, Size> desiredFunctionOutput,
79  ScalarType convergeDifference = ScalarType(1e-3),
80  viskores::IdComponent maxIterations = 10)
81 {
82  using VectorType = viskores::Vec<ScalarType, Size>;
84 
85  VectorType x = initialGuess;
86 
87  bool valid = false;
88  bool converged = false;
89  for (viskores::IdComponent iteration = 0; !converged && (iteration < maxIterations); iteration++)
90  {
91  // For Newton's method, we solve the linear system
92  //
93  // Jacobian x deltaX = currentFunctionOutput - desiredFunctionOutput
94  //
95  // The subtraction on the right side simply makes the target of the solve
96  // at zero, which is what Newton's method solves for. The deltaX tells us
97  // where to move to to solve for a linear system, which we assume will be
98  // closer for our nonlinear system.
99 
100  MatrixType jacobian = jacobianEvaluator(x);
101  VectorType currentFunctionOutput = functionEvaluator(x);
102 
103  VectorType deltaX =
104  viskores::SolveLinearSystem(jacobian, currentFunctionOutput - desiredFunctionOutput, valid);
105  if (!valid)
106  {
107  break;
108  }
109 
110  x = x - deltaX;
111 
112  converged = true;
113  for (viskores::IdComponent index = 0; index < Size; index++)
114  {
115  converged &= (viskores::Abs(deltaX[index]) < convergeDifference);
116  }
117  }
118 
119  // Not checking whether converged.
120  return { valid, converged, x };
121 }
122 
123 } // namespace viskores
124 
125 #endif //viskores_NewtonsMethod_h
viskores::SolveLinearSystem
viskores::Vec< T, Size > SolveLinearSystem(const viskores::Matrix< T, Size, Size > &A, const viskores::Vec< T, Size > &b, bool &valid)
Solve the linear system Ax = b for x.
Definition: Matrix.h:460
Matrix.h
VISKORES_SUPPRESS_EXEC_WARNINGS
#define VISKORES_SUPPRESS_EXEC_WARNINGS
Definition: ExportMacros.h:61
viskores::IdComponent
viskores::Int32 IdComponent
Base type to use to index small lists.
Definition: Types.h:202
VISKORES_EXEC_CONT
#define VISKORES_EXEC_CONT
Definition: ExportMacros.h:60
viskores::NewtonsMethodResult::Valid
bool Valid
True if Newton's method ran into a singularity.
Definition: NewtonsMethod.h:33
viskores
Groups connected points that have the same field value.
Definition: Atomic.h:27
Math.h
viskores::Matrix
Basic Matrix type.
Definition: Matrix.h:41
viskores::NewtonsMethodResult::Converged
bool Converged
True if Newton's method converted to below the convergence value.
Definition: NewtonsMethod.h:35
viskores::NewtonsMethod
NewtonsMethodResult< ScalarType, Size > NewtonsMethod(JacobianFunctor jacobianEvaluator, FunctionFunctor functionEvaluator, viskores::Vec< ScalarType, Size > desiredFunctionOutput, viskores::Vec< ScalarType, Size > initialGuess=viskores::Vec< ScalarType, Size >(ScalarType(0)), ScalarType convergeDifference=ScalarType(1e-3), viskores::IdComponent maxIterations=10)
Uses Newton's method (a.k.a.
Definition: NewtonsMethod.h:74
viskores::Vec< ScalarType, Size >
viskores::NewtonsMethodResult::Solution
viskores::Vec< ScalarType, Size > Solution
The solution found by Newton's method.
Definition: NewtonsMethod.h:39
viskores::NewtonsMethodResult
An object returned from NewtonsMethod() that contains the result and other information about the fina...
Definition: NewtonsMethod.h:30