contrib/mul/msm/msm_wt_mat_2d.h
Go to the documentation of this file.
00001 #ifndef msm_wt_mat_2d_h_
00002 #define msm_wt_mat_2d_h_
00003 //:
00004 // \file
00005 // \brief Represents 2x2 symmetric matrix, used as weight matrix
00006 // \author Tim Cootes
00007 
00008 #include <vcl_cassert.h>
00009 #include <vcl_iosfwd.h>
00010 #include <vcl_string.h>
00011 #include <vsl/vsl_fwd.h>
00012 
00013 //: Represents 2x2 symmetric matrix, used as weight matrix
00014 //  Stores the 3 unique elements and contains various utility
00015 //  functions.
00016 class msm_wt_mat_2d
00017 {
00018  private:
00019   double m11_, m12_, m22_;
00020  public:
00021 
00022   // Default Constructor - set to identity
00023   msm_wt_mat_2d()
00024     : m11_(1.0),m12_(0.0),m22_(1.0) {}
00025 
00026   // Constructor (requires m11_>=0 and m22_>=0)
00027   msm_wt_mat_2d(double m11, double m12, double m22)
00028     : m11_(m11),m12_(m12),m22_(m22)
00029   { assert(m11_>=0); assert(m22_>=0);}
00030 
00031   // Destructor
00032   ~msm_wt_mat_2d() {}
00033 
00034   //: Sets axis (eigenvector) of matrix and var along each
00035   //  Sets to s1*u*u' + s2*v*v', where u is the unit vector
00036   //  (u1 u2)/|u1,u2|, and v is the unit vector orthogonal to
00037   //  this.  u is then an eigenvector with associated eigenvalue s1,
00038   //  v is the other eigenvector, with eigenvalue s2
00039   void set_axes(double u1, double u2, double s1, double s2);
00040 
00041   double m11() const { return m11_; }
00042   double m12() const { return m12_; }
00043   double m21() const { return m12_; }
00044   double m22() const { return m22_; }
00045 
00046   // Determinant
00047   double det() const { return m11_*m22_ - m12_*m12_; }
00048 
00049   //: Calculate eigenvalues
00050   void eigen_values(double& EV1, double& EV2);
00051 
00052   //: Calculates W2=T'WT where T is 2x2 matrix (a,-b;b,a)
00053   msm_wt_mat_2d transform_by(double a, double b) const;
00054 
00055   //: Compute (x y)W(x y)'
00056   double xWx(double x, double y) const
00057   { return x*x*m11_+y*y*m22_+2*x*y*m12_; }
00058 
00059   //: Post multiply by W
00060   msm_wt_mat_2d operator*(msm_wt_mat_2d& W) const;
00061 
00062   //: Multiply this by scalar
00063   msm_wt_mat_2d& operator*=(double s);
00064 
00065   //: Add W to this
00066   msm_wt_mat_2d& operator+=(const msm_wt_mat_2d& W);
00067 
00068   // Returns inverse (or pseudo inverse)
00069   msm_wt_mat_2d inverse() const;
00070 
00071   //: Print class to os
00072   void print_summary(vcl_ostream& os) const;
00073 
00074   //: Save class to binary file stream
00075   void b_write(vsl_b_ostream& bfs) const;
00076 
00077   //: Load class from binary file stream
00078   void b_read(vsl_b_istream& bfs);
00079 
00080   //: Equality test
00081   bool operator==(const msm_wt_mat_2d& wt_mat);
00082 };
00083 
00084 
00085 //: Binary file stream output operator for class reference
00086 void vsl_b_write(vsl_b_ostream& bfs, const msm_wt_mat_2d& pts);
00087 
00088 //: Binary file stream input operator for class reference
00089 void vsl_b_read(vsl_b_istream& bfs, msm_wt_mat_2d& pts);
00090 
00091 //: Stream output operator for class reference
00092 vcl_ostream& operator<<(vcl_ostream& os,const msm_wt_mat_2d& pts);
00093 
00094 //: Stream output operator for class reference
00095 void vsl_print_summary(vcl_ostream& os,const msm_wt_mat_2d& pts);
00096 
00097 #endif // msm_wt_mat_2d_h_