home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fs.zip / octave / octave-2.1.23 / liboctave / chMatrix.cc < prev    next >
C/C++ Source or Header  |  2000-01-15  |  5KB  |  309 lines

  1. // Matrix manipulations.
  2. /*
  3.  
  4. Copyright (C) 1996, 1997 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, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  21.  
  22. */
  23.  
  24. #if defined (__GNUG__)
  25. #pragma implementation
  26. #endif
  27.  
  28. #ifdef HAVE_CONFIG_H
  29. #include <config.h>
  30. #endif
  31.  
  32. #include <string>
  33.  
  34. #include <iostream.h>
  35.  
  36. #include "lo-error.h"
  37. #include "str-vec.h"
  38. #include "mx-base.h"
  39. #include "mx-inlines.cc"
  40.  
  41. // charMatrix class.
  42.  
  43. charMatrix::charMatrix (char c)
  44.   : MArray2<char> ()
  45. {
  46.   int nc = 1;
  47.   int nr = 1;
  48.  
  49.   resize (nr, nc);
  50.  
  51.   elem (0, 0) = c;
  52. }
  53.  
  54. charMatrix::charMatrix (const char *s)
  55.   : MArray2<char> ()
  56. {
  57.   int nc = s ? strlen (s) : 0;
  58.   int nr = s && nc > 0 ? 1 : 0;
  59.  
  60.   resize (nr, nc);
  61.  
  62.   for (int i = 0; i < nc; i++)
  63.     elem (0, i) = s[i];
  64. }
  65.  
  66. charMatrix::charMatrix (const string& s)
  67.   : MArray2<char> ()
  68. {
  69.   int nc = s.length ();
  70.   int nr = nc > 0 ? 1 : 0;
  71.  
  72.   resize (nr, nc);
  73.  
  74.   for (int i = 0; i < nc; i++)
  75.     elem (0, i) = s[i];
  76. }
  77.  
  78. charMatrix::charMatrix (const string_vector& s)
  79.   : MArray2<char> (s.length (), s.max_length (), 0)
  80. {
  81.   int nr = rows ();
  82.  
  83.   for (int i = 0; i < nr; i++)
  84.     {
  85.       int nc = s[i].length ();
  86.       for (int j = 0; j < nc; j++)
  87.     elem (i, j) = s[i][j];
  88.     }
  89. }
  90.  
  91. bool
  92. charMatrix::operator == (const charMatrix& a) const
  93. {
  94.   if (rows () != a.rows () || cols () != a.cols ())
  95.     return 0;
  96.  
  97.   return equal (data (), a.data (), length ());
  98. }
  99.  
  100. bool
  101. charMatrix::operator != (const charMatrix& a) const
  102. {
  103.   return !(*this == a);
  104. }
  105.  
  106. charMatrix&
  107. charMatrix::insert (const char *s, int r, int c)
  108. {
  109.   if (s)
  110.     {
  111.       int s_len = strlen (s);
  112.  
  113.       if (r < 0 || r >= rows () || c < 0 || c + s_len - 1 > cols ())
  114.     {
  115.       (*current_liboctave_error_handler) ("range error for insert");
  116.       return *this;
  117.     }
  118.  
  119.       for (int i = 0; i < s_len; i++)
  120.     elem (r, c+i) = s[i];
  121.     }
  122.   return *this;
  123. }
  124.  
  125. charMatrix&
  126. charMatrix::insert (const charMatrix& a, int r, int c)
  127. {
  128.   Array2<char>::insert (a, r, c);
  129.   return *this;
  130. }
  131.  
  132. string
  133. charMatrix::row_as_string (int r, bool strip_ws = false) const 
  134. {
  135.   string retval;
  136.  
  137.   int nr = rows ();
  138.   int nc = cols ();
  139.  
  140.   if (r == 0 && nr == 0 && nc == 0)
  141.     return retval;
  142.  
  143.   if (r < 0 || r >= nr)
  144.     {
  145.       (*current_liboctave_error_handler) ("range error for row_as_string");
  146.       return retval;
  147.     }
  148.  
  149.   retval.resize (nc, '\0');
  150.  
  151.   for (int i = 0; i < nc; i++)
  152.     retval[i] = elem (r, i);
  153.  
  154.   if (strip_ws)
  155.     {
  156.       while (--nc >= 0)
  157.     {
  158.       char c = retval[nc];
  159.       if (c && c != ' ')
  160.         break;
  161.     }
  162.     }
  163.   else
  164.     {
  165.       while (--nc >= 0)
  166.     if (retval[nc])
  167.       break;
  168.     }
  169.  
  170.   retval.resize (nc+1);
  171.  
  172.   return retval;
  173. }
  174.  
  175. charMatrix
  176. charMatrix::extract (int r1, int c1, int r2, int c2) const
  177. {
  178.   if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; }
  179.   if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; }
  180.  
  181.   int new_r = r2 - r1 + 1;
  182.   int new_c = c2 - c1 + 1;
  183.  
  184.   charMatrix result (new_r, new_c);
  185.  
  186.   for (int j = 0; j < new_c; j++)
  187.     for (int i = 0; i < new_r; i++)
  188.       result.elem (i, j) = elem (r1+i, c1+j);
  189.  
  190.   return result;
  191. }
  192.  
  193. // XXX FIXME XXX -- these should probably return a boolMatrix type
  194. // instead, but that will have to wait for a future version...
  195.  
  196. Matrix
  197. charMatrix::all (void) const
  198. {
  199.   int nr = rows ();
  200.   int nc = cols ();
  201.   Matrix retval;
  202.   if (nr > 0 && nc > 0)
  203.     {
  204.       if (nr == 1)
  205.     {
  206.       retval.resize (1, 1);
  207.       retval.elem (0, 0) = 1.0;
  208.       for (int j = 0; j < nc; j++)
  209.         {
  210.           if (elem (0, j) == 0)
  211.         {
  212.           retval.elem (0, 0) = 0.0;
  213.           break;
  214.         }
  215.         }
  216.     }
  217.       else if (nc == 1)
  218.     {
  219.       retval.resize (1, 1);
  220.       retval.elem (0, 0) = 1.0;
  221.       for (int i = 0; i < nr; i++)
  222.         {
  223.           if (elem (i, 0) == 0)
  224.         {
  225.           retval.elem (0, 0) = 0.0;
  226.           break;
  227.         }
  228.         }
  229.     }
  230.       else
  231.     {
  232.       retval.resize (1, nc);
  233.       for (int j = 0; j < nc; j++)
  234.         {
  235.           retval.elem (0, j) = 1.0;
  236.           for (int i = 0; i < nr; i++)
  237.         {
  238.           if (elem (i, j) == 0)
  239.             {
  240.               retval.elem (0, j) = 0.0;
  241.               break;
  242.             }
  243.         }
  244.         }
  245.     }
  246.     }
  247.   return retval;
  248. }
  249.  
  250. Matrix
  251. charMatrix::any (void) const
  252. {
  253.   int nr = rows ();
  254.   int nc = cols ();
  255.   Matrix retval;
  256.   if (nr > 0 && nc > 0)
  257.     {
  258.       if (nr == 1)
  259.     {
  260.       retval.resize (1, 1);
  261.       retval.elem (0, 0) = 0.0;
  262.       for (int j = 0; j < nc; j++)
  263.         {
  264.           if (elem (0, j) != 0)
  265.         {
  266.           retval.elem (0, 0) = 1.0;
  267.           break;
  268.         }
  269.         }
  270.     }
  271.       else if (nc == 1)
  272.     {
  273.       retval.resize (1, 1);
  274.       retval.elem (0, 0) = 0.0;
  275.       for (int i = 0; i < nr; i++)
  276.         {
  277.           if (elem (i, 0) != 0)
  278.         {
  279.           retval.elem (0, 0) = 1.0;
  280.           break;
  281.         }
  282.         }
  283.     }
  284.       else
  285.     {
  286.       retval.resize (1, nc);
  287.       for (int j = 0; j < nc; j++)
  288.         {
  289.           retval.elem (0, j) = 0.0;
  290.           for (int i = 0; i < nr; i++)
  291.         {
  292.           if (elem (i, j) != 0)
  293.             {
  294.               retval.elem (0, j) = 1.0;
  295.               break;
  296.             }
  297.         }
  298.         }
  299.     }
  300.     }
  301.   return retval;
  302. }
  303.  
  304. /*
  305. ;;; Local Variables: ***
  306. ;;; mode: C++ ***
  307. ;;; End: ***
  308. */
  309.