Defines | Functions
core/vnl/vnl_matrix.txx File Reference

Copyright (C) 1991 Texas Instruments Incorporated. More...

#include "vnl_matrix.h"
#include <vcl_cassert.h>
#include <vcl_cstdio.h>
#include <vcl_cstdlib.h>
#include <vcl_cctype.h>
#include <vcl_iostream.h>
#include <vcl_vector.h>
#include <vcl_algorithm.h>
#include <vnl/vnl_math.h>
#include <vnl/vnl_vector.h>
#include <vnl/vnl_c_vector.h>
#include <vnl/vnl_numeric_traits.h>

Go to the source code of this file.

Defines

#define vnl_matrix_txx_
#define vnl_matrix_construct_hack()
#define vnl_matrix_alloc_blah()
#define vnl_matrix_free_blah
#define VNL_MATRIX_INSTANTIATE(T)

Functions

template<class T >
vcl_ostream & operator<< (vcl_ostream &os, vnl_matrix< T > const &m)
 Prints the 2D array of elements of a matrix out to a stream.
template<class T >
vcl_istream & operator>> (vcl_istream &s, vnl_matrix< T > &M)
 Read a vnl_matrix from an ascii vcl_istream.
template<class T >
vnl_matrix< T > operator- (T const &value, vnl_matrix< T > const &m)
template<class T >
dot_product (vnl_matrix< T > const &m1, vnl_matrix< T > const &m2)
 Returns the dot product of the two matrices. O(m*n).
template<class T >
inner_product (vnl_matrix< T > const &m1, vnl_matrix< T > const &m2)
 Hermitian inner product.
template<class T >
cos_angle (vnl_matrix< T > const &a, vnl_matrix< T > const &b)
template<class T >
vnl_matrix< T > element_product (vnl_matrix< T > const &m1, vnl_matrix< T > const &m2)
 Returns new matrix whose elements are the products m1[ij]*m2[ij].
template<class T >
vnl_matrix< T > element_quotient (vnl_matrix< T > const &m1, vnl_matrix< T > const &m2)
 Returns new matrix whose elements are the quotients m1[ij]/m2[ij].
template<class doublereal >
int vnl_inplace_transpose (doublereal *a, unsigned m, unsigned n, char *move, unsigned iwrk)

Detailed Description

Copyright (C) 1991 Texas Instruments Incorporated.

Copyright (C) 1992 General Electric Company.

Permission is granted to any individual or institution to use, copy, modify, and distribute this software, provided that this complete copyright and permission notice is maintained, intact, in all copies and supporting documentation.

Texas Instruments Incorporated, General Electric Company, provides this software "as is" without express or implied warranty.

Created: MBN Apr 21, 1989 Initial design and implementation Updated: MBN Jun 22, 1989 Removed non-destructive methods Updated: LGO Aug 09, 1989 Inherit from Generic Updated: MBN Aug 20, 1989 Changed template usage to reflect new syntax Updated: MBN Sep 11, 1989 Added conditional exception handling and base class Updated: LGO Oct 05, 1989 Don't re-allocate data in operator= when same size Updated: LGO Oct 19, 1989 Add extra parameter to varargs constructor Updated: MBN Oct 19, 1989 Added optional argument to set_compare method Updated: LGO Dec 08, 1989 Allocate column data in one chunk Updated: LGO Dec 08, 1989 Clean-up get and put, add const everywhere. Updated: LGO Dec 19, 1989 Remove the map and reduce methods Updated: MBN Feb 22, 1990 Changed size arguments from int to unsigned int Updated: MJF Jun 30, 1990 Added base class name to constructor initializer Updated: VDN Feb 21, 1992 New lite version Updated: VDN May 05, 1992 Use envelope to avoid unnecessary copying Updated: VDN Sep 30, 1992 Matrix inversion with singular value decomposition Updated: AWF Aug 21, 1996 set_identity, normalize_rows, scale_row. Updated: AWF Sep 30, 1996 set_row/column methods. Const-correct data_block(). Updated: AWF 14 Feb 1997 get_n_rows, get_n_columns. Updated: PVR 20 Mar 1997 get_row, get_column.

The parameterized vnl_matrix<T> class implements two dimensional arithmetic matrices of a user specified type. The only constraint placed on the type is that it must overload the following operators: +, -, *, and /. Thus, it will be possible to have a vnl_matrix over vcl_complex<T>. The vnl_matrix<T> class is static in size, that is once a vnl_matrix<T> of a particular size has been created, there is no dynamic growth method available. You can resize the matrix, with the loss of any existing data using set_size().

Each matrix contains a protected data section that has a T** slot that points to the physical memory allocated for the two dimensional array. In addition, two integers specify the number of rows and columns for the matrix. These values are provided in the constructors. A single protected slot contains a pointer to a compare function to be used in equality operations. The default function used is the built-in == operator.

Four different constructors are provided. The first constructor takes two integer arguments specifying the row and column size. Enough memory is allocated to hold row*column elements of type Type. The second constructor takes the same two first arguments, but also accepts an additional third argument that is a reference to an object of the appropriate type whose value is used as an initial fill value. The third constructor is similar to the third, except that it accepts a variable number of initialization values for the Matrix. If there are fewer values than elements, the rest are set to zero. Finally, the last constructor takes a single argument consisting of a reference to a Matrix and duplicates its size and element values.

Methods are provided for destructive scalar and Matrix addition, multiplication, check for equality and inequality, fill, reduce, and access and set individual elements. Finally, both the input and output operators are overloaded to allow for formatted input and output of matrix elements.

