matrix.h
上传用户:gzelex
上传日期:2007-01-07
资源大小:707k
文件大小:4k
开发平台:

MultiPlatform

  1. /*******************************************************************************
  2. +
  3. +  LEDA-R  3.2.3
  4. +
  5. +  matrix.h
  6. +
  7. +  Copyright (c) 1995  by  Max-Planck-Institut fuer Informatik
  8. +  Im Stadtwald, 66123 Saarbruecken, Germany     
  9. +  All rights reserved.
  10. *******************************************************************************/
  11. #ifndef LEDA_MATRIX_H
  12. #define LEDA_MATRIX_H
  13. //------------------------------------------------------------------------------
  14. //  matrices
  15. //------------------------------------------------------------------------------
  16. #include <LEDA/basic.h>
  17. #include <LEDA/vector.h>
  18. /*{Manpage {matrix} {}  {Real-Valued Matrices} }*/
  19. class matrix {
  20. /*{Mdefinition
  21. An instance of the data type $matrix$ is a matrix of double variables.  
  22. }*/
  23.   vector** v;
  24.   int  d1;
  25.   int  d2;
  26.   void     flip_rows(int,int);
  27.   void     check_dimensions(const matrix&) const; 
  28.   double&  elem(int i, int j) const { return v[i]->v[j]; }
  29.   double** triang(const matrix&, int&) const;
  30.     
  31. public:
  32. /*{Mcreation M }*/
  33.  matrix(int n=0, int m=0);
  34. /*{Mcreate creates an instance $M$ of type $matrix$, $M$ is initialized to 
  35.            the $n times m$ - zero matrix. }*/
  36.   matrix(const matrix&);
  37.   matrix(const vector&);
  38.   matrix(int,int,double**);
  39.   matrix& operator=(const matrix&);
  40.  ~matrix();
  41.   LEDA_MEMORY(matrix)
  42. /*{Moperations 2 4 }*/
  43. int     dim1()  const  {  return d1; }
  44. /*{Mop  returns $n$, the number of rows of $M$. }*/
  45. int     dim2()  const  {  return d2; }
  46. /*{Mop  returns $m$, the number of columns of $M$. }*/
  47. vector& row(int i) const;
  48. /*{Mop  returns the $i$-th row of $M$ (an $m$-vector).
  49.  precond  $0 le i le n-1$. }*/
  50. vector  col(int i) const;
  51. /*{Mop  returns the $i$-th column of $M$ (an $n$-vector).
  52.  precond  $0 le i le m-1$. }*/
  53. matrix  trans() const;
  54. /*{Mop  returns  $M^T$ ($mtimes n$ - matrix). }*/
  55. matrix  inv()   const;
  56. /*{Mop  returns the inverse matrix of $M$.\
  57.  precond  $M$.det() $neq$ 0. }*/
  58. double  det()   const;
  59. /*{Mop  returns the determinant of $M$.\
  60.  precond  $M$ is quadratic. }*/
  61. matrix solve(const matrix&) const;
  62. vector solve(const vector& b) const { return vector(solve(matrix(b))); }
  63. /*{Mop  returns vector $x$ with $Mcdot x = b$.\
  64.  precond $M$.dim1() = $M$.dim2() = $b$.dim()
  65.  and $M$.det() $neq$ 0. }*/
  66. operator vector() const; 
  67. vector& operator[](int i)    const { return row(i); }
  68. double& operator()(int i, int j);
  69. /*{Mfunop returns $M_{i,j}$. \
  70.   precond $0le ile n-1$ and $0le jle m-1$.}*/
  71. double  operator()(int,int) const;
  72. int     operator==(const matrix&)    const;
  73. int     operator!=(const matrix& x)  const { return !(*this == x); }
  74. matrix operator+(const matrix& M1);
  75. /*{Mbinop Addition. \
  76.      precond $M$.dim1() = $M1$.dim1() and $M$.dim2() = $M1$.dim2().}*/
  77. matrix operator-(const matrix& M1);
  78. /*{Mbinop Subtraction. \
  79.    precond $M$.dim1() = $M1$.dim1() and
  80.    $M$.dim2() = $M1$.dim2().}*/
  81. matrix operator-(); // unary
  82. matrix& operator-=(const matrix&);
  83. matrix& operator+=(const matrix&);
  84. matrix operator*(const matrix& M1);
  85. /*{Mbinop Multiplication. \
  86.    precond $M$.dim2() = $M1$.dim1().}*/
  87. vector operator*(const vector& vec) { return vector(*this * matrix(vec)); }
  88. /*{Mbinop  Multiplication with vector.\
  89.     precond $M$.dim2() = $vec$.dim(). }*/
  90. matrix operator*(double x);
  91. /*{Mbinop Multiplication with double x.}*/
  92. friend ostream& operator<<(ostream& O, const matrix& M);
  93. /*{Mbinopfunc  writes matrix $M$ row by row to the output stream $O$. }*/
  94. friend istream& operator>>(istream& I, matrix& M);
  95. /*{Mbinopfunc  reads matrix $M$ row by row from the input stream $I$. }*/
  96. /*{Mimplementation
  97. Data type $matrix$ is implemented by two-dimensional arrays of double numbers. 
  98. Operations det, solve, and inv take time $O(n^3)$, dim1, dim2, row, and col 
  99. take constant time, all other operations take time $O(nm)$.  
  100. The space requirement is $O(nm)$.}*/
  101. };
  102. inline void Print(const matrix& m, ostream& out) 
  103. { out << m.dim1() << " ";
  104.   out << m.dim2() << " ";
  105.   out << m; 
  106. }
  107. inline void Read(matrix& m, istream& in) 
  108. { int d1, d2;
  109.   in >> d1 >> d2;
  110.   matrix x(d1,d2);
  111.   in >> x;  
  112.   m = x;
  113. }
  114. inline int compare(const matrix&, const matrix&) 
  115. { error_handler(1,"compare not defined for type `matrix`"); 
  116.   return 0;
  117.  }
  118. LEDA_TYPE_PARAMETER(matrix)
  119. #endif