📄 vnl_matrix.h
字号:
void set_row (unsigned i, T const * v);
//: Set the elements of the i'th row to value
void set_row (unsigned i, T value );
//: Set the i-th row
void set_row (unsigned i, vnl_vector<T> const&);
//: Extract a sub-matrix of size rows x cols, starting at (top,left)
// Thus it contains elements [top,top+rows-1][left,left+cols-1]
vnl_matrix<T> extract (unsigned rows, unsigned cols,
unsigned top=0, unsigned left=0) const;
//: Get a vector equal to the given row
vnl_vector<T> get_row (unsigned row) const;
//: Get a vector equal to the given column
vnl_vector<T> get_column(unsigned col) const;
//: Get n rows beginning at rowstart
vnl_matrix<T> get_n_rows (unsigned rowstart, unsigned n) const;
//: Get n columns beginning at colstart
vnl_matrix<T> get_n_columns(unsigned colstart, unsigned n) const;
// mutators
//: Set this matrix to an identity matrix
// Abort if the matrix is not square
void set_identity();
//: Transpose this matrix efficiently
void inplace_transpose();
//: Reverse order of rows.
void flipud();
//: Reverse order of columns.
void fliplr();
//: Normalize each row so it is a unit vector
// Zero rows are ignored
void normalize_rows();
//: Normalize each column so it is a unit vector
// Zero columns are ignored
void normalize_columns();
//: Scale elements in given row by a factor of T
void scale_row (unsigned row, T value);
//: Scale elements in given column by a factor of T
void scale_column(unsigned col, T value);
//: Swap this matrix with that matrix
void swap(vnl_matrix<T> & that);
//: Type def for norms.
typedef typename vnl_c_vector<T>::abs_t abs_t;
//: Return sum of absolute values of elements
abs_t array_one_norm() const { return vnl_c_vector<T>::one_norm(begin(), size()); }
//: Return square root of sum of squared absolute element values
abs_t array_two_norm() const { return vnl_c_vector<T>::two_norm(begin(), size()); }
//: Return largest absolute element value
abs_t array_inf_norm() const { return vnl_c_vector<T>::inf_norm(begin(), size()); }
//: Return sum of absolute values of elements
abs_t absolute_value_sum() const { return array_one_norm(); }
//: Return largest absolute value
abs_t absolute_value_max() const { return array_inf_norm(); }
abs_t operator_one_norm() const;
//abs_t operator_two_norm() const;
abs_t operator_inf_norm() const;
//: Return frobenius norm of matrix (sqrt of sum of squares of its elements)
abs_t frobenius_norm() const { return vnl_c_vector<T>::two_norm(begin(), size()); }
//: Return frobenius norm of matrix (sqrt of sum of squares of its elements)
abs_t fro_norm() const { return frobenius_norm(); }
//: Return RMS of all elements
abs_t rms() const { return vnl_c_vector<T>::rms_norm(begin(), size()); }
//: Return minimum value of elements
T min_value() const { return vnl_c_vector<T>::min_value(begin(), size()); }
//: Return maximum value of elements
T max_value() const { return vnl_c_vector<T>::max_value(begin(), size()); }
//: Return mean of all matrix elements
T mean() const { return vnl_c_vector<T>::mean(begin(), size()); }
#if 0 // deprecated
// <deprecated>
// These two methods have been intentionally poisoned. The new equivalents are:
// array_one_norm() / array_inf_norm()
// or
// absolute_value_sum() / absolute_value_max()
abs_t one_norm(void *) const { return vnl_c_vector<T>::one_norm(begin(), size()); }
abs_t inf_norm(void *) const { return vnl_c_vector<T>::inf_norm(begin(), size()); }
// </deprecated>
#endif
// predicates
//: Return true iff the size is zero.
bool empty() const { return !data || !num_rows || !num_cols; }
//: Return true if all elements equal to identity.
bool is_identity() const;
//: Return true if all elements equal to identity, within given tolerance
bool is_identity(double tol) const;
//: Return true if all elements equal to zero.
bool is_zero() const;
//: Return true if all elements equal to zero, within given tolerance
bool is_zero(double tol) const;
//: Return true if finite
bool is_finite() const;
//: Return true if matrix contains NaNs
bool has_nans() const;
//: abort if size is not as expected
// This function does or tests nothing if NDEBUG is defined
void assert_size(unsigned rows, unsigned cols) const
{
#ifndef NDEBUG
assert_size_internal(rows, cols);
#endif
}
//: abort if matrix containins any INFs or NANs
// This function does or tests nothing if NDEBUG is defined
void assert_finite() const
{
#ifndef NDEBUG
assert_finite_internal();
#endif
}
////----------------------- Input/Output ----------------------------
//: Read a vnl_matrix from an ascii vcl_istream, automatically determining file size if the input matrix has zero size.
static vnl_matrix<T> read(vcl_istream& s);
// : Read a vnl_matrix from an ascii vcl_istream, automatically determining file size if the input matrix has zero size.
bool read_ascii(vcl_istream& s);
//--------------------------------------------------------------------------------
//: Access the contiguous block storing the elements in the matrix row-wise. O(1).
// 1d array, row-major order.
T const* data_block () const { return data[0]; }
//: Access the contiguous block storing the elements in the matrix row-wise. O(1).
// 1d array, row-major order.
T * data_block () { return data[0]; }
//: Access the 2D array, so that elements can be accessed with array[row][col] directly.
// 2d array, [row][column].
T const* const* data_array () const { return data; }
//: Access the 2D array, so that elements can be accessed with array[row][col] directly.
// 2d array, [row][column].
T * * data_array () { return data; }
typedef T element_type;
//: Iterators
typedef T *iterator;
//: Iterator pointing to start of data
iterator begin() { return data?data[0]:0; }
//: Iterator pointing to element beyond end of data
iterator end() { return data?data[0]+num_rows*num_cols:0; }
//: Const iterators
typedef T const *const_iterator;
//: Iterator pointing to start of data
const_iterator begin() const { return data?data[0]:0; }
//: Iterator pointing to element beyond end of data
const_iterator end() const { return data?data[0]+num_rows*num_cols:0; }
//: Return a reference to this.
// Useful in code which would prefer not to know if its argument
// is a matrix, matrix_ref or a matrix_fixed. Note that it doesn't
// return a matrix_ref, so it's only useful in templates or macros.
vnl_matrix<T> const& as_ref() const { return *this; }
//: Return a reference to this.
vnl_matrix<T>& as_ref() { return *this; }
//--------------------------------------------------------------------------------
//: Return true if *this == rhs
bool operator_eq (vnl_matrix<T> const & rhs) const;
//: Equality operator
bool operator==(vnl_matrix<T> const &that) const { return this->operator_eq(that); }
//: Inequality operator
bool operator!=(vnl_matrix<T> const &that) const { return !this->operator_eq(that); }
//: Print matrix to os in some hopefully sensible format
void print(vcl_ostream& os) const;
//: Make the matrix as if it had been default-constructed.
void clear();
//: Resize to r rows by c columns. Old data lost.
// \deprecated Use make_size instead.
bool resize (unsigned r, unsigned c) { return make_size(r,c); }
//: Resize to r rows by c columns.
// Old data lost.
// Returns true if size changed.
bool make_size (unsigned r, unsigned c);
//--------------------------------------------------------------------------------
protected:
unsigned num_rows; // Number of rows
unsigned num_cols; // Number of columns
T** data; // Pointer to the vnl_matrix
void assert_size_internal(unsigned rows, unsigned cols) const;
void assert_finite_internal() const;
//: Delete data
void destroy();
#if VCL_NEED_FRIEND_FOR_TEMPLATE_OVERLOAD
# define v vnl_vector<T>
# define m vnl_matrix<T>
friend m operator+ VCL_NULL_TMPL_ARGS (T const&, m const&);
friend m operator- VCL_NULL_TMPL_ARGS (T const&, m const&);
friend m operator* VCL_NULL_TMPL_ARGS (T const&, m const&);
friend m element_product VCL_NULL_TMPL_ARGS (m const&, m const&);
friend m element_quotient VCL_NULL_TMPL_ARGS (m const&, m const&);
friend T dot_product VCL_NULL_TMPL_ARGS (m const&, m const&);
friend T inner_product VCL_NULL_TMPL_ARGS (m const&, m const&);
friend T cos_angle VCL_NULL_TMPL_ARGS (m const&, m const&);
friend vcl_ostream& operator<< VCL_NULL_TMPL_ARGS (vcl_ostream&, m const&);
friend vcl_istream& operator>> VCL_NULL_TMPL_ARGS (vcl_istream&, m&);
# undef v
# undef m
#endif
// inline function template instantiation hack for gcc 2.97 -- fsm
static void inline_function_tickler();
};
// Definitions of inline functions.
//: Returns the value of the element at specified row and column. O(1).
// Checks for valid range of indices.
template<class T>
inline T vnl_matrix<T>::get (unsigned row, unsigned column) const
{
#if ERROR_CHECKING
if (row >= this->num_rows) // If invalid size specified
vnl_error_matrix_row_index ("get", row); // Raise exception
if (column >= this->num_cols) // If invalid size specified
vnl_error_matrix_col_index ("get", column); // Raise exception
#endif
return this->data[row][column];
}
//: Puts value into element at specified row and column. O(1).
// Checks for valid range of indices.
template<class T>
inline void vnl_matrix<T>::put (unsigned row, unsigned column, T const& value)
{
#if ERROR_CHECKING
if (row >= this->num_rows) // If invalid size specified
vnl_error_matrix_row_index ("put", row); // Raise exception
if (column >= this->num_cols) // If invalid size specified
vnl_error_matrix_col_index ("put", column); // Raise exception
#endif
this->data[row][column] = value; // Assign data value
}
// non-member arithmetical operators.
//:
// \relates vnl_matrix
template<class T>
inline vnl_matrix<T> operator* (T const& value, vnl_matrix<T> const& m)
{
return vnl_matrix<T>(m, value, vnl_tag_mul());
}
//:
// \relates vnl_matrix
template<class T>
inline vnl_matrix<T> operator+ (T const& value, vnl_matrix<T> const& m)
{
return vnl_matrix<T>(m, value, vnl_tag_add());
}
//: Swap two matrices
// \relates vnl_matrix
template<class T>
inline void swap(vnl_matrix<T> &A, vnl_matrix<T> &B) { A.swap(B); }
#endif // vnl_matrix_h_
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -