MUQ  0.4.3
ScalarAlgebra.h
Go to the documentation of this file.
1 #ifndef SCALARALGEBRA_H_
2 #define SCALARALGEBRA_H_
3 
4 #include <assert.h>
5 #include <cmath>
6 
7 #include <boost/none.hpp>
8 #include <boost/any.hpp>
9 
10 namespace muq {
11  namespace Modeling {
13  class ScalarAlgebra {
14  public:
15 
16  ScalarAlgebra();
17 
18  virtual ~ScalarAlgebra();
19 
21 
25  static bool IsScalar(std::type_info const& obj_type);
26 
28 
32  static bool IsZero(boost::any const& obj);
33 
35 
38  static boost::any Zero(std::type_info const& type);
39 
41 
45  static double Norm(boost::any const& obj);
46 
48 
53  static double InnerProduct(boost::any const& vec1, boost::any const& vec2);
54 
56 
61  static boost::any OuterProduct(boost::any const& vec1, boost::any const& vec2);
62 
64 
68  static boost::any Identity(std::type_info const& type);
69 
71 
76  static boost::any Add(boost::any const& in0, boost::any const& in1);
77 
79 
84  static boost::any Subtract(boost::any const& in0, boost::any const& in1);
85 
87 
92  static boost::any Multiply(boost::any const& in0, boost::any const& in1);
93 
95 
99  static boost::any Inverse(boost::any const& obj);
100 
102 
106  static boost::any SquareRoot(boost::any const& obj);
107 
109 
113  static double LogDeterminate(boost::any const& obj);
114 
115  private:
116 
118 
122  template<typename type>
123  static inline double Magnitude(boost::any const& obj) {
124  const double x = (double)boost::any_cast<type const>(obj);
125 
126  return std::abs(x);
127  }
128 
130 
135  template<typename type0, typename type1>
136  static inline boost::any Add(boost::any const& in0, boost::any const& in1) {
137  const type0 x0 = boost::any_cast<type0 const>(in0);
138  const type1 x1 = boost::any_cast<type1 const>(in1);
139 
140  return x0 + x1;
141  }
142 
144 
149  template<typename type0>
150  static inline boost::any Add(boost::any const& in0, boost::any const& in1) {
151  if( in1.type()==typeid(double) ) { return Add<type0, double>(in0, in1); }
152  if( in1.type()==typeid(float) ) { return Add<type0, float>(in0, in1); }
153  if( in1.type()==typeid(int) ) { return Add<type0, int>(in0, in1); }
154  if( in1.type()==typeid(unsigned int) ) { return Add<type0, unsigned int>(in0, in1); }
155 
156  // something went wrong
157  assert(false);
158  return boost::none;
159  }
160 
162 
167  template<typename type0, typename type1>
168  static inline boost::any Subtract(boost::any const& in0, boost::any const& in1) {
169  const type0 x0 = boost::any_cast<type0 const>(in0);
170  const type1 x1 = boost::any_cast<type1 const>(in1);
171 
172  return x0 - x1;
173  }
174 
176 
181  template<typename type0>
182  static inline boost::any Subtract(boost::any const& in0, boost::any const& in1) {
183  if( in1.type()==typeid(double) ) { return Subtract<type0, double>(in0, in1); }
184  if( in1.type()==typeid(float) ) { return Subtract<type0, float>(in0, in1); }
185  if( in1.type()==typeid(int) ) { return Subtract<type0, int>(in0, in1); }
186  if( in1.type()==typeid(unsigned int) ) { return Subtract<type0, unsigned int>(in0, in1); }
187 
188  // something went wrong
189  assert(false);
190  return boost::none;
191  }
192 
194 
199  template<typename type0, typename type1>
200  static inline boost::any Multiply(boost::any const& in0, boost::any const& in1) {
201  const type0 x0 = boost::any_cast<type0 const>(in0);
202  const type1 x1 = boost::any_cast<type1 const>(in1);
203 
204  return x0 * x1;
205  }
206 
208 
213  template<typename type0>
214  static inline boost::any Multiply(boost::any const& in0, boost::any const& in1) {
215  if( in1.type()==typeid(double) ) { return Multiply<type0, double>(in0, in1); }
216  if( in1.type()==typeid(float) ) { return Multiply<type0, float>(in0, in1); }
217  if( in1.type()==typeid(int) ) { return Multiply<type0, int>(in0, in1); }
218  if( in1.type()==typeid(unsigned int) ) { return Multiply<type0, unsigned int>(in0, in1); }
219 
220  // something went wrong
221  assert(false);
222  return boost::none;
223  }
224  };
225  } // namespace Modeling
226 } // namespace muq
227 
228 #endif
Linear algebra for scalar objects.
Definition: ScalarAlgebra.h:13
static boost::any SquareRoot(boost::any const &obj)
Compute the square root of an object.
static bool IsScalar(std::type_info const &obj_type)
Is a boost::any a scalar type (double, float, int, or unsigned int)?
static bool IsZero(boost::any const &obj)
Determine if a scalar is zero.
static boost::any Add(boost::any const &in0, boost::any const &in1)
Add two scalars together.
static boost::any OuterProduct(boost::any const &vec1, boost::any const &vec2)
The outer product between two scalars.
static boost::any Subtract(boost::any const &in0, boost::any const &in1)
Subtract two scalars.
static boost::any Add(boost::any const &in0, boost::any const &in1)
Add two scalars together.
static double Norm(boost::any const &obj)
Get the norm of a scalar (the magnitude)
static boost::any Multiply(boost::any const &in0, boost::any const &in1)
Multiply two scalars.
static boost::any Multiply(boost::any const &in0, boost::any const &in1)
Multiply two scalars.
static boost::any Add(boost::any const &in0, boost::any const &in1)
Add two scalars together.
static double InnerProduct(boost::any const &vec1, boost::any const &vec2)
The inner product between two scalars.
static boost::any Subtract(boost::any const &in0, boost::any const &in1)
Subtract two scalars.
static double Magnitude(boost::any const &obj)
The magnitude of a scalar.
static boost::any Identity(std::type_info const &type)
Compute an identity object for a scalar.
static boost::any Subtract(boost::any const &in0, boost::any const &in1)
Subtract two scalars.
static double LogDeterminate(boost::any const &obj)
Compute the log-determinate.
static boost::any Inverse(boost::any const &obj)
The inverse.
static boost::any Multiply(boost::any const &in0, boost::any const &in1)
Multiply two scalars.
static boost::any Zero(std::type_info const &type)
Compute a zero scalar.