KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
m_mat_util.h
Go to the documentation of this file.
1 /* $Id: m_mat_util.h 18278 2014-11-25 01:42:10Z ksimek $ */
2 /* =========================================================================== *
3  |
4  | Copyright (c) 1994-2010 by Kobus Barnard (author)
5  |
6  | Personal and educational use of this code is granted, provided that this
7  | header is kept intact, and that the authorship is not misrepresented, that
8  | its use is acknowledged in publications, and relevant papers are cited.
9  |
10  | For other use contact the author (kobus AT cs DOT arizona DOT edu).
11  |
12  | Please note that the code in this file has not necessarily been adequately
13  | tested. Naturally, there is no guarantee of performance, support, or fitness
14  | for any particular task. Nonetheless, I am interested in hearing about
15  | problems that you encounter.
16  |
17  | Author: Kyle Simek
18  * =========================================================================== */
19 
20 #ifndef KJB_M_CPP_M_MAT_UTIL_H
21 #define KJB_M_CPP_M_MAT_UTIL_H
22 
23 #include <m_cpp/m_matrix.h>
24 #include <l_cpp/l_int_matrix.h>
25 #include <m_cpp/m_vector.h>
26 #include <l_cpp/l_int_vector.h>
27 
28 #include <iterator>
29 #include <boost/type_traits.hpp>
30 #include <boost/concept_check.hpp>
31 #include <boost/multi_array.hpp>
32 
39 namespace kjb
40 {
41 
47 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
48 
49 template<class value_type>
51 {};
52 
53 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
54 
55 template<>
56 struct Matrix_traits<int>
57 {
59 };
60 
61 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
62 
63 template<>
64 struct Matrix_traits<double>
65 {
67 };
68 
69 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
70 
74 template <class Iterator>
76 create_matrix_from_rows(Iterator begin, Iterator end)
77 {
78  using namespace boost;
79  typedef typename std::iterator_traits<Iterator>::value_type Vector_type;
80  typedef typename Vector_type::value_type Value_type;
81  typedef typename Matrix_traits<Value_type>::Matrix_type Matrix_type;
82 
83  Matrix_type result = create_matrix_from_columns(begin, end);
84 
85  return matrix_transpose(result);
86 }
87 
88 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
89 
93 template <class Iterator>
94 typename Matrix_traits<typename std::iterator_traits<Iterator>::value_type::value_type>::Matrix_type
95 create_matrix_from_columns(const Iterator& begin, const Iterator& end)
96 {
97  using namespace boost;
98  typedef typename std::iterator_traits<Iterator>::value_type Vector_type;
99  typedef typename Vector_type::value_type Value_type;
100  typedef typename Matrix_traits<Value_type>::Matrix_type Matrix_type;
101  typedef typename Vector_type::size_type Size_type;
102 
103  BOOST_CONCEPT_ASSERT((kjb::SimpleVector<Vector_type>));
104 
105  if ( begin == end )
106  {
107  return Matrix_type();
108  }
109 
110  const Size_type NUM_COLS = std::distance(begin, end);
111  const Size_type NUM_ROWS = (*begin).size();
112 
113  for( Iterator it = begin; it != end; ++it)
114  {
115  const Vector_type& column = *it;
116  if ( column.size() != NUM_ROWS )
117  {
119  "Input vectors differ in length" );
120  }
121  }
122 
123  if ( 0 == NUM_ROWS )
124  {
125  return Matrix_type();
126  }
127 
128  Matrix_type result( NUM_ROWS, NUM_COLS );
129 
130  int col = 0;
131  for( Iterator it = begin; it != end; ++it )
132  {
133  result.set_col( col, it->begin(), it->end());
134  col++;
135  }
136 
137  return result;
138 }
139 
140 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
141 
145 template <class Container_>
146 inline
147 typename Matrix_traits<typename Container_::value_type::value_type>::Matrix_type
148 create_matrix_from_columns( const Container_& cols )
149 {
150  using namespace ::boost;
151  BOOST_CONCEPT_ASSERT((Container<Container_>));
152  return create_matrix_from_columns(cols.begin(), cols.end());
153 }
154 
155 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
156 
160 template <class Container_>
161 inline
162 typename Matrix_traits<typename Container_::value_type::value_type>::Matrix_type
163 create_matrix_from_rows( const Container_& rows )
164 {
165  using namespace ::boost;
166  BOOST_CONCEPT_ASSERT((Container<Container_>));
168 
169  Matrix_type result = create_matrix_from_columns(rows);
170  return matrix_transpose(result);
171 }
172 
173 
174 typedef boost::multi_array_ref<double, 2> Matrix_stl_view;
181 Matrix_stl_view get_matrix_stl_view(kjb::Matrix& mat);
182 
185 } // namespace kjb
186 
187 #endif
Definition for the Int_matrix class, a thin wrapper on the KJB Int_matrix struct and its related func...
Definition for the Matrix class, a thin wrapper on the KJB Matrix struct and its related functionalit...
Definition: m_concept.h:30
Object thrown when an argument is of the wrong size or dimensions.
Definition: l_exception.h:426
This class implements matrices, in the linear-algebra sense, restricted to integer-valued elements...
Definition: l_int_matrix.h:71
Matrix_traits< typename std::iterator_traits< Iterator >::value_type::value_type >::Matrix_type create_matrix_from_columns(const Iterator &begin, const Iterator &end)
Build a matrix from a container of column vectors.
Definition: m_mat_util.h:95
kjb::Matrix Matrix_type
Definition: m_mat_util.h:66
Matrix_traits< typename std::iterator_traits< Iterator >::value_type::value_type >::Matrix_type create_matrix_from_rows(Iterator begin, Iterator end)
Build a matrix from a container of row vectors.
Definition: m_mat_util.h:76
Definition: m_mat_util.h:50
Matrix_stl_view get_matrix_stl_view(kjb::Matrix &mat)
Definition: m_mat_util.cpp:26
Int_matrix matrix_transpose(const Int_matrix &op1)
Test for any difference between two matrices.
Definition: l_int_matrix.h:1331
#define KJB_THROW_2(ex, msg)
Definition: l_exception.h:48
boost::multi_array_ref< double, 2 > Matrix_stl_view
Definition: m_mat_util.h:174
kjb::Int_matrix Matrix_type
Definition: m_mat_util.h:58
This class implements matrices, in the linear-algebra sense, with real-valued elements.
Definition: m_matrix.h:94
Definition for the Int_vector class, a thin wrapper on the KJB Int_vector struct and its related func...
Definition for the Vector class, a thin wrapper on the KJB Vector struct and its related functionalit...