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