4508
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 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__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
|
24 #pragma implementation |
|
25 #endif |
|
26 |
|
27 #ifdef HAVE_CONFIG_H |
|
28 #include <config.h> |
|
29 #endif |
|
30 |
|
31 #include "MArrayN.h" |
|
32 #include "ArrayN-idx.h" |
|
33 #include "lo-error.h" |
|
34 |
|
35 #include "MArray-defs.h" |
|
36 |
4513
|
37 // N-dimensional array with math ops. |
4508
|
38 |
|
39 // Element by element MArrayN by scalar ops. |
|
40 |
|
41 template <class T> |
|
42 MArrayN<T>& |
|
43 operator += (MArrayN<T>& a, const T& s) |
|
44 { |
|
45 DO_VS_OP2 (+=) |
|
46 return a; |
|
47 } |
|
48 |
|
49 template <class T> |
|
50 MArrayN<T>& |
|
51 operator -= (MArrayN<T>& a, const T& s) |
|
52 { |
|
53 DO_VS_OP2 (-=) |
|
54 return a; |
|
55 } |
|
56 |
4513
|
57 #if 0 |
4508
|
58 #define MARRAYN_NDS_OP(OP) \ |
|
59 template <class T> \ |
|
60 MArrayN<T> \ |
|
61 operator OP (const MArrayN<T>& a, const T& s) \ |
|
62 { \ |
|
63 MArrayN<T> result (a.dims ()); \ |
|
64 T *r = result.fortran_vec (); \ |
|
65 int l = a.length (); \ |
|
66 const T *v = a.data (); \ |
|
67 DO_VS_OP (r, l, v, OP, s); \ |
|
68 return result; \ |
|
69 } |
|
70 |
|
71 MARRAYN_NDS_OP (+) |
|
72 MARRAYN_NDS_OP (-) |
|
73 MARRAYN_NDS_OP (*) |
|
74 MARRAYN_NDS_OP (/) |
|
75 |
|
76 // Element by element MArrayN by scalar ops. |
|
77 |
|
78 #define MARRAYN_SND_OP(OP) \ |
|
79 template <class T> \ |
|
80 MArrayN<T> \ |
|
81 operator OP (const T& s, const MArrayN<T>& a) \ |
|
82 { \ |
|
83 MArrayN<T> result (a.dims ()); \ |
|
84 T *r = result.fortran_vec (); \ |
|
85 int l = a.length (); \ |
|
86 const T *v = a.data (); \ |
|
87 DO_SV_OP (r, l, s, OP, v); \ |
|
88 return result; \ |
|
89 } |
|
90 |
|
91 MARRAYN_SND_OP (+) |
|
92 MARRAYN_SND_OP (-) |
|
93 MARRAYN_SND_OP (*) |
|
94 MARRAYN_SND_OP (/) |
|
95 |
|
96 #define MARRAY_NDND_OP(FCN, OP) \ |
|
97 template <class T> \ |
|
98 MArrayN<T> \ |
|
99 FCN (const MArrayN<T>& a, const MArrayN<T>& b) \ |
|
100 { \ |
4513
|
101 dim_vector a_dims = a.dims (); \ |
|
102 dim_vector b_dims = b.dims (); \ |
4508
|
103 int dims_ok = 1; \ |
|
104 int any_dims_zero = 0; \ |
|
105 if (a_dims.length () != b_dims.length ()) \ |
|
106 dims_ok = 0; \ |
|
107 else \ |
|
108 { \ |
|
109 for (int i = 0; i < a_dims.length (); i++) \ |
|
110 { \ |
|
111 if (a_dims (i) != b_dims (i)) \ |
|
112 { dims_ok = 0; break; } \ |
|
113 if (a_dims (i) == 0) \ |
|
114 any_dims_zero = 1; \ |
|
115 } \ |
|
116 } \ |
|
117 if (!dims_ok) \ |
|
118 { \ |
|
119 gripe_nonconformant (#FCN, a_dims, b_dims); \ |
|
120 return MArrayN<T> (); \ |
|
121 } \ |
|
122 if (any_dims_zero) \ |
|
123 return MArrayN<T> (a_dims); \ |
|
124 int l = a.length (); \ |
|
125 MArrayN<T> result (a_dims); \ |
|
126 T* r = result.fortran_vec (); \ |
|
127 const T *x = a.data (); \ |
|
128 const T *y = b.data (); \ |
|
129 DO_VV_OP (r, l, x, OP, y); \ |
|
130 return result; \ |
|
131 } |
|
132 |
|
133 MARRAY_NDND_OP (operator +, +) |
|
134 MARRAY_NDND_OP (operator -, -) |
|
135 |
|
136 template <class T> |
|
137 MArrayN<T> |
|
138 operator + (const MArrayN<T>& a) |
|
139 { |
|
140 return a; |
|
141 } |
|
142 |
|
143 template <class T> |
|
144 MArrayN<T> |
|
145 operator - (const MArrayN<T>& a) |
|
146 { |
|
147 int l = a.length (); |
|
148 MArrayN<T> result (a.dims ()); |
|
149 T *r = result.fortran_vec (); |
|
150 const T *x = a.data (); |
|
151 NEG_V (r, l, x); |
|
152 return result; |
|
153 } |
|
154 |
|
155 #endif |
|
156 |
|
157 /* |
|
158 ;;; Local Variables: *** |
|
159 ;;; mode: C++ *** |
|
160 ;;; End: *** |
|
161 */ |