source: sasview/src/sans/simulation/iqPy/libiqPy/tnt/tnt_fortran_array2d_utils.h @ aa639ea

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since aa639ea was aa639ea, checked in by Mathieu Doucet <doucetm@…>, 11 years ago

Move simulation code (unused)

  • Property mode set to 100644
File size: 4.0 KB
Line 
1/*
2*
3* Template Numerical Toolkit (TNT)
4*
5* Mathematical and Computational Sciences Division
6* National Institute of Technology,
7* Gaithersburg, MD USA
8*
9*
10* This software was developed at the National Institute of Standards and
11* Technology (NIST) by employees of the Federal Government in the course
12* of their official duties. Pursuant to title 17 Section 105 of the
13* United States Code, this software is not subject to copyright protection
14* and is in the public domain. NIST assumes no responsibility whatsoever for
15* its use by other parties, and makes no guarantees, expressed or implied,
16* about its quality, reliability, or any other characteristic.
17*
18*/
19
20
21#ifndef TNT_FORTRAN_ARRAY2D_UTILS_H
22#define TNT_FORTRAN_ARRAY2D_UTILS_H
23
24#include <iostream>
25
26namespace TNT
27{
28
29
30template <class T>
31std::ostream& operator<<(std::ostream &s, const Fortran_Array2D<T> &A)
32{
33    int M=A.dim1();
34    int N=A.dim2();
35
36    s << M << " " << N << "\n";
37
38    for (int i=1; i<=M; i++)
39    {
40        for (int j=1; j<=N; j++)
41        {
42            s << A(i,j) << " ";
43        }
44        s << "\n";
45    }
46
47
48    return s;
49}
50
51template <class T>
52std::istream& operator>>(std::istream &s, Fortran_Array2D<T> &A)
53{
54
55    int M, N;
56
57    s >> M >> N;
58
59        Fortran_Array2D<T> B(M,N);
60
61    for (int i=1; i<=M; i++)
62        for (int j=1; j<=N; j++)
63        {
64            s >>  B(i,j);
65        }
66
67        A = B;
68    return s;
69}
70
71
72
73
74template <class T>
75Fortran_Array2D<T> operator+(const Fortran_Array2D<T> &A, const Fortran_Array2D<T> &B)
76{
77        int m = A.dim1();
78        int n = A.dim2();
79
80        if (B.dim1() != m ||  B.dim2() != n )
81                return Fortran_Array2D<T>();
82
83        else
84        {
85                Fortran_Array2D<T> C(m,n);
86
87                for (int i=1; i<=m; i++)
88                {
89                        for (int j=1; j<=n; j++)
90                                C(i,j) = A(i,j) + B(i,j);
91                }
92                return C;
93        }
94}
95
96template <class T>
97Fortran_Array2D<T> operator-(const Fortran_Array2D<T> &A, const Fortran_Array2D<T> &B)
98{
99        int m = A.dim1();
100        int n = A.dim2();
101
102        if (B.dim1() != m ||  B.dim2() != n )
103                return Fortran_Array2D<T>();
104
105        else
106        {
107                Fortran_Array2D<T> C(m,n);
108
109                for (int i=1; i<=m; i++)
110                {
111                        for (int j=1; j<=n; j++)
112                                C(i,j) = A(i,j) - B(i,j);
113                }
114                return C;
115        }
116}
117
118
119template <class T>
120Fortran_Array2D<T> operator*(const Fortran_Array2D<T> &A, const Fortran_Array2D<T> &B)
121{
122        int m = A.dim1();
123        int n = A.dim2();
124
125        if (B.dim1() != m ||  B.dim2() != n )
126                return Fortran_Array2D<T>();
127
128        else
129        {
130                Fortran_Array2D<T> C(m,n);
131
132                for (int i=1; i<=m; i++)
133                {
134                        for (int j=1; j<=n; j++)
135                                C(i,j) = A(i,j) * B(i,j);
136                }
137                return C;
138        }
139}
140
141
142template <class T>
143Fortran_Array2D<T> operator/(const Fortran_Array2D<T> &A, const Fortran_Array2D<T> &B)
144{
145        int m = A.dim1();
146        int n = A.dim2();
147
148        if (B.dim1() != m ||  B.dim2() != n )
149                return Fortran_Array2D<T>();
150
151        else
152        {
153                Fortran_Array2D<T> C(m,n);
154
155                for (int i=1; i<=m; i++)
156                {
157                        for (int j=1; j<=n; j++)
158                                C(i,j) = A(i,j) / B(i,j);
159                }
160                return C;
161        }
162}
163
164
165
166template <class T>
167Fortran_Array2D<T>&  operator+=(Fortran_Array2D<T> &A, const Fortran_Array2D<T> &B)
168{
169        int m = A.dim1();
170        int n = A.dim2();
171
172        if (B.dim1() == m ||  B.dim2() == n )
173        {
174                for (int i=1; i<=m; i++)
175                {
176                        for (int j=1; j<=n; j++)
177                                A(i,j) += B(i,j);
178                }
179        }
180        return A;
181}
182
183template <class T>
184Fortran_Array2D<T>&  operator-=(Fortran_Array2D<T> &A, const Fortran_Array2D<T> &B)
185{
186        int m = A.dim1();
187        int n = A.dim2();
188
189        if (B.dim1() == m ||  B.dim2() == n )
190        {
191                for (int i=1; i<=m; i++)
192                {
193                        for (int j=1; j<=n; j++)
194                                A(i,j) -= B(i,j);
195                }
196        }
197        return A;
198}
199
200template <class T>
201Fortran_Array2D<T>&  operator*=(Fortran_Array2D<T> &A, const Fortran_Array2D<T> &B)
202{
203        int m = A.dim1();
204        int n = A.dim2();
205
206        if (B.dim1() == m ||  B.dim2() == n )
207        {
208                for (int i=1; i<=m; i++)
209                {
210                        for (int j=1; j<=n; j++)
211                                A(i,j) *= B(i,j);
212                }
213        }
214        return A;
215}
216
217template <class T>
218Fortran_Array2D<T>&  operator/=(Fortran_Array2D<T> &A, const Fortran_Array2D<T> &B)
219{
220        int m = A.dim1();
221        int n = A.dim2();
222
223        if (B.dim1() == m ||  B.dim2() == n )
224        {
225                for (int i=1; i<=m; i++)
226                {
227                        for (int j=1; j<=n; j++)
228                                A(i,j) /= B(i,j);
229                }
230        }
231        return A;
232}
233
234} // namespace TNT
235
236#endif
Note: See TracBrowser for help on using the repository browser.