GNU Octave 7.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
MDiagArray2.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1996-2022 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_MDiagArray2_h)
27#define octave_MDiagArray2_h 1
28
29#include "octave-config.h"
30
31#include "DiagArray2.h"
32#include "MArray.h"
33
34template <typename T> class MDiagArray2;
35
36template <typename T> MDiagArray2<T> operator + (const MDiagArray2<T>&);
37template <typename T> MDiagArray2<T> operator - (const MDiagArray2<T>&);
38template <typename T> MDiagArray2<T> operator * (const MDiagArray2<T>&,
39 const T&);
40template <typename T> MDiagArray2<T> operator / (const MDiagArray2<T>&,
41 const T&);
42template <typename T> MDiagArray2<T> operator * (const T&,
43 const MDiagArray2<T>&);
44template <typename T> MDiagArray2<T> operator + (const MDiagArray2<T>&,
45 const MDiagArray2<T>&);
46template <typename T> MDiagArray2<T> operator - (const MDiagArray2<T>&,
47 const MDiagArray2<T>&);
48template <typename T> MDiagArray2<T> product (const MDiagArray2<T>&,
49 const MDiagArray2<T>&);
50
51//! Template for two dimensional diagonal array with math operators.
52template <typename T>
53class
56{
57public:
58
59 MDiagArray2 (void) : DiagArray2<T> () { }
60
62
64 : DiagArray2<T> (r, c, val) { }
65
66 MDiagArray2 (const MDiagArray2<T>& a) : DiagArray2<T> (a) { }
67
68 MDiagArray2 (const DiagArray2<T>& a) : DiagArray2<T> (a) { }
69
70 template <typename U>
71 MDiagArray2 (const DiagArray2<U>& a) : DiagArray2<T> (a) { }
72
73 explicit MDiagArray2 (const Array<T>& a) : DiagArray2<T> (a) { }
74
76 : DiagArray2<T> (a, r, c) { }
77
78 ~MDiagArray2 (void) = default;
79
81 {
83 return *this;
84 }
85
87 {
89 }
90
91 octave_idx_type nnz (void) const
92 {
93 const T *d = this->data ();
94
95 octave_idx_type nel = this->length ();
96
97 static constexpr T zero = T ();
98
99 return std::count_if (d, d + nel,
100 [] (T elem) { return elem != zero; });
101 }
102
104 { return DiagArray2<T>::extract_diag (k); }
105
107 MDiagArray2<T> hermitian (T (*fcn) (const T&) = nullptr) const
108 { return DiagArray2<T>::hermitian (fcn); }
109
110 OCTAVE_API bool is_multiple_of_identity (T val) const;
111
112 // Currently, the OPS functions don't need to be friends, but that
113 // may change.
114
115 friend OCTAVE_API MDiagArray2<T> operator + <> (const MDiagArray2<T>&);
116 friend OCTAVE_API MDiagArray2<T> operator - <> (const MDiagArray2<T>&);
118 operator * <> (const MDiagArray2<T>&, const T&);
120 operator / <> (const MDiagArray2<T>&, const T&);
122 operator * <> (const T&, const MDiagArray2<T>&);
124 operator + <> (const MDiagArray2<T>&, const MDiagArray2<T>&);
126 operator - <> (const MDiagArray2<T>&, const MDiagArray2<T>&);
128 product <> (const MDiagArray2<T>&, const MDiagArray2<T>&);
129
130};
131
132#define MDIAGARRAY2_FORWARD_DEFS(B, R, T) \
133 inline R \
134 operator + (const R& x) \
135 { \
136 return R (operator + (dynamic_cast<const B<T>&> (x))); \
137 } \
138 inline R \
139 operator - (const R& x) \
140 { \
141 return R (operator - (dynamic_cast<const B<T>&> (x))); \
142 } \
143 inline R \
144 operator * (const R& x, const T& y) \
145 { \
146 return R (operator * (dynamic_cast<const B<T>&> (x), (y))); \
147 } \
148 inline R \
149 operator / (const R& x, const T& y) \
150 { \
151 return R (operator / (dynamic_cast<const B<T>&> (x), (y))); \
152 } \
153 inline R \
154 operator * (const T& x, const R& y) \
155 { \
156 return R (operator * ( (x), dynamic_cast<const B<T>&> (y))); \
157 } \
158 inline R \
159 operator + (const R& x, const R& y) \
160 { \
161 return R (operator + (dynamic_cast<const B<T>&> (x), \
162 dynamic_cast<const B<T>&> (y))); \
163 } \
164 inline R \
165 operator - (const R& x, const R& y) \
166 { \
167 return R (operator - (dynamic_cast<const B<T>&> (x), \
168 dynamic_cast<const B<T>&> (y))); \
169 } \
170 inline R \
171 product (const R& x, const R& y) \
172 { \
173 return R (product (dynamic_cast<const B<T>&> (x), \
174 dynamic_cast<const B<T>&> (y))); \
175 }
176
177#endif
MDiagArray2< T > product(const MDiagArray2< T > &, const MDiagArray2< T > &)
Definition: MDiagArray2.cc:95
MDiagArray2< T > operator-(const MDiagArray2< T > &)
Definition: MDiagArray2.cc:108
MDiagArray2< T > operator/(const MDiagArray2< T > &, const T &)
Definition: MDiagArray2.cc:67
MDiagArray2< T > operator*(const MDiagArray2< T > &, const T &)
Definition: MDiagArray2.cc:66
MDiagArray2< T > operator+(const MDiagArray2< T > &)
Definition: MDiagArray2.cc:101
static int elem
Definition: __contourc__.cc:54
OCTAVE_API Array< T > extract_diag(octave_idx_type k=0) const
Definition: DiagArray2.cc:50
OCTAVE_API DiagArray2< T > hermitian(T(*fcn)(const T &)=nullptr) const
Definition: DiagArray2.cc:76
octave_idx_type length(void) const
Definition: DiagArray2.h:95
OCTAVE_API Array< T > array_value(void) const
Definition: DiagArray2.cc:116
OCTAVE_API DiagArray2< T > transpose(void) const
Definition: DiagArray2.cc:69
const T * data(void) const
Definition: DiagArray2.h:169
DiagArray2< T > & operator=(const DiagArray2< T > &a)
Definition: DiagArray2.h:74
Template for N-dimensional array classes with like-type math operators.
Definition: MArray.h:63
Template for two dimensional diagonal array with math operators.
Definition: MDiagArray2.h:56
MArray< T > array_value() const
Definition: MDiagArray2.h:86
MDiagArray2(octave_idx_type r, octave_idx_type c)
Definition: MDiagArray2.h:61
MDiagArray2(const DiagArray2< U > &a)
Definition: MDiagArray2.h:71
octave_idx_type nnz(void) const
Definition: MDiagArray2.h:91
MArray< T > diag(octave_idx_type k=0) const
Definition: MDiagArray2.h:103
MDiagArray2< T > & operator=(const MDiagArray2< T > &a)
Definition: MDiagArray2.h:80
~MDiagArray2(void)=default
OCTAVE_API bool is_multiple_of_identity(T val) const
Definition: MDiagArray2.cc:36
MDiagArray2< T > transpose(void) const
Definition: MDiagArray2.h:106
MDiagArray2(const Array< T > &a)
Definition: MDiagArray2.h:73
MDiagArray2(const DiagArray2< T > &a)
Definition: MDiagArray2.h:68
MDiagArray2(const MDiagArray2< T > &a)
Definition: MDiagArray2.h:66
MDiagArray2(const Array< T > &a, octave_idx_type r, octave_idx_type c)
Definition: MDiagArray2.h:75
MDiagArray2< T > hermitian(T(*fcn)(const T &)=nullptr) const
Definition: MDiagArray2.h:107
MDiagArray2(octave_idx_type r, octave_idx_type c, const T &val)
Definition: MDiagArray2.h:63
MDiagArray2(void)
Definition: MDiagArray2.h:59
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
#define OCTAVE_API
Definition: main.in.cc:55