GNU Octave  6.2.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
DiagArray2.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1996-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if ! defined (octave_DiagArray2_h)
27 #define octave_DiagArray2_h 1
28 
29 #include "octave-config.h"
30 
31 #include <cassert>
32 #include <cstdlib>
33 
34 #include "Array.h"
35 
36 // Array<T> is inherited privately so that some methods, like index, don't
37 // produce unexpected results.
38 
39 template <typename T>
40 class
41 DiagArray2 : protected Array<T>
42 {
43 protected:
45 
46 public:
47 
48  using typename Array<T>::element_type;
49 
50  DiagArray2 (void)
51  : Array<T> (), d1 (0), d2 (0) { }
52 
54  : Array<T> (dim_vector (std::min (r, c), 1)), d1 (r), d2 (c) { }
55 
57  : Array<T> (dim_vector (std::min (r, c), 1), val), d1 (r), d2 (c) { }
58 
59  explicit DiagArray2 (const Array<T>& a)
60  : Array<T> (a.as_column ()), d1 (a.numel ()), d2 (a.numel ()) { }
61 
63 
65  : Array<T> (a), d1 (a.d1), d2 (a.d2) { }
66 
67  template <typename U>
69  : Array<T> (a.extract_diag ()), d1 (a.dim1 ()), d2 (a.dim2 ()) { }
70 
71  ~DiagArray2 (void) = default;
72 
73  DiagArray2<T>& operator = (const DiagArray2<T>& a)
74  {
75  if (this != &a)
76  {
78  d1 = a.d1;
79  d2 = a.d2;
80  }
81 
82  return *this;
83  }
84 
85  octave_idx_type dim1 (void) const { return d1; }
86  octave_idx_type dim2 (void) const { return d2; }
87 
88  octave_idx_type rows (void) const { return dim1 (); }
89  octave_idx_type cols (void) const { return dim2 (); }
90  octave_idx_type columns (void) const { return dim2 (); }
91 
92  octave_idx_type diag_length (void) const { return Array<T>::numel (); }
93  // FIXME: a dangerous ambiguity?
94  octave_idx_type length (void) const { return Array<T>::numel (); }
95  octave_idx_type nelem (void) const { return dim1 () * dim2 (); }
96  octave_idx_type numel (void) const { return nelem (); }
97 
98  size_t byte_size (void) const { return Array<T>::byte_size (); }
99 
100  dim_vector dims (void) const { return dim_vector (d1, d2); }
101 
102  bool isempty (void) const { return numel () == 0; }
103 
104  int ndims (void) const { return 2; }
105 
106  Array<T> extract_diag (octave_idx_type k = 0) const;
107 
109  {
110  return DiagArray2<T> (array_value ());
111  }
112 
113  // Warning: the non-const two-index versions will silently ignore assignments
114  // to off-diagonal elements.
115 
117  {
118  return (r == c) ? Array<T>::elem (r) : T (0);
119  }
120 
122 
124  { return Array<T>::elem (i); }
125 
127  { return Array<T>::elem (i); }
128 
130  { return check_idx (r, c) ? elem (r, c) : T (0); }
131 
132  T operator () (octave_idx_type r, octave_idx_type c) const
133  {
134  return elem (r, c);
135  }
136 
137  T& checkelem (octave_idx_type r, octave_idx_type c);
138 
139  T& operator () (octave_idx_type r, octave_idx_type c)
140  {
141  return elem (r, c);
142  }
143 
144  // No checking.
145 
147  {
148  return (r == c) ? Array<T>::xelem (r) : T (0);
149  }
150 
152  { return Array<T>::xelem (i); }
153 
155  { return Array<T>::xelem (i); }
156 
157  void resize (octave_idx_type n, octave_idx_type m, const T& rfv);
159  {
160  resize (n, m, Array<T>::resize_fill_value ());
161  }
162 
163  DiagArray2<T> transpose (void) const;
164  DiagArray2<T> hermitian (T (*fcn) (const T&) = nullptr) const;
165 
166  Array<T> array_value (void) const;
167 
168  const T * data (void) const { return Array<T>::data (); }
169 
170  const T * fortran_vec (void) const { return Array<T>::fortran_vec (); }
171 
172  T * fortran_vec (void) { return Array<T>::fortran_vec (); }
173 
174  void print_info (std::ostream& os, const std::string& prefix) const
175  { Array<T>::print_info (os, prefix); }
176 
177 private:
178 
179  bool check_idx (octave_idx_type r, octave_idx_type c) const;
180 };
181 
182 #endif
static int elem
Definition: __contourc__.cc:52
charNDArray min(char d, const charNDArray &m)
Definition: chNDArray.cc:207
N Dimensional Array with copy-on-write semantics.
Definition: Array.h:128
T & xelem(octave_idx_type n)
Size of the specified dimension.
Definition: Array.h:469
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:377
Array< T > & operator=(const Array< T > &a)
Definition: Array.h:325
T & elem(octave_idx_type n)
Size of the specified dimension.
Definition: Array.h:499
size_t byte_size(void) const
Size of the specified dimension.
Definition: Array.h:449
const T * data(void) const
Size of the specified dimension.
Definition: Array.h:581
void print_info(std::ostream &os, const std::string &prefix) const
Size of the specified dimension.
Definition: Array.cc:2734
T element_type
Definition: Array.h:202
const T * fortran_vec(void) const
Size of the specified dimension.
Definition: Array.h:583
T & dgelem(octave_idx_type i)
Definition: DiagArray2.h:126
T * fortran_vec(void)
Definition: DiagArray2.h:172
octave_idx_type diag_length(void) const
Definition: DiagArray2.h:92
void print_info(std::ostream &os, const std::string &prefix) const
Definition: DiagArray2.h:174
dim_vector dims(void) const
Definition: DiagArray2.h:100
size_t byte_size(void) const
Definition: DiagArray2.h:98
octave_idx_type d1
Definition: DiagArray2.h:44
T dgelem(octave_idx_type i) const
Definition: DiagArray2.h:123
DiagArray2(octave_idx_type r, octave_idx_type c, const T &val)
Definition: DiagArray2.h:56
octave_idx_type dim1(void) const
Definition: DiagArray2.h:85
void resize(octave_idx_type n, octave_idx_type m)
Definition: DiagArray2.h:158
DiagArray2(void)
Definition: DiagArray2.h:50
T elem(octave_idx_type r, octave_idx_type c) const
Definition: DiagArray2.h:116
DiagArray2< T > build_diag_matrix() const
Definition: DiagArray2.h:108
T checkelem(octave_idx_type r, octave_idx_type c) const
Definition: DiagArray2.h:129
const T * data(void) const
Definition: DiagArray2.h:168
octave_idx_type nelem(void) const
Definition: DiagArray2.h:95
T & dgxelem(octave_idx_type i)
Definition: DiagArray2.h:151
T xelem(octave_idx_type r, octave_idx_type c) const
Definition: DiagArray2.h:146
int ndims(void) const
Definition: DiagArray2.h:104
octave_idx_type numel(void) const
Definition: DiagArray2.h:96
octave_idx_type length(void) const
Definition: DiagArray2.h:94
octave_idx_type cols(void) const
Definition: DiagArray2.h:89
DiagArray2(const DiagArray2< T > &a)
Definition: DiagArray2.h:64
octave_idx_type d2
Definition: DiagArray2.h:44
DiagArray2(const Array< T > &a)
Definition: DiagArray2.h:59
octave_idx_type columns(void) const
Definition: DiagArray2.h:90
DiagArray2(octave_idx_type r, octave_idx_type c)
Definition: DiagArray2.h:53
~DiagArray2(void)=default
octave_idx_type dim2(void) const
Definition: DiagArray2.h:86
DiagArray2(const DiagArray2< U > &a)
Definition: DiagArray2.h:68
octave_idx_type rows(void) const
Definition: DiagArray2.h:88
bool isempty(void) const
Definition: DiagArray2.h:102
const T * fortran_vec(void) const
Definition: DiagArray2.h:170
T dgxelem(octave_idx_type i) const
Definition: DiagArray2.h:154
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:95
T octave_idx_type m
Definition: mx-inlines.cc:773
octave_idx_type n
Definition: mx-inlines.cc:753
T * r
Definition: mx-inlines.cc:773
T::size_type numel(const T &str)
Definition: oct-string.cc:71
static void transpose(octave_idx_type N, const octave_idx_type *ridx, const octave_idx_type *cidx, octave_idx_type *ridx2, octave_idx_type *cidx2)
Definition: symrcm.cc:389