GNU Octave  4.0.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-2015 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 // Matlab returns [] 0x1 for out-of-range diagonal
70  d.resize (dim_vector (0, 1));
71 
72  return d;
73 }
74 
75 template <class T>
78 {
79  return DiagArray2<T> (*this, d2, d1);
80 }
81 
82 template <class T>
84 DiagArray2<T>::hermitian (T (* fcn) (const T&)) const
85 {
86  return DiagArray2<T> (Array<T>::template map<T> (fcn), d2, d1);
87 }
88 
89 // A two-dimensional array with diagonal elements only.
90 
91 template <class T>
92 void
94  const T& rfv)
95 {
96  if (r < 0 || c < 0)
97  {
98  (*current_liboctave_error_handler) ("can't resize to negative dimensions");
99  return;
100  }
101 
102  if (r != dim1 () || c != dim2 ())
103  {
104  Array<T>::resize (dim_vector (std::min (r, c), 1), rfv);
105  d1 = r; d2 = c;
106  }
107 }
108 
109 template <class T>
110 Array<T>
112 {
113  Array<T> result (dims (), T (0));
114 
115  for (octave_idx_type i = 0, len = length (); i < len; i++)
116  result.xelem (i, i) = dgelem (i);
117 
118  return result;
119 }
120 
121 template <typename T>
122 bool
124 {
125  bool ok = true;
126 
127  if (r < 0 || r >= dim1 ())
128  {
129  gripe_index_out_of_range (2, 1, r+1, dim1 ());
130  ok = false;
131  }
132 
133  if (c < 0 || c >= dim2 ())
134  {
135  gripe_index_out_of_range (2, 2, c+1, dim2 ());
136  ok = false;
137  }
138 
139  return ok;
140 }
void resize(octave_idx_type n, octave_idx_type m, const T &rfv)
Definition: DiagArray2.cc:93
bool check_idx(octave_idx_type r, octave_idx_type c) const
Definition: DiagArray2.cc:123
Array< T > extract_diag(octave_idx_type k=0) const
Definition: DiagArray2.cc:58
DiagArray2(void)
Definition: DiagArray2.h:48
DiagArray2< T > hermitian(T(*fcn)(const T &)=0) const
Definition: DiagArray2.cc:84
F77_RET_T const double const double double * d
static void gripe_index_out_of_range(void)
Definition: idx-vector.cc:51
void resize(const dim_vector &dv, const T &rfv)
Definition: Array.cc:1033
Array< T > array_value(void) const
Definition: DiagArray2.cc:111
T & xelem(octave_idx_type n)
Definition: Array.h:353
Array< T > diag(octave_idx_type k=0) const GCC_ATTR_DEPRECATED
Definition: DiagArray2.cc:51
Handles the reference counting for all the derived classes.
Definition: Array.h:45
octave_idx_type length(void) const
Number of elements in the array.
Definition: Array.h:267
DiagArray2< T > transpose(void) const
Definition: DiagArray2.cc:77
charNDArray min(char d, const charNDArray &m)
Definition: chNDArray.cc:210