1 | //$$ newmat2.cpp Matrix row and column operations |
---|
2 | |
---|
3 | // Copyright (C) 1991,2,3,4: R B Davies |
---|
4 | |
---|
5 | #define WANT_MATH |
---|
6 | |
---|
7 | #include "include.h" |
---|
8 | |
---|
9 | #include "newmat.h" |
---|
10 | #include "newmatrc.h" |
---|
11 | |
---|
12 | #ifdef use_namespace |
---|
13 | namespace NEWMAT { |
---|
14 | #endif |
---|
15 | |
---|
16 | |
---|
17 | #ifdef DO_REPORT |
---|
18 | #define REPORT { static ExeCounter ExeCount(__LINE__,2); ++ExeCount; } |
---|
19 | #else |
---|
20 | #define REPORT {} |
---|
21 | #endif |
---|
22 | |
---|
23 | //#define MONITOR(what,storage,store) { cout << what << " " << storage << " at " << (long)store << "\n"; } |
---|
24 | |
---|
25 | #define MONITOR(what,store,storage) {} |
---|
26 | |
---|
27 | /************************** Matrix Row/Col functions ************************/ |
---|
28 | |
---|
29 | void MatrixRowCol::Add(const MatrixRowCol& mrc) |
---|
30 | { |
---|
31 | // THIS += mrc |
---|
32 | REPORT |
---|
33 | int f = mrc.skip; int l = f + mrc.storage; int lx = skip + storage; |
---|
34 | if (f < skip) f = skip; if (l > lx) l = lx; l -= f; |
---|
35 | if (l<=0) return; |
---|
36 | Real* elx=data+(f-skip); Real* el=mrc.data+(f-mrc.skip); |
---|
37 | while (l--) *elx++ += *el++; |
---|
38 | } |
---|
39 | |
---|
40 | void MatrixRowCol::AddScaled(const MatrixRowCol& mrc, Real x) |
---|
41 | { |
---|
42 | REPORT |
---|
43 | // THIS += (mrc * x) |
---|
44 | int f = mrc.skip; int l = f + mrc.storage; int lx = skip + storage; |
---|
45 | if (f < skip) f = skip; if (l > lx) l = lx; l -= f; |
---|
46 | if (l<=0) return; |
---|
47 | Real* elx=data+(f-skip); Real* el=mrc.data+(f-mrc.skip); |
---|
48 | while (l--) *elx++ += *el++ * x; |
---|
49 | } |
---|
50 | |
---|
51 | void MatrixRowCol::Sub(const MatrixRowCol& mrc) |
---|
52 | { |
---|
53 | REPORT |
---|
54 | // THIS -= mrc |
---|
55 | int f = mrc.skip; int l = f + mrc.storage; int lx = skip + storage; |
---|
56 | if (f < skip) f = skip; if (l > lx) l = lx; l -= f; |
---|
57 | if (l<=0) return; |
---|
58 | Real* elx=data+(f-skip); Real* el=mrc.data+(f-mrc.skip); |
---|
59 | while (l--) *elx++ -= *el++; |
---|
60 | } |
---|
61 | |
---|
62 | void MatrixRowCol::Inject(const MatrixRowCol& mrc) |
---|
63 | // copy stored elements only |
---|
64 | { |
---|
65 | REPORT |
---|
66 | int f = mrc.skip; int l = f + mrc.storage; int lx = skip + storage; |
---|
67 | if (f < skip) f = skip; if (l > lx) l = lx; l -= f; |
---|
68 | if (l<=0) return; |
---|
69 | Real* elx=data+(f-skip); Real* ely=mrc.data+(f-mrc.skip); |
---|
70 | while (l--) *elx++ = *ely++; |
---|
71 | } |
---|
72 | |
---|
73 | Real DotProd(const MatrixRowCol& mrc1, const MatrixRowCol& mrc2) |
---|
74 | { |
---|
75 | REPORT // not accessed |
---|
76 | int f = mrc1.skip; int f2 = mrc2.skip; |
---|
77 | int l = f + mrc1.storage; int l2 = f2 + mrc2.storage; |
---|
78 | if (f < f2) f = f2; if (l > l2) l = l2; l -= f; |
---|
79 | if (l<=0) return 0.0; |
---|
80 | |
---|
81 | Real* el1=mrc1.data+(f-mrc1.skip); Real* el2=mrc2.data+(f-mrc2.skip); |
---|
82 | Real sum = 0.0; |
---|
83 | while (l--) sum += *el1++ * *el2++; |
---|
84 | return sum; |
---|
85 | } |
---|
86 | |
---|
87 | void MatrixRowCol::Add(const MatrixRowCol& mrc1, const MatrixRowCol& mrc2) |
---|
88 | { |
---|
89 | // THIS = mrc1 + mrc2 |
---|
90 | int f = skip; int l = skip + storage; |
---|
91 | int f1 = mrc1.skip; int l1 = f1 + mrc1.storage; |
---|
92 | if (f1<f) f1=f; if (l1>l) l1=l; |
---|
93 | int f2 = mrc2.skip; int l2 = f2 + mrc2.storage; |
---|
94 | if (f2<f) f2=f; if (l2>l) l2=l; |
---|
95 | Real* el = data + (f-skip); |
---|
96 | Real* el1 = mrc1.data+(f1-mrc1.skip); Real* el2 = mrc2.data+(f2-mrc2.skip); |
---|
97 | if (f1<f2) |
---|
98 | { |
---|
99 | int i = f1-f; while (i--) *el++ = 0.0; |
---|
100 | if (l1<=f2) // disjoint |
---|
101 | { |
---|
102 | REPORT // not accessed |
---|
103 | i = l1-f1; while (i--) *el++ = *el1++; |
---|
104 | i = f2-l1; while (i--) *el++ = 0.0; |
---|
105 | i = l2-f2; while (i--) *el++ = *el2++; |
---|
106 | i = l-l2; while (i--) *el++ = 0.0; |
---|
107 | } |
---|
108 | else |
---|
109 | { |
---|
110 | i = f2-f1; while (i--) *el++ = *el1++; |
---|
111 | if (l1<=l2) |
---|
112 | { |
---|
113 | REPORT |
---|
114 | i = l1-f2; while (i--) *el++ = *el1++ + *el2++; |
---|
115 | i = l2-l1; while (i--) *el++ = *el2++; |
---|
116 | i = l-l2; while (i--) *el++ = 0.0; |
---|
117 | } |
---|
118 | else |
---|
119 | { |
---|
120 | REPORT |
---|
121 | i = l2-f2; while (i--) *el++ = *el1++ + *el2++; |
---|
122 | i = l1-l2; while (i--) *el++ = *el1++; |
---|
123 | i = l-l1; while (i--) *el++ = 0.0; |
---|
124 | } |
---|
125 | } |
---|
126 | } |
---|
127 | else |
---|
128 | { |
---|
129 | int i = f2-f; while (i--) *el++ = 0.0; |
---|
130 | if (l2<=f1) // disjoint |
---|
131 | { |
---|
132 | REPORT // not accessed |
---|
133 | i = l2-f2; while (i--) *el++ = *el2++; |
---|
134 | i = f1-l2; while (i--) *el++ = 0.0; |
---|
135 | i = l1-f1; while (i--) *el++ = *el1++; |
---|
136 | i = l-l1; while (i--) *el++ = 0.0; |
---|
137 | } |
---|
138 | else |
---|
139 | { |
---|
140 | i = f1-f2; while (i--) *el++ = *el2++; |
---|
141 | if (l2<=l1) |
---|
142 | { |
---|
143 | REPORT |
---|
144 | i = l2-f1; while (i--) *el++ = *el1++ + *el2++; |
---|
145 | i = l1-l2; while (i--) *el++ = *el1++; |
---|
146 | i = l-l1; while (i--) *el++ = 0.0; |
---|
147 | } |
---|
148 | else |
---|
149 | { |
---|
150 | REPORT |
---|
151 | i = l1-f1; while (i--) *el++ = *el1++ + *el2++; |
---|
152 | i = l2-l1; while (i--) *el++ = *el2++; |
---|
153 | i = l-l2; while (i--) *el++ = 0.0; |
---|
154 | } |
---|
155 | } |
---|
156 | } |
---|
157 | } |
---|
158 | |
---|
159 | void MatrixRowCol::Sub(const MatrixRowCol& mrc1, const MatrixRowCol& mrc2) |
---|
160 | { |
---|
161 | // THIS = mrc1 - mrc2 |
---|
162 | int f = skip; int l = skip + storage; |
---|
163 | int f1 = mrc1.skip; int l1 = f1 + mrc1.storage; |
---|
164 | if (f1<f) f1=f; if (l1>l) l1=l; |
---|
165 | int f2 = mrc2.skip; int l2 = f2 + mrc2.storage; |
---|
166 | if (f2<f) f2=f; if (l2>l) l2=l; |
---|
167 | Real* el = data + (f-skip); |
---|
168 | Real* el1 = mrc1.data+(f1-mrc1.skip); Real* el2 = mrc2.data+(f2-mrc2.skip); |
---|
169 | if (f1<f2) |
---|
170 | { |
---|
171 | int i = f1-f; while (i--) *el++ = 0.0; |
---|
172 | if (l1<=f2) // disjoint |
---|
173 | { |
---|
174 | REPORT // not accessed |
---|
175 | i = l1-f1; while (i--) *el++ = *el1++; |
---|
176 | i = f2-l1; while (i--) *el++ = 0.0; |
---|
177 | i = l2-f2; while (i--) *el++ = - *el2++; |
---|
178 | i = l-l2; while (i--) *el++ = 0.0; |
---|
179 | } |
---|
180 | else |
---|
181 | { |
---|
182 | i = f2-f1; while (i--) *el++ = *el1++; |
---|
183 | if (l1<=l2) |
---|
184 | { |
---|
185 | REPORT |
---|
186 | i = l1-f2; while (i--) *el++ = *el1++ - *el2++; |
---|
187 | i = l2-l1; while (i--) *el++ = - *el2++; |
---|
188 | i = l-l2; while (i--) *el++ = 0.0; |
---|
189 | } |
---|
190 | else |
---|
191 | { |
---|
192 | REPORT |
---|
193 | i = l2-f2; while (i--) *el++ = *el1++ - *el2++; |
---|
194 | i = l1-l2; while (i--) *el++ = *el1++; |
---|
195 | i = l-l1; while (i--) *el++ = 0.0; |
---|
196 | } |
---|
197 | } |
---|
198 | } |
---|
199 | else |
---|
200 | { |
---|
201 | int i = f2-f; while (i--) *el++ = 0.0; |
---|
202 | if (l2<=f1) // disjoint |
---|
203 | { |
---|
204 | REPORT // not accessed |
---|
205 | i = l2-f2; while (i--) *el++ = - *el2++; |
---|
206 | i = f1-l2; while (i--) *el++ = 0.0; |
---|
207 | i = l1-f1; while (i--) *el++ = *el1++; |
---|
208 | i = l-l1; while (i--) *el++ = 0.0; |
---|
209 | } |
---|
210 | else |
---|
211 | { |
---|
212 | i = f1-f2; while (i--) *el++ = - *el2++; |
---|
213 | if (l2<=l1) |
---|
214 | { |
---|
215 | REPORT |
---|
216 | i = l2-f1; while (i--) *el++ = *el1++ - *el2++; |
---|
217 | i = l1-l2; while (i--) *el++ = *el1++; |
---|
218 | i = l-l1; while (i--) *el++ = 0.0; |
---|
219 | } |
---|
220 | else |
---|
221 | { |
---|
222 | REPORT |
---|
223 | i = l1-f1; while (i--) *el++ = *el1++ - *el2++; |
---|
224 | i = l2-l1; while (i--) *el++ = - *el2++; |
---|
225 | i = l-l2; while (i--) *el++ = 0.0; |
---|
226 | } |
---|
227 | } |
---|
228 | } |
---|
229 | } |
---|
230 | |
---|
231 | |
---|
232 | void MatrixRowCol::Add(const MatrixRowCol& mrc1, Real x) |
---|
233 | { |
---|
234 | // THIS = mrc1 + x |
---|
235 | REPORT |
---|
236 | if (!storage) return; |
---|
237 | int f = mrc1.skip; int l = f + mrc1.storage; int lx = skip + storage; |
---|
238 | if (f < skip) { f = skip; if (l < f) l = f; } |
---|
239 | if (l > lx) { l = lx; if (f > lx) f = lx; } |
---|
240 | |
---|
241 | Real* elx = data; Real* ely = mrc1.data+(f-mrc1.skip); |
---|
242 | |
---|
243 | int l1 = f-skip; while (l1--) *elx++ = x; |
---|
244 | l1 = l-f; while (l1--) *elx++ = *ely++ + x; |
---|
245 | lx -= l; while (lx--) *elx++ = x; |
---|
246 | } |
---|
247 | |
---|
248 | void MatrixRowCol::NegAdd(const MatrixRowCol& mrc1, Real x) |
---|
249 | { |
---|
250 | // THIS = x - mrc1 |
---|
251 | REPORT |
---|
252 | if (!storage) return; |
---|
253 | int f = mrc1.skip; int l = f + mrc1.storage; int lx = skip + storage; |
---|
254 | if (f < skip) { f = skip; if (l < f) l = f; } |
---|
255 | if (l > lx) { l = lx; if (f > lx) f = lx; } |
---|
256 | |
---|
257 | Real* elx = data; Real* ely = mrc1.data+(f-mrc1.skip); |
---|
258 | |
---|
259 | int l1 = f-skip; while (l1--) *elx++ = x; |
---|
260 | l1 = l-f; while (l1--) *elx++ = x - *ely++; |
---|
261 | lx -= l; while (lx--) *elx++ = x; |
---|
262 | } |
---|
263 | |
---|
264 | void MatrixRowCol::RevSub(const MatrixRowCol& mrc1) |
---|
265 | { |
---|
266 | // THIS = mrc - THIS |
---|
267 | REPORT |
---|
268 | if (!storage) return; |
---|
269 | int f = mrc1.skip; int l = f + mrc1.storage; int lx = skip + storage; |
---|
270 | if (f < skip) { f = skip; if (l < f) l = f; } |
---|
271 | if (l > lx) { l = lx; if (f > lx) f = lx; } |
---|
272 | |
---|
273 | Real* elx = data; Real* ely = mrc1.data+(f-mrc1.skip); |
---|
274 | |
---|
275 | int l1 = f-skip; while (l1--) { *elx = - *elx; elx++; } |
---|
276 | l1 = l-f; while (l1--) { *elx = *ely++ - *elx; elx++; } |
---|
277 | lx -= l; while (lx--) { *elx = - *elx; elx++; } |
---|
278 | } |
---|
279 | |
---|
280 | void MatrixRowCol::ConCat(const MatrixRowCol& mrc1, const MatrixRowCol& mrc2) |
---|
281 | { |
---|
282 | // THIS = mrc1 | mrc2 |
---|
283 | REPORT |
---|
284 | int f1 = mrc1.skip; int l1 = f1 + mrc1.storage; int lx = skip + storage; |
---|
285 | if (f1 < skip) { f1 = skip; if (l1 < f1) l1 = f1; } |
---|
286 | if (l1 > lx) { l1 = lx; if (f1 > lx) f1 = lx; } |
---|
287 | |
---|
288 | Real* elx = data; |
---|
289 | |
---|
290 | int i = f1-skip; while (i--) *elx++ =0.0; |
---|
291 | i = l1-f1; |
---|
292 | if (i) // in case f1 would take ely out of range |
---|
293 | { Real* ely = mrc1.data+(f1-mrc1.skip); while (i--) *elx++ = *ely++; } |
---|
294 | |
---|
295 | int f2 = mrc2.skip; int l2 = f2 + mrc2.storage; i = mrc1.length; |
---|
296 | int skipx = l1 - i; lx -= i; // addresses rel to second seg, maybe -ve |
---|
297 | if (f2 < skipx) { f2 = skipx; if (l2 < f2) l2 = f2; } |
---|
298 | if (l2 > lx) { l2 = lx; if (f2 > lx) f2 = lx; } |
---|
299 | |
---|
300 | i = f2-skipx; while (i--) *elx++ = 0.0; |
---|
301 | i = l2-f2; |
---|
302 | if (i) // in case f2 would take ely out of range |
---|
303 | { Real* ely = mrc2.data+(f2-mrc2.skip); while (i--) *elx++ = *ely++; } |
---|
304 | lx -= l2; while (lx--) *elx++ = 0.0; |
---|
305 | } |
---|
306 | |
---|
307 | void MatrixRowCol::Multiply(const MatrixRowCol& mrc1) |
---|
308 | // element by element multiply into |
---|
309 | { |
---|
310 | REPORT |
---|
311 | if (!storage) return; |
---|
312 | int f = mrc1.skip; int l = f + mrc1.storage; int lx = skip + storage; |
---|
313 | if (f < skip) { f = skip; if (l < f) l = f; } |
---|
314 | if (l > lx) { l = lx; if (f > lx) f = lx; } |
---|
315 | |
---|
316 | Real* elx = data; Real* ely = mrc1.data+(f-mrc1.skip); |
---|
317 | |
---|
318 | int l1 = f-skip; while (l1--) *elx++ = 0; |
---|
319 | l1 = l-f; while (l1--) *elx++ *= *ely++; |
---|
320 | lx -= l; while (lx--) *elx++ = 0; |
---|
321 | } |
---|
322 | |
---|
323 | void MatrixRowCol::Multiply(const MatrixRowCol& mrc1, const MatrixRowCol& mrc2) |
---|
324 | // element by element multiply |
---|
325 | { |
---|
326 | int f = skip; int l = skip + storage; |
---|
327 | int f1 = mrc1.skip; int l1 = f1 + mrc1.storage; |
---|
328 | if (f1<f) f1=f; if (l1>l) l1=l; |
---|
329 | int f2 = mrc2.skip; int l2 = f2 + mrc2.storage; |
---|
330 | if (f2<f) f2=f; if (l2>l) l2=l; |
---|
331 | Real* el = data + (f-skip); int i; |
---|
332 | if (f1<f2) f1 = f2; if (l1>l2) l1 = l2; |
---|
333 | if (l1<=f1) { REPORT i = l-f; while (i--) *el++ = 0.0; } // disjoint |
---|
334 | else |
---|
335 | { |
---|
336 | REPORT |
---|
337 | Real* el1 = mrc1.data+(f1-mrc1.skip); |
---|
338 | Real* el2 = mrc2.data+(f1-mrc2.skip); |
---|
339 | i = f1-f ; while (i--) *el++ = 0.0; |
---|
340 | i = l1-f1; while (i--) *el++ = *el1++ * *el2++; |
---|
341 | i = l-l1; while (i--) *el++ = 0.0; |
---|
342 | } |
---|
343 | } |
---|
344 | |
---|
345 | void MatrixRowCol::KP(const MatrixRowCol& mrc1, const MatrixRowCol& mrc2) |
---|
346 | // row for Kronecker product |
---|
347 | { |
---|
348 | int f = skip; int s = storage; Real* el = data; int i; |
---|
349 | |
---|
350 | i = mrc1.skip * mrc2.length; |
---|
351 | if (i > f) |
---|
352 | { |
---|
353 | i -= f; f = 0; if (i > s) { i = s; s = 0; } else s -= i; |
---|
354 | while (i--) *el++ = 0.0; |
---|
355 | if (s == 0) return; |
---|
356 | } |
---|
357 | else f -= i; |
---|
358 | |
---|
359 | i = mrc1.storage; Real* el1 = mrc1.data; |
---|
360 | int mrc2_skip = mrc2.skip; int mrc2_storage = mrc2.storage; |
---|
361 | int mrc2_length = mrc2.length; |
---|
362 | int mrc2_remain = mrc2_length - mrc2_skip - mrc2_storage; |
---|
363 | while (i--) |
---|
364 | { |
---|
365 | int j; Real* el2 = mrc2.data; Real vel1 = *el1; |
---|
366 | if (f == 0 && mrc2_length <= s) |
---|
367 | { |
---|
368 | j = mrc2_skip; s -= j; while (j--) *el++ = 0.0; |
---|
369 | j = mrc2_storage; s -= j; while (j--) *el++ = vel1 * *el2++; |
---|
370 | j = mrc2_remain; s -= j; while (j--) *el++ = 0.0; |
---|
371 | } |
---|
372 | else if (f >= mrc2_length) f -= mrc2_length; |
---|
373 | else |
---|
374 | { |
---|
375 | j = mrc2_skip; |
---|
376 | if (j > f) |
---|
377 | { |
---|
378 | j -= f; f = 0; if (j > s) { j = s; s = 0; } else s -= j; |
---|
379 | while (j--) *el++ = 0.0; |
---|
380 | } |
---|
381 | else f -= j; |
---|
382 | |
---|
383 | j = mrc2_storage; |
---|
384 | if (j > f) |
---|
385 | { |
---|
386 | j -= f; el2 += f; f = 0; if (j > s) { j = s; s = 0; } else s -= j; |
---|
387 | while (j--) *el++ = vel1 * *el2++; |
---|
388 | } |
---|
389 | else f -= j; |
---|
390 | |
---|
391 | j = mrc2_remain; |
---|
392 | if (j > f) |
---|
393 | { |
---|
394 | j -= f; f = 0; if (j > s) { j = s; s = 0; } else s -= j; |
---|
395 | while (j--) *el++ = 0.0; |
---|
396 | } |
---|
397 | else f -= j; |
---|
398 | } |
---|
399 | if (s == 0) return; |
---|
400 | ++el1; |
---|
401 | } |
---|
402 | |
---|
403 | i = (mrc1.length - mrc1.skip - mrc1.storage) * mrc2.length; |
---|
404 | if (i > f) |
---|
405 | { |
---|
406 | i -= f; if (i > s) i = s; |
---|
407 | while (i--) *el++ = 0.0; |
---|
408 | } |
---|
409 | } |
---|
410 | |
---|
411 | |
---|
412 | void MatrixRowCol::Copy(const MatrixRowCol& mrc1) |
---|
413 | { |
---|
414 | // THIS = mrc1 |
---|
415 | REPORT |
---|
416 | if (!storage) return; |
---|
417 | int f = mrc1.skip; int l = f + mrc1.storage; int lx = skip + storage; |
---|
418 | if (f < skip) { f = skip; if (l < f) l = f; } |
---|
419 | if (l > lx) { l = lx; if (f > lx) f = lx; } |
---|
420 | |
---|
421 | Real* elx = data; Real* ely = 0; |
---|
422 | |
---|
423 | if (l-f) ely = mrc1.data+(f-mrc1.skip); |
---|
424 | |
---|
425 | int l1 = f-skip; while (l1--) *elx++ = 0.0; |
---|
426 | l1 = l-f; while (l1--) *elx++ = *ely++; |
---|
427 | lx -= l; while (lx--) *elx++ = 0.0; |
---|
428 | } |
---|
429 | |
---|
430 | void MatrixRowCol::CopyCheck(const MatrixRowCol& mrc1) |
---|
431 | // Throw an exception if this would lead to a loss of data |
---|
432 | { |
---|
433 | REPORT |
---|
434 | if (!storage) return; |
---|
435 | int f = mrc1.skip; int l = f + mrc1.storage; int lx = skip + storage; |
---|
436 | if (f < skip || l > lx) Throw(ProgramException("Illegal Conversion")); |
---|
437 | |
---|
438 | Real* elx = data; Real* ely = mrc1.data+(f-mrc1.skip); |
---|
439 | |
---|
440 | int l1 = f-skip; while (l1--) *elx++ = 0.0; |
---|
441 | l1 = l-f; while (l1--) *elx++ = *ely++; |
---|
442 | lx -= l; while (lx--) *elx++ = 0.0; |
---|
443 | } |
---|
444 | |
---|
445 | void MatrixRowCol::Check(const MatrixRowCol& mrc1) |
---|
446 | // Throw an exception if +=, -=, copy etc would lead to a loss of data |
---|
447 | { |
---|
448 | REPORT |
---|
449 | int f = mrc1.skip; int l = f + mrc1.storage; int lx = skip + storage; |
---|
450 | if (f < skip || l > lx) Throw(ProgramException("Illegal Conversion")); |
---|
451 | } |
---|
452 | |
---|
453 | void MatrixRowCol::Check() |
---|
454 | // Throw an exception if +=, -= of constant would lead to a loss of data |
---|
455 | // that is: check full row is present |
---|
456 | // may not be appropriate for symmetric matrices |
---|
457 | { |
---|
458 | REPORT |
---|
459 | if (skip!=0 || storage!=length) |
---|
460 | Throw(ProgramException("Illegal Conversion")); |
---|
461 | } |
---|
462 | |
---|
463 | void MatrixRowCol::Negate(const MatrixRowCol& mrc1) |
---|
464 | { |
---|
465 | // THIS = -mrc1 |
---|
466 | REPORT |
---|
467 | if (!storage) return; |
---|
468 | int f = mrc1.skip; int l = f + mrc1.storage; int lx = skip + storage; |
---|
469 | if (f < skip) { f = skip; if (l < f) l = f; } |
---|
470 | if (l > lx) { l = lx; if (f > lx) f = lx; } |
---|
471 | |
---|
472 | Real* elx = data; Real* ely = mrc1.data+(f-mrc1.skip); |
---|
473 | |
---|
474 | int l1 = f-skip; while (l1--) *elx++ = 0.0; |
---|
475 | l1 = l-f; while (l1--) *elx++ = - *ely++; |
---|
476 | lx -= l; while (lx--) *elx++ = 0.0; |
---|
477 | } |
---|
478 | |
---|
479 | void MatrixRowCol::Multiply(const MatrixRowCol& mrc1, Real s) |
---|
480 | { |
---|
481 | // THIS = mrc1 * s |
---|
482 | REPORT |
---|
483 | if (!storage) return; |
---|
484 | int f = mrc1.skip; int l = f + mrc1.storage; int lx = skip + storage; |
---|
485 | if (f < skip) { f = skip; if (l < f) l = f; } |
---|
486 | if (l > lx) { l = lx; if (f > lx) f = lx; } |
---|
487 | |
---|
488 | Real* elx = data; Real* ely = mrc1.data+(f-mrc1.skip); |
---|
489 | |
---|
490 | int l1 = f-skip; while (l1--) *elx++ = 0.0; |
---|
491 | l1 = l-f; while (l1--) *elx++ = *ely++ * s; |
---|
492 | lx -= l; while (lx--) *elx++ = 0.0; |
---|
493 | } |
---|
494 | |
---|
495 | void DiagonalMatrix::Solver(MatrixColX& mrc, const MatrixColX& mrc1) |
---|
496 | { |
---|
497 | // mrc = mrc / mrc1 (elementwise) |
---|
498 | REPORT |
---|
499 | int f = mrc1.skip; int f0 = mrc.skip; |
---|
500 | int l = f + mrc1.storage; int lx = f0 + mrc.storage; |
---|
501 | if (f < f0) { f = f0; if (l < f) l = f; } |
---|
502 | if (l > lx) { l = lx; if (f > lx) f = lx; } |
---|
503 | |
---|
504 | Real* elx = mrc.data; Real* eld = store+f; |
---|
505 | |
---|
506 | int l1 = f-f0; while (l1--) *elx++ = 0.0; |
---|
507 | l1 = l-f; while (l1--) *elx++ /= *eld++; |
---|
508 | lx -= l; while (lx--) *elx++ = 0.0; |
---|
509 | // Solver makes sure input and output point to same memory |
---|
510 | } |
---|
511 | |
---|
512 | void IdentityMatrix::Solver(MatrixColX& mrc, const MatrixColX& mrc1) |
---|
513 | { |
---|
514 | // mrc = mrc / mrc1 (elementwise) |
---|
515 | REPORT |
---|
516 | int f = mrc1.skip; int f0 = mrc.skip; |
---|
517 | int l = f + mrc1.storage; int lx = f0 + mrc.storage; |
---|
518 | if (f < f0) { f = f0; if (l < f) l = f; } |
---|
519 | if (l > lx) { l = lx; if (f > lx) f = lx; } |
---|
520 | |
---|
521 | Real* elx = mrc.data; Real eldv = *store; |
---|
522 | |
---|
523 | int l1 = f-f0; while (l1--) *elx++ = 0.0; |
---|
524 | l1 = l-f; while (l1--) *elx++ /= eldv; |
---|
525 | lx -= l; while (lx--) *elx++ = 0.0; |
---|
526 | // Solver makes sure input and output point to same memory |
---|
527 | } |
---|
528 | |
---|
529 | void MatrixRowCol::Copy(const Real*& r) |
---|
530 | { |
---|
531 | // THIS = *r |
---|
532 | REPORT |
---|
533 | Real* elx = data; const Real* ely = r+skip; r += length; |
---|
534 | int l = storage; while (l--) *elx++ = *ely++; |
---|
535 | } |
---|
536 | |
---|
537 | void MatrixRowCol::Copy(Real r) |
---|
538 | { |
---|
539 | // THIS = r |
---|
540 | REPORT Real* elx = data; int l = storage; while (l--) *elx++ = r; |
---|
541 | } |
---|
542 | |
---|
543 | void MatrixRowCol::Zero() |
---|
544 | { |
---|
545 | // THIS = 0 |
---|
546 | REPORT Real* elx = data; int l = storage; while (l--) *elx++ = 0; |
---|
547 | } |
---|
548 | |
---|
549 | void MatrixRowCol::Multiply(Real r) |
---|
550 | { |
---|
551 | // THIS *= r |
---|
552 | REPORT Real* elx = data; int l = storage; while (l--) *elx++ *= r; |
---|
553 | } |
---|
554 | |
---|
555 | void MatrixRowCol::Add(Real r) |
---|
556 | { |
---|
557 | // THIS += r |
---|
558 | REPORT |
---|
559 | Real* elx = data; int l = storage; while (l--) *elx++ += r; |
---|
560 | } |
---|
561 | |
---|
562 | Real MatrixRowCol::SumAbsoluteValue() |
---|
563 | { |
---|
564 | REPORT |
---|
565 | Real sum = 0.0; Real* elx = data; int l = storage; |
---|
566 | while (l--) sum += fabs(*elx++); |
---|
567 | return sum; |
---|
568 | } |
---|
569 | |
---|
570 | // max absolute value of r and elements of row/col |
---|
571 | // we use <= or >= in all of these so we are sure of getting |
---|
572 | // r reset at least once. |
---|
573 | Real MatrixRowCol::MaximumAbsoluteValue1(Real r, int& i) |
---|
574 | { |
---|
575 | REPORT |
---|
576 | Real* elx = data; int l = storage; int li = -1; |
---|
577 | while (l--) { Real f = fabs(*elx++); if (r <= f) { r = f; li = l; } } |
---|
578 | i = (li >= 0) ? storage - li + skip : 0; |
---|
579 | return r; |
---|
580 | } |
---|
581 | |
---|
582 | // min absolute value of r and elements of row/col |
---|
583 | Real MatrixRowCol::MinimumAbsoluteValue1(Real r, int& i) |
---|
584 | { |
---|
585 | REPORT |
---|
586 | Real* elx = data; int l = storage; int li = -1; |
---|
587 | while (l--) { Real f = fabs(*elx++); if (r >= f) { r = f; li = l; } } |
---|
588 | i = (li >= 0) ? storage - li + skip : 0; |
---|
589 | return r; |
---|
590 | } |
---|
591 | |
---|
592 | // max value of r and elements of row/col |
---|
593 | Real MatrixRowCol::Maximum1(Real r, int& i) |
---|
594 | { |
---|
595 | REPORT |
---|
596 | Real* elx = data; int l = storage; int li = -1; |
---|
597 | while (l--) { Real f = *elx++; if (r <= f) { r = f; li = l; } } |
---|
598 | i = (li >= 0) ? storage - li + skip : 0; |
---|
599 | return r; |
---|
600 | } |
---|
601 | |
---|
602 | // min value of r and elements of row/col |
---|
603 | Real MatrixRowCol::Minimum1(Real r, int& i) |
---|
604 | { |
---|
605 | REPORT |
---|
606 | Real* elx = data; int l = storage; int li = -1; |
---|
607 | while (l--) { Real f = *elx++; if (r >= f) { r = f; li = l; } } |
---|
608 | i = (li >= 0) ? storage - li + skip : 0; |
---|
609 | return r; |
---|
610 | } |
---|
611 | |
---|
612 | Real MatrixRowCol::Sum() |
---|
613 | { |
---|
614 | REPORT |
---|
615 | Real sum = 0.0; Real* elx = data; int l = storage; |
---|
616 | while (l--) sum += *elx++; |
---|
617 | return sum; |
---|
618 | } |
---|
619 | |
---|
620 | void MatrixRowCol::SubRowCol(MatrixRowCol& mrc, int skip1, int l1) const |
---|
621 | { |
---|
622 | mrc.length = l1; int d = skip - skip1; |
---|
623 | if (d<0) { mrc.skip = 0; mrc.data = data - d; } |
---|
624 | else { mrc.skip = d; mrc.data = data; } |
---|
625 | d = skip + storage - skip1; |
---|
626 | d = ((l1 < d) ? l1 : d) - mrc.skip; mrc.storage = (d < 0) ? 0 : d; |
---|
627 | mrc.cw = 0; |
---|
628 | } |
---|
629 | |
---|
630 | #ifdef use_namespace |
---|
631 | } |
---|
632 | #endif |
---|
633 | |
---|