Code-Eli  0.3.6
iterative_system_root_base.hpp
Go to the documentation of this file.
1 /*********************************************************************************
2 * Copyright (c) 2013 David D. Marshall <ddmarsha@calpoly.edu>
3 *
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * David D. Marshall - initial code and implementation
11 ********************************************************************************/
12 
13 #ifndef eli_mutil_nls_iterative_root_system_method_hpp
14 #define eli_mutil_nls_iterative_root_system_method_hpp
15 
17 
18 namespace eli
19 {
20  namespace mutil
21  {
22  namespace nls
23  {
24  template<typename data__, size_t N__, size_t NSOL__>
26  {
27  public:
28  typedef Eigen::Matrix<data__, N__, NSOL__> solution_matrix;
29  typedef Eigen::Matrix<data__, N__, N__> jacobian_matrix;
30 
32  {
33  L1=100, // for vectors this is sum of maxes, for matrices this is maximum absolute column sum
34  L2=200, // for vectors this is sqrt of sum of squares, for matrices this will be equivalent to the Frobenius norm
35  Linf=300, // for vectors this is the largest (in magnitude) terms, for matrices this is maximum abolute row sum
36  max_norm=400, // for vectors this is equivalent to Linf, for matrices it is the largest element in matrix
37  Frobenius_norm=500 // for vectors this is L2, for matrices this is the sqrt of sum of squares of each term
38  };
39 
40  public:
42  : iterative_root_base<data__>(), norm_type(iterative_system_root_base<data__, N__, NSOL__>::max_norm)
43  {
44  }
45 
47  : iterative_root_base<data__>(isrb), norm_type(isrb.norm_type)
48  {
49  }
50 
52  {
53  }
54 
56  {
57  return norm_type;
58  }
59 
61  {
62  norm_type = snt;
63  }
64 
65  protected:
66  data__ calculate_norm(const solution_matrix &mat) const
67  {
68  // handle the cases that do not need to distinguish between vectors and matrices
69  switch (get_norm_type())
70  {
71  case(L1): // for vectors this is sum of maxes, for matrices this is maximum absolute column sum
72  {
73  data__ rtn_val(-1), tmp;
74 
75  for (typename solution_matrix::Index nc=0; nc<mat.cols(); ++nc)
76  {
77  tmp=mat.col(nc).cwiseAbs().sum();
78  if (tmp>rtn_val)
79  rtn_val=tmp;
80  }
81 
82  return rtn_val;
83  break;
84  }
85  case(Linf): // for vectors this is the largest (in magnitude) terms, for matrices this is maximum absolute row sum
86  {
87  data__ rtn_val(-1), tmp;
88 
89  for (typename solution_matrix::Index nr=0; nr<mat.rows(); ++nr)
90  {
91  tmp=mat.row(nr).cwiseAbs().sum();
92  if (tmp>rtn_val)
93  rtn_val=tmp;
94  }
95 
96  return rtn_val;
97  break;
98  }
99  case(max_norm): // for vectors this is equivalent to Linf, for matrices it is the largest element in matrix
100  {
101  data__ maxval = std::abs(mat.maxCoeff());
102  data__ minval = std::abs(mat.minCoeff());
103  if( maxval > minval ) return maxval;
104  else return minval;
105  // The following should work, but it throws an error: Expected expression.
106  // return mat.lpNorm<Eigen::Infinity>();
107  break;
108  }
109  case(L2): // for vectors this is sqrt of sum of squares, for matrices this will be equivalent to the Frobenius norm
110  case(Frobenius_norm): // for vectors this is L2, for matrices this is the sqrt of sum of squares of each term
111  {
112  data__ rtn_val(0);
113 
114  for (typename solution_matrix::Index nc=0; nc<mat.cols(); ++nc)
115  rtn_val+=mat.col(nc).squaredNorm();
116 
117  return std::sqrt(rtn_val);
118  break;
119  }
120  default:
121  {
122  // unknown norm type
123  assert(false);
124  return static_cast<data__>(-1);
125  }
126  }
127 
128  return static_cast<data__>(-1);
129  }
130 
131  private:
133  };
134  }
135  }
136 }
137 #endif
Definition: iterative_system_root_base.hpp:33
iterative_system_root_base(const iterative_system_root_base< data__, N__, NSOL__ > &isrb)
Definition: iterative_system_root_base.hpp:46
Definition: math.hpp:20
Definition: iterative_system_root_base.hpp:35
Eigen::Matrix< data__, N__, NSOL__ > solution_matrix
Definition: iterative_system_root_base.hpp:28
Eigen::Matrix< data__, N__, N__ > jacobian_matrix
Definition: iterative_system_root_base.hpp:29
Definition: iterative_system_root_base.hpp:25
iterative_system_root_base()
Definition: iterative_system_root_base.hpp:41
Definition: iterative_root_base.hpp:151
system_norm_type get_norm_type() const
Definition: iterative_system_root_base.hpp:55
data__ calculate_norm(const solution_matrix &mat) const
Definition: iterative_system_root_base.hpp:66
Definition: iterative_system_root_base.hpp:34
void set_norm_type(system_norm_type snt)
Definition: iterative_system_root_base.hpp:60
system_norm_type norm_type
Definition: iterative_system_root_base.hpp:132
system_norm_type
Definition: iterative_system_root_base.hpp:31
Definition: iterative_system_root_base.hpp:37
Definition: iterative_system_root_base.hpp:36
~iterative_system_root_base()
Definition: iterative_system_root_base.hpp:51