Optimo  0.1.0
A C++ header library for optimization
 All Classes Functions Variables Pages
matrix_utils.h
1 // Copyright (C) 2013 Victor Fragoso <vfragoso@cs.ucsb.edu>
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following
13 // disclaimer in the documentation and/or other materials provided
14 // with the distribution.
15 //
16 // * Neither the name of the University of California, Santa Barbara nor the
17 // names of its contributors may be used to endorse or promote products
18 // derived from this software without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 // ARE DISCLAIMED. IN NO EVENT SHALL VICTOR FRAGOSO BE LIABLE FOR ANY DIRECT,
24 // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 //
31 
32 #ifndef OPTIMO_UTILS_MATRIX_UTILS_H_
33 #define OPTIMO_UTILS_MATRIX_UTILS_H_
34 
35 #include <Eigen/Core>
36 #include <cmath>
37 
38 namespace optimo {
39 namespace utils {
40 template <typename Scalar, uint m, uint n>
41 inline bool isFinite(const Eigen::Matrix<Scalar, m, n>& x) {
42  bool flag = true;
43  const Scalar* data = x.data();
44  unsigned int i = 0;
45  const unsigned int size = x.size();
46  while (i < size && flag) {
47  flag = std::isfinite(data[i++]);
48  }
49  return flag;
50 }
51 
52 template <typename Scalar>
53 inline bool
54 isFinite(const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>& x) {
55  bool flag = true;
56  const Scalar* data = x.data();
57  unsigned int i = 0;
58  const unsigned int size = x.size();
59  while (i < size && flag) {
60  flag = std::isfinite(data[i++]);
61  }
62  return flag;
63 }
64 
65 template <typename Scalar>
66 inline bool isFinite(const Eigen::Matrix<Scalar, Eigen::Dynamic, 1>& x) {
67  bool flag = true;
68  const Scalar* data = x.data();
69  unsigned int i = 0;
70  const unsigned int size = x.size();
71  while (i < size && flag) {
72  flag = std::isfinite(data[i++]);
73  }
74  return flag;
75 }
76 } // utils
77 } // optimo
78 #endif