GNU Octave  8.1.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-2023 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
42 DiagArray2 : protected Array<T>
43 {
44 protected:
46 
47 public:
48 
49  using typename Array<T>::element_type;
50 
51  DiagArray2 (void)
52  : Array<T> (), m_d1 (0), m_d2 (0) { }
53 
55  : Array<T> (dim_vector (std::min (r, c), 1)), m_d1 (r), m_d2 (c) { }
56 
58  : Array<T> (dim_vector (std::min (r, c), 1), val), m_d1 (r), m_d2 (c) { }
59 
60  explicit DiagArray2 (const Array<T>& a)
61  : Array<T> (a.as_column ()), m_d1 (a.numel ()), m_d2 (a.numel ()) { }
62 
64 
66  : Array<T> (a), m_d1 (a.m_d1), m_d2 (a.m_d2) { }
67 
68  template <typename U>
70  : Array<T> (a.extract_diag ()), m_d1 (a.dim1 ()), m_d2 (a.dim2 ()) { }
71 
72  ~DiagArray2 (void) = default;
73 
74  DiagArray2<T>& operator = (const DiagArray2<T>& a)
75  {
76  if (this != &a)
77  {
79  m_d1 = a.m_d1;
80  m_d2 = a.m_d2;
81  }
82 
83  return *this;
84  }
85 
86  octave_idx_type dim1 (void) const { return m_d1; }
87  octave_idx_type dim2 (void) const { return m_d2; }
88 
89  octave_idx_type rows (void) const { return dim1 (); }
90  octave_idx_type cols (void) const { return dim2 (); }
91  octave_idx_type columns (void) const { return dim2 (); }
92 
93  octave_idx_type diag_length (void) const { return Array<T>::numel (); }
94  // FIXME: a dangerous ambiguity?
95  octave_idx_type length (void) const { return Array<T>::numel (); }
96  octave_idx_type nelem (void) const { return dim1 () * dim2 (); }
97  octave_idx_type numel (void) const { return nelem (); }
98 
99  std::size_t byte_size (void) const { return Array<T>::byte_size (); }
100 
101  dim_vector dims (void) const { return dim_vector (m_d1, m_d2); }
102 
103  bool isempty (void) const { return numel () == 0; }
104 
105  int ndims (void) const { return 2; }
106 
107  OCTAVE_API Array<T> extract_diag (octave_idx_type k = 0) const;
108 
110  {
111  return DiagArray2<T> (array_value ());
112  }
113 
114  // Warning: the non-const two-index versions will silently ignore assignments
115  // to off-diagonal elements.
116 
118  {
119  return (r == c) ? Array<T>::elem (r) : T (0);
120  }
121 
123 
125  { return Array<T>::elem (i); }
126 
128  { return Array<T>::elem (i); }
129 
131  { return check_idx (r, c) ? elem (r, c) : T (0); }
132 
133  T operator () (octave_idx_type r, octave_idx_type c) const
134  {
135  return elem (r, c);
136  }
137 
138  T& checkelem (octave_idx_type r, octave_idx_type c);
139 
140  T& operator () (octave_idx_type r, octave_idx_type c)
141  {
142  return elem (r, c);
143  }
144 
145  // No checking.
146 
148  {
149  return (r == c) ? Array<T>::xelem (r) : T (0);
150  }
151 
153  { return Array<T>::xelem (i); }
154 
156  { return Array<T>::xelem (i); }
157 
158  OCTAVE_API void resize (octave_idx_type n, octave_idx_type m, const T& rfv);
160  {
161  resize (n, m, Array<T>::resize_fill_value ());
162  }
163 
164  OCTAVE_API DiagArray2<T> transpose (void) const;
165  OCTAVE_API DiagArray2<T> hermitian (T (*fcn) (const T&) = nullptr) const;
166 
167  OCTAVE_API Array<T> array_value (void) const;
168 
169  const T * data (void) const { return Array<T>::data (); }
170 
171 #if defined (OCTAVE_PROVIDE_DEPRECATED_SYMBOLS)
172  OCTAVE_DEPRECATED (7, "for read-only access, use 'data' method instead")
173  const T * fortran_vec (void) const { return Array<T>::data (); }
174 #endif
175 
176  T * fortran_vec (void) { return Array<T>::fortran_vec (); }
177 
178  void print_info (std::ostream& os, const std::string& prefix) const
179  { Array<T>::print_info (os, prefix); }
180 
181 private:
182 
183  bool check_idx (octave_idx_type r, octave_idx_type c) const;
184 };
185 
186 #endif
static int elem
Definition: __contourc__.cc:54
charNDArray min(char d, const charNDArray &m)
Definition: chNDArray.cc:207
OCTARRAY_OVERRIDABLE_FUNC_API std::size_t byte_size(void) const
Size of the specified dimension.
Definition: Array.h:499
OCTARRAY_OVERRIDABLE_FUNC_API const T * data(void) const
Size of the specified dimension.
Definition: Array.h:663
OCTARRAY_OVERRIDABLE_FUNC_API octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:414
OCTARRAY_API T * fortran_vec(void)
Size of the specified dimension.
Definition: Array-base.cc:1766
Array< T, Alloc > & operator=(const Array< T, Alloc > &a)
Definition: Array.h:361
OCTARRAY_API void print_info(std::ostream &os, const std::string &prefix) const
Size of the specified dimension.
Definition: Array-base.cc:2749
OCTARRAY_OVERRIDABLE_FUNC_API T & elem(octave_idx_type n)
Size of the specified dimension.
Definition: Array.h:562
OCTARRAY_OVERRIDABLE_FUNC_API T & xelem(octave_idx_type n)
Size of the specified dimension.
Definition: Array.h:524
T & dgelem(octave_idx_type i)
Definition: DiagArray2.h:127
T * fortran_vec(void)
Definition: DiagArray2.h:176
octave_idx_type diag_length(void) const
Definition: DiagArray2.h:93
void print_info(std::ostream &os, const std::string &prefix) const
Definition: DiagArray2.h:178
dim_vector dims(void) const
Definition: DiagArray2.h:101
T dgelem(octave_idx_type i) const
Definition: DiagArray2.h:124
std::size_t byte_size(void) const
Definition: DiagArray2.h:99
DiagArray2(octave_idx_type r, octave_idx_type c, const T &val)
Definition: DiagArray2.h:57
octave_idx_type dim1(void) const
Definition: DiagArray2.h:86
void resize(octave_idx_type n, octave_idx_type m)
Definition: DiagArray2.h:159
DiagArray2(void)
Definition: DiagArray2.h:51
T elem(octave_idx_type r, octave_idx_type c) const
Definition: DiagArray2.h:117
DiagArray2< T > build_diag_matrix() const
Definition: DiagArray2.h:109
T checkelem(octave_idx_type r, octave_idx_type c) const
Definition: DiagArray2.h:130
const T * data(void) const
Definition: DiagArray2.h:169
octave_idx_type nelem(void) const
Definition: DiagArray2.h:96
T & dgxelem(octave_idx_type i)
Definition: DiagArray2.h:152
T xelem(octave_idx_type r, octave_idx_type c) const
Definition: DiagArray2.h:147
int ndims(void) const
Definition: DiagArray2.h:105
octave_idx_type numel(void) const
Definition: DiagArray2.h:97
octave_idx_type length(void) const
Definition: DiagArray2.h:95
octave_idx_type m_d1
Definition: DiagArray2.h:45
octave_idx_type cols(void) const
Definition: DiagArray2.h:90
DiagArray2(const DiagArray2< T > &a)
Definition: DiagArray2.h:65
DiagArray2(const Array< T > &a)
Definition: DiagArray2.h:60
octave_idx_type columns(void) const
Definition: DiagArray2.h:91
DiagArray2(octave_idx_type r, octave_idx_type c)
Definition: DiagArray2.h:54
~DiagArray2(void)=default
octave_idx_type m_d2
Definition: DiagArray2.h:45
octave_idx_type dim2(void) const
Definition: DiagArray2.h:87
DiagArray2(const DiagArray2< U > &a)
Definition: DiagArray2.h:69
octave_idx_type rows(void) const
Definition: DiagArray2.h:89
bool isempty(void) const
Definition: DiagArray2.h:103
T dgxelem(octave_idx_type i) const
Definition: DiagArray2.h:155
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
#define OCTAVE_API
Definition: main.in.cc:55
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:391