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 // This file should not include config.h.  It is only included in other
27 // C++ source files that should have included config.h before including
28 // this file.
29 
30 #include "MDiagArray2.h"
31 #include "Array-util.h"
32 #include "lo-error.h"
33 
34 template <typename T>
35 bool
is_multiple_of_identity(T val) const36 MDiagArray2<T>::is_multiple_of_identity (T val) const
37 {
38   bool retval = this->rows () == this->cols ();
39   if (retval)
40     {
41       octave_idx_type len = this->length ();
42       octave_idx_type i = 0;
43       for (; i < len; i++)
44         if (DiagArray2<T>::elem (i, i) != val) break;
45       retval = i == len;
46     }
47 
48   return retval;
49 }
50 
51 // Two dimensional diagonal array with math ops.
52 
53 // Element by element MDiagArray2 by MDiagArray2 ops.
54 
55 // Element by element MDiagArray2 by scalar ops.
56 
57 #define MARRAY_DAS_OP(OP, FN)                                           \
58   template <typename T>                                                 \
59   MDiagArray2<T>                                                        \
60   operator OP (const MDiagArray2<T>& a, const T& s)                     \
61   {                                                                     \
62     return MDiagArray2<T> (do_ms_binary_op<T, T, T> (a, s, FN), a.d1, a.d2); \
63   }
64 
65 MARRAY_DAS_OP (*, mx_inline_mul)
66 MARRAY_DAS_OP (/, mx_inline_div)
67 
68 // Element by element scalar by MDiagArray2 ops.
69 
70 template <typename T>
71 MDiagArray2<T>
operator *(const T & s,const MDiagArray2<T> & a)72 operator * (const T& s, const MDiagArray2<T>& a)
73 {
74   return MDiagArray2<T> (do_sm_binary_op<T, T, T> (s, a, mx_inline_mul),
75                          a.d1, a.d2);
76 }
77 
78 // Element by element MDiagArray2 by MDiagArray2 ops.
79 
80 #define MARRAY_DADA_OP(FCN, OP, FN)                                     \
81   template <typename T>                                                 \
82   MDiagArray2<T>                                                        \
83   FCN (const MDiagArray2<T>& a, const MDiagArray2<T>& b)                \
84   {                                                                     \
85     if (a.d1 != b.d1 || a.d2 != b.d2)                                   \
86       octave::err_nonconformant (#FCN, a.d1, a.d2, b.d1, b.d2);                 \
87                                                                         \
88     return MDiagArray2<T> (do_mm_binary_op<T, T, T> (a, b, FN, FN, FN, #FCN), a.d1, a.d2); \
89   }
90 
91 MARRAY_DADA_OP (operator +, +, mx_inline_add)
92 MARRAY_DADA_OP (operator -, -, mx_inline_sub)
93 MARRAY_DADA_OP (product,    *, mx_inline_mul)
94 
95 // Unary MDiagArray2 ops.
96 
97 template <typename T>
98 MDiagArray2<T>
operator +(const MDiagArray2<T> & a)99 operator + (const MDiagArray2<T>& a)
100 {
101   return a;
102 }
103 
104 template <typename T>
105 MDiagArray2<T>
operator -(const MDiagArray2<T> & a)106 operator - (const MDiagArray2<T>& a)
107 {
108   return MDiagArray2<T> (do_mx_unary_op<T, T> (a, mx_inline_uminus),
109                          a.d1, a.d2);
110 }
111