home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3257 / Matrix.H < prev   
C/C++ Source or Header  |  1991-04-29  |  3KB  |  108 lines

  1. #ifndef __MATRIX_H__
  2. #define __MATRIX_H__
  3.  
  4. /* $Id: Matrix.H,v 1.4 91/04/16 18:03:17 bjaspan Exp $ */
  5.  
  6. #include <assert.h>  /* for abort() */
  7. #include <stream.h>
  8.  
  9. class Matrix {
  10. public:
  11.      /* Constructors */
  12.      Matrix();
  13.      Matrix(int, int);
  14.      Matrix(int, int, int *);
  15.      Matrix(int, int, double *);
  16.      Matrix(int, int, int ...);
  17.      Matrix(int, int, double ...);
  18.      Matrix(const Matrix&);
  19.      Matrix(const Matrix&,int,int,int,int);
  20.      ~Matrix();
  21.  
  22.      /* Member functions */
  23.      Matrix T() const;
  24.      inline int Rows() const;
  25.      inline int Cols() const;
  26.      inline int SameSize(const Matrix&) const;
  27.      inline double& operator()(int, int) const;
  28.      inline double *Row(int) const;
  29.      void Print() const;
  30.  
  31.      /* Overloaded operators */
  32.      friend Matrix operator+(const Matrix&, const Matrix&);
  33.      friend Matrix operator-(const Matrix&, const Matrix&);
  34.      friend Matrix operator*(const Matrix&, const Matrix&);
  35.      friend Matrix operator*(double, const Matrix&);
  36.      Matrix& operator+=(const Matrix&);
  37.      Matrix& operator-=(const Matrix&);
  38.      inline friend Matrix& operator*=(Matrix&, const Matrix&);
  39.      inline friend Matrix operator/(const Matrix&, double);
  40.      friend int operator==(const Matrix&, const Matrix&);
  41.      friend Matrix operator|(const Matrix&, const Matrix&);
  42.      friend Matrix operator&(const Matrix&, const Matrix&);
  43.      Matrix& operator=(const Matrix&);
  44.      friend ostream& operator<<(ostream&, const Matrix&);
  45.  
  46.      /* Special matrix creators */
  47.      static inline const Matrix& NAM();
  48.  
  49.      /* Matrix mutators */
  50.      void Identify();
  51.  
  52. private:
  53.      void Init(const int, const int);
  54.  
  55.      static Matrix *M_NAM;
  56.      static Matrix *MakeNAM();
  57.      
  58.      int M_m, M_n, nam;
  59.      double *d;
  60. };
  61.  
  62. inline int Matrix::Rows() const
  63. {
  64.      return M_m;
  65. }
  66.  
  67. inline int Matrix::Cols() const
  68. {
  69.      return M_n;
  70. }
  71.  
  72. inline Matrix& operator*=(Matrix& m1, const Matrix& m)
  73. {
  74.    return(m1 = m1*m);
  75. }
  76.  
  77. inline Matrix operator/(const Matrix& m, double k)
  78. {
  79.    return((1.0/k)*m);
  80. }
  81.  
  82. inline double& Matrix::operator()(int m, int n) const
  83. {
  84.      /* My kingdom for exception handling ... */
  85.      if (m > M_m || n > M_n || m < 1 || n < 1)
  86.       abort();
  87.      
  88.      return d[M_n*(m-1) + n - 1];
  89. }
  90.  
  91. inline double *Matrix::Row(int r) const
  92. {
  93.    return(d+(r-1)*M_n);
  94. }
  95.  
  96. inline const Matrix& Matrix::NAM()
  97. {
  98.      return *Matrix::M_NAM;
  99. }
  100.  
  101. inline int Matrix::SameSize(const Matrix& m) const
  102. {
  103.      return (M_m == m.M_m && M_n == m.M_n);
  104. }
  105.  
  106. /* DO NOT ADD ANYTHING AFTER THIS #endif */
  107. #endif /* __MATRIX_H__ */
  108.