home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / octave-1.1.1p1-base.tgz / octave-1.1.1p1-base.tar / fsf / octave / src / tc-inlines.h < prev    next >
C/C++ Source or Header  |  1995-01-17  |  7KB  |  314 lines

  1. // tc-inlines.h                                          -*- C++ -*-
  2. /*
  3.  
  4. Copyright (C) 1992, 1993, 1994, 1995 John W. Eaton
  5.  
  6. This file is part of Octave.
  7.  
  8. Octave is free software; you can redistribute it and/or modify it
  9. under the terms of the GNU General Public License as published by the
  10. Free Software Foundation; either version 2, or (at your option) any
  11. later version.
  12.  
  13. Octave is distributed in the hope that it will be useful, but WITHOUT
  14. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16. for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with Octave; see the file COPYING.  If not, write to the Free
  20. Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22. */
  23.  
  24. // Just a coupla more helper functions.
  25.  
  26. static inline int
  27. tree_to_mat_idx (double x)
  28. {
  29.   if (x > 0)
  30.     return ((int) (x + 0.5) - 1);
  31.   else
  32.     return ((int) (x - 0.5) - 1);
  33. }
  34.  
  35. static inline int
  36. range_max_check (int i, int imax)
  37. {
  38.   i++;
  39.   if (i > imax)
  40.     {
  41.       error ("matrix index = %d exceeds maximum dimension = %d", i, imax);
  42.       return -1;
  43.     }
  44.   return 0;
  45. }
  46.  
  47. static inline int
  48. range_max_check (int i, int j, int nr, int nc)
  49. {
  50.   int status = 0;
  51.   i++;
  52.   if (i > nr)
  53.     {
  54.       error ("matrix row index = %d exceeds maximum row dimension = %d",
  55.          i, nr);
  56.       status = -1;
  57.     }
  58.  
  59.   j++;
  60.   if (j > nc)
  61.     {
  62.       error ("matrix column index = %d exceeds maximum column dimension = %d",
  63.          j, nc);
  64.       status = -1;
  65.     }
  66.   return status;
  67. }
  68.  
  69. static inline int
  70. indexed_assign_conforms (int lhs_nr, int lhs_nc, int rhs_nr, int rhs_nc)
  71. {
  72.   return (lhs_nr == rhs_nr && lhs_nc == rhs_nc);
  73. }
  74.  
  75. static inline int
  76. is_one_zero (const Range& r)
  77. {
  78.   double b = r.base ();
  79.   double l = r.limit ();
  80.   return (r.nelem () == 2 && NINT (b) == 1 && NINT (l) == 0);
  81. }
  82.  
  83. static inline int
  84. is_zero_one (const Range& r)
  85. {
  86.   double b = r.base ();
  87.   double l = r.limit ();
  88.   return (r.nelem () == 2 && NINT (b) == 0 && NINT (l) == 1);
  89. }
  90.  
  91. static inline int
  92. index_check (int i, char *rc)
  93. {
  94.   if (i < 0)
  95.     {
  96.       error ("invalid %s index = %d", rc, i+1);
  97.       return -1;
  98.     }
  99.   return 0;
  100. }
  101.  
  102. static inline int
  103. index_check (const Range& r, char *rc)
  104. {
  105.   if (r.nelem () < 1)
  106.     {
  107.       error ("range invalid as %s index", rc);
  108.       return -1;
  109.     }
  110.  
  111.   int imin = tree_to_mat_idx (r.min ());
  112.  
  113.   if (imin < 0)
  114.     {
  115.       error ("invalid %s index = %d", rc, imin+1);
  116.       return -1;
  117.     }
  118.  
  119.   return 0;
  120. }
  121.  
  122. static inline int
  123. fortran_row (int i, int nr)
  124. {
  125.   int r;
  126.   r = i % nr;
  127.   if (r == 0)
  128.     r = nr;
  129.   return r;
  130. }
  131.  
  132. static inline int
  133. fortran_column (int i, int nr)
  134. {
  135.   int c;
  136.   int r;
  137.   r = fortran_row (i, nr);
  138.   c = (i - r) / nr + 1;
  139.   return c;
  140. }
  141.  
  142. // How about a few macros?
  143.  
  144. #define TC_REP tree_constant::tree_constant_rep
  145.  
  146. #ifndef MAX
  147. #define MAX(a,b) ((a) > (b) ? (a) : (b))
  148. #endif
  149.  
  150. #ifndef MIN
  151. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  152. #endif
  153.  
  154. #ifndef ABS
  155. #define ABS(x) (((x) < 0) ? (-x) : (x))
  156. #endif
  157.  
  158. // The following are used by some of the functions in the
  159. // tree_constant_rep class that must deal with real and complex
  160. // matrices.  This was not done with overloaded or virtual functions
  161. // from the Matrix class because there is no clean way to do that --
  162. // the necessary functions (like elem) need to return values of
  163. // different types...
  164.  
  165. // Given a tree_constant, and the names to be used for the real and
  166. // complex matrix and their dimensions, declare a real or complex
  167. // matrix, and initialize it from the tree_constant.  Note that m, cm,
  168. // nr, and nc must not be previously declared, and they must not be
  169. // expressions.  Since only one of the matrices will be defined after
  170. // this macro is used, only one set of dimesions is declared.
  171.  
  172. // This macro only makes sense inside a friend or member function of
  173. // the tree_constant_rep class
  174.  
  175. #define REP_RHS_MATRIX(tc,m,cm,nr,nc) \
  176.   int nr = 0; \
  177.   int nc = 0; \
  178.   Matrix m; \
  179.   ComplexMatrix cm; \
  180.   if ((tc).const_type () == TC_REP::complex_matrix_constant) \
  181.     { \
  182.       cm = (tc).complex_matrix_value (); \
  183.       nr = (cm).rows (); \
  184.       nc = (cm).columns (); \
  185.     } \
  186.   else if ((tc).const_type () == TC_REP::matrix_constant) \
  187.     { \
  188.       m = (tc).matrix_value (); \
  189.       nr = (m).rows (); \
  190.       nc = (m).columns (); \
  191.     } \
  192.   else \
  193.     panic_impossible ();
  194.  
  195. // Assign a real or complex value to a tree_constant.
  196. //
  197. // This macro only makes sense inside a friend or member function of
  198. // the tree_constant_rep class.
  199.  
  200. #define REP_ELEM_ASSIGN(i,j,rval,cval,real_type) \
  201.   do \
  202.     { \
  203.       if (type_tag == TC_REP::matrix_constant) \
  204.         { \
  205.           if (real_type) \
  206.             matrix->elem ((i), (j)) = (rval); \
  207.           else \
  208.             panic_impossible (); \
  209.         } \
  210.       else \
  211.         { \
  212.           if (real_type) \
  213.             complex_matrix->elem ((i), (j)) = (rval); \
  214.           else \
  215.             complex_matrix->elem ((i), (j)) = (cval); \
  216.         } \
  217.     } \
  218.   while (0)
  219.  
  220. // Given a real and complex matrix and row and column dimensions,
  221. // declare both and size one of them.  Only one of the matrices should
  222. // be used after this macro has been used.
  223.  
  224. // This macro only makes sense inside a friend or member function of
  225. // the tree_constant_rep class.
  226.  
  227. #define CRMATRIX(m,cm,nr,nc) \
  228.   Matrix m; \
  229.   ComplexMatrix cm; \
  230.   if (type_tag == TC_REP::matrix_constant) \
  231.     (m).resize ((nr), (nc)); \
  232.   else if (type_tag == complex_matrix_constant) \
  233.     (cm).resize ((nr), (nc)); \
  234.   else \
  235.     panic_impossible (); \
  236.  
  237. // Assign a real or complex matrix to a tree constant.
  238.  
  239. // This macro only makes sense inside a friend or member function of
  240. // the tree_constant_rep class.
  241.  
  242. #define ASSIGN_CRMATRIX_TO(tc,m,cm) \
  243.   do \
  244.     { \
  245.       if (type_tag == matrix_constant) \
  246.         tc = tree_constant (m); \
  247.       else \
  248.         tc = tree_constant (cm); \
  249.     } \
  250.   while (0)
  251.  
  252. // Assign an element of this tree_constant_rep's real or complex
  253. // matrix to another real or complex matrix.
  254.  
  255. // This macro only makes sense inside a friend or member function of
  256. // the tree_constant_rep class.
  257.  
  258. #define CRMATRIX_ASSIGN_REP_ELEM(m,cm,i1,j1,i2,j2) \
  259.   do \
  260.     { \
  261.       if (type_tag == matrix_constant) \
  262.         (m).elem ((i1), (j1)) = matrix->elem ((i2), (j2)); \
  263.       else \
  264.         (cm).elem ((i1), (j1)) = complex_matrix->elem ((i2), (j2)); \
  265.     } \
  266.   while (0)
  267.  
  268. // Assign a value to an element of a real or complex matrix.  Assumes
  269. // that the lhs and rhs are either both real or both complex types.
  270.  
  271. #define CRMATRIX_ASSIGN_ELEM(m,cm,i,j,rval,cval,real_type) \
  272.   do \
  273.     { \
  274.       if (real_type) \
  275.         (m).elem ((i), (j)) = (rval); \
  276.       else \
  277.         (cm).elem ((i), (j)) = (cval); \
  278.     } \
  279.   while (0)
  280.  
  281.  
  282. // One more...
  283.  
  284. static inline int
  285. valid_scalar_indices (const Octave_object& args)
  286. {
  287.   int nargin = args.length ();
  288.  
  289.   return ((nargin == 2
  290.        && args(1).valid_as_scalar_index ()
  291.        && args(0).valid_as_scalar_index ())
  292.       || (nargin == 1
  293.           && args(0).valid_as_scalar_index ()));
  294. }
  295.  
  296. static inline int
  297. valid_zero_index (const Octave_object& args)
  298. {
  299.   int nargin = args.length ();
  300.  
  301.   return ((nargin == 2
  302.        && args(1).valid_as_zero_index ()
  303.        && args(0).valid_as_zero_index ())
  304.       || (nargin == 1
  305.           && args(0).valid_as_zero_index ()));
  306. }
  307.  
  308. /*
  309. ;;; Local Variables: ***
  310. ;;; mode: C++ ***
  311. ;;; page-delimiter: "^/\\*" ***
  312. ;;; End: ***
  313. */
  314.