DiagArray2.cc

Go to the documentation of this file.
00001 // Template array classes
00002 /*
00003 
00004 Copyright (C) 1996-2012 John W. Eaton
00005 Copyright (C) 2010 VZLU Prague
00006 
00007 This file is part of Octave.
00008 
00009 Octave is free software; you can redistribute it and/or modify it
00010 under the terms of the GNU General Public License as published by the
00011 Free Software Foundation; either version 3 of the License, or (at your
00012 option) any later version.
00013 
00014 Octave is distributed in the hope that it will be useful, but WITHOUT
00015 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00016 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00017 for more details.
00018 
00019 You should have received a copy of the GNU General Public License
00020 along with Octave; see the file COPYING.  If not, see
00021 <http://www.gnu.org/licenses/>.
00022 
00023 */
00024 
00025 #ifdef HAVE_CONFIG_H
00026 #include <config.h>
00027 #endif
00028 
00029 #include <cassert>
00030 
00031 #include <iostream>
00032 
00033 #include <algorithm>
00034 
00035 #include "DiagArray2.h"
00036 
00037 #include "lo-error.h"
00038 
00039 template <class T>
00040 DiagArray2<T>::DiagArray2 (const Array<T>& a, octave_idx_type r,
00041                            octave_idx_type c)
00042   : Array<T> (a.as_column ()), d1 (r), d2 (c)
00043 {
00044   octave_idx_type rcmin = std::min (r, c);
00045   if (rcmin != a.length ())
00046     Array<T>::resize (dim_vector (rcmin, 1));
00047 }
00048 
00049 template <class T>
00050 Array<T>
00051 DiagArray2<T>::extract_diag (octave_idx_type k) const
00052 {
00053   return diag (k);
00054 }
00055 
00056 template <class T>
00057 Array<T>
00058 DiagArray2<T>::diag (octave_idx_type k) const
00059 {
00060   Array<T> d;
00061 
00062   if (k == 0)
00063     // The main diagonal is shallow-copied.
00064     d = *this;
00065   else if (k > 0 && k < cols ())
00066     d = Array<T> (dim_vector (std::min (cols () - k, rows ()), 1), T ());
00067   else if (k < 0 && -k < rows ())
00068     d = Array<T> (dim_vector (std::min (rows () + k, cols ()), 1), T ());
00069   else
00070     (*current_liboctave_error_handler)
00071       ("diag: requested diagonal out of range");
00072 
00073   return d;
00074 }
00075 
00076 template <class T>
00077 DiagArray2<T>
00078 DiagArray2<T>::transpose (void) const
00079 {
00080   return DiagArray2<T> (*this, d2, d1);
00081 }
00082 
00083 template <class T>
00084 DiagArray2<T>
00085 DiagArray2<T>::hermitian (T (* fcn) (const T&)) const
00086 {
00087   return DiagArray2<T> (Array<T>::template map<T> (fcn), d2, d1);
00088 }
00089 
00090 // A two-dimensional array with diagonal elements only.
00091 
00092 template <class T>
00093 void
00094 DiagArray2<T>::resize (octave_idx_type r, octave_idx_type c,
00095                        const T& rfv)
00096 {
00097   if (r < 0 || c < 0)
00098     {
00099       (*current_liboctave_error_handler) ("can't resize to negative dimensions");
00100       return;
00101     }
00102 
00103   if (r != dim1 () || c != dim2 ())
00104     {
00105       Array<T>::resize (dim_vector (std::min (r, c), 1), rfv);
00106       d1 = r; d2 = c;
00107     }
00108 }
00109 
00110 template <class T>
00111 Array<T>
00112 DiagArray2<T>::array_value (void) const
00113 {
00114   Array<T> result (dims (), T (0));
00115 
00116   for (octave_idx_type i = 0, len = length (); i < len; i++)
00117     result.xelem (i, i) = dgelem (i);
00118 
00119   return result;
00120 }
00121 
00122 template <typename T>
00123 bool
00124 DiagArray2<T>::check_idx (octave_idx_type r, octave_idx_type c) const
00125 {
00126   bool ok = true;
00127 
00128   if (r < 0 || r >= dim1 ())
00129     {
00130       gripe_index_out_of_range (2, 1, r+1, dim1 ());
00131       ok = false;
00132     }
00133 
00134   if (c < 0 || c >= dim2 ())
00135     {
00136       gripe_index_out_of_range (2, 2, c+1, dim2 ());
00137       ok = false;
00138     }
00139 
00140   return ok;
00141 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines