GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
DiagArray2.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1996-2025 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 <cstdlib>
32
33#include "Array.h"
34
35// Array<T> is inherited privately so that some methods, like index, don't
36// produce unexpected results.
37
38template <typename T>
39class OCTAVE_API DiagArray2 : protected Array<T>
40{
41protected:
43
44public:
45
46 using typename Array<T>::element_type;
47
49 : Array<T> (), m_d1 (0), m_d2 (0) { }
50
52 : Array<T> (dim_vector (std::min (r, c), 1)), m_d1 (r), m_d2 (c) { }
53
55 : Array<T> (dim_vector (std::min (r, c), 1), val), m_d1 (r), m_d2 (c) { }
56
57 explicit DiagArray2 (const Array<T>& a)
58 : Array<T> (a.as_column ()), m_d1 (a.numel ()), m_d2 (a.numel ()) { }
59
61
63 : Array<T> (a), m_d1 (a.m_d1), m_d2 (a.m_d2) { }
64
65 template <typename U>
67 : Array<T> (a.extract_diag ()), m_d1 (a.dim1 ()), m_d2 (a.dim2 ()) { }
68
69 ~DiagArray2 () = default;
70
72 {
73 if (this != &a)
74 {
76 m_d1 = a.m_d1;
77 m_d2 = a.m_d2;
78 }
79
80 return *this;
81 }
82
83 octave_idx_type dim1 () const { return m_d1; }
84 octave_idx_type dim2 () const { return m_d2; }
85
86 octave_idx_type rows () const { return dim1 (); }
87 octave_idx_type cols () const { return dim2 (); }
88 octave_idx_type columns () const { return dim2 (); }
89
91 // FIXME: a dangerous ambiguity?
92 octave_idx_type length () const { return Array<T>::numel (); }
93 octave_idx_type nelem () const { return dim1 () * dim2 (); }
94 octave_idx_type numel () const { return nelem (); }
95
96 std::size_t byte_size () const { return Array<T>::byte_size (); }
97
98 dim_vector dims () const { return dim_vector (m_d1, m_d2); }
99
100 bool isempty () const { return numel () == 0; }
101
102 int ndims () const { return 2; }
103
104 OCTAVE_API Array<T> extract_diag (octave_idx_type k = 0) const;
105
107 {
108 return DiagArray2<T> (array_value ());
109 }
110
111 // Warning: the non-const two-index versions will silently ignore assignments
112 // to off-diagonal elements.
113
115 {
116 return (r == c) ? Array<T>::elem (r) : T (0);
117 }
118
120
122 { return Array<T>::elem (i); }
123
125 { return Array<T>::elem (i); }
126
128 { return check_idx (r, c) ? elem (r, c) : T (0); }
129
131 {
132 return elem (r, c);
133 }
134
136
138 {
139 return elem (r, c);
140 }
141
142 // No checking.
143
145 {
146 return (r == c) ? Array<T>::xelem (r) : T (0);
147 }
148
150 { return Array<T>::xelem (i); }
151
153 { return Array<T>::xelem (i); }
154
155 OCTAVE_API void resize (octave_idx_type n, octave_idx_type m, const T& rfv);
160
162 OCTAVE_API DiagArray2<T> hermitian (T (*fcn) (const T&) = nullptr) const;
163
164 OCTAVE_API Array<T> array_value () const;
165
166 const T * data () const { return Array<T>::data (); }
167
168 T * rwdata () { return Array<T>::rwdata (); }
169 inline T * fortran_vec () { return rwdata (); }
170
171 void print_info (std::ostream& os, const std::string& prefix) const
172 { Array<T>::print_info (os, prefix); }
173
174private:
175
176 bool check_idx (octave_idx_type r, octave_idx_type c) const;
177};
178
179#endif
charNDArray min(char d, const charNDArray &m)
Definition chNDArray.cc:207
N Dimensional Array with copy-on-write semantics.
Definition Array.h:130
T & xelem(octave_idx_type n)
Size of the specified dimension.
Definition Array.h:525
std::size_t byte_size() const
Size of the specified dimension.
Definition Array.h:503
Array< T, Alloc > transpose() const
Size of the specified dimension.
Array< T, Alloc > & operator=(const Array< T, Alloc > &a)
Definition Array.h:365
Array< T, Alloc > hermitian(T(*fcn)(const T &)=nullptr) const
Size of the specified dimension.
T & elem(octave_idx_type n)
Size of the specified dimension.
Definition Array.h:563
void resize(const dim_vector &dv, const T &rfv)
Size of the specified dimension.
octave_idx_type dim2() const
Definition Array.h:471
octave_idx_type dim1() const
Definition Array.h:460
const T * data() const
Size of the specified dimension.
Definition Array.h:665
T element_type
Definition Array.h:234
void print_info(std::ostream &os, const std::string &prefix) const
Size of the specified dimension.
T * rwdata()
Size of the specified dimension.
T & checkelem(octave_idx_type n)
Size of the specified dimension.
T & operator()(octave_idx_type n)
Size of the specified dimension.
Definition Array.h:578
octave_idx_type numel() const
Number of elements in the array.
Definition Array.h:418
int ndims() const
Definition DiagArray2.h:102
void print_info(std::ostream &os, const std::string &prefix) const
Definition DiagArray2.h:171
octave_idx_type nelem() const
Definition DiagArray2.h:93
octave_idx_type rows() const
Definition DiagArray2.h:86
T dgelem(octave_idx_type i) const
Definition DiagArray2.h:121
DiagArray2(octave_idx_type r, octave_idx_type c, const T &val)
Definition DiagArray2.h:54
void resize(octave_idx_type n, octave_idx_type m)
Definition DiagArray2.h:156
octave_idx_type length() const
Definition DiagArray2.h:92
T elem(octave_idx_type r, octave_idx_type c) const
Definition DiagArray2.h:114
T checkelem(octave_idx_type r, octave_idx_type c) const
Definition DiagArray2.h:127
T * fortran_vec()
Definition DiagArray2.h:169
octave_idx_type dim2() const
Definition DiagArray2.h:84
T xelem(octave_idx_type r, octave_idx_type c) const
Definition DiagArray2.h:144
bool isempty() const
Definition DiagArray2.h:100
octave_idx_type m_d1
Definition DiagArray2.h:42
DiagArray2(const DiagArray2< T > &a)
Definition DiagArray2.h:62
dim_vector dims() const
Definition DiagArray2.h:98
octave_idx_type columns() const
Definition DiagArray2.h:88
DiagArray2(const Array< T > &a)
Definition DiagArray2.h:57
octave_idx_type cols() const
Definition DiagArray2.h:87
~DiagArray2()=default
octave_idx_type dim1() const
Definition DiagArray2.h:83
T & dgxelem(octave_idx_type i)
Definition DiagArray2.h:149
DiagArray2(octave_idx_type r, octave_idx_type c)
Definition DiagArray2.h:51
DiagArray2< T > build_diag_matrix() const
Definition DiagArray2.h:106
octave_idx_type diag_length() const
Definition DiagArray2.h:90
std::size_t byte_size() const
Definition DiagArray2.h:96
octave_idx_type m_d2
Definition DiagArray2.h:42
T & dgelem(octave_idx_type i)
Definition DiagArray2.h:124
DiagArray2(const DiagArray2< U > &a)
Definition DiagArray2.h:66
octave_idx_type numel() const
Definition DiagArray2.h:94
const T * data() const
Definition DiagArray2.h:166
T * rwdata()
Definition DiagArray2.h:168
T dgxelem(octave_idx_type i) const
Definition DiagArray2.h:152
Vector representing the dimensions (size) of an Array.
Definition dim-vector.h:90
#define OCTAVE_API
Definition main.in.cc:55
T::size_type numel(const T &str)
Definition oct-string.cc:74