KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
n_eig.h
Go to the documentation of this file.
1 /* $Id: n_eig.h 14050 2013-03-08 04:47:06Z jguan1 $ */
2 /* {{{=========================================================================== *
3  |
4  | Copyright (c) 1994-2011 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 // vim: tabstop=4 shiftwidth=4 foldmethod=marker
21 
22 #include <l_cpp/l_exception.h>
23 #include <m_cpp/m_matrix.h>
24 #include <m_cpp/m_vector.h>
25 #include <n/n_diagonalize.h>
26 
27 #ifdef KJB_HAVE_BOOST
28 #include <boost/tuple/tuple.hpp>
29 #endif
30 
31 #ifndef KJB_CPP_EIG_H
32 #define KJB_CPP_EIG_H
33 
34 namespace kjb {
35 
36 
38 inline
39 void diagonalize (const Matrix& M, Matrix& eig_vectors, Vector& eig_values, bool symmetric = false)
40 {
41  if(M.get_num_rows() != M.get_num_cols())
42  KJB_THROW_2(Illegal_argument, "diagonalize() failed: M matrix must be square.");
43 
44  // If not already the right size, resize them.
45  // If already the right size, or larger, storage is re-used
46  eig_values.resize(M.get_num_rows());
47  eig_vectors.resize(M.get_num_rows(), M.get_num_cols());
48 
49  // Get at the underlying c-pointer, so we can pass to the c implementation.
50  // This is normally discouraged, but it allows us to reuse the allocated memory if possible.
51  kjb_c::Vector* c_eig_values = eig_values.get_underlying_representation_with_guilt();
52  kjb_c::Matrix* c_eig_vectors = eig_vectors.get_underlying_representation_with_guilt();
53 
54  // make the C call
55  if(symmetric)
56  ETX(kjb_c::diagonalize_symmetric(M.get_c_matrix(), &c_eig_vectors, &c_eig_values));
57  else
58  ETX(kjb_c::diagonalize(M.get_c_matrix(), &c_eig_vectors, &c_eig_values));
59 
60 
61 }
62 
63 
69 #ifdef KJB_HAVE_BOOST
70 inline
71 boost::tuple<Matrix, Vector> eig(const Matrix& M, bool symmetric = false)
72 {
73  Matrix evec;
74  Vector eval;
75 
76  diagonalize(M, evec, eval, symmetric);
77 
78  return boost::make_tuple(evec, eval);
79 }
80 #endif
81 
82 } // namespace kjb
83 
84 #endif
Vector & resize(int new_length, Value_type pad=Value_type(0))
Resize vector, retaining previous values.
Definition: m_vector.cpp:242
#define ETX(a)
Definition: l_exception.h:67
Definition for the Matrix class, a thin wrapper on the KJB Matrix struct and its related functionalit...
void diagonalize(const Matrix &M, Matrix &eig_vectors, Vector &eig_values, bool symmetric=false)
KJB c-style syntax for eigenvalue decomposition.
Definition: n_eig.h:39
int get_num_rows() const
Return the number of rows in the matrix.
Definition: m_matrix.h:543
Impl_type *& get_underlying_representation_with_guilt()
Get pointer to the underlying kjb_c::Matrix C struct.
Definition: m_matrix.h:591
This class implements vectors, in the linear-algebra sense, with real-valued elements.
Definition: m_vector.h:87
Impl_type *& get_underlying_representation_with_guilt()
Get pointer to the underlying kjb_c::Vector C struct.
Definition: m_vector.h:857
int get_num_cols() const
Return the number of columns in the matrix.
Definition: m_matrix.h:554
#define KJB_THROW_2(ex, msg)
Definition: l_exception.h:48
Matrix & resize(int new_rows, int new_cols, Value_type pad=Value_type(0))
Resize this matrix, retaining previous values. Space is reused if possible. Otherwise requires a new ...
Definition: m_matrix.cpp:457
Object thrown when an argument to a function is not acceptable.
Definition: l_exception.h:377
const Impl_type * get_c_matrix() const
Get const pointer to the underlying kjb_c::Matrix C struct.
Definition: m_matrix.h:601
This class implements matrices, in the linear-algebra sense, with real-valued elements.
Definition: m_matrix.h:94
Support for error handling exception classes in libKJB.
Definition for the Vector class, a thin wrapper on the KJB Vector struct and its related functionalit...