GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DiagArray2.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1996-2024 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 
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 () = 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 () const { return m_d1; }
87  octave_idx_type dim2 () const { return m_d2; }
88 
89  octave_idx_type rows () const { return dim1 (); }
90  octave_idx_type cols () const { return dim2 (); }
91  octave_idx_type columns () const { return dim2 (); }
92 
94  // FIXME: a dangerous ambiguity?
95  octave_idx_type length () const { return Array<T>::numel (); }
96  octave_idx_type nelem () const { return dim1 () * dim2 (); }
97  octave_idx_type numel () const { return nelem (); }
98 
99  std::size_t byte_size () const { return Array<T>::byte_size (); }
100 
101  dim_vector dims () const { return dim_vector (m_d1, m_d2); }
102 
103  bool isempty () const { return numel () == 0; }
104 
105  int ndims () 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 () const;
165  OCTAVE_API DiagArray2<T> hermitian (T (*fcn) (const T&) = nullptr) const;
166 
167  OCTAVE_API Array<T> array_value () const;
168 
169  const T * data () const { return Array<T>::data (); }
170 
171  T * fortran_vec () { return Array<T>::fortran_vec (); }
172 
173  void print_info (std::ostream& os, const std::string& prefix) const
174  { Array<T>::print_info (os, prefix); }
175 
176 private:
177 
178  bool check_idx (octave_idx_type r, octave_idx_type c) const;
179 };
180 
181 #endif
charNDArray min(char d, const charNDArray &m)
Definition: chNDArray.cc:207
T & elem(octave_idx_type n)
Size of the specified dimension.
Definition: Array.h:562
T * fortran_vec()
Size of the specified dimension.
Definition: Array-base.cc:1764
std::size_t byte_size() const
Size of the specified dimension.
Definition: Array.h:499
const T * data() const
Size of the specified dimension.
Definition: Array.h:663
Array< T, Alloc > & operator=(const Array< T, Alloc > &a)
Definition: Array.h:361
void print_info(std::ostream &os, const std::string &prefix) const
Size of the specified dimension.
Definition: Array-base.cc:2747
T & xelem(octave_idx_type n)
Size of the specified dimension.
Definition: Array.h:524
octave_idx_type numel() const
Number of elements in the array.
Definition: Array.h:414
T & dgelem(octave_idx_type i)
Definition: DiagArray2.h:127
int ndims() const
Definition: DiagArray2.h:105
T * fortran_vec()
Definition: DiagArray2.h:171
void print_info(std::ostream &os, const std::string &prefix) const
Definition: DiagArray2.h:173
octave_idx_type nelem() const
Definition: DiagArray2.h:96
octave_idx_type rows() const
Definition: DiagArray2.h:89
T dgelem(octave_idx_type i) const
Definition: DiagArray2.h:124
DiagArray2(octave_idx_type r, octave_idx_type c, const T &val)
Definition: DiagArray2.h:57
void resize(octave_idx_type n, octave_idx_type m)
Definition: DiagArray2.h:159
octave_idx_type length() const
Definition: DiagArray2.h:95
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
T & dgxelem(octave_idx_type i)
Definition: DiagArray2.h:152
octave_idx_type dim2() const
Definition: DiagArray2.h:87
T xelem(octave_idx_type r, octave_idx_type c) const
Definition: DiagArray2.h:147
bool isempty() const
Definition: DiagArray2.h:103
octave_idx_type m_d1
Definition: DiagArray2.h:45
DiagArray2(const DiagArray2< T > &a)
Definition: DiagArray2.h:65
dim_vector dims() const
Definition: DiagArray2.h:101
octave_idx_type columns() const
Definition: DiagArray2.h:91
DiagArray2(const Array< T > &a)
Definition: DiagArray2.h:60
octave_idx_type cols() const
Definition: DiagArray2.h:90
~DiagArray2()=default
octave_idx_type dim1() const
Definition: DiagArray2.h:86
DiagArray2(octave_idx_type r, octave_idx_type c)
Definition: DiagArray2.h:54
octave_idx_type diag_length() const
Definition: DiagArray2.h:93
std::size_t byte_size() const
Definition: DiagArray2.h:99
const T * data() const
Definition: DiagArray2.h:169
octave_idx_type m_d2
Definition: DiagArray2.h:45
DiagArray2(const DiagArray2< U > &a)
Definition: DiagArray2.h:69
octave_idx_type numel() const
Definition: DiagArray2.h:97
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.cc:55
T octave_idx_type m
Definition: mx-inlines.cc:781
octave_idx_type n
Definition: mx-inlines.cc:761
T * r
Definition: mx-inlines.cc:781
T::size_type numel(const T &str)
Definition: oct-string.cc:74