Good matrix inversion is needed. We choose singular value decomposition, since it is general and works great for nearly singular cases. Singular value decomposition is preferred to LU decomposition, since the accuracy of the pivots is independent from the left->right top->down elimination. LU decomposition also does not give eigenvectors and eigenvalues when the matrix is symmetric.

Several different constructors are provided. See .h file for brief descriptions.

Definition in file vnl_matrix.txx.


Define Documentation

#define vnl_matrix_alloc_blah ( )
Value:
do { \
  if (this->num_rows && this->num_cols) { \
    /* Allocate memory to hold the row pointers */ \
    this->data = vnl_c_vector<T>::allocate_Tptr(this->num_rows); \
    /* Allocate memory to hold the elements of the matrix */ \
    T* elmns = vnl_c_vector<T>::allocate_T(this->num_rows * this->num_cols); \
    /* Fill in the array of row pointers */ \
    for (unsigned int i = 0; i < this->num_rows; ++ i) \
      this->data[i] = elmns + i*this->num_cols; \
  } \
  else { \
   /* This is to make sure .begin() and .end() work for 0xN matrices: */ \
   (this->data = vnl_c_vector<T>::allocate_Tptr(1))[0] = 0; \
  } \
} while (false)

Definition at line 105 of file vnl_matrix.txx.

#define vnl_matrix_construct_hack ( )

Definition at line 101 of file vnl_matrix.txx.

#define vnl_matrix_free_blah
Value:
do { \
  if (this->data) { \
    if (this->num_cols && this->num_rows) { \
      vnl_c_vector<T>::deallocate(this->data[0], this->num_cols * this->num_rows); \
      vnl_c_vector<T>::deallocate(this->data, this->num_rows); \
    } \
    else { \
      vnl_c_vector<T>::deallocate(this->data, 1); \
    } \
  } \
} while (false)

Definition at line 123 of file vnl_matrix.txx.

#define VNL_MATRIX_INSTANTIATE (   T)
Value:
template class vnl_matrix<T >; \
template vnl_matrix<T > operator-(T const &, vnl_matrix<T > const &); \
VCL_INSTANTIATE_INLINE(vnl_matrix<T > operator+(T const &, vnl_matrix<T > const &)); \
VCL_INSTANTIATE_INLINE(vnl_matrix<T > operator*(T const &, vnl_matrix<T > const &)); \
template T dot_product(vnl_matrix<T > const &, vnl_matrix<T > const &); \
template T inner_product(vnl_matrix<T > const &, vnl_matrix<T > const &); \
template T cos_angle(vnl_matrix<T > const &, vnl_matrix<T > const &); \
template vnl_matrix<T > element_product(vnl_matrix<T > const &, vnl_matrix<T > const &); \
template vnl_matrix<T > element_quotient(vnl_matrix<T > const &, vnl_matrix<T > const &); \
template int vnl_inplace_transpose(T*, unsigned, unsigned, char*, unsigned); \
template vcl_ostream & operator<<(vcl_ostream &, vnl_matrix<T > const &); \
template vcl_istream & operator>>(vcl_istream &, vnl_matrix<T >       &)

Definition at line 1668 of file vnl_matrix.txx.

#define vnl_matrix_txx_

Definition at line 3 of file vnl_matrix.txx.


Function Documentation

template<class T >
T cos_angle ( vnl_matrix< T > const &  a,
vnl_matrix< T > const &  b 
)

Definition at line 844 of file vnl_matrix.txx.

template<class T >
T dot_product ( vnl_matrix< T > const &  m1,
vnl_matrix< T > const &  m2 
)

Returns the dot product of the two matrices. O(m*n).

This is the sum of all pairwise products of the elements m1[i,j]*m2[i,j].

Definition at line 815 of file vnl_matrix.txx.

template<class T >
vnl_matrix<T> element_product ( vnl_matrix< T > const &  m1,
vnl_matrix< T > const &  m2 
)

Returns new matrix whose elements are the products m1[ij]*m2[ij].

O(m*n).

Definition at line 859 of file vnl_matrix.txx.

template<class T >
vnl_matrix<T> element_quotient ( vnl_matrix< T > const &  m1,
vnl_matrix< T > const &  m2 
)

Returns new matrix whose elements are the quotients m1[ij]/m2[ij].

O(m*n).

Definition at line 878 of file vnl_matrix.txx.

template<class T >
T inner_product ( vnl_matrix< T > const &  m1,
vnl_matrix< T > const &  m2 
)

Hermitian inner product.

O(mn).

Definition at line 830 of file vnl_matrix.txx.

template<class T >
vnl_matrix<T> operator- ( T const &  value,
vnl_matrix< T > const &  m 
)

Definition at line 626 of file vnl_matrix.txx.

template<class T >
vcl_ostream& operator<< ( vcl_ostream &  os,
vnl_matrix< T > const &  m 
)

Prints the 2D array of elements of a matrix out to a stream.

O(m*n).

Definition at line 520 of file vnl_matrix.txx.

template<class T >
vcl_istream& operator>> ( vcl_istream &  s,
vnl_matrix< T > &  M 
)

Read a vnl_matrix from an ascii vcl_istream.

Automatically determines file size if the input matrix has zero size.

Definition at line 533 of file vnl_matrix.txx.

template<class doublereal >
int vnl_inplace_transpose ( doublereal *  a,
unsigned  m,
unsigned  n,
char *  move,
unsigned  iwrk 
)

Definition at line 1521 of file vnl_matrix.txx.