1988
|
1 /* |
|
2 |
|
3 Copyright (C) 1996 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #if defined (__GNUG__) |
|
24 #pragma implementation |
|
25 #endif |
|
26 |
|
27 #ifdef HAVE_CONFIG_H |
|
28 #include <config.h> |
|
29 #endif |
|
30 |
|
31 #include "MDiagArray2.h" |
|
32 #include "lo-error.h" |
|
33 |
|
34 #include "MArray-defs.h" |
|
35 |
|
36 // Two dimensional diagonal array with math ops. |
|
37 |
|
38 // Element by element MDiagArray2 by MDiagArray2 ops. |
|
39 |
|
40 template <class T> |
|
41 MDiagArray2<T>& |
|
42 operator += (MDiagArray2<T>& a, const MDiagArray2<T>& b) |
|
43 { |
|
44 int r = a.rows (); |
|
45 int c = a.cols (); |
|
46 if (r != b.rows () || c != b.cols ()) |
|
47 { |
|
48 (*current_liboctave_error_handler) |
|
49 ("nonconformant array operator += attempted"); |
|
50 return MDiagArray2<T> (); |
|
51 } |
|
52 else |
|
53 { |
|
54 int l = a.length (); |
|
55 DO_VV_OP2 (+=); |
|
56 } |
|
57 return a; |
|
58 } |
|
59 |
|
60 template <class T> |
|
61 MDiagArray2<T>& |
|
62 operator -= (MDiagArray2<T>& a, const MDiagArray2<T>& b) |
|
63 { |
|
64 int r = a.rows (); |
|
65 int c = a.cols (); |
|
66 if (r != b.rows () || c != b.cols ()) |
|
67 { |
|
68 (*current_liboctave_error_handler) |
|
69 ("nonconformant array operator -= attempted"); |
|
70 return MDiagArray2<T> (); |
|
71 } |
|
72 else |
|
73 { |
|
74 int l = a.length (); |
|
75 DO_VV_OP2 (-=); |
|
76 } |
|
77 return a; |
|
78 } |
|
79 |
|
80 // Element by element MDiagArray2 by scalar ops. |
|
81 |
|
82 #define MARRAY_DAS_OP(OP) \ |
|
83 template <class T> \ |
|
84 MDiagArray2<T> \ |
|
85 operator OP (const MDiagArray2<T>& a, const T& s) \ |
|
86 { \ |
|
87 DO_VS_OP (OP); \ |
|
88 return MDiagArray2<T> (result, a.rows (), a.cols ()); \ |
|
89 } |
|
90 |
|
91 MARRAY_DAS_OP (*) |
|
92 MARRAY_DAS_OP (/) |
|
93 |
|
94 // Element by element scalar by MDiagArray2 ops. |
|
95 |
|
96 template <class T> |
|
97 MDiagArray2<T> |
|
98 operator * (const T& s, const MDiagArray2<T>& a) |
|
99 { |
|
100 DO_SV_OP (*); |
|
101 return MDiagArray2<T> (result, a.rows (), a.cols ()); |
|
102 } |
|
103 |
|
104 // Element by element MDiagArray2 by MDiagArray2 ops. |
|
105 |
|
106 #define MARRAY_DADA_OP(FCN, OP, OP_STR) \ |
|
107 template <class T> \ |
|
108 MDiagArray2<T> \ |
|
109 FCN (const MDiagArray2<T>& a, const MDiagArray2<T>& b) \ |
|
110 { \ |
|
111 int r = a.rows (); \ |
|
112 int c = a.cols (); \ |
|
113 if (r != b.rows () || c != b.cols ()) \ |
|
114 { \ |
|
115 (*current_liboctave_error_handler) \ |
|
116 ("nonconformant diagonal array " OP_STR " attempted"); \ |
|
117 return MDiagArray2<T> (); \ |
|
118 } \ |
|
119 if (c == 0 || r == 0) \ |
|
120 return MDiagArray2<T> (); \ |
|
121 int l = a.length (); \ |
|
122 DO_VV_OP (OP); \ |
|
123 return MDiagArray2<T> (result, r, c); \ |
|
124 } |
|
125 |
|
126 MARRAY_DADA_OP (operator +, +, "addition") |
|
127 MARRAY_DADA_OP (operator -, -, "subtraction") |
|
128 MARRAY_DADA_OP (product, *, "product") |
|
129 |
|
130 // Unary MDiagArray2 ops. |
|
131 |
|
132 template <class T> |
|
133 MDiagArray2<T> |
|
134 operator - (const MDiagArray2<T>& a) |
|
135 { |
|
136 NEG_V; |
|
137 return MDiagArray2<T> (result, a.rows (), a.cols ()); |
|
138 } |
|
139 |
|
140 /* |
|
141 ;;; Local Variables: *** |
|
142 ;;; mode: C++ *** |
|
143 ;;; End: *** |
|
144 */ |