MUQ  0.4.3
ScalarAlgebra.cpp
Go to the documentation of this file.
2 
3 using namespace muq::Modeling;
4 
6 
8 
9 bool ScalarAlgebra::IsScalar(std::type_info const& obj_type) {
10  // is this a scalar type?
11  return typeid(double)==obj_type || typeid(float)==obj_type || typeid(int)==obj_type || typeid(unsigned int)==obj_type;
12 }
13 
14 bool ScalarAlgebra::IsZero(boost::any const& obj) {
15  if( obj.type()==typeid(double) ) { return boost::any_cast<double const>(obj)==0.0; }
16  if( obj.type()==typeid(float) ) { return boost::any_cast<float const>(obj)==0.0; }
17  if( obj.type()==typeid(int) ) { return boost::any_cast<int const>(obj)==0; }
18  if( obj.type()==typeid(unsigned int) ) { return boost::any_cast<unsigned int const>(obj)==0; }
19 
20  // something when wrong
21  assert(false);
22  return false;
23 }
24 
25 boost::any ScalarAlgebra::Zero(std::type_info const& type) {
26  if( typeid(double)==type ) { return (double)0.0; }
27  if( typeid(float)==type ) { return (float)0.0; }
28  if( typeid(int)==type ) { return (int)0; }
29  if( typeid(unsigned int)==type ) { return (unsigned int)0; }
30 
31  // something went wrong
32  assert(false);
33  return boost::none;
34 }
35 
36 double ScalarAlgebra::Norm(boost::any const& obj) {
37  if( typeid(double)==obj.type() ) { return Magnitude<double>(obj); }
38  if( typeid(float)==obj.type() ) { return Magnitude<float>(obj); }
39  if( typeid(int)==obj.type() ) { return Magnitude<int>(obj); }
40  if( typeid(unsigned int)==obj.type() ) { return Magnitude<unsigned int>(obj); }
41 
42  // something went wrong
43  assert(false);
44  return -1.0;
45 }
46 
47 double ScalarAlgebra::InnerProduct(boost::any const& in0, boost::any const& in1) {
48  const boost::any result = ScalarAlgebra::Multiply(in0, in1);
49 
50  if( result.type()==typeid(double) ) { return boost::any_cast<double const>(result); }
51  if( result.type()==typeid(float) ) { return (double)boost::any_cast<float const>(result); }
52  if( result.type()==typeid(int) ) { return (double)boost::any_cast<int const>(result); }
53  if( result.type()==typeid(unsigned int) ) { return (double)boost::any_cast<unsigned int const>(result); }
54 
55  // something went wrong
56  return std::numeric_limits<double>::quiet_NaN();
57 }
58 
59 boost::any ScalarAlgebra::OuterProduct(boost::any const& in0, boost::any const& in1) {
60  return ScalarAlgebra::Multiply(in0, in1);
61  /*if( in0.type()==typeid(double) ) { return Multiply<double>(in0, in1); }
62  if( in0.type()==typeid(float) ) { return Multiply<float>(in0, in1); }
63  if( in0.type()==typeid(int) ) { return Multiply<int>(in0, in1); }
64  if( in0.type()==typeid(unsigned int) ) { return Multiply<unsigned int>(in0, in1); }
65 
66  // something went wrong
67  assert(false);
68  return boost::none;*/
69 }
70 
71 boost::any ScalarAlgebra::Identity(std::type_info const& type) {
72  if( type==typeid(double) ) { return (double)1.0; }
73  if( type==typeid(float) ) { return (float)1.0; }
74  if( type==typeid(int) ) { return (int)1; }
75  if( type==typeid(unsigned int) ) { return (unsigned int)1; }
76 
77  // something went wrong
78  assert(false);
79  return boost::none;
80 }
81 
82 boost::any ScalarAlgebra::Add(boost::any const& in0, boost::any const& in1) {
83  if( in0.type()==typeid(double) ) { return Add<double>(in0, in1); }
84  if( in0.type()==typeid(float) ) { return Add<float>(in0, in1); }
85  if( in0.type()==typeid(int) ) { return Add<int>(in0, in1); }
86  if( in0.type()==typeid(unsigned int) ) { return Add<unsigned int>(in0, in1); }
87 
88  // something went wrong
89  assert(false);
90  return boost::none;
91 }
92 
93 boost::any ScalarAlgebra::Subtract(boost::any const& in0, boost::any const& in1) {
94  if( in0.type()==typeid(double) ) { return Subtract<double>(in0, in1); }
95  if( in0.type()==typeid(float) ) { return Subtract<float>(in0, in1); }
96  if( in0.type()==typeid(int) ) { return Subtract<int>(in0, in1); }
97  if( in0.type()==typeid(unsigned int) ) { return Subtract<unsigned int>(in0, in1); }
98 
99  // something went wrong
100  assert(false);
101  return boost::none;
102 }
103 
104 boost::any ScalarAlgebra::Inverse(boost::any const& obj) {
105  if( obj.type()==typeid(double) ) { return 1.0/boost::any_cast<double const>(obj); }
106  if( obj.type()==typeid(float) ) { return (float)1.0/boost::any_cast<float const>(obj); }
107  if( obj.type()==typeid(int) ) { return 1.0/boost::any_cast<int const>(obj); }
108  if( obj.type()==typeid(unsigned int) ) { return 1.0/boost::any_cast<unsigned int const>(obj); }
109 
110  // something went wrong
111  assert(false);
112  return boost::none;
113 }
114 
115 boost::any ScalarAlgebra::Multiply(boost::any const& in0, boost::any const& in1) {
116  if( in0.type()==typeid(double) ) { return Multiply<double>(in0, in1); }
117  if( in0.type()==typeid(float) ) { return Multiply<float>(in0, in1); }
118  if( in0.type()==typeid(int) ) { return Multiply<int>(in0, in1); }
119  if( in0.type()==typeid(unsigned int) ) { return Multiply<unsigned int>(in0, in1); }
120 
121  // something went wrong
122  assert(false);
123  return boost::none;
124 }
125 
126 boost::any ScalarAlgebra::SquareRoot(boost::any const& obj) {
127  if( obj.type()==typeid(double) ) { return std::sqrt(boost::any_cast<double>(obj)); }
128  if( obj.type()==typeid(float) ) { return std::sqrt(boost::any_cast<float>(obj)); }
129  if( obj.type()==typeid(int) ) { return std::sqrt(boost::any_cast<int>(obj)); }
130  if( obj.type()==typeid(unsigned int) ) { return std::sqrt(boost::any_cast<unsigned int>(obj)); }
131 
132  // something went wrong
133  assert(false);
134  return boost::none;
135 }
136 
137 double ScalarAlgebra::LogDeterminate(boost::any const& obj) {
138  if( typeid(double)==obj.type() ) { return std::log(Magnitude<double>(obj)); }
139  if( typeid(float)==obj.type() ) { return std::log(Magnitude<float>(obj)); }
140  if( typeid(int)==obj.type() ) { return std::log(Magnitude<int>(obj)); }
141  if( typeid(unsigned int)==obj.type() ) { return std::log(Magnitude<unsigned int>(obj)); }
142 
143  // something went wrong
144  assert(false);
145  return -1.0;
146 }
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 OuterProduct(boost::any const &vec1, boost::any const &vec2)
The outer product between 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 double InnerProduct(boost::any const &vec1, boost::any const &vec2)
The inner product between two scalars.
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 Zero(std::type_info const &type)
Compute a zero scalar.