GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
DiagArray2.cc
Go to the documentation of this file.
1 // Template array classes
2 /*
3 
4 Copyright (C) 1996-2013 John W. Eaton
5 Copyright (C) 2010 VZLU Prague
6 
7 This file is part of Octave.
8 
9 Octave is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by the
11 Free Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
13 
14 Octave is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with Octave; see the file COPYING. If not, see
21 <http://www.gnu.org/licenses/>.
22 
23 */
24 
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28 
29 #include <cassert>
30 
31 #include <iostream>
32 
33 #include <algorithm>
34 
35 #include "DiagArray2.h"
36 
37 #include "lo-error.h"
38 
39 template <class T>
42  : Array<T> (a.as_column ()), d1 (r), d2 (c)
43 {
44  octave_idx_type rcmin = std::min (r, c);
45  if (rcmin != a.length ())
46  Array<T>::resize (dim_vector (rcmin, 1));
47 }
48 
49 template <class T>
52 {
53  return extract_diag (k);
54 }
55 
56 template <class T>
59 {
60  Array<T> d;
61 
62  if (k == 0)
63  // The main diagonal is shallow-copied.
64  d = *this;
65  else if (k > 0 && k < cols ())
66  d = Array<T> (dim_vector (std::min (cols () - k, rows ()), 1), T ());
67  else if (k < 0 && -k < rows ())
68  d = Array<T> (dim_vector (std::min (rows () + k, cols ()), 1), T ());
69  else
71  ("diag: requested diagonal out of range");
72 
73  return d;
74 }
75 
76 template <class T>
79 {
80  return DiagArray2<T> (*this, d2, d1);
81 }
82 
83 template <class T>
85 DiagArray2<T>::hermitian (T (* fcn) (const T&)) const
86 {
87  return DiagArray2<T> (Array<T>::template map<T> (fcn), d2, d1);
88 }
89 
90 // A two-dimensional array with diagonal elements only.
91 
92 template <class T>
93 void
95  const T& rfv)
96 {
97  if (r < 0 || c < 0)
98  {
99  (*current_liboctave_error_handler) ("can't resize to negative dimensions");
100  return;
101  }
102 
103  if (r != dim1 () || c != dim2 ())
104  {
105  Array<T>::resize (dim_vector (std::min (r, c), 1), rfv);
106  d1 = r; d2 = c;
107  }
108 }
109 
110 template <class T>
111 Array<T>
113 {
114  Array<T> result (dims (), T (0));
115 
116  for (octave_idx_type i = 0, len = length (); i < len; i++)
117  result.xelem (i, i) = dgelem (i);
118 
119  return result;
120 }
121 
122 template <typename T>
123 bool
125 {
126  bool ok = true;
127 
128  if (r < 0 || r >= dim1 ())
129  {
130  gripe_index_out_of_range (2, 1, r+1, dim1 ());
131  ok = false;
132  }
133 
134  if (c < 0 || c >= dim2 ())
135  {
136  gripe_index_out_of_range (2, 2, c+1, dim2 ());
137  ok = false;
138  }
139 
140  return ok;
141 }