Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
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
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 }