home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2399 / realmat.h
Encoding:
C/C++ Source or Header  |  1990-12-28  |  7.8 KB  |  239 lines

  1. /* ----------------------------------------------------------------------------
  2.  
  3. realmat: realmat.h
  4.  
  5. realmat is a C++ matrix class. The header file realmat.h describes its use.
  6.  
  7. Copyright (C) 1990.
  8.  
  9. A. Ronald Gallant
  10. P.O. Box 5513 
  11. Raleigh NC 27650-5513 
  12. USA   
  13.  
  14. Permission to use, copy, modify, and distribute this software and its 
  15. documentation for any purpose and without fee is hereby granted, provided 
  16. that the above copyright notice appear in all copies and that both that 
  17. copyright notice and this permission notice appear in supporting 
  18. documentation.  
  19.  
  20. This software is provided "as is" without any expressed or implied warranty.
  21.  
  22. ------------------------------------------------------------------------------ 
  23.  
  24. This header defines a matrix class.  A matrix X with r rows, and c columns is 
  25. constructed using realmat X(r,c).  X is stored columnwise with no wasted space 
  26. which is to say that vec(X) is what is stored.  Indexing of the elements starts 
  27. with 1, not zero.  The protected members of realmat are rows, cols, len, and x.  
  28.  
  29. As an illustration, suppose X is constructed using realmat X(r,c).  Then 
  30. X.rows=r, X.cols=c, X.len=r*c, the pointer X.x points to the first element of 
  31. vec(X), X[i] returns the i-th element of vec(X), i=1,...,X.len, X.elem(i,j) 
  32. returns the (i,j)-th element of X, i=1,...,X.rows, j=1,...,.X.cols.  An element 
  33. of X is of type REAL and X.rows, X.cols, and X.len are of type INTEGER.  The 
  34. typedef of REAL and INTEGER is in usual.h.  To allocate store at run time, use
  35. realmat X; X.resize(r,c).
  36.  
  37. The operators +, -, *, =, and << are defined.  T(X) transposes X.  X.check1(i) 
  38. and X.check2(i,j) access the elements of vex(X) and X respectively with range 
  39. checking.  invpsd(X) or invpsd(X,eps) returns a g-inverse of a positive semi-
  40. definite, symmetric matrix X with minimal error checking; eps is the tolerance 
  41. used in rank determination and defaults to 1.e-13.  rows(X) returns X.rows and 
  42. cols(X) returns X.cols.
  43.  
  44. -----------------------------------------------------------------------------*/
  45.  
  46. #ifndef __FILE_REALMAT_H_SEEN__
  47. #pragma once
  48. #define __FILE_REALMAT_H_SEEN__ 1
  49.  
  50. #include "usual.h"
  51. #include "tools.h"
  52.  
  53. class realmat
  54. {
  55. protected:
  56.   INTEGER       rows;
  57.   INTEGER       cols;
  58.   INTEGER       len;
  59.   REAL          *x;
  60.                 realmat(INTEGER r, INTEGER c, REAL* a);
  61. void            realmat_constructor(INTEGER r, INTEGER c, REAL fill_value);
  62. void            resize_constructor(INTEGER r, INTEGER c, REAL fill_value);
  63.  
  64. public:
  65.                 realmat();
  66.  
  67.                 realmat(INTEGER r, INTEGER c);
  68.                 realmat(INTEGER r, INTEGER c, REAL fill_value);
  69.                 realmat(INTEGER r, INTEGER c, int fill_value);
  70.  
  71.                 realmat(int r, int c);
  72.                 realmat(int r, int c, REAL fill_value);
  73.                 realmat(int r, int c, int fill_value);
  74.  
  75.                 realmat(INTEGER r, int c);
  76.                 realmat(INTEGER r, int c, REAL fill_value);
  77.                 realmat(INTEGER r, int c, int fill_value);
  78.  
  79.                 realmat(int r, INTEGER c);
  80.                 realmat(int r, INTEGER c, REAL fill_value);
  81.                 realmat(int r, INTEGER c, int fill_value);
  82.  
  83.                 realmat(realmat&);
  84.  
  85.                 ~realmat();
  86.  
  87. void            resize(INTEGER r, INTEGER c);
  88. void            resize(INTEGER r, INTEGER c, REAL fill_value);
  89. void            resize(INTEGER r, INTEGER c, int fill_value);
  90.  
  91. void            resize(int r, int c);
  92. void            resize(int r, int c, REAL fill_value);
  93. void            resize(int r, int c, int fill_value);
  94.  
  95. void            resize(INTEGER r, int c);
  96. void            resize(INTEGER r, int c, REAL fill_value);
  97. void            resize(INTEGER r, int c, int fill_value);
  98.  
  99. void            resize(int r, INTEGER c);
  100. void            resize(int r, INTEGER c, REAL fill_value);
  101. void            resize(int r, INTEGER c, int fill_value);
  102.  
  103. realmat&        operator=(realmat& a);
  104.  
  105. REAL&           operator[](INTEGER i);
  106. REAL&           elem(INTEGER i, INTEGER j);
  107. REAL&           check1(INTEGER i);
  108. REAL&           check2(INTEGER i, INTEGER j);
  109.  
  110. void            error(const char* msg);
  111.  
  112. friend INTEGER  rows(realmat& a);
  113. friend INTEGER  cols(realmat& a);
  114.  
  115. friend ostream& operator<<(ostream& stream, realmat& a);
  116.  
  117. friend realmat  operator+(realmat&  a, realmat& b);
  118. friend realmat  operator+(realmat&  a);
  119.  
  120. friend realmat  operator-(realmat&  a, realmat& b);
  121. friend realmat  operator-(realmat&  a);
  122.  
  123. friend realmat  operator*(realmat&  a, realmat& b);
  124. friend realmat  operator*(REAL&     a, realmat& b);
  125. friend realmat  operator*(INTEGER&  a, realmat& b);
  126.  
  127. friend realmat  T(realmat& a);
  128.  
  129. friend realmat  invpsd(realmat& a, REAL eps = 1.0e-13); //a is psd, symmetric
  130.  
  131. };
  132.  
  133.  
  134. inline REAL& realmat::operator[](INTEGER i)
  135. {
  136.   return x[i-1];
  137. }
  138.  
  139. inline REAL& realmat::elem(INTEGER i, INTEGER j)
  140. {
  141.   return x[i + rows*j - rows - 1];  //  return x[rows*(j-1)+i-1]
  142. }
  143.  
  144. inline INTEGER rows(realmat& a)
  145. {
  146.   return a.rows;
  147. }
  148.  
  149. inline INTEGER cols(realmat& a)
  150. {
  151.   return a.cols;
  152. }
  153.  
  154. inline realmat::realmat()
  155. { rows=0; cols=0; len=0; x=0;}
  156.  
  157. inline realmat::realmat(INTEGER r, INTEGER c)
  158.   {realmat_constructor(r, c, (REAL) 0);}
  159.  
  160. inline realmat::realmat(INTEGER r, INTEGER c, REAL fill_value)
  161.   {realmat_constructor(r, c, fill_value);}
  162.  
  163. inline realmat::realmat(INTEGER r, INTEGER c, int fill_value)
  164.   {realmat_constructor(r, c, (REAL) fill_value);}
  165.  
  166. inline realmat::realmat(int r, int c)
  167.   {realmat_constructor((INTEGER) r, (INTEGER) c, (REAL) 0);}
  168.  
  169. inline realmat::realmat(int r, int c, REAL fill_value)
  170.   {realmat_constructor((INTEGER) r, (INTEGER) c, fill_value);}
  171.  
  172. inline realmat::realmat(int r, int c, int fill_value)
  173.   {realmat_constructor((INTEGER) r, (INTEGER) c, (REAL) fill_value);}
  174.  
  175. inline realmat::realmat(INTEGER r, int c)
  176.   {realmat_constructor(r, (INTEGER) c, (REAL) 0);}
  177.  
  178. inline realmat::realmat(INTEGER r, int c, REAL fill_value)
  179.   {realmat_constructor(r, (INTEGER) c, fill_value);}
  180.  
  181. inline realmat::realmat(INTEGER r, int c, int fill_value)
  182.   {realmat_constructor(r, (INTEGER) c, (REAL) fill_value);}
  183.  
  184. inline realmat::realmat(int r, INTEGER c)
  185.   {realmat_constructor((INTEGER) r, c, (REAL) 0);}
  186.  
  187. inline realmat::realmat(int r, INTEGER c, REAL fill_value)
  188.   {realmat_constructor((INTEGER) r, c, fill_value);}
  189.  
  190. inline realmat::realmat(int r, INTEGER c, int fill_value)
  191.   {realmat_constructor((INTEGER) r, c, (REAL) fill_value);}
  192.  
  193. inline realmat::~realmat()
  194. {delete x;} 
  195.  
  196. inline void realmat::resize(INTEGER r, INTEGER c)
  197.   {resize_constructor(r, c, (REAL) 0);}
  198.  
  199. inline void realmat::resize(INTEGER r, INTEGER c, REAL fill_value)
  200.   {resize_constructor(r, c, fill_value);}
  201.  
  202. inline void realmat::resize(INTEGER r, INTEGER c, int fill_value)
  203.   {resize_constructor(r, c, (REAL) fill_value);}
  204.  
  205. inline void realmat::resize(int r, int c)
  206.   {resize_constructor((INTEGER) r, (INTEGER) c, (REAL) 0);}
  207.  
  208. inline void realmat::resize(int r, int c, REAL fill_value)
  209.   {resize_constructor((INTEGER) r, (INTEGER) c, fill_value);}
  210.  
  211. inline void realmat::resize(int r, int c, int fill_value)
  212.   {resize_constructor((INTEGER) r, (INTEGER) c, (REAL) fill_value);}
  213.  
  214. inline void realmat::resize(INTEGER r, int c)
  215.   {resize_constructor(r, (INTEGER) c, (REAL) 0);}
  216.  
  217. inline void realmat::resize(INTEGER r, int c, REAL fill_value)
  218.   {resize_constructor(r, (INTEGER) c, fill_value);}
  219.  
  220. inline void realmat::resize(INTEGER r, int c, int fill_value)
  221.   {resize_constructor(r, (INTEGER) c, (REAL) fill_value);}
  222.  
  223. inline void realmat::resize(int r, INTEGER c)
  224.   {resize_constructor((INTEGER) r, c, (REAL) 0);}
  225.  
  226. inline void realmat::resize(int r, INTEGER c, REAL fill_value)
  227.   {resize_constructor((INTEGER) r, c, fill_value);}
  228.  
  229. inline void realmat::resize(int r, INTEGER c, int fill_value)
  230.   {resize_constructor((INTEGER) r, c, (REAL) fill_value);}
  231.  
  232.  
  233. extern void default_realmat_error_handler(const char* msg);
  234. extern ONE_ARG_ERROR_HANDLER_T realmat_error_handler;
  235. extern ONE_ARG_ERROR_HANDLER_T 
  236.   set_realmat_error_handler(ONE_ARG_ERROR_HANDLER_T f);
  237.  
  238. #endif
  239.