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