home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / misc / volume34 / newmat06 / part04 < prev    next >
Encoding:
Text File  |  1992-12-06  |  53.6 KB  |  1,839 lines

  1. Newsgroups: comp.sources.misc
  2. From: robertd@kauri.vuw.ac.nz (Robert Davies)
  3. Subject:  v34i010:  newmat06 - A matrix package in C++, Part04/07
  4. Message-ID: <1992Dec6.045621.4360@sparky.imd.sterling.com>
  5. X-Md4-Signature: d17a4f699df34ed06901a26911345406
  6. Date: Sun, 6 Dec 1992 04:56:21 GMT
  7. Approved: kent@sparky.imd.sterling.com
  8.  
  9. Submitted-by: robertd@kauri.vuw.ac.nz (Robert Davies)
  10. Posting-number: Volume 34, Issue 10
  11. Archive-name: newmat06/part04
  12. Environment: C++
  13. Supersedes: newmat04: Volume 26, Issue 87-91
  14.  
  15. #! /bin/sh
  16. # This is a shell archive.  Remove anything before this line, then unpack
  17. # it by saving it into a file and typing "sh file".  To overwrite existing
  18. # files, type "sh file -c".  You can also feed this as standard input via
  19. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  20. # will see the following message at the end:
  21. #        "End of archive 4 (of 7)."
  22. # Contents:  newmat1.cxx newmat2.cxx newmat3.cxx newmat4.cxx
  23. #   newmatrm.cxx
  24. # Wrapped by robert@kea on Thu Dec  3 22:37:09 1992
  25. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  26. if test -f 'newmat1.cxx' -a "${1}" != "-c" ; then 
  27.   echo shar: Will not clobber existing file \"'newmat1.cxx'\"
  28. else
  29. echo shar: Extracting \"'newmat1.cxx'\" \(4998 characters\)
  30. sed "s/^X//" >'newmat1.cxx' <<'END_OF_FILE'
  31. X//$$ newmat1.cxx   Matrix type functions
  32. X
  33. X// Copyright (C) 1991,2: R B Davies
  34. X
  35. X//#define WANT_STREAM                     // to include stream package
  36. X
  37. X#include "include.h"
  38. X
  39. X#include "newmat.h"
  40. X
  41. X
  42. X
  43. X/************************* MatrixType functions *****************************/
  44. X
  45. X
  46. Xint MatrixType::BestFit() const
  47. X// given attributes find a matrix type that best fits them
  48. X{
  49. X
  50. X   if (!(attribute & Valid)) return USX;
  51. X
  52. X   if (attribute & (LUDeco + OneRow + OneCol))
  53. X   {
  54. X      if (attribute & OneCol) return CVX;
  55. X      if (attribute & OneRow) return RVX;
  56. X      if (attribute & LUDeco) return (attribute & Band) ? BCX : CtX;
  57. X   }
  58. X
  59. X   if (attribute & Band)
  60. X   {
  61. X      if (attribute & Lower)
  62. X     return (attribute & (Symmetric + Upper)) ? DgX : LBX;
  63. X      if (attribute & Upper)
  64. X     return (attribute & (Symmetric + Lower)) ? DgX : UBX;
  65. X      if (attribute & Symmetric) return SBX;
  66. X      return BMX;
  67. X   }
  68. X
  69. X   if (attribute & (Lower + Upper + Symmetric))
  70. X   {
  71. X      if (attribute & Lower)
  72. X     return (attribute & (Symmetric + Upper)) ? DgX : LTX;
  73. X      if (attribute & Upper)
  74. X     return (attribute & (Symmetric + Lower)) ? DgX : UTX;
  75. X      return SmX;
  76. X   }
  77. X
  78. X   return RtX;   
  79. X
  80. X}
  81. X
  82. XBoolean MatrixType::operator>=(const MatrixType& t) const
  83. X{
  84. X   return ( attribute & (t.attribute | (OneRow + OneCol)) ) == attribute;
  85. X}
  86. X
  87. XMatrixType MatrixType::operator+(const MatrixType& mt) const
  88. X{ return attribute & mt.attribute; }
  89. X
  90. XMatrixType MatrixType::operator*(const MatrixType& mt) const
  91. X{
  92. X   if ( ((attribute & (Upper+Lower)) == (Upper+Lower))
  93. X      && ((mt.attribute & (Upper+Lower)) == (Upper+Lower)) )
  94. X         return Dg;
  95. X   else return (attribute & mt.attribute & ~Symmetric)
  96. X      | (attribute & OneRow) | (mt.attribute & OneCol);
  97. X}
  98. X
  99. XMatrixType MatrixType::i() const
  100. X{ return attribute & ~(Band+LUDeco) ; }
  101. X
  102. XMatrixType MatrixType::t() const
  103. X{
  104. X   int a = attribute & ~(Upper+Lower+OneRow+OneCol);
  105. X   if (attribute & Upper) a |= Lower;
  106. X   if (attribute & Lower) a |= Upper;
  107. X   if (attribute & OneRow) a |= OneCol;
  108. X   if (attribute & OneCol) a |= OneRow;
  109. X   return a;
  110. X}
  111. X
  112. XMatrixType MatrixType::AddEqualEl() const
  113. X{ return attribute & (Valid + Symmetric + OneRow + OneCol); }
  114. X
  115. XMatrixType MatrixType::MultRHS() const
  116. X{
  117. X   if (attribute & (Upper + Lower)) return attribute;
  118. X   else return attribute & ~Symmetric;
  119. X}
  120. X
  121. XMatrixType MatrixType::sub() const
  122. X{ return attribute & (Valid + OneRow + OneCol); }
  123. X
  124. X
  125. XMatrixType MatrixType::ssub() const
  126. X{
  127. X   return *this;                  // won't work for selection matrix
  128. X}
  129. X
  130. XMatrixType::operator char*() const
  131. X{
  132. X   switch (BestFit())
  133. X   {
  134. X   case USX:  return "UnSp ";
  135. X   case UTX:  return "UT   ";
  136. X   case LTX:  return "LT   ";
  137. X   case RtX:  return "Rect ";
  138. X   case SmX:  return "Sym  ";
  139. X   case DgX:  return "Diag ";
  140. X   case RVX:  return "RowV ";
  141. X   case CVX:  return "ColV ";
  142. X   case BMX:  return "Band ";
  143. X   case UBX:  return "UpBnd";
  144. X   case LBX:  return "LwBnd";
  145. X   case SBX:  return "SmBnd";
  146. X   case CtX:  return "Crout";
  147. X   case BCX:  return "BndLU";
  148. X   }
  149. X   return "?????";
  150. X}
  151. X
  152. XGeneralMatrix* MatrixType::New() const
  153. X{
  154. X   GeneralMatrix* gm;
  155. X   switch (BestFit())
  156. X   {
  157. X   case USX:  Throw(CannotBuildException("UnSp"));
  158. X   case UTX:  gm = new UpperTriangularMatrix; break;
  159. X   case LTX:  gm = new LowerTriangularMatrix; break;
  160. X   case RtX:  gm = new Matrix; break;
  161. X   case SmX:  gm = new SymmetricMatrix; break;
  162. X   case DgX:  gm = new DiagonalMatrix; break;
  163. X   case RVX:  gm = new RowVector; break;
  164. X   case CVX:  gm = new ColumnVector; break;
  165. X   case BMX:  gm = new BandMatrix; break;
  166. X   case UBX:  gm = new UpperBandMatrix; break;
  167. X   case LBX:  gm = new LowerBandMatrix; break;
  168. X   case SBX:  gm = new SymmetricBandMatrix; break;
  169. X   case CtX:  Throw(CannotBuildException("Crout"));
  170. X   case BCX:  Throw(CannotBuildException("Band LU"));
  171. X   }
  172. X   MatrixErrorNoSpace(gm);
  173. X   gm->Protect(); return gm;
  174. X}
  175. X
  176. XGeneralMatrix* MatrixType::New(int nr, int nc, BaseMatrix* bm) const
  177. X{
  178. X   Tracer tr("New");
  179. X   GeneralMatrix* gm;
  180. X   switch (BestFit())
  181. X   {
  182. X   case USX:  Throw(CannotBuildException("UnSp"));
  183. X   case UTX:  gm = new UpperTriangularMatrix(nr); break;
  184. X   case LTX:  gm = new LowerTriangularMatrix(nr); break;
  185. X   case RtX:  gm = new Matrix(nr, nc); break;
  186. X   case SmX:  gm = new SymmetricMatrix(nr); break;
  187. X   case DgX:  gm = new DiagonalMatrix(nr); break;
  188. X   case RVX:  if (nr!=1) Throw(VectorException());
  189. X           gm = new RowVector(nc); break;
  190. X   case CVX:  if (nc!=1) Throw(VectorException());
  191. X           gm = new ColumnVector(nr); break;
  192. X   case BMX:  {
  193. X                 MatrixBandWidth bw = bm->BandWidth();
  194. X                 gm = new BandMatrix(nr,bw.lower,bw.upper); break;
  195. X              }
  196. X   case UBX:  gm = new UpperBandMatrix(nr,bm->BandWidth().upper); break;
  197. X   case LBX:  gm = new LowerBandMatrix(nr,bm->BandWidth().lower); break;
  198. X   case SBX:  gm = new SymmetricBandMatrix(nr,bm->BandWidth().lower); break;
  199. X   case CtX:  Throw(CannotBuildException("Crout"));
  200. X   case BCX:  Throw(CannotBuildException("Band LU"));
  201. X   }
  202. X   MatrixErrorNoSpace(gm);
  203. X   gm->Protect(); return gm;
  204. X}
  205. X
  206. END_OF_FILE
  207. if test 4998 -ne `wc -c <'newmat1.cxx'`; then
  208.     echo shar: \"'newmat1.cxx'\" unpacked with wrong size!
  209. fi
  210. # end of 'newmat1.cxx'
  211. fi
  212. if test -f 'newmat2.cxx' -a "${1}" != "-c" ; then 
  213.   echo shar: Will not clobber existing file \"'newmat2.cxx'\"
  214. else
  215. echo shar: Extracting \"'newmat2.cxx'\" \(10467 characters\)
  216. sed "s/^X//" >'newmat2.cxx' <<'END_OF_FILE'
  217. X//$$ newmat2.cxx      Matrix row and column operations
  218. X
  219. X// Copyright (C) 1991,2: R B Davies
  220. X
  221. X#define WANT_MATH
  222. X
  223. X#include "include.h"
  224. X
  225. X#include "newmat.h"
  226. X#include "newmatrc.h"
  227. X
  228. X//#define REPORT { static ExeCounter ExeCount(__LINE__,2); ExeCount++; }
  229. X
  230. X#define REPORT {}
  231. X
  232. X//#define MONITOR(what,storage,store) \
  233. X//   { cout << what << " " << storage << " at " << (long)store << "\n"; }
  234. X
  235. X#define MONITOR(what,store,storage) {}
  236. X
  237. X/************************** Matrix Row/Col functions ************************/
  238. X
  239. Xvoid MatrixRowCol::Add(const MatrixRowCol& mrc)
  240. X{
  241. X   REPORT
  242. X   int f = mrc.skip; int l = f + mrc.storage; int lx = skip + storage;
  243. X   if (f < skip) f = skip; if (l > lx) l = lx; l -= f;
  244. X   if (l<=0) return;
  245. X   Real* elx=store+f; Real* el=mrc.store+f;
  246. X   while (l--) *elx++ += *el++;
  247. X}
  248. X
  249. Xvoid MatrixRowCol::AddScaled(const MatrixRowCol& mrc, Real x)
  250. X{
  251. X   REPORT
  252. X   int f = mrc.skip; int l = f + mrc.storage; int lx = skip + storage;
  253. X   if (f < skip) f = skip; if (l > lx) l = lx; l -= f;
  254. X   if (l<=0) return;
  255. X   Real* elx=store+f; Real* el=mrc.store+f;
  256. X   while (l--) *elx++ += *el++ * x;
  257. X}
  258. X
  259. Xvoid MatrixRowCol::Sub(const MatrixRowCol& mrc)
  260. X{
  261. X   REPORT
  262. X   int f = mrc.skip; int l = f + mrc.storage; int lx = skip + storage;
  263. X   if (f < skip) f = skip; if (l > lx) l = lx; l -= f;
  264. X   if (l<=0) return;
  265. X   Real* elx=store+f; Real* el=mrc.store+f;
  266. X   while (l--) *elx++ -= *el++;
  267. X}
  268. X
  269. Xvoid MatrixRowCol::Inject(const MatrixRowCol& mrc)
  270. X// copy stored elements only
  271. X{
  272. X   REPORT
  273. X   int f = mrc.skip; int l = f + mrc.storage; int lx = skip + storage;
  274. X   if (f < skip) f = skip; if (l > lx) l = lx; l -= f;
  275. X   if (l<=0) return;
  276. X   Real* elx = store+f; Real* ely = mrc.store+f;
  277. X   while (l--) *elx++ = *ely++;
  278. X}
  279. X
  280. XReal DotProd(const MatrixRowCol& mrc1, const MatrixRowCol& mrc2)
  281. X{
  282. X   REPORT                                         // not accessed
  283. X   int f = mrc1.skip; int f2 = mrc2.skip;
  284. X   int l = f + mrc1.storage; int l2 = f2 + mrc2.storage;
  285. X   if (f < f2) f = f2; if (l > l2) l = l2; l -= f;
  286. X   if (l<=0) return 0.0;
  287. X   
  288. X   Real* el1=mrc1.store+f; Real* el2=mrc2.store+f;
  289. X   Real sum = 0.0;
  290. X   while (l--) sum += *el1++ * *el2++;
  291. X   return sum;
  292. X}
  293. X
  294. Xvoid MatrixRowCol::Add(const MatrixRowCol& mrc1, const MatrixRowCol& mrc2)
  295. X{
  296. X   int f = skip; int l = skip + storage;
  297. X   int f1 = mrc1.skip; int l1 = f1 + mrc1.storage;
  298. X   if (f1<f) f1=f; if (l1>l) l1=l;
  299. X   int f2 = mrc2.skip; int l2 = f2 + mrc2.storage;
  300. X   if (f2<f) f2=f; if (l2>l) l2=l;
  301. X   Real* el = store + f;
  302. X   Real* el1 = mrc1.store+f1; Real* el2 = mrc2.store+f2;
  303. X   if (f1<f2)
  304. X   {
  305. X      register int i = f1-f; while (i--) *el++ = 0.0;
  306. X      if (l1<=f2)                              // disjoint
  307. X      {
  308. X     REPORT                                // not accessed
  309. X         i = l1-f1;     while (i--) *el++ = *el1++;
  310. X         i = f2-l1;     while (i--) *el++ = 0.0;
  311. X         i = l2-f2;     while (i--) *el++ = *el2++;
  312. X         i = l-l2;      while (i--) *el++ = 0.0;
  313. X      }
  314. X      else
  315. X      {
  316. X         i = f2-f1;    while (i--) *el++ = *el1++;
  317. X         if (l1<=l2)
  318. X         {
  319. X            REPORT
  320. X            i = l1-f2; while (i--) *el++ = *el1++ + *el2++;
  321. X            i = l2-l1; while (i--) *el++ = *el2++;
  322. X            i = l-l2;  while (i--) *el++ = 0.0;
  323. X         }
  324. X         else
  325. X         {
  326. X            REPORT
  327. X            i = l2-f2; while (i--) *el++ = *el1++ + *el2++;
  328. X            i = l1-l2; while (i--) *el++ = *el1++;
  329. X            i = l-l1;  while (i--) *el++ = 0.0;
  330. X         }
  331. X      }
  332. X   }
  333. X   else
  334. X   {
  335. X      register int i = f2-f; while (i--) *el++ = 0.0;
  336. X      if (l2<=f1)                              // disjoint
  337. X      {
  338. X     REPORT                                // not accessed
  339. X         i = l2-f2;     while (i--) *el++ = *el2++;
  340. X         i = f1-l2;     while (i--) *el++ = 0.0;
  341. X     i = l1-f1;     while (i--) *el++ = *el1++;
  342. X     i = l-l1;      while (i--) *el++ = 0.0;
  343. X      }
  344. X      else
  345. X      {
  346. X         i = f1-f2;    while (i--) *el++ = *el2++;
  347. X         if (l2<=l1)
  348. X         {
  349. X        REPORT
  350. X            i = l2-f1; while (i--) *el++ = *el1++ + *el2++;
  351. X            i = l1-l2; while (i--) *el++ = *el1++;
  352. X            i = l-l1;  while (i--) *el++ = 0.0;
  353. X         }
  354. X         else
  355. X         {
  356. X        REPORT
  357. X            i = l1-f1; while (i--) *el++ = *el1++ + *el2++;
  358. X            i = l2-l1; while (i--) *el++ = *el2++;
  359. X            i = l-l2;  while (i--) *el++ = 0.0;
  360. X         }
  361. X      }
  362. X   }
  363. X}
  364. X
  365. Xvoid MatrixRowCol::Sub(const MatrixRowCol& mrc1, const MatrixRowCol& mrc2)
  366. X{
  367. X   int f = skip; int l = skip + storage;
  368. X   int f1 = mrc1.skip; int l1 = f1 + mrc1.storage;
  369. X   if (f1<f) f1=f; if (l1>l) l1=l;
  370. X   int f2 = mrc2.skip; int l2 = f2 + mrc2.storage;
  371. X   if (f2<f) f2=f; if (l2>l) l2=l;
  372. X   Real* el = store + f;
  373. X   Real* el1 = mrc1.store+f1; Real* el2 = mrc2.store+f2;
  374. X   if (f1<f2)
  375. X   {
  376. X      register int i = f1-f; while (i--) *el++ = 0.0;
  377. X      if (l1<=f2)                              // disjoint
  378. X      {
  379. X     REPORT                                // not accessed
  380. X         i = l1-f1;     while (i--) *el++ = *el1++;
  381. X         i = f2-l1;     while (i--) *el++ = 0.0;
  382. X         i = l2-f2;     while (i--) *el++ = - *el2++;
  383. X         i = l-l2;      while (i--) *el++ = 0.0;
  384. X      }
  385. X      else
  386. X      {
  387. X         i = f2-f1;    while (i--) *el++ = *el1++;
  388. X         if (l1<=l2)
  389. X         {
  390. X        REPORT
  391. X            i = l1-f2; while (i--) *el++ = *el1++ - *el2++;
  392. X            i = l2-l1; while (i--) *el++ = - *el2++;
  393. X            i = l-l2;  while (i--) *el++ = 0.0;
  394. X         }
  395. X         else
  396. X         {
  397. X        REPORT
  398. X            i = l2-f2; while (i--) *el++ = *el1++ - *el2++;
  399. X            i = l1-l2; while (i--) *el++ = *el1++;
  400. X            i = l-l1;  while (i--) *el++ = 0.0;
  401. X         }
  402. X      }
  403. X   }
  404. X   else
  405. X   {
  406. X      register int i = f2-f; while (i--) *el++ = 0.0;
  407. X      if (l2<=f1)                              // disjoint
  408. X      {
  409. X     REPORT                                // not accessed
  410. X         i = l2-f2;     while (i--) *el++ = - *el2++;
  411. X         i = f1-l2;     while (i--) *el++ = 0.0;
  412. X         i = l1-f1;     while (i--) *el++ = *el1++;
  413. X         i = l-l1;      while (i--) *el++ = 0.0;
  414. X      }
  415. X      else
  416. X      {
  417. X         i = f1-f2;    while (i--) *el++ = - *el2++;
  418. X         if (l2<=l1)
  419. X         {
  420. X        REPORT
  421. X            i = l2-f1; while (i--) *el++ = *el1++ - *el2++;
  422. X            i = l1-l2; while (i--) *el++ = *el1++;
  423. X            i = l-l1;  while (i--) *el++ = 0.0;
  424. X         }
  425. X         else
  426. X         {
  427. X            REPORT
  428. X            i = l1-f1; while (i--) *el++ = *el1++ - *el2++;
  429. X            i = l2-l1; while (i--) *el++ = - *el2++;
  430. X            i = l-l2;  while (i--) *el++ = 0.0;
  431. X         }
  432. X      }
  433. X   }
  434. X}
  435. X
  436. X
  437. Xvoid MatrixRowCol::Add(const MatrixRowCol& mrc1, Real x)
  438. X{
  439. X   REPORT
  440. X   int f = mrc1.skip; int l = f + mrc1.storage; int lx = skip + storage;
  441. X   if (f < skip) { f = skip; if (l < f) l = f; }
  442. X   if (l > lx) { l = lx; if (f > lx) f = lx; }
  443. X
  444. X   Real* elx = store+skip; Real* ely = mrc1.store+f;
  445. X
  446. X   int l1 = f-skip;  while (l1--) *elx++ = x;
  447. X       l1 = l-f;     while (l1--) *elx++ = *ely++ + x;
  448. X       lx -= l;      while (lx--) *elx++ = x;
  449. X}
  450. X
  451. Xvoid MatrixRowCol::RevSub(const MatrixRowCol& mrc1)
  452. X{
  453. X   REPORT
  454. X   int f = mrc1.skip; int l = f + mrc1.storage; int lx = skip + storage;
  455. X   if (f < skip) { f = skip; if (l < f) l = f; }
  456. X   if (l > lx) { l = lx; if (f > lx) f = lx; }
  457. X
  458. X   Real* elx = store+skip; Real* ely = mrc1.store+f;
  459. X
  460. X   int l1 = f-skip;  while (l1--) { *elx = - *elx; elx++; }
  461. X       l1 = l-f;     while (l1--) { *elx = *ely++ - *elx; elx++; }
  462. X       lx -= l;      while (lx--) { *elx = - *elx; elx++; }
  463. X}
  464. X
  465. Xvoid MatrixRowCol::Copy(const MatrixRowCol& mrc1)
  466. X{
  467. X   REPORT
  468. X   int f = mrc1.skip; int l = f + mrc1.storage; int lx = skip + storage;
  469. X   if (f < skip) { f = skip; if (l < f) l = f; }
  470. X   if (l > lx) { l = lx; if (f > lx) f = lx; }
  471. X
  472. X   Real* elx = store+skip; Real* ely = mrc1.store+f;
  473. X
  474. X   int l1 = f-skip;  while (l1--) *elx++ = 0.0;
  475. X       l1 = l-f;     while (l1--) *elx++ = *ely++;
  476. X       lx -= l;      while (lx--) *elx++ = 0.0;
  477. X}
  478. X
  479. Xvoid MatrixRowCol::Negate(const MatrixRowCol& mrc1)
  480. X{
  481. X   REPORT
  482. X   int f = mrc1.skip; int l = f + mrc1.storage; int lx = skip + storage;
  483. X   if (f < skip) { f = skip; if (l < f) l = f; }
  484. X   if (l > lx) { l = lx; if (f > lx) f = lx; }
  485. X
  486. X   Real* elx = store+skip; Real* ely = mrc1.store+f;
  487. X
  488. X   int l1 = f-skip;  while (l1--) *elx++ = 0.0;
  489. X       l1 = l-f;     while (l1--) *elx++ = - *ely++;
  490. X       lx -= l;      while (lx--) *elx++ = 0.0;
  491. X}
  492. X
  493. Xvoid MatrixRowCol::Multiply(const MatrixRowCol& mrc1, Real s)
  494. X{
  495. X   REPORT
  496. X   int f = mrc1.skip; int l = f + mrc1.storage; int lx = skip + storage;
  497. X   if (f < skip) { f = skip; if (l < f) l = f; }
  498. X   if (l > lx) { l = lx; if (f > lx) f = lx; }
  499. X
  500. X   Real* elx = store+skip; Real* ely = mrc1.store+f;
  501. X
  502. X   int l1 = f-skip;  while (l1--) *elx++ = 0.0;
  503. X       l1 = l-f;     while (l1--) *elx++ = *ely++ * s;
  504. X       lx -= l;      while (lx--) *elx++ = 0.0;
  505. X}
  506. X
  507. Xvoid DiagonalMatrix::Solver(MatrixRowCol& mrc, const MatrixRowCol& mrc1)
  508. X{
  509. X   REPORT
  510. X   int f = mrc1.skip; int f0 = mrc.skip;
  511. X   int l = f + mrc1.storage; int lx = f0 + mrc.storage;
  512. X   if (f < f0) { f = f0; if (l < f) l = f; }
  513. X   if (l > lx) { l = lx; if (f > lx) f = lx; }
  514. X
  515. X   Real* elx = mrc.store+f0; Real* eld = store+f;
  516. X
  517. X   int l1 = f-f0;    while (l1--) *elx++ = 0.0;
  518. X       l1 = l-f;     while (l1--) *elx++ /= *eld++;
  519. X       lx -= l;      while (lx--) *elx++ = 0.0;
  520. X   // Solver makes sure input and output point to same memory
  521. X}
  522. X
  523. Xvoid MatrixRowCol::Copy(const Real*& r)
  524. X{
  525. X   REPORT
  526. X   Real* elx = store+skip; const Real* ely = r+skip; r += length;
  527. X   int l = storage; while (l--) *elx++ = *ely++;
  528. X}
  529. X
  530. Xvoid MatrixRowCol::Copy(Real r)
  531. X{
  532. X   REPORT
  533. X   Real* elx = store+skip; int l = storage; while (l--) *elx++ = r;
  534. X}
  535. X
  536. XReal MatrixRowCol::SumAbsoluteValue()
  537. X{
  538. X   REPORT
  539. X   Real sum = 0.0; Real* elx = store+skip; int l = storage;
  540. X   while (l--) sum += fabs(*elx++);
  541. X   return sum;
  542. X}
  543. X
  544. Xvoid MatrixRowCol::SubRowCol(MatrixRowCol& mrc, int skip1, int l1) const
  545. X{
  546. X   mrc.length = l1;  mrc.store = store + skip1;  int d = skip - skip1;
  547. X   mrc.skip = (d < 0) ? 0 : d;  d = skip + storage - skip1;
  548. X   d = ((l1 < d) ? l1 : d) - mrc.skip;  mrc.storage = (d < 0) ? 0 : d;
  549. X   mrc.cw = 0;
  550. X}
  551. X
  552. XMatrixRowCol::~MatrixRowCol()
  553. X{
  554. X   if (+(cw*IsACopy) && !(cw*StoreHere))
  555. X   {
  556. X      Real* f = store+skip;
  557. X      MONITOR_REAL_DELETE("Free    (RowCol)",storage,f) 
  558. X#ifdef Version21
  559. X      delete [] f;
  560. X#else
  561. X      delete [storage] f;
  562. X#endif
  563. X   }
  564. X}
  565. X
  566. XMatrixRow::~MatrixRow() { if (+(cw*StoreOnExit)) gm->RestoreRow(*this); }
  567. X
  568. XMatrixCol::~MatrixCol() { if (+(cw*StoreOnExit)) gm->RestoreCol(*this); }
  569. X
  570. END_OF_FILE
  571. if test 10467 -ne `wc -c <'newmat2.cxx'`; then
  572.     echo shar: \"'newmat2.cxx'\" unpacked with wrong size!
  573. fi
  574. # end of 'newmat2.cxx'
  575. fi
  576. if test -f 'newmat3.cxx' -a "${1}" != "-c" ; then 
  577.   echo shar: Will not clobber existing file \"'newmat3.cxx'\"
  578. else
  579. echo shar: Extracting \"'newmat3.cxx'\" \(14201 characters\)
  580. sed "s/^X//" >'newmat3.cxx' <<'END_OF_FILE'
  581. X//$$ newmat3.cxx        Matrix get and restore rows and columns
  582. X
  583. X// Copyright (C) 1991,2: R B Davies
  584. X
  585. X
  586. X#include "include.h"
  587. X
  588. X#include "newmat.h"
  589. X#include "newmatrc.h"
  590. X
  591. X//#define REPORT { static ExeCounter ExeCount(__LINE__,3); ExeCount++; }
  592. X
  593. X#define REPORT {}
  594. X
  595. X//#define MONITOR(what,storage,store) \
  596. X//   { cout << what << " " << storage << " at " << (long)store << "\n"; }
  597. X
  598. X#define MONITOR(what,store,storage) {}
  599. X
  600. X
  601. X// Control bits codes for GetRow, GetCol, RestoreRow, RestoreCol
  602. X//
  603. X// LoadOnEntry:
  604. X//    Load data into MatrixRow or Col dummy array under GetRow or GetCol
  605. X// StoreOnExit:
  606. X//    Restore data to original matrix under RestoreRow or RestoreCol
  607. X// IsACopy:
  608. X//    Set by GetRow/Col: MatrixRow or Col array is a copy
  609. X// DirectPart:
  610. X//    Load or restore only part directly stored; must be set with StoreOnExit
  611. X//    Still have decide  how to handle this with symmetric
  612. X// StoreHere:
  613. X//    used in columns only - store data at supplied storage address, adjusted
  614. X//    for skip; used for GetCol, NextCol & RestoreCol. No need to fill out
  615. X//    zeros.
  616. X
  617. X
  618. X// These will work as a default
  619. X// but need to override NextRow for efficiency
  620. X
  621. X// Assume pointer arithmetic works for pointers out of range - not strict C++.
  622. X
  623. X
  624. Xvoid GeneralMatrix::NextRow(MatrixRowCol& mrc)
  625. X{
  626. X   REPORT
  627. X   if (+(mrc.cw*StoreOnExit)) { REPORT this->RestoreRow(mrc); }
  628. X   if (+(mrc.cw*IsACopy))
  629. X   {
  630. X      REPORT
  631. X      Real* s = mrc.store + mrc.skip;
  632. X      MONITOR_REAL_DELETE("Free   (NextRow)",mrc.storage,s)
  633. X#ifdef Version21
  634. X      delete [] s;
  635. X#else
  636. X      delete [mrc.storage] s;
  637. X#endif
  638. X   }
  639. X   mrc.rowcol++;
  640. X   if (mrc.rowcol<nrows) { REPORT this->GetRow(mrc); }
  641. X   else { REPORT mrc.cw -= (StoreOnExit+IsACopy); }
  642. X}
  643. X
  644. Xvoid GeneralMatrix::NextCol(MatrixRowCol& mrc)
  645. X{
  646. X   REPORT                                                // 423
  647. X   if (+(mrc.cw*StoreOnExit)) { REPORT this->RestoreCol(mrc); }
  648. X   if ( +(mrc.cw*IsACopy) && (!(mrc.cw*StoreHere)) )
  649. X   {
  650. X      REPORT                                             // not accessed
  651. X      Real* s = mrc.store + mrc.skip;
  652. X      MONITOR_REAL_DELETE("Free   (NextCol)",mrc.storage,s) 
  653. X#ifdef Version21
  654. X      delete [] s;
  655. X#else
  656. X      delete [mrc.storage] s;
  657. X#endif
  658. X   }
  659. X   mrc.rowcol++;
  660. X   if (mrc.rowcol<ncols) { REPORT this->GetCol(mrc); }
  661. X   else { REPORT mrc.cw -= (StoreOnExit+IsACopy); }
  662. X}
  663. X
  664. X
  665. X// routines for matrix
  666. X
  667. Xvoid Matrix::GetRow(MatrixRowCol& mrc)
  668. X{
  669. X   REPORT
  670. X   mrc.skip=0; mrc.cw-=IsACopy; mrc.storage=ncols;
  671. X   mrc.store=store+mrc.rowcol*ncols;
  672. X}
  673. X
  674. X
  675. Xvoid Matrix::GetCol(MatrixRowCol& mrc)
  676. X{
  677. X   REPORT
  678. X   mrc.skip=0; mrc.storage=nrows;
  679. X   if ( ncols==1 && !(mrc.cw*StoreHere) )
  680. X      { REPORT mrc.cw-=IsACopy; mrc.store=store; }           // not accessed
  681. X   else
  682. X   {
  683. X      mrc.cw+=IsACopy; Real* ColCopy;
  684. X      if ( !(mrc.cw*StoreHere) )
  685. X      {
  686. X         REPORT
  687. X         ColCopy = new Real [nrows]; MatrixErrorNoSpace(ColCopy);
  688. X         MONITOR_REAL_NEW("Make (MatGetCol)",nrows,ColCopy)
  689. X         mrc.store = ColCopy;
  690. X      }
  691. X      else { REPORT ColCopy = mrc.store; }
  692. X      if (+(mrc.cw*LoadOnEntry))
  693. X      {
  694. X         REPORT
  695. X         Real* Mstore = store+mrc.rowcol; int i=nrows;
  696. X         while (i--) { *ColCopy++ = *Mstore; Mstore+=ncols; }
  697. X      }
  698. X   }
  699. X}
  700. X
  701. Xvoid Matrix::RestoreCol(MatrixRowCol& mrc)
  702. X{
  703. X//  if (mrc.cw*StoreOnExit)
  704. X   REPORT                                   // 429
  705. X   if (+(mrc.cw*IsACopy))
  706. X   {
  707. X      REPORT                                // 426
  708. X      Real* Mstore = store+mrc.rowcol; int i=nrows; Real* Cstore = mrc.store;
  709. X      while (i--) { *Mstore = *Cstore++; Mstore+=ncols; }
  710. X   }
  711. X}
  712. X
  713. Xvoid Matrix::NextRow(MatrixRowCol& mrc) { REPORT mrc.IncrMat(); }  // 1808
  714. X
  715. Xvoid Matrix::NextCol(MatrixRowCol& mrc)
  716. X{
  717. X   REPORT                                        // 632
  718. X   if (+(mrc.cw*StoreOnExit)) { REPORT RestoreCol(mrc); }
  719. X   mrc.rowcol++;
  720. X   if (mrc.rowcol<ncols)
  721. X   {
  722. X      if (+(mrc.cw*LoadOnEntry))
  723. X      {
  724. X     REPORT
  725. X         Real* ColCopy = mrc.store;
  726. X         Real* Mstore = store+mrc.rowcol; int i=nrows;
  727. X         while (i--) { *ColCopy++ = *Mstore; Mstore+=ncols; }
  728. X      }
  729. X   }
  730. X   else { REPORT mrc.cw -= StoreOnExit; }
  731. X}
  732. X
  733. X// routines for diagonal matrix
  734. X
  735. Xvoid DiagonalMatrix::GetRow(MatrixRowCol& mrc)
  736. X{
  737. X   REPORT
  738. X   mrc.skip=mrc.rowcol; mrc.cw-=IsACopy; mrc.storage=1; mrc.store=store;
  739. X}
  740. X
  741. Xvoid DiagonalMatrix::GetCol(MatrixRowCol& mrc)
  742. X{
  743. X   REPORT 
  744. X   mrc.skip=mrc.rowcol; mrc.storage=1;
  745. X   if (+(mrc.cw*StoreHere))
  746. X      { REPORT *(mrc.store+mrc.rowcol)=*(store+mrc.rowcol); mrc.cw+=IsACopy; }
  747. X   else { REPORT mrc.store = store; mrc.cw-=IsACopy; }     // not accessed
  748. X}
  749. X
  750. Xvoid DiagonalMatrix::NextRow(MatrixRowCol& mrc) { REPORT mrc.IncrDiag(); }
  751. X                              // 800
  752. Xvoid DiagonalMatrix::NextCol(MatrixRowCol& mrc)
  753. X{
  754. X   REPORT
  755. X   if (+(mrc.cw*StoreHere))
  756. X   {
  757. X      if (+(mrc.cw*StoreOnExit))
  758. X         { REPORT *(store+mrc.rowcol)=*(mrc.store+mrc.rowcol); }
  759. X      mrc.IncrDiag();
  760. X      if (+(mrc.cw*LoadOnEntry) && mrc.rowcol < ncols)
  761. X         { REPORT *(mrc.store+mrc.rowcol)=*(store+mrc.rowcol); }
  762. X   }
  763. X   else { REPORT mrc.IncrDiag(); }                     // not accessed
  764. X}
  765. X
  766. X// routines for upper triangular matrix
  767. X
  768. Xvoid UpperTriangularMatrix::GetRow(MatrixRowCol& mrc)
  769. X{
  770. X   REPORT
  771. X   int row = mrc.rowcol; mrc.skip=row; mrc.cw-=IsACopy;
  772. X   mrc.storage=ncols-row; mrc.store=store+(row*(2*ncols-row-1))/2;
  773. X}
  774. X
  775. X
  776. Xvoid UpperTriangularMatrix::GetCol(MatrixRowCol& mrc)
  777. X{
  778. X   REPORT
  779. X   mrc.skip=0; mrc.cw+=IsACopy; int i=mrc.rowcol+1; mrc.storage=i;
  780. X   Real* ColCopy;
  781. X   if ( !(mrc.cw*StoreHere) )
  782. X   {
  783. X      REPORT                                              // not accessed
  784. X      ColCopy = new Real [i]; MatrixErrorNoSpace(ColCopy);
  785. X      MONITOR_REAL_NEW("Make (UT GetCol)",i,ColCopy) 
  786. X      mrc.store = ColCopy;
  787. X   }
  788. X   else { REPORT ColCopy = mrc.store; }
  789. X   if (+(mrc.cw*LoadOnEntry))
  790. X   {
  791. X      REPORT
  792. X      Real* Mstore = store+mrc.rowcol; int j = ncols;
  793. X      while (i--) { *ColCopy++ = *Mstore; Mstore += --j; }
  794. X   }
  795. X}
  796. X
  797. Xvoid UpperTriangularMatrix::RestoreCol(MatrixRowCol& mrc)
  798. X{
  799. X//  if (mrc.cw*StoreOnExit)
  800. X  {
  801. X     REPORT
  802. X     Real* Mstore = store+mrc.rowcol; int i=mrc.rowcol+1; int j = ncols;
  803. X     Real* Cstore = mrc.store;
  804. X     while (i--) { *Mstore = *Cstore++; Mstore += --j; }
  805. X  }
  806. X}
  807. X
  808. Xvoid UpperTriangularMatrix::NextRow(MatrixRowCol& mrc) { REPORT mrc.IncrUT(); }
  809. X                              // 722
  810. X
  811. X// routines for lower triangular matrix
  812. X
  813. Xvoid LowerTriangularMatrix::GetRow(MatrixRowCol& mrc)
  814. X{
  815. X   REPORT
  816. X   int row=mrc.rowcol; mrc.skip=0; mrc.cw-=IsACopy; mrc.storage=row+1;
  817. X   mrc.store=store+(row*(row+1))/2;
  818. X}
  819. X
  820. Xvoid LowerTriangularMatrix::GetCol(MatrixRowCol& mrc)
  821. X{
  822. X   REPORT
  823. X   int col=mrc.rowcol; mrc.skip=col; mrc.cw+=IsACopy;
  824. X   int i=nrows-col; mrc.storage=i; Real* ColCopy;
  825. X   if ( !(mrc.cw*StoreHere) )
  826. X   {
  827. X      REPORT                                            // not accessed
  828. X      ColCopy = new Real [i]; MatrixErrorNoSpace(ColCopy);
  829. X      MONITOR_REAL_NEW("Make (LT GetCol)",i,ColCopy) 
  830. X      mrc.store = ColCopy-col;
  831. X   }
  832. X   else { REPORT ColCopy = mrc.store+col; }
  833. X   if (+(mrc.cw*LoadOnEntry))
  834. X   {
  835. X      REPORT
  836. X      Real* Mstore = store+(col*(col+3))/2;
  837. X      while (i--) { *ColCopy++ = *Mstore; Mstore += ++col; }
  838. X   }
  839. X}
  840. X
  841. Xvoid LowerTriangularMatrix::RestoreCol(MatrixRowCol& mrc)
  842. X{
  843. X//  if (mrc.cw*StoreOnExit)
  844. X   {
  845. X      REPORT
  846. X      int col=mrc.rowcol; Real* Cstore = mrc.store+col;
  847. X      Real* Mstore = store+(col*(col+3))/2; int i=nrows-col;
  848. X      while (i--) { *Mstore = *Cstore++; Mstore += ++col; }
  849. X   }
  850. X}
  851. X
  852. Xvoid LowerTriangularMatrix::NextRow(MatrixRowCol& mrc) { REPORT mrc.IncrLT(); }
  853. X                                     //712
  854. X// routines for symmetric matrix
  855. X
  856. Xvoid SymmetricMatrix::GetRow(MatrixRowCol& mrc)
  857. X{
  858. X   REPORT                                                //571
  859. X   mrc.skip=0; int row=mrc.rowcol;
  860. X   if (+(mrc.cw*DirectPart))
  861. X   {
  862. X      REPORT
  863. X      mrc.cw-=IsACopy; mrc.storage=row+1; mrc.store=store+(row*(row+1))/2;
  864. X   }
  865. X   else
  866. X   {
  867. X      mrc.cw+=IsACopy; mrc.storage=ncols;
  868. X      Real* RowCopy = new Real [ncols]; MatrixErrorNoSpace(RowCopy);
  869. X      MONITOR_REAL_NEW("Make (SymGetRow)",ncols,RowCopy) 
  870. X      mrc.store = RowCopy;
  871. X      if (+(mrc.cw*LoadOnEntry))
  872. X      {
  873. X     REPORT                                         // 544
  874. X         Real* Mstore = store+(row*(row+1))/2; int i = row;
  875. X         while (i--) *RowCopy++ = *Mstore++;
  876. X         i = ncols-row;
  877. X     while (i--) { *RowCopy++ = *Mstore; Mstore += ++row; }
  878. X      }
  879. X   }
  880. X}
  881. X
  882. X// need to check this out under StoreHere
  883. X
  884. Xvoid SymmetricMatrix::GetCol(MatrixRowCol& mrc)
  885. X{
  886. X   REPORT
  887. X   mrc.skip=0; int col=mrc.rowcol;
  888. X   if (+(mrc.cw*DirectPart))
  889. X   {
  890. X      REPORT                                         // not accessed
  891. X      mrc.cw-=IsACopy; mrc.storage=col+1; mrc.store=store+(col*(col+1))/2;
  892. X   }
  893. X   else
  894. X   {
  895. X      mrc.cw+=IsACopy; mrc.storage=ncols; Real* ColCopy;
  896. X      if ( !(mrc.cw*StoreHere) )
  897. X      {
  898. X         REPORT                                      // not accessed
  899. X         ColCopy = new Real [ncols]; MatrixErrorNoSpace(ColCopy);
  900. X         MONITOR_REAL_NEW("Make (SymGetCol)",ncols,ColCopy) 
  901. X         mrc.store = ColCopy;
  902. X      }
  903. X      else { REPORT ColCopy = mrc.store; }
  904. X      if (+(mrc.cw*LoadOnEntry))
  905. X      {
  906. X         REPORT
  907. X         Real* Mstore = store+(col*(col+1))/2; int i = col;
  908. X         while (i--) *ColCopy++ = *Mstore++;
  909. X         i = ncols-col;
  910. X     while (i--) { *ColCopy++ = *Mstore; Mstore += ++col; }
  911. X      }
  912. X   }
  913. X}
  914. X
  915. X//void SymmetricMatrix::RestoreRow(int row, Real* Rstore)
  916. X//{
  917. X////   if (cw*IsACopy && cw*StoreOnExit)
  918. X//   {
  919. X//      Real* Mstore = store+(row*(row+1))/2; int i = row+1;
  920. X//      while (i--) *Mstore++ = *Rstore++;
  921. X//   }
  922. X//}
  923. X
  924. X//void SymmetricMatrix::RestoreCol(int col, Real* Cstore)
  925. X//{
  926. X////   if (cw*IsACopy && cw*StoreOnExit)
  927. X//   {
  928. X//      Real* Mstore = store+(col*(col+3))/2;
  929. X//      int i = nrows-col; int j = col;
  930. X//      while (i--) { *Mstore = *Cstore++; Mstore+= ++j; }
  931. X//   }
  932. X//}
  933. X
  934. X// routines for row vector
  935. X
  936. Xvoid RowVector::GetCol(MatrixRowCol& mrc)
  937. X{
  938. X   REPORT 
  939. X   mrc.skip=0; mrc.storage=1;
  940. X   if (mrc.cw >= StoreHere)
  941. X   {
  942. X      if (mrc.cw >= LoadOnEntry) { REPORT *(mrc.store) = *(store+mrc.rowcol); }
  943. X      mrc.cw+=IsACopy;
  944. X   }
  945. X   else  { REPORT mrc.store = store+mrc.rowcol; mrc.cw-=IsACopy; }
  946. X                                                         // not accessed
  947. X}
  948. X
  949. Xvoid RowVector::NextCol(MatrixRowCol& mrc) 
  950. X{
  951. X   REPORT
  952. X   if (+(mrc.cw*StoreHere))
  953. X   {
  954. X      if (+(mrc.cw*StoreOnExit)) { REPORT *(store+mrc.rowcol)=*(mrc.store); }
  955. X                             // not accessed
  956. X      mrc.rowcol++;
  957. X      if (mrc.rowcol < ncols)
  958. X      {
  959. X     if (+(mrc.cw*LoadOnEntry)) { REPORT *(mrc.store)=*(store+mrc.rowcol); }
  960. X      }
  961. X      else { REPORT mrc.cw -= StoreOnExit; }
  962. X   }
  963. X   else  { REPORT mrc.rowcol++; mrc.store++; }             // not accessed
  964. X}
  965. X
  966. Xvoid RowVector::RestoreCol(MatrixRowCol& mrc)
  967. X{
  968. X   REPORT                                            // not accessed
  969. X   if (mrc.cw>=IsACopy)  { REPORT *(store+mrc.rowcol)=*(mrc.store); }
  970. X}
  971. X
  972. X
  973. X// routines for band matrices
  974. X
  975. Xvoid BandMatrix::GetRow(MatrixRowCol& mrc)
  976. X{
  977. X   REPORT
  978. X   mrc.cw -= IsACopy; int r = mrc.rowcol; int w = lower+1+upper;
  979. X   int s = r-lower; mrc.store = store+(r*w-s); if (s<0) { w += s; s = 0; }
  980. X   mrc.skip = s; s += w-ncols; if (s>0) w -= s; mrc.storage = w;
  981. X}
  982. X
  983. X// make special versions of this for upper and lower band matrices
  984. X
  985. Xvoid BandMatrix::NextRow(MatrixRowCol& mrc)
  986. X{
  987. X   REPORT
  988. X   int r = ++mrc.rowcol; mrc.store += lower+upper;
  989. X   if (r<=lower) mrc.storage++; else mrc.skip++;
  990. X   if (r>=ncols-upper) mrc.storage--;
  991. X}
  992. X
  993. Xvoid BandMatrix::GetCol(MatrixRowCol& mrc)
  994. X{
  995. X   REPORT
  996. X   mrc.cw += IsACopy; int c = mrc.rowcol; int n = lower+upper; int w = n+1;
  997. X   int b; int s = c-upper; Real* ColCopy;
  998. X   if (s<=0) { w += s; s = 0; b = c+lower; } else b = s*w+n;
  999. X   mrc.skip = s; s += w-nrows; if (s>0) w -= s; mrc.storage = w;
  1000. X   if ( !(mrc.cw*StoreHere) )
  1001. X   {
  1002. X      REPORT
  1003. X      ColCopy = new Real [w]; MatrixErrorNoSpace(ColCopy);
  1004. X      MONITOR_REAL_NEW("Make (BMGetCol)",w,ColCopy)
  1005. X      mrc.store = ColCopy-mrc.skip;
  1006. X   }
  1007. X   else { REPORT ColCopy = mrc.store+mrc.skip; }
  1008. X   if (+(mrc.cw*LoadOnEntry))
  1009. X   {
  1010. X      REPORT
  1011. X      Real* Mstore = store+b;
  1012. X      while (w--) { *ColCopy++ = *Mstore; Mstore+=n; }
  1013. X   }
  1014. X}
  1015. X
  1016. Xvoid BandMatrix::RestoreCol(MatrixRowCol& mrc)
  1017. X{
  1018. X//  if (mrc.cw*StoreOnExit)
  1019. X   REPORT
  1020. X   int c = mrc.rowcol; int n = lower+upper; int s = c-upper;
  1021. X   Real* Mstore = store + ((s<=0) ? c+lower : s*n+s+n);
  1022. X   Real* Cstore = mrc.store+mrc.skip; int w = mrc.storage;
  1023. X   while (w--) { *Mstore = *Cstore++; Mstore += n; }
  1024. X}
  1025. X
  1026. X// routines for symmetric band matrix
  1027. X
  1028. Xvoid SymmetricBandMatrix::GetRow(MatrixRowCol& mrc)
  1029. X{
  1030. X   REPORT
  1031. X   int r=mrc.rowcol; int s = r-lower; int w1 = lower+1; int o = r*w1;
  1032. X   if (s<0) { w1 += s; o -= s; s = 0; }  mrc.skip = s;
  1033. X
  1034. X   if (+(mrc.cw*DirectPart))
  1035. X      { REPORT  mrc.cw -= IsACopy; mrc.store = store+o-s; mrc.storage = w1; }
  1036. X   else
  1037. X   {
  1038. X      mrc.cw += IsACopy; int w = w1+lower; s += w-ncols;
  1039. X      if (s>0) w -= s; mrc.storage = w; int w2 = w-w1;
  1040. X      Real* RowCopy = new Real [w]; MatrixErrorNoSpace(RowCopy);
  1041. X      MONITOR_REAL_NEW("Make (SmBGetRow)",w,RowCopy) 
  1042. X      mrc.store = RowCopy-mrc.skip;
  1043. X      if (+(mrc.cw*LoadOnEntry))
  1044. X      {
  1045. X     REPORT
  1046. X         Real* Mstore = store+o;
  1047. X         while (w1--) *RowCopy++ = *Mstore++;   Mstore--;
  1048. X         while (w2--) { Mstore += lower; *RowCopy++ = *Mstore; }
  1049. X      }
  1050. X   }
  1051. X}
  1052. X
  1053. X// need to check this out under StoreHere
  1054. X
  1055. Xvoid SymmetricBandMatrix::GetCol(MatrixRowCol& mrc)
  1056. X{
  1057. X   REPORT
  1058. X   int c=mrc.rowcol; int s = c-lower; int w1 = lower+1; int o = c*w1;
  1059. X   if (s<0) { w1 += s; o -= s; s = 0; }  mrc.skip = s;
  1060. X
  1061. X   if (+(mrc.cw*DirectPart))
  1062. X      { REPORT  mrc.cw -= IsACopy; mrc.store = store+o-s; mrc.storage = w1; }
  1063. X   else
  1064. X   {
  1065. X      mrc.cw += IsACopy; int w = w1+lower; s += w-ncols;
  1066. X      if (s>0) w -= s; mrc.storage = w; int w2 = w-w1; Real* ColCopy;
  1067. X      if ( !(mrc.cw*StoreHere) )
  1068. X      {
  1069. X         ColCopy = new Real [w]; MatrixErrorNoSpace(ColCopy);
  1070. X         MONITOR_REAL_NEW("Make (SmBGetCol)",w,ColCopy) 
  1071. X         mrc.store = ColCopy-mrc.skip;
  1072. X      }
  1073. X      else { REPORT ColCopy = mrc.store+mrc.skip; }
  1074. X      if (+(mrc.cw*LoadOnEntry))
  1075. X      {
  1076. X     REPORT
  1077. X         Real* Mstore = store+o;
  1078. X         while (w1--) *ColCopy++ = *Mstore++;   Mstore--;
  1079. X         while (w2--) { Mstore += lower; *ColCopy++ = *Mstore; }
  1080. X      }
  1081. X   }
  1082. X}
  1083. X
  1084. END_OF_FILE
  1085. if test 14201 -ne `wc -c <'newmat3.cxx'`; then
  1086.     echo shar: \"'newmat3.cxx'\" unpacked with wrong size!
  1087. fi
  1088. # end of 'newmat3.cxx'
  1089. fi
  1090. if test -f 'newmat4.cxx' -a "${1}" != "-c" ; then 
  1091.   echo shar: Will not clobber existing file \"'newmat4.cxx'\"
  1092. else
  1093. echo shar: Extracting \"'newmat4.cxx'\" \(16770 characters\)
  1094. sed "s/^X//" >'newmat4.cxx' <<'END_OF_FILE'
  1095. X//$$ newmat4.cxx       Constructors, ReDimension, basic utilities
  1096. X
  1097. X// Copyright (C) 1991,2: R B Davies
  1098. X
  1099. X#include "include.h"
  1100. X
  1101. X#include "newmat.h"
  1102. X#include "newmatrc.h"
  1103. X
  1104. X//#define REPORT { static ExeCounter ExeCount(__LINE__,4); ExeCount++; }
  1105. X
  1106. X#define REPORT {}
  1107. X
  1108. X//#define REPORT1 { static ExeCounter ExeCount(__LINE__,4); ExeCount++; }
  1109. X
  1110. X// REPORT1 constructors only - doesn't work in turbo and Borland C++
  1111. X
  1112. X#define REPORT1 {}
  1113. X
  1114. X
  1115. X/*************************** general utilities *************************/
  1116. X
  1117. Xstatic int tristore(int n)                      // els in triangular matrix
  1118. X{ return (n*(n+1))/2; }
  1119. X
  1120. X
  1121. X/****************************** constructors ***************************/
  1122. X
  1123. XGeneralMatrix::GeneralMatrix()
  1124. X{ store=0; storage=0; nrows=0; ncols=0; tag=-1; }
  1125. X
  1126. XGeneralMatrix::GeneralMatrix(ArrayLengthSpecifier s)
  1127. X{
  1128. X   REPORT1
  1129. X   storage=s.Value(); tag=-1;
  1130. X   store = new Real [storage]; MatrixErrorNoSpace(store);
  1131. X   MONITOR_REAL_NEW("Make (GenMatrix)",storage,store)
  1132. X}
  1133. X
  1134. XMatrix::Matrix(int m, int n) : GeneralMatrix(m*n)
  1135. X{ REPORT1 nrows=m; ncols=n; }
  1136. X
  1137. XSymmetricMatrix::SymmetricMatrix(ArrayLengthSpecifier n)
  1138. X   : GeneralMatrix(tristore(n.Value()))
  1139. X{ REPORT1 nrows=n.Value(); ncols=n.Value(); }
  1140. X
  1141. XUpperTriangularMatrix::UpperTriangularMatrix(ArrayLengthSpecifier n)
  1142. X   : GeneralMatrix(tristore(n.Value()))
  1143. X{ REPORT1 nrows=n.Value(); ncols=n.Value(); }
  1144. X
  1145. XLowerTriangularMatrix::LowerTriangularMatrix(ArrayLengthSpecifier n)
  1146. X   : GeneralMatrix(tristore(n.Value()))
  1147. X{ REPORT1 nrows=n.Value(); ncols=n.Value(); }
  1148. X
  1149. XDiagonalMatrix::DiagonalMatrix(ArrayLengthSpecifier m) : GeneralMatrix(m)
  1150. X{ REPORT1 nrows=m.Value(); ncols=m.Value(); }
  1151. X
  1152. XMatrix::Matrix(const BaseMatrix& M)
  1153. X{
  1154. X   REPORT1 CheckConversion(M);
  1155. X   GeneralMatrix* gmx=((BaseMatrix&)M).Evaluate(MatrixType::Rt);
  1156. X   GetMatrix(gmx);
  1157. X}
  1158. X
  1159. XRowVector::RowVector(const BaseMatrix& M) : Matrix(M)
  1160. X{
  1161. X   if (nrows!=1)
  1162. X   {
  1163. X      Tracer tr("RowVector");
  1164. X      Throw(VectorException(*this));
  1165. X   }
  1166. X}
  1167. X
  1168. XColumnVector::ColumnVector(const BaseMatrix& M) : Matrix(M)
  1169. X{
  1170. X   if (ncols!=1)
  1171. X   {
  1172. X      Tracer tr("ColumnVector");
  1173. X      Throw(VectorException(*this));
  1174. X   }
  1175. X}
  1176. X
  1177. XSymmetricMatrix::SymmetricMatrix(const BaseMatrix& M)
  1178. X{
  1179. X   REPORT1 CheckConversion(M);
  1180. X   GeneralMatrix* gmx=((BaseMatrix&)M).Evaluate(MatrixType::Sm);
  1181. X   GetMatrix(gmx);
  1182. X}
  1183. X
  1184. XUpperTriangularMatrix::UpperTriangularMatrix(const BaseMatrix& M)
  1185. X{
  1186. X   REPORT1 CheckConversion(M);
  1187. X   GeneralMatrix* gmx=((BaseMatrix&)M).Evaluate(MatrixType::UT);
  1188. X   GetMatrix(gmx);
  1189. X}
  1190. X
  1191. XLowerTriangularMatrix::LowerTriangularMatrix(const BaseMatrix& M)
  1192. X{
  1193. X   REPORT1 CheckConversion(M);
  1194. X   GeneralMatrix* gmx=((BaseMatrix&)M).Evaluate(MatrixType::LT);
  1195. X   GetMatrix(gmx);
  1196. X}
  1197. X
  1198. XDiagonalMatrix::DiagonalMatrix(const BaseMatrix& M)
  1199. X{
  1200. X   REPORT1 CheckConversion(M);
  1201. X   GeneralMatrix* gmx=((BaseMatrix&)M).Evaluate(MatrixType::Dg);
  1202. X   GetMatrix(gmx);
  1203. X}
  1204. X
  1205. XGeneralMatrix::~GeneralMatrix()
  1206. X{
  1207. X   if (store)
  1208. X   {
  1209. X      MONITOR_REAL_DELETE("Free (GenMatrix)",storage,store)
  1210. X#ifdef Version21
  1211. X      delete [] store;
  1212. X#else
  1213. X      delete [storage] store;
  1214. X#endif
  1215. X   }
  1216. X}
  1217. X
  1218. XCroutMatrix::CroutMatrix(const BaseMatrix& m)
  1219. X{
  1220. X   REPORT1
  1221. X   Tracer tr("CroutMatrix");
  1222. X   GeneralMatrix* gm = ((BaseMatrix&)m).Evaluate(MatrixType::Rt);
  1223. X   GetMatrix(gm);
  1224. X   if (nrows!=ncols) Throw(NotSquareException(*this));
  1225. X   d=TRUE; sing=FALSE;
  1226. X   indx=new int [nrows]; MatrixErrorNoSpace(indx);
  1227. X   MONITOR_INT_NEW("Index (CroutMat)",nrows,indx)
  1228. X   ludcmp();
  1229. X}
  1230. X
  1231. XCroutMatrix::~CroutMatrix()
  1232. X{
  1233. X   MONITOR_INT_DELETE("Index (CroutMat)",nrows,indx)
  1234. X#ifdef Version21
  1235. X   delete [] indx;
  1236. X#else
  1237. X   delete [nrows] indx;
  1238. X#endif
  1239. X}
  1240. X
  1241. X//ReturnMatrixX::ReturnMatrixX(GeneralMatrix& gmx)
  1242. X//{
  1243. X//   REPORT1
  1244. X//   gm = gmx.Type().New();  MatrixErrorNoSpace(gm);
  1245. X//   gm->GetMatrix(&gmx);  gm->ReleaseAndDelete();
  1246. X//}
  1247. X
  1248. X#ifndef TEMPS_DESTROYED_QUICKLY
  1249. X
  1250. XGeneralMatrix::operator ReturnMatrixX() const
  1251. X{
  1252. X   REPORT
  1253. X   GeneralMatrix* gm = Type().New();  MatrixErrorNoSpace(gm);
  1254. X   gm->GetMatrix(this);  gm->ReleaseAndDelete();
  1255. X   return ReturnMatrixX(gm);
  1256. X}
  1257. X
  1258. X#else
  1259. X
  1260. XGeneralMatrix::operator ReturnMatrixX&() const
  1261. X{
  1262. X   REPORT
  1263. X   GeneralMatrix* gm = Type().New();  MatrixErrorNoSpace(gm);
  1264. X   gm->GetMatrix(this);  gm->ReleaseAndDelete();
  1265. X   ReturnMatrixX* x = new ReturnMatrixX(gm);
  1266. X   MatrixErrorNoSpace(x); return *x;
  1267. X}
  1268. X
  1269. X#endif
  1270. X
  1271. X/**************************** ReDimension matrices ***************************/
  1272. X
  1273. Xvoid GeneralMatrix::ReDimension(int nr, int nc, int s)
  1274. X{
  1275. X   REPORT 
  1276. X   if (store)
  1277. X   {
  1278. X      MONITOR_REAL_DELETE("Free (ReDimensi)",storage,store)
  1279. X#ifdef Version21
  1280. X      delete [] store;
  1281. X#else
  1282. X      delete [storage] store;
  1283. X#endif
  1284. X   }
  1285. X   storage=s; nrows=nr; ncols=nc; tag=-1;
  1286. X   store = new Real [storage]; MatrixErrorNoSpace(store);
  1287. X   MONITOR_REAL_NEW("Make (ReDimensi)",storage,store)
  1288. X}
  1289. X
  1290. Xvoid Matrix::ReDimension(int nr, int nc)
  1291. X{ REPORT GeneralMatrix::ReDimension(nr,nc,nr*nc); }
  1292. X
  1293. Xvoid SymmetricMatrix::ReDimension(int nr)
  1294. X{ REPORT GeneralMatrix::ReDimension(nr,nr,tristore(nr)); }
  1295. X
  1296. Xvoid UpperTriangularMatrix::ReDimension(int nr)
  1297. X{ REPORT GeneralMatrix::ReDimension(nr,nr,tristore(nr)); }
  1298. X
  1299. Xvoid LowerTriangularMatrix::ReDimension(int nr)
  1300. X{ REPORT GeneralMatrix::ReDimension(nr,nr,tristore(nr)); }
  1301. X
  1302. Xvoid DiagonalMatrix::ReDimension(int nr)
  1303. X{ REPORT GeneralMatrix::ReDimension(nr,nr,nr); }
  1304. X
  1305. Xvoid RowVector::ReDimension(int nc)
  1306. X{ REPORT GeneralMatrix::ReDimension(1,nc,nc); }
  1307. X
  1308. Xvoid ColumnVector::ReDimension(int nr)
  1309. X{ REPORT GeneralMatrix::ReDimension(nr,1,nr); }
  1310. X
  1311. X
  1312. X/********************* manipulate types, storage **************************/
  1313. X
  1314. Xint GeneralMatrix::search(const BaseMatrix* s) const
  1315. X{ REPORT return (s==this) ? 1 : 0; }
  1316. X
  1317. Xint MultipliedMatrix::search(const BaseMatrix* s) const
  1318. X{ REPORT return bm1->search(s) + bm2->search(s); }
  1319. X
  1320. Xint ShiftedMatrix::search(const BaseMatrix* s) const
  1321. X{ REPORT return bm->search(s); }
  1322. X
  1323. Xint NegatedMatrix::search(const BaseMatrix* s) const
  1324. X{ REPORT return bm->search(s); }
  1325. X
  1326. Xint ConstMatrix::search(const BaseMatrix* s) const
  1327. X{ REPORT return (s==cgm) ? 1 : 0; }
  1328. X
  1329. Xint ReturnMatrixX::search(const BaseMatrix* s) const
  1330. X{ REPORT return (s==gm) ? 1 : 0; }
  1331. X
  1332. XMatrixType Matrix::Type() const { return MatrixType::Rt; }
  1333. XMatrixType SymmetricMatrix::Type() const { return MatrixType::Sm; }
  1334. XMatrixType UpperTriangularMatrix::Type() const { return MatrixType::UT; }
  1335. XMatrixType LowerTriangularMatrix::Type() const { return MatrixType::LT; }
  1336. XMatrixType DiagonalMatrix::Type() const { return MatrixType::Dg; }
  1337. XMatrixType RowVector::Type() const { return MatrixType::RV; }
  1338. XMatrixType ColumnVector::Type() const { return MatrixType::CV; }
  1339. XMatrixType CroutMatrix::Type() const { return MatrixType::Ct; }
  1340. XMatrixType BandMatrix::Type() const { return MatrixType::BM; }
  1341. XMatrixType UpperBandMatrix::Type() const { return MatrixType::UB; }
  1342. XMatrixType LowerBandMatrix::Type() const { return MatrixType::LB; }
  1343. XMatrixType SymmetricBandMatrix::Type() const { return MatrixType::SB; }
  1344. XMatrixType RowedMatrix::Type() const { return MatrixType::RV; }
  1345. XMatrixType ColedMatrix::Type() const { return MatrixType::CV; }
  1346. XMatrixType DiagedMatrix::Type() const { return MatrixType::Dg; }
  1347. XMatrixType MatedMatrix::Type() const { return MatrixType::Rt; }
  1348. XMatrixType GetSubMatrix::Type() const { return mt; }
  1349. X
  1350. XMatrixType AddedMatrix::Type() const
  1351. X{ REPORT return bm1->Type() + bm2->Type(); }
  1352. X
  1353. XMatrixType MultipliedMatrix::Type() const
  1354. X{ REPORT return bm1->Type() * bm2->Type(); }
  1355. X
  1356. XMatrixType SolvedMatrix::Type() const
  1357. X{ REPORT return bm1->Type().i() * bm2->Type(); }
  1358. X
  1359. XMatrixType SubtractedMatrix::Type() const
  1360. X{ REPORT return bm1->Type() + bm2->Type(); }
  1361. X
  1362. XMatrixType ShiftedMatrix::Type() const
  1363. X{ REPORT return bm->Type().AddEqualEl(); }
  1364. X
  1365. XMatrixType ScaledMatrix::Type() const { REPORT return bm->Type(); }
  1366. XMatrixType TransposedMatrix::Type() const { REPORT return bm->Type().t(); }
  1367. XMatrixType NegatedMatrix::Type() const { REPORT return bm->Type(); }
  1368. XMatrixType InvertedMatrix::Type() const { REPORT return bm->Type().i(); }
  1369. XMatrixType ConstMatrix::Type() const { REPORT return cgm->Type(); }
  1370. XMatrixType ReturnMatrixX::Type() const { REPORT return gm->Type(); }
  1371. X
  1372. X/*
  1373. Xint GeneralMatrix::NrowsV() const { return nrows; }
  1374. Xint RowedMatrix::NrowsV() const { return 1; }
  1375. Xint MatedMatrix::NrowsV() const { return nr; }
  1376. Xint GetSubMatrix::NrowsV() const { return row_number; }
  1377. Xint MultipliedMatrix::NrowsV() const  { return bm1->NrowsV(); }
  1378. Xint ShiftedMatrix::NrowsV() const { return bm->NrowsV(); }
  1379. Xint TransposedMatrix::NrowsV() const { return bm->NcolsV(); }
  1380. Xint NegatedMatrix::NrowsV() const { return bm->NrowsV(); }
  1381. Xint ColedMatrix::NrowsV() const { return bm->NrowsV() * bm->NcolsV(); }
  1382. Xint DiagedMatrix::NrowsV() const { return bm->NrowsV() * bm->NcolsV(); }
  1383. Xint ConstMatrix::NrowsV() const { return cgm->Nrows(); }
  1384. Xint ReturnMatrixX::NrowsV() const { return gm->Nrows(); }
  1385. X
  1386. Xint GeneralMatrix::NcolsV() const { return ncols; }
  1387. Xint ColedMatrix::NcolsV() const { return 1; }
  1388. Xint MatedMatrix::NcolsV() const { return nc; }
  1389. Xint GetSubMatrix::NcolsV() const { return col_number; }
  1390. Xint MultipliedMatrix::NcolsV() const  { return bm2->NcolsV(); }
  1391. Xint ShiftedMatrix::NcolsV() const { return bm->NcolsV(); }
  1392. Xint TransposedMatrix::NcolsV() const { return bm->NrowsV(); }
  1393. Xint NegatedMatrix::NcolsV() const { return bm->NcolsV(); }
  1394. Xint RowedMatrix::NcolsV() const { return bm->NrowsV() * bm->NcolsV(); }
  1395. Xint DiagedMatrix::NcolsV() const { return bm->NrowsV() * bm->NcolsV(); }
  1396. Xint ConstMatrix::NcolsV() const { return cgm->Ncols(); }
  1397. Xint ReturnMatrixX::NcolsV() const { return gm->Ncols(); }
  1398. X*/
  1399. X
  1400. XMatrixBandWidth BaseMatrix::BandWidth() const { return -1; }
  1401. XMatrixBandWidth DiagonalMatrix::BandWidth() const { return 0; }
  1402. X
  1403. XMatrixBandWidth BandMatrix::BandWidth() const
  1404. X   { return MatrixBandWidth(lower,upper); }
  1405. X
  1406. XMatrixBandWidth AddedMatrix::BandWidth() const
  1407. X{ return gm1->BandWidth() + gm2->BandWidth(); }
  1408. X
  1409. XMatrixBandWidth MultipliedMatrix::BandWidth() const
  1410. X{ return gm1->BandWidth() * gm2->BandWidth(); }
  1411. X
  1412. XMatrixBandWidth SolvedMatrix::BandWidth() const { return -1; }
  1413. XMatrixBandWidth ScaledMatrix::BandWidth() const { return gm->BandWidth(); }
  1414. XMatrixBandWidth NegatedMatrix::BandWidth() const { return gm->BandWidth(); }
  1415. X
  1416. XMatrixBandWidth TransposedMatrix::BandWidth() const
  1417. X{ return gm->BandWidth().t(); }
  1418. X
  1419. XMatrixBandWidth InvertedMatrix::BandWidth() const { return -1; }
  1420. XMatrixBandWidth RowedMatrix::BandWidth() const { return -1; }
  1421. XMatrixBandWidth ColedMatrix::BandWidth() const { return -1; }
  1422. XMatrixBandWidth DiagedMatrix::BandWidth() const { return 0; }
  1423. XMatrixBandWidth MatedMatrix::BandWidth() const { return -1; }
  1424. XMatrixBandWidth ConstMatrix::BandWidth() const { return cgm->BandWidth(); }
  1425. XMatrixBandWidth ReturnMatrixX::BandWidth() const { return gm->BandWidth(); }
  1426. X
  1427. XMatrixBandWidth GetSubMatrix::BandWidth() const
  1428. X{
  1429. X
  1430. X   if (row_skip==col_skip && row_number==col_number) return gm->BandWidth();
  1431. X   else return MatrixBandWidth(-1);
  1432. X}
  1433. X
  1434. X
  1435. X
  1436. X//  Rules regarding tDelete, reuse, GetStore
  1437. X//    All matrices processed during expression evaluation must be subject
  1438. X//    to exactly one of reuse(), tDelete(), GetStore() or BorrowStore().
  1439. X//    If reuse returns TRUE the matrix must be reused.
  1440. X//    GetMatrix(gm) always calls gm->GetStore()
  1441. X//    gm->Evaluate obeys rules
  1442. X//    bm->Evaluate obeys rules for matrices in bm structure
  1443. X
  1444. Xvoid GeneralMatrix::tDelete()
  1445. X{
  1446. X   if (tag<0)
  1447. X   {
  1448. X      if (tag<-1) { REPORT store=0; delete this; return; }  // borrowed
  1449. X      else { REPORT return; }
  1450. X   }
  1451. X   if (tag==1)
  1452. X   {
  1453. X      REPORT  MONITOR_REAL_DELETE("Free   (tDelete)",storage,store)
  1454. X#ifdef Version21
  1455. X      if (store) delete [] store;
  1456. X#else
  1457. X      if (store) delete [storage] store;
  1458. X#endif
  1459. X      store=0; tag=-1; return;
  1460. X   }
  1461. X   if (tag==0) { REPORT delete this; return; }
  1462. X   REPORT tag--; return;
  1463. X}
  1464. X
  1465. Xstatic void BlockCopy(int n, Real* from, Real* to)
  1466. X{
  1467. X   REPORT
  1468. X   int i = (n >> 3);
  1469. X   while (i--)
  1470. X   {
  1471. X      *to++ = *from++; *to++ = *from++; *to++ = *from++; *to++ = *from++;
  1472. X      *to++ = *from++; *to++ = *from++; *to++ = *from++; *to++ = *from++;
  1473. X   }
  1474. X   i = n & 7; while (i--) *to++ = *from++;
  1475. X}
  1476. X
  1477. XBoolean GeneralMatrix::reuse()
  1478. X{
  1479. X   if (tag<-1)
  1480. X   {
  1481. X      REPORT
  1482. X      Real* s = new Real [storage]; MatrixErrorNoSpace(s);
  1483. X      MONITOR_REAL_NEW("Make     (reuse)",storage,s)
  1484. X      BlockCopy(storage, store, s); store=s; tag=0; return TRUE;
  1485. X   }
  1486. X   if (tag<0) { REPORT return FALSE; }
  1487. X   if (tag<=1)  { REPORT return TRUE; }
  1488. X   REPORT tag--; return FALSE;
  1489. X}
  1490. X
  1491. XReal* GeneralMatrix::GetStore()
  1492. X{
  1493. X   if (tag<0 || tag>1)
  1494. X   {
  1495. X      Real* s = new Real [storage]; MatrixErrorNoSpace(s);
  1496. X      MONITOR_REAL_NEW("Make  (GetStore)",storage,s)
  1497. X      BlockCopy(storage, store, s);
  1498. X      if (tag>1) { REPORT tag--; }
  1499. X      else if (tag < -1) { REPORT store=0; delete this; } // borrowed store
  1500. X      else { REPORT }
  1501. X      return s;
  1502. X   }
  1503. X   Real* s=store; store=0;
  1504. X   if (tag==0) { REPORT delete this; }
  1505. X   else { REPORT tag=-1; }
  1506. X   return s;
  1507. X}
  1508. X
  1509. X/*
  1510. X#ifndef __ZTC__
  1511. Xvoid GeneralMatrix::GetMatrixC(const GeneralMatrix* gmx)
  1512. X{
  1513. X   REPORT tag=-1;
  1514. X   nrows=gmx->nrows; ncols=gmx->ncols; storage=gmx->storage;
  1515. X   SetParameters(gmx); 
  1516. X   store = new Real [storage]; MatrixErrorNoSpace(store);
  1517. X   MONITOR_REAL_NEW("Make (GetMatrix)",storage,store)
  1518. X   BlockCopy(storage, gmx->store, store);
  1519. X}
  1520. X#endif
  1521. X*/
  1522. X
  1523. Xvoid GeneralMatrix::GetMatrix(const GeneralMatrix* gmx)
  1524. X{
  1525. X   REPORT  tag=-1; nrows=gmx->Nrows(); ncols=gmx->Ncols();
  1526. X   storage=gmx->storage; SetParameters(gmx);
  1527. X   store=((GeneralMatrix*)gmx)->GetStore();
  1528. X}
  1529. X
  1530. XGeneralMatrix* GeneralMatrix::BorrowStore(GeneralMatrix* gmx, MatrixType mt)
  1531. X// Copy storage of *this to storage of *gmx. Then convert to type mt.
  1532. X// If mt == 0 just let *gm point to storage of *this if tag<0.
  1533. X{
  1534. X   if (!mt)
  1535. X   {
  1536. X      if (tag == -1) { REPORT gmx->tag = -2; gmx->store = store; }
  1537. X      else { REPORT gmx->tag = 0; gmx->store = GetStore(); }
  1538. X   }
  1539. X   else if (mt!=gmx->Type())
  1540. X   {
  1541. X      REPORT gmx->tag = -2; gmx->store = store;
  1542. X      gmx = gmx->Evaluate(mt); gmx->tag = 0; tDelete();
  1543. X   }
  1544. X   else { REPORT  gmx->tag = 0; gmx->store = GetStore(); }
  1545. X
  1546. X   return gmx;
  1547. X}
  1548. X
  1549. Xvoid GeneralMatrix::Eq(const BaseMatrix& X, MatrixType mt)
  1550. X// Count number of references to this in X.
  1551. X// If zero delete storage in X;
  1552. X// otherwise tag X to show when storage can be deleted
  1553. X// evaluate X and copy to current object
  1554. X{
  1555. X   int counter=X.search(this);
  1556. X   if (counter==0)
  1557. X   {
  1558. X      REPORT
  1559. X      if (store)
  1560. X      {
  1561. X         MONITOR_REAL_DELETE("Free (operator=)",storage,store)
  1562. X#ifdef Version21
  1563. X         REPORT delete [] store; storage=0;
  1564. X#else
  1565. X         REPORT delete [storage] store; storage=0;
  1566. X#endif
  1567. X      }
  1568. X   }
  1569. X   else { REPORT Release(counter); }
  1570. X   GeneralMatrix* gmx = ((BaseMatrix&)X).Evaluate(mt);
  1571. X   if (gmx!=this) { REPORT GetMatrix(gmx); }
  1572. X   else { REPORT }
  1573. X   Protect();
  1574. X}
  1575. X
  1576. Xvoid GeneralMatrix::Inject(const GeneralMatrix& X)
  1577. X// copy stored values of X; otherwise leave els of *this unchanged
  1578. X{
  1579. X   REPORT
  1580. X   Tracer tr("Inject");
  1581. X   if (nrows != X.nrows || ncols != X.ncols)
  1582. X      Throw(IncompatibleDimensionsException());
  1583. X   MatrixRow mr((GeneralMatrix*)&X, LoadOnEntry);
  1584. X   MatrixRow mrx(this, LoadOnEntry+StoreOnExit+DirectPart);
  1585. X   int i=nrows;
  1586. X   while (i--) { mrx.Inject(mr); mrx.Next(); mr.Next(); }
  1587. X}  
  1588. X
  1589. Xvoid GeneralMatrix::CheckConversion(const BaseMatrix& M)
  1590. X{
  1591. X   if (!(this->Type() >= M.Type()))
  1592. X      Throw(ProgramException("Illegal Conversion"));
  1593. X}
  1594. X
  1595. X
  1596. X/************************* nricMatrix routines *****************************/
  1597. X
  1598. Xvoid nricMatrix::MakeRowPointer()
  1599. X{
  1600. X   row_pointer = new Real* [nrows]; MatrixErrorNoSpace(row_pointer);
  1601. X   Real* s = Store() - 1; int i = nrows; Real** rp = row_pointer;
  1602. X   while (i--) { *rp++ = s; s+=ncols; }
  1603. X}
  1604. X
  1605. Xvoid nricMatrix::DeleteRowPointer()
  1606. X#ifdef Version21
  1607. X{ if (nrows) delete [] row_pointer; }
  1608. X#else
  1609. X{ if (nrows) delete [nrows] row_pointer; }
  1610. X#endif
  1611. X
  1612. Xvoid GeneralMatrix::CheckStore() const
  1613. X{
  1614. X   if (!store) 
  1615. X      Throw(ProgramException("NRIC accessing matrix with unset dimensions"));
  1616. X}
  1617. X
  1618. X
  1619. X/***************************** CleanUp routines *****************************/
  1620. X
  1621. Xvoid GeneralMatrix::CleanUp()
  1622. X{
  1623. X   // set matrix dimensions to zero, delete storage
  1624. X   REPORT
  1625. X   if (store && storage)
  1626. X   {
  1627. X      MONITOR_REAL_DELETE("Free (CleanUp)    ",storage,store)
  1628. X#ifdef Version21
  1629. X         REPORT delete [] store;
  1630. X#else
  1631. X         REPORT delete [storage] store;
  1632. X#endif
  1633. X   }
  1634. X   store=0; storage=0; nrows=0; ncols=0;
  1635. X}
  1636. X
  1637. Xvoid nricMatrix::CleanUp()
  1638. X{ DeleteRowPointer(); GeneralMatrix::CleanUp(); }
  1639. X
  1640. Xvoid RowVector::CleanUp()
  1641. X{ GeneralMatrix::CleanUp(); nrows=1; }
  1642. X
  1643. Xvoid ColumnVector::CleanUp()
  1644. X{ GeneralMatrix::CleanUp(); ncols=1; }
  1645. X
  1646. Xvoid CroutMatrix::CleanUp()
  1647. X{ 
  1648. X#ifdef Version21
  1649. X   if (nrows) delete [] indx;
  1650. X#else
  1651. X   if (nrows) delete [nrows] indx;
  1652. X#endif
  1653. X   GeneralMatrix::CleanUp();
  1654. X}
  1655. X
  1656. Xvoid BandLUMatrix::CleanUp()
  1657. X{ 
  1658. X#ifdef Version21
  1659. X   if (nrows) delete [] indx;
  1660. X   if (storage2) delete [] store2;
  1661. X#else
  1662. X   if (nrows) delete [nrows] indx;
  1663. X   if (storage2) delete [storage2] store2;
  1664. X#endif
  1665. X   GeneralMatrix::CleanUp();
  1666. X}
  1667. X
  1668. X
  1669. END_OF_FILE
  1670. if test 16770 -ne `wc -c <'newmat4.cxx'`; then
  1671.     echo shar: \"'newmat4.cxx'\" unpacked with wrong size!
  1672. fi
  1673. # end of 'newmat4.cxx'
  1674. fi
  1675. if test -f 'newmatrm.cxx' -a "${1}" != "-c" ; then 
  1676.   echo shar: Will not clobber existing file \"'newmatrm.cxx'\"
  1677. else
  1678. echo shar: Extracting \"'newmatrm.cxx'\" \(3386 characters\)
  1679. sed "s/^X//" >'newmatrm.cxx' <<'END_OF_FILE'
  1680. X//$$newmatrm.cxx                         rectangular matrix operations
  1681. X
  1682. X// Copyright (C) 1991,2: R B Davies
  1683. X
  1684. X#include "include.h"
  1685. X
  1686. X#include "newmat.h"
  1687. X#include "newmatrm.h"
  1688. X
  1689. X
  1690. X// operations on rectangular matrices
  1691. X
  1692. X
  1693. Xvoid RectMatrixRow::Reset (const Matrix& M, int row, int skip, int length)
  1694. X{
  1695. X   RectMatrixRowCol::Reset
  1696. X      ( M.Store()+row*M.Ncols()+skip, length, 1, M.Ncols() );
  1697. X}
  1698. X
  1699. Xvoid RectMatrixRow::Reset (const Matrix& M, int row)
  1700. X{
  1701. X   RectMatrixRowCol::Reset( M.Store()+row*M.Ncols(), M.Ncols(), 1, M.Ncols() );
  1702. X}
  1703. X
  1704. Xvoid RectMatrixCol::Reset (const Matrix& M, int skip, int col, int length)
  1705. X{
  1706. X   RectMatrixRowCol::Reset
  1707. X      ( M.Store()+col+skip*M.Ncols(), length, M.Ncols(), 1 );
  1708. X}
  1709. X
  1710. Xvoid RectMatrixCol::Reset (const Matrix& M, int col)
  1711. X   { RectMatrixRowCol::Reset( M.Store()+col, M.Nrows(), M.Ncols(), 1 ); }
  1712. X
  1713. X
  1714. XReal RectMatrixRowCol::SumSquare() const
  1715. X{
  1716. X   long_Real sum = 0.0; int i = n; Real* s = store; int d = spacing;
  1717. X   while (i--) { sum += (long_Real)*s * *s; s += d; }
  1718. X   return (Real)sum;
  1719. X}
  1720. X
  1721. XReal RectMatrixRowCol::operator*(const RectMatrixRowCol& rmrc) const
  1722. X{
  1723. X   long_Real sum = 0.0; int i = n;
  1724. X   Real* s = store; int d = spacing;
  1725. X   Real* s1 = rmrc.store; int d1 = rmrc.spacing;
  1726. X   if (i!=rmrc.n)
  1727. X   {
  1728. X      Tracer tr("newmatrm");
  1729. X      Throw(InternalException("Dimensions differ in *"));
  1730. X   }
  1731. X   while (i--) { sum += (long_Real)*s * *s1; s += d; s1 += d1; }
  1732. X   return (Real)sum;
  1733. X}
  1734. X
  1735. Xvoid RectMatrixRowCol::AddScaled(const RectMatrixRowCol& rmrc, Real r)
  1736. X{
  1737. X   int i = n; Real* s = store; int d = spacing;
  1738. X   Real* s1 = rmrc.store; int d1 = rmrc.spacing;
  1739. X   if (i!=rmrc.n)
  1740. X   {
  1741. X      Tracer tr("newmatrm");
  1742. X      Throw(InternalException("Dimensions differ in AddScaled"));
  1743. X   }
  1744. X   while (i--) { *s += *s1 * r; s += d; s1 += d1; }
  1745. X}
  1746. X
  1747. Xvoid RectMatrixRowCol::Divide(const RectMatrixRowCol& rmrc, Real r)
  1748. X{
  1749. X   int i = n; Real* s = store; int d = spacing;
  1750. X   Real* s1 = rmrc.store; int d1 = rmrc.spacing;
  1751. X   if (i!=rmrc.n)
  1752. X   {
  1753. X      Tracer tr("newmatrm");
  1754. X      Throw(InternalException("Dimensions differ in Divide"));
  1755. X   }
  1756. X   while (i--) { *s = *s1 / r; s += d; s1 += d1; }
  1757. X}
  1758. X
  1759. Xvoid RectMatrixRowCol::Divide(Real r)
  1760. X{
  1761. X   int i = n; Real* s = store; int d = spacing;
  1762. X   while (i--) { *s /= r; s += d; }
  1763. X}
  1764. X
  1765. Xvoid RectMatrixRowCol::Negate()
  1766. X{
  1767. X   int i = n; Real* s = store; int d = spacing;
  1768. X   while (i--) { *s = - *s; s += d; }
  1769. X}
  1770. X
  1771. Xvoid RectMatrixRowCol::Zero()
  1772. X{
  1773. X   int i = n; Real* s = store; int d = spacing;
  1774. X   while (i--) { *s = 0.0; s += d; }
  1775. X}
  1776. X
  1777. Xvoid ComplexScale(RectMatrixCol& U, RectMatrixCol& V, Real x, Real y)
  1778. X{
  1779. X   int n = U.n;
  1780. X   if (n != V.n)
  1781. X   {
  1782. X      Tracer tr("newmatrm");
  1783. X      Throw(InternalException("Dimensions differ in ComplexScale"));
  1784. X   }
  1785. X   Real* u = U.store; Real* v = V.store; 
  1786. X   int su = U.spacing; int sv = V.spacing;
  1787. X   while (n--)
  1788. X   {
  1789. X      Real z = *u * x - *v * y;  *v =  *u * y + *v * x;  *u = z;
  1790. X      u += su;  v += sv;
  1791. X   }
  1792. X}
  1793. X
  1794. Xvoid Rotate(RectMatrixCol& U, RectMatrixCol& V, Real tau, Real s)
  1795. X{
  1796. X   //  (U, V) = (U, V) * (c, s)  where  tau = s/(1+c), c^2 + s^2 = 1
  1797. X   int n = U.n;
  1798. X   if (n != V.n)
  1799. X   {
  1800. X      Tracer tr("newmatrm");
  1801. X      Throw(InternalException("Dimensions differ in Rotate"));
  1802. X   }
  1803. X   Real* u = U.store; Real* v = V.store; 
  1804. X   int su = U.spacing; int sv = V.spacing;
  1805. X   while (n--)
  1806. X   {
  1807. X      Real zu = *u; Real zv = *v;
  1808. X      *u -= s * (zv + zu * tau); *v += s * (zu - zv * tau);
  1809. X      u += su;  v += sv;
  1810. X   }
  1811. X}
  1812. X
  1813. X
  1814. END_OF_FILE
  1815. if test 3386 -ne `wc -c <'newmatrm.cxx'`; then
  1816.     echo shar: \"'newmatrm.cxx'\" unpacked with wrong size!
  1817. fi
  1818. # end of 'newmatrm.cxx'
  1819. fi
  1820. echo shar: End of archive 4 \(of 7\).
  1821. cp /dev/null ark4isdone
  1822. MISSING=""
  1823. for I in 1 2 3 4 5 6 7 ; do
  1824.     if test ! -f ark${I}isdone ; then
  1825.     MISSING="${MISSING} ${I}"
  1826.     fi
  1827. done
  1828. if test "${MISSING}" = "" ; then
  1829.     echo You have unpacked all 7 archives.
  1830.     rm -f ark[1-9]isdone
  1831. else
  1832.     echo You still need to unpack the following archives:
  1833.     echo "        " ${MISSING}
  1834. fi
  1835. ##  End of shell archive.
  1836. exit 0
  1837.  
  1838. exit 0 # Just in case...
  1839.