KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
l_int_matrix.h
Go to the documentation of this file.
1 /* ======================================================================== *
2  | |
3  | Copyright (c) 2007-2010, by members of University of Arizona Computer |
4  | Vision group (the authors) including |
5  | Luca Del Pero, Andrew Predoehl. |
6  | |
7  | For use outside the University of Arizona Computer Vision group please |
8  | contact Kobus Barnard. |
9  | |
10  * ======================================================================== */
11 
12 /* $Id: l_int_matrix.h 18296 2014-11-26 03:09:46Z qtung $ */
13 
14 #ifndef L_CPP_INT_MATRIX_WRAP_H
15 #define L_CPP_INT_MATRIX_WRAP_H
16 
37 #include "l/l_int_matrix.h"
38 #include "l/l_int_mat_io.h"
39 #include "l_cpp/l_exception.h"
40 #include "l_cpp/l_int_vector.h"
41 #include <vector>
42 #include <ostream>
43 
44 #ifdef KJB_HAVE_BST_SERIAL
45 #include <boost/serialization/access.hpp>
46 #endif
47 
48 
49 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
50 
51 
52 namespace kjb {
53 
59 class Matrix;
60 
72 {
73 #ifdef KJB_HAVE_BST_SERIAL
74  friend class boost::serialization::access;
75 #endif
76 public:
77 
78  /* Class users: to make your code as maintanable as possible, use the
79  * *_type typedefs below instead of directly referencing the specific
80  * type.
81  */
82 
83  typedef int Value_type;
84  typedef int Size_type;
85  typedef kjb_c::Int_matrix Impl_type;
86  typedef Int_matrix Mat_type;
87  typedef Int_vector Vec_type;
89 
90 private:
91 
92  Impl_type* m_matrix;
93 
103  void compute_row_col(
104  int index,
105  int* row,
106  int* col
107  ) const
108  {
109  // Test program was HERE.
110  *row = index / get_num_cols();
111  *col = index - *row * get_num_cols();
112  }
113 
122  void compute_row_col_carefully(
123  int index,
124  int* row,
125  int* col
126  ) const
127  {
128  if ( 0 == get_num_cols() )
129  {
130  // Test program was HERE.
131  *row = *col = 0;
132  }
133  else
134  {
135  // Test program was HERE.
136  compute_row_col( index, row, col );
137  }
138  }
139 
140  void throw_bad_bounds( int row_ix, int col_ix ) const;
141 
142 public:
143 
144  /* ------------------------------------------------------------------
145  * CONSTRUCTORS
146  * ------------------------------------------------------------------ */
147 
152  : m_matrix( 0 )
153  {
154  // Test program was HERE.
155  ETX( kjb_c::get_target_int_matrix( &m_matrix, 0, 0 ) );
156  }
157 
158  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
159 
163  Int_matrix(int rows, int cols)
164  : m_matrix( 0 )
165  {
166  // Test program was HERE.
167  ETX( kjb_c::get_target_int_matrix( &m_matrix, rows, cols ) );
168  }
169 
170  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
171 
176  Int_matrix(unsigned rows, unsigned cols)
177  : m_matrix( 0 )
178  {
179  // Test program was HERE.
180  Int_matrix m( static_cast<int>( rows ), static_cast<int>( cols ) );
181  swap( m );
182  }
183 
184  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
185 
190  Int_matrix(unsigned rows, unsigned cols, Value_type val)
191  : m_matrix( 0 )
192  {
193  // Test program was HERE.
194  Int_matrix m( static_cast<int>( rows ), static_cast<int>( cols ), val );
195  swap( m );
196  }
197 
198  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
199 
204  Int_matrix(unsigned long rows, unsigned long cols)
205  : m_matrix( 0 )
206  {
207  // Test program was HERE.
208  Int_matrix m( static_cast<int>( rows ), static_cast<int>( cols ) );
209  swap( m );
210  }
211 
212  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
213 
218  Int_matrix(unsigned long rows, unsigned long cols, Value_type val)
219  : m_matrix( 0 )
220  {
221  // Test program was HERE.
222  Int_matrix m( static_cast<int>( rows ), static_cast<int>( cols ), val );
223  swap( m );
224  }
225 
226  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
227 
231  Int_matrix(int rows, int cols, Value_type num)
232  : m_matrix( 0 )
233  {
234  // Test program was HERE.
235  ETX( kjb_c::get_initialized_int_matrix( &m_matrix, rows, cols, num ) );
236  }
237 
238  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
239 
246  Int_matrix(int rows, int cols, const Value_type* data);
247 
248  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
249 
250 
251  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
252 
272  : m_matrix( mat_ptr )
273  {
274  if ( 0 == mat_ptr )
275  {
276  // Test program was HERE.
277  ETX( kjb_c::get_target_int_matrix( &m_matrix, 0, 0 ) );
278  }
279  //else
280  //{
281  // // Test program was HERE.
282  //}
283  }
284 
285  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
286 
296  explicit Int_matrix(const Impl_type& mat_ref)
297  : m_matrix( 0 )
298  {
299  // Test program was HERE.
300  ETX( kjb_c::copy_int_matrix( &m_matrix, &mat_ref ) );
301  }
302 
303  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
304 
309  Int_matrix(const std::string& file_name);
310 
311  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
312 
316  Int_matrix(const Int_matrix& mat_ref)
317  : m_matrix( 0 )
318  {
319  // Test program was HERE.
320  ETX( kjb_c::copy_int_matrix( &m_matrix, mat_ref.m_matrix ) );
321  }
322 
323  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
324 
325 
327  {
328  // Test program was HERE.
329  kjb_c::free_int_matrix( m_matrix );
330  }
331 
332 
333  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
334 
335 
343  /* Int_matrix& init_identity( int rank )
344  {
345  // Test program was HERE.
346  ETX( kjb_c::get_int_identity_matrix( &m_matrix, rank ) );
347  return *this;
348  } */
349 
350  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
351 
352 
359  Int_matrix& zero_out( int num_rows, int num_cols )
360  {
361  // Test program was HERE.
362  ETX( kjb_c::get_zero_int_matrix( &m_matrix, num_rows, num_cols ) );
363  return *this;
364  }
365 
366  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
367 
374  Int_matrix& zero_out( int rows )
375  {
376  // Test program was HERE.
377  return zero_out( rows, rows );
378  }
379 
380  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
381 
388  {
389  // Test program was HERE.
390  return zero_out( get_num_rows(), get_num_cols() );
391  }
392 
393  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
394 
400  /* Int_matrix& init_zero(int num_rows, int num_cols)
401  {
402  // Test program was HERE.
403  return zero_out( num_rows, num_cols );
404  } */
405 
406 
407  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
408 
409 
410  /* ------------------------------------------------------------------
411  * ASSIGNMENT OPERATORS
412  * ------------------------------------------------------------------ */
413 
425  Int_matrix& operator=(const Impl_type& mat_ref)
426  {
427  if ( m_matrix != &mat_ref )
428  {
429  // Test program was HERE.
430  ETX( kjb_c::copy_int_matrix( &m_matrix, &mat_ref ) );
431  }
432  //else
433  //{
434  // // Test program was HERE.
435  //}
436  return *this;
437  }
438 
439  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
440 
447  {
448  // Test program was HERE.
449  // call assignment operator for kjb_c::Int_matrix
450  return operator=( *src.m_matrix );
451  }
452 
453 
454  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
455 
459  void swap( Int_matrix& other )
460  {
461  // Test program was HERE.
462  std::swap( m_matrix, other.m_matrix );
463  }
464 
465  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
466 
470  int get_num_rows() const
471  {
472  // Test program was HERE.
473  return m_matrix -> num_rows;
474  }
475 
476  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
477 
481  int get_num_cols() const
482  {
483  // Test program was HERE.
484  return m_matrix -> num_cols;
485  }
486 
487  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
488 
493  int get_length() const // rows * cols
494  {
495  // Test program was HERE.
496  return get_num_rows() * get_num_cols();
497  }
498 
499  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
500 
504  const Impl_type* get_c_matrix() const
505  {
506  // Test program was HERE.
507  return m_matrix;
508  }
509 
510  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
511 
517  {
518  return m_matrix;
519  }
520 
524  {
525  if(!m_matrix)
526  {
527  KJB_THROW_2(KJB_error,"Trying to access an integer matrix that has not been allocated");
528  }
529  return m_matrix->elements;
530  }
531 
532  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
533 
538  int new_rows,
539  int new_cols,
540  Value_type pad = Value_type(0)
541  );
542 
543  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
544 
549  int new_rows,
550  int new_cols)
551  {
552  ETX(kjb_c::get_target_int_matrix(&m_matrix, new_rows, new_cols));
553 
554  return *this;
555  }
556  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
557 
563  {
564  // Test program was HERE.
565  kjb_c::Int_matrix* result = 0;
566  ETX(kjb_c::get_int_transpose( &result, m_matrix ) );
567  return Int_matrix(result);
568  }
569 
570  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
571 
572  /* (Inverse is not supported for Int_matrix, although with "big integers"
573  * one could make a Rational_matrix class, and perform potentially exact
574  * inversion. But would anyone use it?
575  */
576 
577  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
578 
586  void check_bounds( int row, int col ) const
587  {
588  if( row < 0 || get_num_rows() <= row
589  || col < 0 || get_num_cols() <= col )
590  {
591  // Test program was HERE.
592  throw_bad_bounds( row, col );
593  }
594  //else
595  //{
596  // // Test program was HERE.
597  //}
598  }
599 
600  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
601 
609  {
610  // Test program was HERE.
611  return operator()( i );
612  }
613 
614  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
615 
623  {
624  // Test program was HERE.
625  return operator()( i );
626  }
627 
628  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
629 
637  {
638  // Test program was HERE.
639  int row, col;
640  compute_row_col( i, &row, &col );
641  return operator()( row, col );
642  }
643 
644  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
645 
653  {
654  // Test program was HERE.
655  int row, col;
656  compute_row_col( i, &row, &col );
657  return operator()( row, col );
658  }
659 
660  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
661 
670  Value_type& operator()(int row, int col)
671  {
672  // Test program was HERE.
673  return m_matrix -> elements[ row ][ col ];
674  }
675 
676  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
677 
686  Value_type operator()(int row, int col) const
687  {
688  // Test program was HERE.
689  return m_matrix -> elements[ row ][ col ];
690  }
691 
692  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
693 
699  {
700  // Test program was HERE.
701  int row, col;
702  compute_row_col_carefully( i, &row, &col );
703  check_bounds( row, col );
704  return operator()( row, col );
705  }
706 
707  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
708 
713  Value_type at(int i) const
714  {
715  // Test program was HERE.
716  int row, col;
717  compute_row_col_carefully( i, &row, &col );
718  check_bounds( row, col );
719  return operator()( row, col );
720  }
721 
722  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
723 
727  Value_type& at(int row, int col)
728  {
729  // Test program was HERE.
730  check_bounds( row, col );
731  return operator()(row, col);
732  }
733 
734  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
735 
739  Value_type at(int row, int col) const
740  {
741  // Test program was HERE.
742  check_bounds( row, col );
743  return operator()(row, col);
744  }
745 
746 
747  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
748 
749 
750  /* ------------------------------------------------------------------
751  * EXTRACTION ROUTINES
752  * ------------------------------------------------------------------ */
753 
758  Vec_type get_row(int row) const;
759 
760  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
761 
766  Vec_type get_col(int col) const;
767 
768  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
769 
773  template <class Generic_vector>
774  void set_row(int row, const Generic_vector& v);
775 
776  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
777 
781  template <class Iterator>
782  void set_row(int row, Iterator begin, Iterator end);
783 
784  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
785 
790  template <class Generic_vector>
791  void set_col(int col, const Generic_vector& v);
792 
793  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
794 
798  template <class Iterator>
799  void set_col(int col, Iterator begin, Iterator end);
800 
801  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
802 
803 
812  int write( const char* filename = 0 ) const
813  {
814  // Test program was HERE.
815  return kjb_c::write_int_matrix( m_matrix, filename );
816  }
817 
818 
819  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
820 
821  /* ------------------------------------------------------------------
822  * ARITHMETIC OPERATORS
823  * Modifying arithmetic operators. That is, operators that modify
824  * this matrix when applied. The non-modifying arithmetic operators
825  * are non-member functions (and are below).
826  * ------------------------------------------------------------------ */
827 
837  {
838  // Test program was HERE.
839  ETX(multiply_int_matrices(&m_matrix, m_matrix, op2.m_matrix));
840  return *this;
841  }
842 
843  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
844 
854  {
855  // Test program was HERE.
856  return operator*=( op2 );
857  }
858 
859  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
860 
865  {
866  // Test program was HERE.
867  ETX(kjb_c::ow_multiply_int_matrix_by_int_scalar(m_matrix, op2));
868  return *this;
869  }
870 
871  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
872 
877  {
878  // Test program was HERE.
879  return operator*=( op2 );
880  }
881 
882 
883  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
884 
893 
894  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
895 
904  {
905  // Test program was HERE.
906  return operator/=( op2 );
907  }
908 
909  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
910 
915  {
916  // Test program was HERE.
917  ETX(kjb_c::ow_add_int_matrices(m_matrix, op2.m_matrix));
918  return *this;
919  }
920 
921  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
922 
926  Int_matrix& add(const Int_matrix& op2)
927  {
928  // Test program was HERE.
929  return operator+=( op2 );
930  }
931 
932  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
933 
938  {
939  // Test program was HERE.
940  ETX(kjb_c::ow_subtract_int_matrices(m_matrix, op2.m_matrix));
941  return *this;
942  }
943 
944  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
945 
950  {
951  // Test program was HERE.
952  return operator-=( op2 );
953  }
954 
955  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
956 
962  {
963  // Test program was HERE.
964  return operator*=( Value_type(-1) );
965  }
966 
967  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
968 
975  Mat_type& mapcar( Mapper );
976 
977  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
978 
979 
980 
981 private:
982  template<class Archive>
983  void serialize(Archive &ar, const unsigned int version)
984  {
985  return kjb_serialize(ar, *this, version);
986  }
987 };
988 
989 /* ------------------------------------------------------------------
990  * TEMPLATE MEMBERS
991  * ------------------------------------------------------------------ */
992 
993 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
994 
995 template <class Generic_vector>
996 void Int_matrix::set_row( int row, const Generic_vector& v )
997 {
998  // Test program was HERE.
999  using namespace boost;
1000  BOOST_CONCEPT_ASSERT((Container<Generic_vector>));
1001 
1002  typedef typename Generic_vector::value_type Vector_value_type;
1003  BOOST_CONCEPT_ASSERT((Convertible<Vector_value_type, Value_type>));
1004 
1005  set_row(row, v.begin(), v.end());
1006 }
1007 
1008 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1009 
1010 template <class Iterator>
1011 void Int_matrix::set_row( int row, Iterator begin, Iterator end )
1012 {
1013  Iterator it = begin;
1014  for ( int col = 0; col < get_num_cols(); ++col )
1015  {
1016  if(it == end)
1017  {
1018  // range too short
1020  }
1021 
1022  operator()(row, col) = *it;
1023  it++;
1024  }
1025 
1026  if(it != end)
1027  {
1028  // range too long
1030  }
1031 }
1032 
1033 
1034 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1035 
1036 
1037 template <class Generic_vector>
1038 void Int_matrix::set_col(int col, const Generic_vector& v)
1039 {
1040  // Test program was HERE.
1041  using namespace boost;
1042  BOOST_CONCEPT_ASSERT((Container<Generic_vector>));
1043 
1044  typedef typename Generic_vector::value_type Vector_value_type;
1045  BOOST_CONCEPT_ASSERT((Convertible<Vector_value_type, Value_type>));
1046 
1047  set_col(col, v.begin(), v.end());
1048 }
1049 
1050 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1051 
1052 template <class Iterator>
1053 void Int_matrix::set_col(int col, Iterator begin, Iterator end)
1054 {
1055  Iterator it = begin;
1056  for ( int row = 0; row < get_num_rows(); ++row )
1057  {
1058  if(it == end)
1059  {
1060  // range is too short
1062  }
1063 
1064  operator()(row, col) = *it;
1065  it++;
1066  }
1067 
1068  if(it != end)
1069  {
1070  // range is too short
1072  }
1073 
1074 }
1075 
1076 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1077 
1078 /* ------------------------------------------------------------------
1079  * "NAMED CONSTRUCTORS"
1080  * The "named constructor" idiom is when we use functions to create
1081  * objects. The benefit is that the names indicate the purpose of the
1082  * method. The following functions act as named constructors.
1083  * ------------------------------------------------------------------ */
1084 
1088 inline
1090 {
1091  // Test program was HERE.
1092  kjb_c::Int_matrix* result = 0;
1093  ETX( kjb_c::get_int_identity_matrix( &result, rank ) );
1094  return Int_matrix(result);
1095 }
1096 
1097 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1098 
1102 inline
1103 Int_matrix create_zero_int_matrix( int rows, int columns )
1104 {
1105  // Test program was HERE.
1106  kjb_c::Int_matrix* result = 0;
1107  ETX( kjb_c::get_zero_int_matrix( &result, rows, columns ) );
1108  return Int_matrix(result);
1109 }
1110 
1111 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1112 
1116 inline
1118 {
1119  // Test program was HERE.
1120  return create_zero_int_matrix( rows, rows );
1121 }
1122 
1123 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1124 
1129 
1130 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1131 
1136 
1137 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1138 
1143 
1144 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1145 
1146 /* ------------------------------------------------------------------
1147  * ARITHMETIC OPERATORS
1148  * Non-modifying arithmetic operators. That is, operators that do not
1149  * modify the operators when applied. The modifying arithmetic operators
1150  * are member functions.
1151  * ------------------------------------------------------------------ */
1152 
1158 inline
1160 {
1161  // Test program was HERE.
1162  return Int_matrix(op1) *= op2;
1163 }
1164 
1165 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1166 
1171 inline
1173 {
1174  // Test program was HERE.
1175  return Int_matrix(op1) *= op2;
1176 }
1177 
1178 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1179 
1183 inline
1185 {
1186  // Test program was HERE.
1187  return op2 * op1;
1188 }
1189 
1190 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1191 
1198 inline
1200 {
1201  // Test program was HERE.
1202  return Int_matrix(op1) /= op2;
1203 }
1204 
1205 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1206 
1210 inline
1212 {
1213  // Test program was HERE.
1214  return Int_matrix(op1) += op2;
1215 }
1216 
1217 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1218 
1222 inline
1224 {
1225  // Test program was HERE.
1226  return Int_matrix(op1) -= op2;
1227 }
1228 
1229 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1230 
1234 inline
1236 {
1237  // Test program was HERE.
1238  return op1 * (-1);
1239 }
1240 
1241 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1242 
1243 /* ------------------------------------------------------------------
1244  * COMPARISON OPERATORS
1245  * Comparison operators -- i.e., == and !=.
1246  * ------------------------------------------------------------------ */
1247 
1252 bool operator==(const Int_matrix& op1, const Int_matrix::Impl_type& op2);
1253 
1254 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1255 
1260 inline
1261 bool operator==(const Int_matrix& op1, const Int_matrix& op2)
1262 {
1263  // Test program was HERE.
1264  return op1 == *op2.get_c_matrix();
1265 }
1266 
1267 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1268 
1273 inline
1274 bool operator!=(const Int_matrix& op1, const Int_matrix::Impl_type& op2)
1275 {
1276  // Test program was HERE.
1277  return !(op1 == op2);
1278 }
1279 
1280 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1281 
1286 inline
1287 bool operator!=(const Int_matrix& op1, const Int_matrix& op2)
1288 {
1289  // Test program was HERE.
1290  return !(op1 == op2);
1291 }
1292 
1293 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1294 
1299 inline
1300 bool operator==( const Int_matrix::Impl_type& op1, const Int_matrix& op2 )
1301 {
1302  // Test program was HERE.
1303  return op2 == op1;
1304 }
1305 
1306 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1307 
1312 inline
1313 bool operator!=( const Int_matrix::Impl_type& op1, const Int_matrix& op2 )
1314 {
1315  // Test program was HERE.
1316  return op2 != op1;
1317 }
1318 
1319 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1320 
1321 /* ------------------------------------------------------------------
1322  * OTHER STUFF
1323  * Other stuff =)
1324  * ------------------------------------------------------------------ */
1325 
1330 inline
1332 {
1333  kjb_c::Int_matrix* kjb_matrix = 0;
1334  ETX( kjb_c::get_int_transpose( &kjb_matrix, op1.get_c_matrix() ) );
1335  return Int_matrix(kjb_matrix);
1336 }
1337 
1338 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1339 
1343 inline
1345 {
1346  // Test program was HERE.
1347  kjb_c::Int_matrix* result = 0;
1348  ETX( kjb_c::get_abs_of_int_matrix( &result, mat.get_c_matrix() ) );
1349  return Int_matrix(result);
1350 }
1351 
1352 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1353 
1363 inline
1365 {
1366  if ( op1.get_num_rows() != op2.get_num_rows()
1367  || op1.get_num_cols() != op2.get_num_cols() )
1368  {
1369  // Test program was HERE.
1371  }
1372  //else
1373  //{
1374  // // Test program was HERE.
1375  //}
1376  return kjb_c::max_abs_int_matrix_difference( op1.get_c_matrix(), op2.get_c_matrix() );
1377 }
1378 
1379 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1380 
1384 inline
1386 {
1387  // Test program was HERE.
1388  return kjb_c::min_int_matrix_element(mat.get_c_matrix());
1389 }
1390 
1391 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1392 
1396 inline
1398 {
1399  // Test program was HERE.
1400  return kjb_c::max_int_matrix_element(mat.get_c_matrix());
1401 }
1402 
1403 
1404 
1405 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1406 
1413 std::ostream& operator<<(std::ostream& out, const Int_matrix& m);
1414 
1415 
1418 } // namespace kjb
1419 #endif
1420 
Int_matrix create_row_matrix(const Int_matrix::Vec_type &vec)
Construct a one-row matrix by deep-copying a vector.
Definition: l_int_matrix.cpp:105
Int_matrix & operator=(const Int_matrix &src)
Assign contents from a kjb::Int_matrix, a C++ object.
Definition: l_int_matrix.h:446
Value_type & operator[](int i)
Subscript matrix like a one-dimensional C array, e.g., A[10], using row-major ordering, and returning an lvalue.
Definition: l_int_matrix.h:608
#define ETX(a)
Definition: l_exception.h:67
void set_row(int row, const Generic_vector &v)
Replace a row of the matrix with the given vector.
Definition: l_int_matrix.h:996
Int_matrix::Value_type max(const Int_matrix &mat)
Return the maximum value in this matrix.
Definition: l_int_matrix.h:1397
kjb_c::Int_matrix Impl_type
the underlying implementation
Definition: l_int_matrix.h:85
Value_type at(int i) const
Access matrix like a one-dimensional array, using row-major ordering, and returning an rvalue...
Definition: l_int_matrix.h:713
Value_type(* Mapper)(Value_type)
element transformer fun
Definition: l_int_matrix.h:88
Object thrown when an argument is of the wrong size or dimensions.
Definition: l_exception.h:426
Mat_type & mapcar(Mapper)
Transform the elements of a matrix.
Definition: l_int_matrix.cpp:252
This class implements matrices, in the linear-algebra sense, restricted to integer-valued elements...
Definition: l_int_matrix.h:71
Value_type at(int row, int col) const
Access matrix with bounds checking and return an rvalue.
Definition: l_int_matrix.h:739
Int_matrix(unsigned long rows, unsigned long cols)
Ctor builds a matrix of specified number of rows and columns.
Definition: l_int_matrix.h:204
Int_matrix::Value_type max_abs_difference(const Int_matrix &op1, const Int_matrix &op2)
Find the largest difference between two matrices.
Definition: l_int_matrix.h:1364
Int_matrix create_diagonal_matrix(const Int_matrix::Vec_type &diagonal)
Construct a one-row matrix by deep-copying a vector.
Definition: l_int_matrix.cpp:118
Int_matrix Mat_type
the associated matrix type
Definition: l_int_matrix.h:86
Int_matrix & operator*=(const Int_matrix &op2)
Compute product of this matrix (on the left) and another, and replace this matrix with the result...
Definition: l_int_matrix.h:836
Int_matrix & multiply(const Int_matrix &op2)
Compute product of this matrix (on the left) and another, and replace this matrix with the result...
Definition: l_int_matrix.h:853
~Int_matrix()
Definition: l_int_matrix.h:326
Int_matrix & operator-=(const Int_matrix &op2)
Subtract another matrix from this matrix, in place.
Definition: l_int_matrix.h:937
int Size_type
Definition: l_int_matrix.h:84
Int_matrix & subtract(const Int_matrix &op2)
Subtract another matrix from this matrix, in place.
Definition: l_int_matrix.h:949
Image operator-(const Image &im1, const Image &im2)
Subtract two images.
Definition: i_image.h:843
#define KJB_THROW(ex)
Definition: l_exception.h:46
Int_matrix(const Int_matrix &mat_ref)
Copy ctor.
Definition: l_int_matrix.h:316
Int_matrix transpose()
Transpose this matrix, in-place.
Definition: l_int_matrix.h:562
Int_matrix create_column_matrix(const Int_matrix::Vec_type &vec)
Construct a one-column matrix by deep-copying a vector.
Definition: l_int_matrix.cpp:127
void check_bounds(int row, int col) const
Test whether the given row and column indices are valid.
Definition: l_int_matrix.h:586
Value_type ** ptr_to_storage_area() const
Access a pointer to the underlying implementation, use with care.
Definition: l_int_matrix.h:523
Int_matrix(int rows, int cols)
Ctor builds a matrix of specified number of rows and columns.
Definition: l_int_matrix.h:163
Value_type operator()(int row, int col) const
Access matrix like a Fortran or MATLAB two-dimensional array, e.g., A(2,4), and return an rvalue...
Definition: l_int_matrix.h:686
Image operator+(const Image &op1, const Image &op2)
Add two images.
Definition: i_image.h:834
int Value_type
data type of the elements
Definition: l_int_matrix.h:83
Int_matrix & zero_out(int num_rows, int num_cols)
Resize matrix, replace contents with the identity matrix.
Definition: l_int_matrix.h:359
void kjb_serialize(Archive &ar, KJB_readable_writable &obj, const unsigned int version)
Definition: l_serialization.h:170
Int_matrix & operator/=(Int_matrix::Value_type op2)
Perform integer division on each entry in the matrix by a scalar value.
Definition: l_int_matrix.cpp:192
Value_type & at(int row, int col)
Access matrix with bounds checking and return an lvalue.
Definition: l_int_matrix.h:727
Value_type & operator()(int row, int col)
Access matrix like a Fortran or MATLAB two-dimensional array, e.g., A(2,4), and return an lvalue...
Definition: l_int_matrix.h:670
Value_type operator()(int i) const
MATLAB-style one-dimensional subscript of matrix, e.g., A(10), using row-major ordering, and returning an rvalue.
Definition: l_int_matrix.h:652
Vec_type get_row(int row) const
Return a specified row of this matrix, in the form of an Int_vector.
Definition: l_int_matrix.cpp:168
void set_col(int col, const Generic_vector &v)
Replace a column of the matrix with the given vector. "vector" can be any collection of values conver...
Definition: l_int_matrix.h:1038
Int_matrix & realloc(int new_rows, int new_cols)
Resize this matrix, discarding previous values (faster than resize, because storage is reused) ...
Definition: l_int_matrix.h:548
Int_matrix matrix_transpose(const Int_matrix &op1)
Test for any difference between two matrices.
Definition: l_int_matrix.h:1331
kjb_c::Pixel abs(const kjb_c::Pixel &p)
Take the channel-wise absolute value of a kjb_c::Pixel.
Definition: i_pixel.h:354
Impl_type * get_underlying_representation_with_guilt()
Get pointer to the underlying kjb_c::Matrix C struct.
Definition: l_int_matrix.h:516
Int_vector Vec_type
the associated vector type
Definition: l_int_matrix.h:87
void swap(Int_matrix &other)
Swap the representations of two matrices.
Definition: l_int_matrix.h:459
Value_type & operator()(int i)
MATLAB-style one-dimensional subscript of matrix, e.g., A(10), using row-major ordering, and returning an lvalue.
Definition: l_int_matrix.h:636
This class implements vectors, in the linear-algebra sense, restricted to integer-valued elements...
Definition: l_int_vector.h:83
Int_matrix(int rows, int cols, Value_type num)
Ctor builds a matrix of specified size, all entries set to num.
Definition: l_int_matrix.h:231
Int_matrix & zero_out()
Clobber contents of matrix with zeroes.
Definition: l_int_matrix.h:387
Int_matrix & multiply(Value_type op2)
Multiply this matrix by a scalar, in place.
Definition: l_int_matrix.h:876
std::ofstream & operator<<(std::ofstream &out, const Quaternion &q)
Definition: turntable_camera.cpp:77
Image operator/(const Image &op1, double op2)
Scale an image in channel space, yielding a new image.
Definition: i_image.h:825
#define KJB_THROW_2(ex, msg)
Definition: l_exception.h:48
bool operator!=(const Int_matrix &op1, const Int_matrix::Impl_type &op2)
Test for any difference between two matrices.
Definition: l_int_matrix.h:1274
Int_matrix & divide(Int_matrix::Value_type op2)
Perform integer division on each entry in the matrix by a scalar value.
Definition: l_int_matrix.h:903
const Impl_type * get_c_matrix() const
Get const pointer to the underlying kjb_c::Int_matrix C struct.
Definition: l_int_matrix.h:504
void swap(kjb::Gsl_Multimin_fdf &m1, kjb::Gsl_Multimin_fdf &m2)
Swap two wrapped multimin objects.
Definition: gsl_multimin.h:693
Int_matrix(const Impl_type &mat_ref)
Ctor copies contents (i.e., a deep copy) of an existing C-struct matrix.
Definition: l_int_matrix.h:296
int get_length() const
Return the number of elements in the matrix.
Definition: l_int_matrix.h:493
Int_matrix(unsigned rows, unsigned cols, Value_type val)
Ctor builds a matrix of specified number of rows and columns.
Definition: l_int_matrix.h:190
Int_matrix create_identity_int_matrix(int rank)
Construct an identity matrix of specified rank.
Definition: l_int_matrix.h:1089
Int_matrix & resize(int new_rows, int new_cols, Value_type pad=Value_type(0))
Resize this matrix, retaining previous values.
Definition: l_int_matrix.cpp:140
Exception often thrown when wrapped C functions return error codes.
Definition: l_exception.h:262
bool operator==(const Int_matrix &op1, const Int_matrix::Impl_type &op2)
Test for exact equality between two matrices.
Definition: l_int_matrix.cpp:218
Int_matrix(unsigned long rows, unsigned long cols, Value_type val)
Ctor builds a matrix of specified number of rows and columns.
Definition: l_int_matrix.h:218
Int_matrix & operator+=(const Int_matrix &op2)
Add, in place, a matrix to this matrix.
Definition: l_int_matrix.h:914
Int_matrix::Value_type min(const Int_matrix &mat)
Return the minimum value in this matrix.
Definition: l_int_matrix.h:1385
Value_type & at(int i)
Access matrix like a one-dimensional array, using row-major ordering, and returning an lvalue...
Definition: l_int_matrix.h:698
Value_type operator[](int i) const
Subscript matrix like a one-dimensional C array, e.g., A[10], using row-major ordering, and returning an rvalue.
Definition: l_int_matrix.h:622
int get_num_rows() const
Return the number of rows in the matrix.
Definition: l_int_matrix.h:470
Int_matrix & operator=(const Impl_type &mat_ref)
Clone of zero_out(int,int)
Definition: l_int_matrix.h:425
Int_matrix(unsigned rows, unsigned cols)
Ctor builds a matrix of specified number of rows and columns.
Definition: l_int_matrix.h:176
Int_matrix()
Default ctor builds a matrix of zero rows, zero columns.
Definition: l_int_matrix.h:151
int get_num_cols() const
Return the number of columns in the matrix.
Definition: l_int_matrix.h:481
Int_matrix(Impl_type *mat_ptr)
Conversion ctor: claim ownership of an existing int matrix pointer (i.e., make a shallow copy)...
Definition: l_int_matrix.h:271
get the indices of edges in each direction for i
Definition: APPgetLargeConnectedEdges.m:48
Vec_type get_col(int col) const
Return a specified column of this matrix, in the form of an Int_vector.
Definition: l_int_matrix.cpp:177
for m
Definition: APPgetLargeConnectedEdges.m:64
Support for error handling exception classes in libKJB.
Int_matrix & zero_out(int rows)
Resize matrix to be square, and clobber contents with zeroes.
Definition: l_int_matrix.h:374
Definition for the Int_vector class, a thin wrapper on the KJB Int_vector struct and its related func...
Int_matrix & negate()
Transform this matrix into the additive inverse of its former value, in place.
Definition: l_int_matrix.h:961
Int_matrix create_zero_int_matrix(int rows, int columns)
Construct a zero matrix of specified size.
Definition: l_int_matrix.h:1103
Gsl_Vector operator*(double scalar, const Gsl_Vector &vector)
multiply scalar and vector, scalar written on the left side
Definition: gsl_vector.h:661
int write(const char *filename=0) const
Writes the matrix contents to a file specified by name.
Definition: l_int_matrix.h:812
Int_matrix & add(const Int_matrix &op2)
Add, in place, a matrix to this matrix.
Definition: l_int_matrix.h:926