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

  1. Newsgroups: comp.sources.misc
  2. From: robertd@kauri.vuw.ac.nz (Robert Davies)
  3. Subject:  v34i012:  newmat06 - A matrix package in C++, Part06/07
  4. Message-ID: <1992Dec6.045758.4573@sparky.imd.sterling.com>
  5. X-Md4-Signature: 84add1245459c3d674e91bc77ec7e3d0
  6. Date: Sun, 6 Dec 1992 04:57:58 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 12
  11. Archive-name: newmat06/part06
  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 6 (of 7)."
  22. # Contents:  bandmat.cxx cholesky.cxx evalue.cxx except.cxx fft.cxx
  23. #   hholder.cxx jacobi.cxx sort.cxx submat.cxx svd.cxx
  24. # Wrapped by robert@kea on Thu Dec  3 22:37:37 1992
  25. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  26. if test -f 'bandmat.cxx' -a "${1}" != "-c" ; then 
  27.   echo shar: Will not clobber existing file \"'bandmat.cxx'\"
  28. else
  29. echo shar: Extracting \"'bandmat.cxx'\" \(10052 characters\)
  30. sed "s/^X//" >'bandmat.cxx' <<'END_OF_FILE'
  31. X//$$ bandmat.cxx                     Band matrix definitions
  32. X
  33. X// Copyright (C) 1991,2: R B Davies
  34. X
  35. X#define WANT_MATH                    // include.h will get math fns
  36. X
  37. X#include "include.h"
  38. X
  39. X#include "newmat.h"
  40. X#include "newmatrc.h"
  41. X
  42. X//#define REPORT { static ExeCounter ExeCount(__LINE__,4); ++ExeCount; }
  43. X
  44. X#define REPORT {}
  45. X
  46. X//#define REPORT1 { static ExeCounter ExeCount(__LINE__,4); ExeCount++; }
  47. X
  48. X// REPORT1 constructors only - doesn't work in turbo and Borland C++
  49. X
  50. X#define REPORT1 {}
  51. X
  52. X//#define MONITOR(what,storage,store) \
  53. X//   { cout << what << " " << storage << " at " << (long)store << "\n"; }
  54. X
  55. X#define MONITOR(what,store,storage) {}
  56. X
  57. XBandMatrix::BandMatrix(const BaseMatrix& M)
  58. X{
  59. X   REPORT1 CheckConversion(M);
  60. X   GeneralMatrix* gmx=((BaseMatrix&)M).Evaluate(MatrixType::BM);
  61. X   GetMatrix(gmx); CornerClear();
  62. X}
  63. X
  64. Xvoid BandMatrix::SetParameters(const GeneralMatrix* gmx)
  65. X{
  66. X   MatrixBandWidth bw = gmx->BandWidth();
  67. X   lower = bw.lower; upper = bw.upper;
  68. X}
  69. X
  70. Xvoid BandMatrix::ReDimension(int n, int lb, int ub)
  71. X{
  72. X   REPORT
  73. X   Tracer tr("BandMatrix::ReDimension");
  74. X   if (lb<0 || ub<0) Throw(ProgramException("Undefined bandwidth"));
  75. X   lower = (lb<=n) ? lb : n-1; upper = (ub<=n) ? ub : n-1;
  76. X   GeneralMatrix::ReDimension(n,n,n*(lower+1+upper)); CornerClear();
  77. X}
  78. X
  79. Xvoid BandMatrix::operator=(const BaseMatrix& X)
  80. X{ REPORT CheckConversion(X); Eq(X,MatrixType::BM); CornerClear(); }
  81. X
  82. Xvoid BandMatrix::CornerClear() const
  83. X{
  84. X   // set unused parts of BandMatrix to zero
  85. X   REPORT
  86. X   int i = lower; Real* s = store; int bw = lower + 1 + upper;
  87. X   while (i)
  88. X      { int j = i--; Real* sj = s; s += bw; while (j--) *sj++ = 0.0; }
  89. X   i = upper; s = store + storage;
  90. X   while (i)
  91. X      { int j = i--; Real* sj = s; s -= bw; while (j--) *(--sj) = 0.0; }
  92. X}
  93. X
  94. XMatrixBandWidth MatrixBandWidth::operator+(const MatrixBandWidth& bw) const
  95. X{
  96. X   int l = bw.lower; int u = bw.upper;
  97. X   l = (lower < 0 || l < 0) ? -1 : (lower > l) ? lower : l;
  98. X   u = (upper < 0 || u < 0) ? -1 : (upper > u) ? upper : u;
  99. X   return MatrixBandWidth(l,u);
  100. X}
  101. X
  102. XMatrixBandWidth MatrixBandWidth::operator*(const MatrixBandWidth& bw) const
  103. X{
  104. X   int l = bw.lower; int u = bw.upper;
  105. X   l = (lower < 0 || l < 0) ? -1 : lower+l;
  106. X   u = (upper < 0 || u < 0) ? -1 : upper+u;
  107. X   return MatrixBandWidth(l,u);
  108. X}
  109. X
  110. XUpperBandMatrix::UpperBandMatrix(const BaseMatrix& M)
  111. X{
  112. X   REPORT1 CheckConversion(M);
  113. X   GeneralMatrix* gmx=((BaseMatrix&)M).Evaluate(MatrixType::UB);
  114. X   GetMatrix(gmx); CornerClear();
  115. X}
  116. X
  117. Xvoid UpperBandMatrix::operator=(const BaseMatrix& X)
  118. X{ REPORT CheckConversion(X); Eq(X,MatrixType::UB); CornerClear(); }
  119. X
  120. XLowerBandMatrix::LowerBandMatrix(const BaseMatrix& M)
  121. X{
  122. X   REPORT1 CheckConversion(M);
  123. X   GeneralMatrix* gmx=((BaseMatrix&)M).Evaluate(MatrixType::LB);
  124. X   GetMatrix(gmx); CornerClear();
  125. X}
  126. X
  127. Xvoid LowerBandMatrix::operator=(const BaseMatrix& X)
  128. X{ REPORT CheckConversion(X); Eq(X,MatrixType::LB); CornerClear(); }
  129. X
  130. XBandLUMatrix::BandLUMatrix(const BaseMatrix& m)
  131. X{
  132. X   REPORT1
  133. X   Tracer tr("BandLUMatrix");
  134. X   GeneralMatrix* gm = ((BaseMatrix&)m).Evaluate(MatrixType::BM);
  135. X   GetMatrix(gm);
  136. X   m1 = ((BandMatrix*)gm)->lower; m2 = ((BandMatrix*)gm)->upper;
  137. X   if (nrows!=ncols) Throw(NotSquareException(*this));
  138. X   d = TRUE; sing = FALSE;
  139. X   indx = new int [nrows]; MatrixErrorNoSpace(indx);
  140. X   MONITOR_INT_NEW("Index (BndLUMat)",nrows,indx)
  141. X   storage2 = nrows * m1;
  142. X   store2 = new Real [storage2]; MatrixErrorNoSpace(store2);
  143. X   MONITOR_REAL_NEW("Make (BandLUMat)",storage2,store2)
  144. X   ludcmp();
  145. X}
  146. X
  147. XBandLUMatrix::~BandLUMatrix()
  148. X{
  149. X   MONITOR_INT_DELETE("Index (BndLUMat)",nrows,indx)
  150. X   MONITOR_REAL_DELETE("Delete (BndLUMt)",storage2,store2)
  151. X#ifdef Version21
  152. X   delete [] indx; delete [] store2;
  153. X#else
  154. X   delete [nrows] indx; delete [storage2] store2;
  155. X#endif
  156. X}
  157. X
  158. XMatrixType BandLUMatrix::Type() const { return MatrixType::BC; }
  159. X
  160. X
  161. XLogAndSign BandLUMatrix::LogDeterminant() const
  162. X{
  163. X   if (sing) return 0.0;
  164. X   Real* a = store; int w = m1+1+m2; LogAndSign sum; int i = nrows;
  165. X   while (i--) { sum *= *a; a += w; }
  166. X   if (!d) sum.ChangeSign(); return sum;
  167. X}
  168. X
  169. XGeneralMatrix* BandMatrix::MakeSolver()
  170. X{
  171. X   REPORT
  172. X   GeneralMatrix* gm = new BandLUMatrix(*this);
  173. X   MatrixErrorNoSpace(gm); gm->ReleaseAndDelete(); return gm;
  174. X}
  175. X
  176. X
  177. Xvoid BandLUMatrix::ludcmp()
  178. X{
  179. X   REPORT
  180. X   Real* a = store;
  181. X   int i = m1; int j = m2; int k; int n = nrows; int w = m1 + 1 + m2;
  182. X   while (i)
  183. X   {
  184. X      Real* ai = a + i;
  185. X      k = ++j; while (k--) *a++ = *ai++;
  186. X      k = i--; while (k--) *a++ = 0.0;
  187. X   }
  188. X
  189. X   a = store; int l = m1;
  190. X   for (k=0; k<n; k++)
  191. X   {
  192. X      Real x = *a; i = k; Real* aj = a;
  193. X      if (l < n) l++;
  194. X      for (j=k+1; j<l; j++)
  195. X         { aj += w; if (fabs(x) < fabs(*aj)) { x = *aj; i = j; } }
  196. X      indx[k] = i;
  197. X      if (x==0) { sing = TRUE; return; }
  198. X      if (i!=k)
  199. X      {
  200. X         d = !d; Real* ak = a; Real* ai = store + i * w; j = w;
  201. X         while (j--) { x = *ak; *ak++ = *ai; *ai++ = x; }
  202. X      }
  203. X      aj = a + w; Real* m = store2 + m1 * k;
  204. X      for (j=k+1; j<l; j++)
  205. X      {
  206. X         *m++ = x = *aj / *a; i = w; Real* ak = a;
  207. X     while (--i) { Real* aj1 = aj++; *aj1 = *aj - x * *(++ak); }
  208. X         *aj++ = 0.0;
  209. X      }
  210. X      a += w;
  211. X   }
  212. X}
  213. X
  214. Xvoid BandLUMatrix::lubksb(Real* B, int mini)
  215. X{
  216. X   REPORT
  217. X   Tracer tr("BandLUMatrix::lubksb");
  218. X   if (sing) Throw(SingularException(*this));
  219. X   int n = nrows; int l = m1; int w = m1 + 1 + m2;
  220. X
  221. X   for (int k=0; k<n; k++)
  222. X   {
  223. X      int i = indx[k];
  224. X      if (i!=k) { Real x=B[k]; B[k]=B[i]; B[i]=x; }
  225. X      if (l<n) l++;
  226. X      Real* m = store2 + k*m1; Real* b = B+k; Real* bi = b;
  227. X      for (i=k+1; i<l; i++)  *(++bi) -= *m++ * *b;
  228. X   }
  229. X
  230. X   l = -m1;
  231. X   for (int i = n-1; i>=mini; i--)
  232. X   {
  233. X      Real* b = B + i; Real* bk = b; Real x = *bk;
  234. X      Real* a = store + w*i; Real y = *a;
  235. X      int k = l+m1; while (k--) x -=  *(++a) * *(++bk);
  236. X      *b = x / y;
  237. X      if (l < m2) l++;
  238. X   }
  239. X}
  240. X
  241. Xvoid BandLUMatrix::Solver(MatrixRowCol& mcout, const MatrixRowCol& mcin)
  242. X{
  243. X   REPORT
  244. X   Real* el = mcin.store; int i = mcin.skip;
  245. X   while (i--) *el++ = 0.0;
  246. X   el += mcin.storage; i = nrows - mcin.skip - mcin.storage;
  247. X   while (i--) *el++ = 0.0;
  248. X   lubksb(mcin.store, mcout.skip);
  249. X}
  250. X
  251. X// Do we need check for entirely zero output?
  252. X
  253. X
  254. Xvoid UpperBandMatrix::Solver(MatrixRowCol& mcout,
  255. X   const MatrixRowCol& mcin)
  256. X{
  257. X   REPORT
  258. X   Real* elx = mcin.store+mcout.skip; int i = mcin.skip-mcout.skip;
  259. X   while (i-- > 0) *elx++ = 0.0;
  260. X   int nr = mcin.skip+mcin.storage; elx = mcin.store+nr; Real* el = elx;
  261. X   int j = mcout.skip+mcout.storage-nr; i = nr-mcout.skip;
  262. X   while (j-- > 0) *elx++ = 0.0;
  263. X
  264. X   Real* Ael = store + (upper+1)*(i-1)+1; j = 0;
  265. X   while (i-- > 0)
  266. X   {
  267. X      elx = el; Real sum = 0.0; int jx = j;
  268. X      while (jx--) sum += *(--Ael) * *(--elx);
  269. X      elx--; *elx = (*elx - sum) / *(--Ael);
  270. X      if (j<upper) Ael -= upper - (++j); else el--;
  271. X   }
  272. X}
  273. X
  274. Xvoid LowerBandMatrix::Solver(MatrixRowCol& mcout,
  275. X   const MatrixRowCol& mcin)
  276. X{
  277. X   REPORT
  278. X   Real* elx = mcin.store+mcout.skip; int i = mcin.skip-mcout.skip;
  279. X   while (i-- > 0) *elx++ = 0.0;
  280. X   int nc = mcin.skip; i = nc+mcin.storage; elx = mcin.store+i;
  281. X   int nr = mcout.skip+mcout.storage; int j = nr-i; i = nr-nc;
  282. X   while (j-- > 0) *elx++ = 0.0;
  283. X
  284. X   Real* el = mcin.store+nc; Real* Ael = store + (lower+1)*nc + lower; j = 0;
  285. X   while (i-- > 0)
  286. X   {
  287. X      elx = el; Real sum = 0.0; int jx = j;
  288. X      while (jx--) sum += *Ael++ * *elx++;
  289. X      *elx = (*elx - sum) / *Ael++;
  290. X      if (j<lower) Ael += lower - (++j); else el++;
  291. X   }
  292. X}
  293. X
  294. X
  295. XLogAndSign BandMatrix::LogDeterminant() const
  296. X{
  297. X   REPORT
  298. X   BandLUMatrix C(*this); return C.LogDeterminant();
  299. X}
  300. X
  301. XLogAndSign LowerBandMatrix::LogDeterminant() const
  302. X{
  303. X   REPORT
  304. X   int i = nrows; LogAndSign sum; Real* s = store + lower; int j = lower + 1;
  305. X   while (i--) { sum *= *s; s += j; }
  306. X   ((GeneralMatrix&)*this).tDelete(); return sum;
  307. X}
  308. X
  309. XLogAndSign UpperBandMatrix::LogDeterminant() const
  310. X{
  311. X   REPORT
  312. X   int i = nrows; LogAndSign sum; Real* s = store; int j = upper + 1;
  313. X   while (i--) { sum *= *s; s += j; }
  314. X   ((GeneralMatrix&)*this).tDelete(); return sum;
  315. X}
  316. X
  317. XGeneralMatrix* SymmetricBandMatrix::MakeSolver()
  318. X{
  319. X   REPORT
  320. X   GeneralMatrix* gm = new BandLUMatrix(*this);
  321. X   MatrixErrorNoSpace(gm); gm->ReleaseAndDelete(); return gm;
  322. X}
  323. X
  324. XSymmetricBandMatrix::SymmetricBandMatrix(const BaseMatrix& M)
  325. X{
  326. X   REPORT1 CheckConversion(M);
  327. X   GeneralMatrix* gmx=((BaseMatrix&)M).Evaluate(MatrixType::SB);
  328. X   GetMatrix(gmx);
  329. X}
  330. X
  331. XGeneralMatrix* SymmetricBandMatrix::Transpose(TransposedMatrix*, MatrixType mt)
  332. X{ REPORT  return Evaluate(mt); }
  333. X
  334. XLogAndSign SymmetricBandMatrix::LogDeterminant() const
  335. X{
  336. X   REPORT
  337. X   BandLUMatrix C(*this); return C.LogDeterminant();
  338. X}
  339. X
  340. Xvoid SymmetricBandMatrix::SetParameters(const GeneralMatrix* gmx)
  341. X{ lower = gmx->BandWidth().lower; }
  342. X
  343. Xvoid SymmetricBandMatrix::ReDimension(int n, int lb)
  344. X{
  345. X   REPORT
  346. X   Tracer tr("SymmetricBandMatrix::ReDimension");
  347. X   if (lb<0) Throw(ProgramException("Undefined bandwidth"));
  348. X   lower = (lb<=n) ? lb : n-1;
  349. X   GeneralMatrix::ReDimension(n,n,n*(lower+1));
  350. X}
  351. X
  352. Xvoid SymmetricBandMatrix::operator=(const BaseMatrix& X)
  353. X{ REPORT CheckConversion(X); Eq(X,MatrixType::SB); }
  354. X
  355. Xvoid SymmetricBandMatrix::CornerClear() const
  356. X{
  357. X   // set unused parts of BandMatrix to zero
  358. X   REPORT
  359. X   int i = lower; Real* s = store; int bw = lower + 1;
  360. X   while (i)
  361. X      { int j = i--; Real* sj = s; s += bw; while (j--) *sj++ = 0.0; }
  362. X}
  363. X
  364. XMatrixBandWidth SymmetricBandMatrix::BandWidth() const
  365. X   { return MatrixBandWidth(lower,lower); }
  366. X
  367. Xinline Real square(Real x) { return x*x; }
  368. X
  369. X
  370. XReal SymmetricBandMatrix::SumSquare() const
  371. X{
  372. X   REPORT
  373. X   CornerClear();
  374. X   Real sum1=0.0; Real sum2=0.0; Real* s=store; int i=nrows; int l=lower;
  375. X   while (i--)
  376. X      { int j = l; while (j--) sum2 += square(*s++); sum1 += square(*s++); }
  377. X   ((GeneralMatrix&)*this).tDelete(); return sum1 + 2.0 * sum2;
  378. X}
  379. X
  380. XReal SymmetricBandMatrix::SumAbsoluteValue() const
  381. X{
  382. X   REPORT
  383. X   CornerClear();
  384. X   Real sum1=0.0; Real sum2=0.0; Real* s=store; int i=nrows; int l=lower;
  385. X   while (i--)
  386. X      { int j = l; while (j--) sum2 += fabs(*s++); sum1 += fabs(*s++); }
  387. X   ((GeneralMatrix&)*this).tDelete(); return sum1 + 2.0 * sum2;
  388. X}
  389. X
  390. END_OF_FILE
  391. if test 10052 -ne `wc -c <'bandmat.cxx'`; then
  392.     echo shar: \"'bandmat.cxx'\" unpacked with wrong size!
  393. fi
  394. # end of 'bandmat.cxx'
  395. fi
  396. if test -f 'cholesky.cxx' -a "${1}" != "-c" ; then 
  397.   echo shar: Will not clobber existing file \"'cholesky.cxx'\"
  398. else
  399. echo shar: Extracting \"'cholesky.cxx'\" \(1781 characters\)
  400. sed "s/^X//" >'cholesky.cxx' <<'END_OF_FILE'
  401. X//$$ cholesky.cxx                     cholesky decomposition
  402. X
  403. X// Copyright (C) 1991,2: R B Davies
  404. X
  405. X#define WANT_MATH
  406. X
  407. X#include "include.h"
  408. X
  409. X#include "newmat.h"
  410. X
  411. X
  412. X/********* Cholesky decomposition of a positive definite matrix *************/
  413. X
  414. X// Suppose S is symmetrix and positive definite. Then there exists a unique
  415. X// lower triangular matrix L such that L L.t() = S;
  416. X
  417. Xstatic Real square(Real x) { return x*x; }
  418. X
  419. XReturnMatrix Cholesky(const SymmetricMatrix& S)
  420. X{
  421. X   Tracer trace("Cholesky");
  422. X   int nr = S.Nrows();
  423. X   LowerTriangularMatrix T(nr);
  424. X   Real* s = S.Store(); Real* t = T.Store(); Real* ti = t;
  425. X   for (int i=0; i<nr; i++)
  426. X   {
  427. X      Real* tj = t; Real sum; int k;
  428. X      for (int j=0; j<i; j++)
  429. X      {
  430. X     Real* tk = ti; sum = 0.0; k = j;
  431. X     while (k--) { sum += *tj++ * *tk++; }
  432. X     *tk = (*s++ - sum) / *tj++;
  433. X      }
  434. X      sum = 0.0; k = i;
  435. X      while (k--) { sum += square(*ti++); }
  436. X      Real d = *s++ - sum;
  437. X      if (d<=0.0) Throw(NPDException(S));
  438. X      *ti++ = sqrt(d);
  439. X   }
  440. X   T.Release(); return (ReturnMatrix)T;
  441. X}
  442. X
  443. XReturnMatrix Cholesky(const SymmetricBandMatrix& S)
  444. X{
  445. X   Tracer trace("Band-Cholesky");
  446. X   int nr = S.Nrows(); int m = S.lower;
  447. X   LowerBandMatrix T(nr,m);
  448. X   Real* s = S.Store(); Real* t = T.Store(); Real* ti = t;
  449. X
  450. X   for (int i=0; i<nr; i++)
  451. X   {
  452. X      Real* tj = t; Real sum; int l;
  453. X      if (i<m) { l = m-i; s += l; ti += l; l = i; }
  454. X      else { t += (m+1); l = m; }
  455. X
  456. X      for (int j=0; j<l; j++)
  457. X      {
  458. X     Real* tk = ti; sum = 0.0; int k = j; tj += (m-j);
  459. X     while (k--) { sum += *tj++ * *tk++; }
  460. X     *tk = (*s++ - sum) / *tj++;
  461. X      }
  462. X      sum = 0.0;
  463. X      while (l--) { sum += square(*ti++); }
  464. X      Real d = *s++ - sum;
  465. X      if (d<=0.0)  Throw(NPDException(S));
  466. X      *ti++ = sqrt(d);
  467. X   }
  468. X
  469. X   T.Release(); return (ReturnMatrix)T;
  470. X}
  471. X
  472. END_OF_FILE
  473. if test 1781 -ne `wc -c <'cholesky.cxx'`; then
  474.     echo shar: \"'cholesky.cxx'\" unpacked with wrong size!
  475. fi
  476. # end of 'cholesky.cxx'
  477. fi
  478. if test -f 'evalue.cxx' -a "${1}" != "-c" ; then 
  479.   echo shar: Will not clobber existing file \"'evalue.cxx'\"
  480. else
  481. echo shar: Extracting \"'evalue.cxx'\" \(7811 characters\)
  482. sed "s/^X//" >'evalue.cxx' <<'END_OF_FILE'
  483. X//$$evalue.cxx                           eigen-value decomposition
  484. X
  485. X// Copyright (C) 1991,2: R B Davies
  486. X
  487. X#define WANT_MATH
  488. X
  489. X#include "include.h"
  490. X#include "newmat.h"
  491. X#include "newmatrm.h"
  492. X#include "precisio.h"
  493. X
  494. X
  495. Xstatic void tred2(const SymmetricMatrix& A, DiagonalMatrix& D,
  496. X   DiagonalMatrix& E, Matrix& Z)
  497. X{
  498. X   Tracer et("Evalue(tred2)");
  499. X   Real tol =
  500. X      FloatingPointPrecision::Minimum()/FloatingPointPrecision::Epsilon();
  501. X   int n = A.Nrows(); Z.ReDimension(n,n); Z.Inject(A);
  502. X   D.ReDimension(n); E.ReDimension(n);
  503. X   Real* z = Z.Store();
  504. X
  505. X   for (int i=n-1; i > 0; i--)                   // i=0 is excluded
  506. X   {
  507. X      Real f = Z.element(i,i-1); Real g = 0.0;
  508. X      int k = i-1; Real* zik = z + i*n;
  509. X      while (k--) g += square(*zik++);
  510. X      Real h = g + square(f);
  511. X      if (g <= tol) { E.element(i) = f; h = 0.0; }
  512. X      else
  513. X      {
  514. X     g = sign(-sqrt(h), f); E.element(i) = g; h -= f*g;
  515. X     Z.element(i,i-1) = f-g; f = 0.0;
  516. X         Real* zji = z + i; Real* zij = z + i*n; Real* ej = E.Store();
  517. X     for (int j=0; j<i; j++)
  518. X     {
  519. X        *zji = (*zij++)/h; g = 0.0;
  520. X            Real* zjk = z + j*n; zik = z + i*n;
  521. X            k = j; while (k--) g += *zjk++ * (*zik++);
  522. X            k = i-j; while (k--) { g += *zjk * (*zik++); zjk += n; }
  523. X        *ej++ = g/h; f += g * (*zji); zji += n;
  524. X     }
  525. X     Real hh = f / (h + h); zij = z + i*n; ej = E.Store();
  526. X     for (j=0; j<i; j++)
  527. X     {
  528. X        f = *zij++; g = *ej - hh * f; *ej++ = g;
  529. X            Real* zjk = z + j*n; Real* zik = z + i*n;
  530. X            Real* ek = E.Store(); k = j+1;
  531. X            while (k--)  *zjk++ -= ( f*(*ek++) + g*(*zik++) ); 
  532. X     }
  533. X      }
  534. X      D.element(i) = h;
  535. X   }
  536. X
  537. X   D.element(0) = 0.0; E.element(0) = 0.0;
  538. X   for (i=0; i<n; i++)
  539. X   {
  540. X      if (D.element(i) != 0.0)
  541. X      {
  542. X     for (int j=0; j<i; j++)
  543. X     {
  544. X        Real g = 0.0;
  545. X            Real* zik = z + i*n; Real* zkj = z + j;
  546. X            int k = i; while (k--) { g += *zik++ * (*zkj); zkj += n; }
  547. X            Real* zki = z + i; zkj = z + j;
  548. X            k = i; while (k--) { *zkj -= g * (*zki); zkj += n; zki += n; }
  549. X     }
  550. X      }
  551. X      Real* zij = z + i*n; Real* zji = z + i;
  552. X      int j = i; while (j--)  { *zij++ = 0.0; *zji = 0.0; zji += n; }
  553. X      D.element(i) = *zij; *zij = 1.0;
  554. X   }
  555. X}
  556. X
  557. Xstatic void tql2(DiagonalMatrix& D, DiagonalMatrix& E, Matrix& Z)
  558. X{
  559. X   Tracer et("Evalue(tql2)");
  560. X   Real eps = FloatingPointPrecision::Epsilon();
  561. X   int n = D.Nrows(); Real* z = Z.Store();
  562. X   for (int l=1; l<n; l++) E.element(l-1) = E.element(l);
  563. X   Real b = 0.0; Real f = 0.0; E.element(n-1) = 0.0;
  564. X   for (l=0; l<n; l++)
  565. X   {
  566. X      int i,j;
  567. X      Real& dl = D.element(l); Real& el = E.element(l);
  568. X      Real h = eps * ( fabs(dl) + fabs(el) );
  569. X      if (b < h) b = h;
  570. X      for (int m=l; m<n; m++) if (fabs(E.element(m)) <= b) break;
  571. X      Boolean test = FALSE;
  572. X      for (j=0; j<30; j++)
  573. X      {
  574. X     if (m==l) { test = TRUE; break; }
  575. X     Real& dl1 = D.element(l+1);
  576. X     Real g = dl; Real p = (dl1-g) / (2.0*el); Real r = sqrt(p*p + 1.0);
  577. X     dl = el / (p < 0.0 ? p-r : p+r); Real h = g - dl; f += h;
  578. X     Real* dlx = &dl1; i = n-l-1; while (i--) *dlx++ -= h;
  579. X
  580. X     p = D.element(m); Real c = 1.0; Real s = 0.0;
  581. X     for (i=m-1; i>=l; i--)
  582. X     {
  583. X        Real ei = E.element(i); Real di = D.element(i);
  584. X        Real& ei1 = E.element(i+1);
  585. X        g = c * ei; h = c * p;
  586. X        if ( fabs(p) >= fabs(ei))
  587. X        {
  588. X           c = ei / p; r = sqrt(c*c + 1.0);
  589. X           ei1 = s*p*r; s = c/r; c = 1.0/r;
  590. X        }
  591. X        else
  592. X        {
  593. X           c = p / ei; r = sqrt(c*c + 1.0);
  594. X           ei1 = s * ei * r; s = 1.0/r; c /= r;
  595. X        }
  596. X        p = c * di - s*g; D.element(i+1) = h + s * (c*g + s*di);
  597. X
  598. X        Real* zki = z + i; Real* zki1 = zki + 1; int k = n;
  599. X        while (k--)
  600. X        {
  601. X           h = *zki1; *zki1 = s*(*zki) + c*h; *zki = c*(*zki) - s*h;
  602. X           zki += n; zki1 += n;
  603. X        }
  604. X     }
  605. X     el = s*p; dl = c*p;
  606. X     if (fabs(el) <= b) { test = TRUE; break; }
  607. X      }
  608. X      if (!test) Throw ( ConvergenceException(D) );
  609. X      dl += f;
  610. X   }
  611. X
  612. X   for (int i=0; i<n; i++)
  613. X   {
  614. X      int k = i; Real p = D.element(i);
  615. X      for (int j=i+1; j<n; j++)
  616. X         { if (D.element(j) < p) { k = j; p = D.element(j); } }
  617. X      if (k != i)
  618. X      {
  619. X         D.element(k) = D.element(i); D.element(i) = p; int j = n;
  620. X     Real* zji = z + i; Real* zjk = z + k;
  621. X         while (j--) { p = *zji; *zji = *zjk; *zjk = p; zji += n; zjk += n; }
  622. X      }
  623. X   }
  624. X
  625. X}
  626. X
  627. Xstatic void tred3(const SymmetricMatrix& X, DiagonalMatrix& D,
  628. X   DiagonalMatrix& E, SymmetricMatrix& A)
  629. X{
  630. X   Tracer et("Evalue(tred3)");
  631. X   Real tol =
  632. X      FloatingPointPrecision::Minimum()/FloatingPointPrecision::Epsilon();
  633. X   int n = X.Nrows(); A = X; D.ReDimension(n); E.ReDimension(n);
  634. X   Real* ei = E.Store() + n;
  635. X   for (int i = n-1; i >= 0; i--)
  636. X   {
  637. X      Real h = 0.0; Real f;
  638. X      Real* d = D.Store(); Real* a = A.Store() + (i*(i+1))/2; int k = i;
  639. X      while (k--) { f = *a++; *d++ = f; h += square(f); }
  640. X      if (h <= tol) { *(--ei) = 0.0; h = 0.0; }
  641. X      else
  642. X      {
  643. X     Real g = sign(-sqrt(h), f); *(--ei) = g; h -= f*g;
  644. X         f -= g; *(d-1) = f; *(a-1) = f; f = 0.0;
  645. X         Real* dj = D.Store(); Real* ej = E.Store();
  646. X         for (int j = 0; j < i; j++)
  647. X         {
  648. X            Real* dk = D.Store(); Real* ak = A.Store()+(j*(j+1))/2;
  649. X            Real g = 0.0; k = j;
  650. X            while (k--)  g += *ak++ * *dk++;
  651. X            k = i-j; int l = j; 
  652. X            while (k--) { g += *ak * *dk++; ak += ++l; }
  653. X        g /= h; *ej++ = g; f += g * *dj++;
  654. X         }  
  655. X     Real hh = f / (2 * h); Real* ak = A.Store();
  656. X         dj = D.Store(); ej = E.Store();
  657. X         for (j = 0; j < i; j++)
  658. X         {
  659. X        f = *dj++; g = *ej - hh * f; *ej++ = g;
  660. X            Real* dk = D.Store(); Real* ek = E.Store(); k = j+1;
  661. X        while (k--) { *ak++ -= (f * *ek++ + g * *dk++); }
  662. X     }
  663. X      }
  664. X      *d = *a; *a = h;
  665. X   }
  666. X}
  667. X
  668. Xstatic void tql1(DiagonalMatrix& D, DiagonalMatrix& E)
  669. X{
  670. X   Tracer et("Evalue(tql1)");
  671. X   Real eps = FloatingPointPrecision::Epsilon();
  672. X   int n = D.Nrows();
  673. X   for (int l=1; l<n; l++) E.element(l-1) = E.element(l);
  674. X   Real b = 0.0; Real f = 0.0; E.element(n-1) = 0.0;
  675. X   for (l=0; l<n; l++)
  676. X   {
  677. X      int i,j;
  678. X      Real& dl = D.element(l); Real& el = E.element(l);
  679. X      Real h = eps * ( fabs(dl) + fabs(el) );
  680. X      if (b < h) b = h;
  681. X      for (int m=l; m<n; m++) if (fabs(E.element(m)) <= b) break;
  682. X      Boolean test = FALSE;
  683. X      for (j=0; j<30; j++)
  684. X      {
  685. X         if (m==l) { test = TRUE; break; }
  686. X         Real& dl1 = D.element(l+1);
  687. X     Real g = dl; Real p = (dl1-g) / (2.0*el); Real r = sqrt(p*p + 1.0);
  688. X     dl = el / (p < 0.0 ? p-r : p+r); Real h = g - dl; f += h;
  689. X         Real* dlx = &dl1; i = n-l-1; while (i--) *dlx++ -= h;
  690. X
  691. X     p = D.element(m); Real c = 1.0; Real s = 0.0;
  692. X     for (i=m-1; i>=l; i--)
  693. X     {
  694. X            Real ei = E.element(i); Real di = D.element(i);
  695. X            Real& ei1 = E.element(i+1);
  696. X        g = c * ei; h = c * p;
  697. X        if ( fabs(p) >= fabs(ei))
  698. X        {
  699. X           c = ei / p; r = sqrt(c*c + 1.0); 
  700. X               ei1 = s*p*r; s = c/r; c = 1.0/r;
  701. X        }
  702. X        else
  703. X        {
  704. X           c = p / ei; r = sqrt(c*c + 1.0);
  705. X           ei1 = s * ei * r; s = 1.0/r; c /= r;
  706. X        }
  707. X        p = c * di - s*g; D.element(i+1) = h + s * (c*g + s*di);
  708. X     }
  709. X     el = s*p; dl = c*p;
  710. X     if (fabs(el) <= b) { test = TRUE; break; }
  711. X      }
  712. X      if (!test) Throw ( ConvergenceException(D) );
  713. X      Real p = dl + f;
  714. X      test = FALSE;
  715. X      for (i=l; i>0; i--)
  716. X      {
  717. X         if (p < D.element(i-1)) D.element(i) = D.element(i-1);
  718. X         else { test = TRUE; break; }
  719. X      }
  720. X      if (!test) i=0;
  721. X      D.element(i) = p;
  722. X   }
  723. X}
  724. X
  725. Xvoid EigenValues(const SymmetricMatrix& A, DiagonalMatrix& D, Matrix& Z)
  726. X{ DiagonalMatrix E; tred2(A, D, E, Z); tql2(D, E, Z); }
  727. X
  728. Xvoid EigenValues(const SymmetricMatrix& X, DiagonalMatrix& D)
  729. X{ DiagonalMatrix E; SymmetricMatrix A; tred3(X,D,E,A); tql1(D,E); }
  730. X
  731. Xvoid EigenValues(const SymmetricMatrix& X, DiagonalMatrix& D,
  732. X   SymmetricMatrix& A)
  733. X{ DiagonalMatrix E; tred3(X,D,E,A); tql1(D,E); }
  734. X
  735. END_OF_FILE
  736. if test 7811 -ne `wc -c <'evalue.cxx'`; then
  737.     echo shar: \"'evalue.cxx'\" unpacked with wrong size!
  738. fi
  739. # end of 'evalue.cxx'
  740. fi
  741. if test -f 'except.cxx' -a "${1}" != "-c" ; then 
  742.   echo shar: Will not clobber existing file \"'except.cxx'\"
  743. else
  744. echo shar: Extracting \"'except.cxx'\" \(5504 characters\)
  745. sed "s/^X//" >'except.cxx' <<'END_OF_FILE'
  746. X//$$except.cxx                        Exception handler
  747. X
  748. X
  749. X
  750. X#define WANT_STREAM                  // include.h will get stream fns
  751. X
  752. X
  753. X#include "include.h"                 // include standard files
  754. X#include "boolean.h"
  755. X
  756. X
  757. X#include "except.h"                  // for exception handling
  758. X
  759. X
  760. Xvoid Throw()
  761. X{
  762. X   for (Janitor* jan = JumpBase::jl->janitor; jan; jan = jan->NextJanitor)
  763. X      jan->CleanUp();
  764. X   JumpBase::jl = JumpBase::jl->ji;
  765. X   if ( ! JumpBase::jl ) Terminate();
  766. X   Exception::last = JumpBase::jl->trace;
  767. X   longjmp(JumpBase::jl->env, 1);
  768. X}
  769. X
  770. Xvoid Throw(const Exception& exc) { JumpBase::type = exc.type(); Throw(); }
  771. X
  772. X
  773. Xvoid Exception::PrintTrace(Boolean)
  774. X{
  775. X   cout << "\n";
  776. X   {
  777. X      for (Tracer* et = last; et; et=et->previous)
  778. X         cout << "  * " << et->entry << "\n";
  779. X   }
  780. X}
  781. X
  782. XException::Exception(int action)
  783. X{
  784. X   if (action)
  785. X   {
  786. X      cout << "\nAn exception has occurred: call trace follows.";
  787. X      PrintTrace();
  788. X      if (action < 0) exit(1);
  789. X   }
  790. X}   
  791. X
  792. XJanitor::Janitor()
  793. X{
  794. X   if (do_not_link) { do_not_link = FALSE; NextJanitor = 0; OnStack = FALSE; }
  795. X   else
  796. X   {
  797. X      OnStack = TRUE;
  798. X      NextJanitor = JumpBase::jl->janitor; JumpBase::jl->janitor=this;
  799. X   }
  800. X}
  801. X
  802. XJanitor::~Janitor() { if (OnStack) JumpBase::jl->janitor = NextJanitor; }
  803. X
  804. XJumpItem* JumpBase::jl = 0;
  805. Xlong JumpBase::type;
  806. XTracer* Exception::last = 0;
  807. XBoolean Janitor::do_not_link = FALSE;
  808. X
  809. Xstatic JumpItem JI;                  // need JumpItem at head of list
  810. X
  811. X
  812. X
  813. Xvoid Terminate()
  814. X{
  815. X   cout << "\nThere has been an exception with no handler - exiting\n";
  816. X   exit(1);
  817. X}
  818. X
  819. X
  820. X
  821. X
  822. X
  823. X
  824. X#ifdef DO_FREE_CHECK
  825. X// Routines for tracing whether new and delete calls are balanced
  826. X
  827. XFreeCheckLink::FreeCheckLink() : next(FreeCheck::next)
  828. X   { FreeCheck::next = this; }
  829. X
  830. XFCLClass::FCLClass(void* t, char* name) : ClassName(name) { ClassStore=t; }
  831. X   
  832. XFCLRealArray::FCLRealArray(void* t, char* o, int s)
  833. X  : Operation(o), size(s) { ClassStore=t; }
  834. X
  835. XFCLIntArray::FCLIntArray(void* t, char* o, int s)
  836. X  : Operation(o), size(s) { ClassStore=t; }
  837. X
  838. XFreeCheckLink* FreeCheck::next = 0;
  839. X
  840. Xvoid FCLClass::Report()
  841. X{ cout << "   " << ClassName << "   " << (unsigned long)ClassStore << "\n"; }
  842. X
  843. Xvoid FCLRealArray::Report()
  844. X{
  845. X   cout << "   " << Operation << "   " << (unsigned long)ClassStore << 
  846. X      "   " << size << "\n";
  847. X}
  848. X
  849. Xvoid FCLIntArray::Report()
  850. X{
  851. X   cout << "   " << Operation << "   " << (unsigned long)ClassStore << 
  852. X      "   " << size << "\n";
  853. X}
  854. X
  855. Xvoid FreeCheck::Register(void* t, char* name)
  856. X{
  857. X   FCLClass* f = new FCLClass(t,name);
  858. X   if (!f) { cout << "Out of memory in FreeCheck\n"; exit(1); }
  859. X//   cout << "Registering   " << name << "   " << (unsigned long)t << "\n";
  860. X}
  861. X
  862. Xvoid FreeCheck::RegisterR(void* t, char* o, int s)
  863. X{
  864. X   FCLRealArray* f = new FCLRealArray(t,o,s);
  865. X   if (!f) { cout << "Out of memory in FreeCheck\n"; exit(1); }
  866. X//   cout << o << "   " << s << "   " << (unsigned long)t << "\n";
  867. X}
  868. X
  869. Xvoid FreeCheck::RegisterI(void* t, char* o, int s)
  870. X{
  871. X   FCLIntArray* f = new FCLIntArray(t,o,s);
  872. X   if (!f) { cout << "Out of memory in FreeCheck\n"; exit(1); }
  873. X//   cout << o << "   " << s << "   " << (unsigned long)t << "\n";
  874. X}
  875. X
  876. Xvoid FreeCheck::DeRegister(void* t, char* name)
  877. X{
  878. X   FreeCheckLink* last = 0;
  879. X//   cout << "Deregistering " << name << "   " << (unsigned long)t << "\n";
  880. X   for (FreeCheckLink* fcl = next; fcl; fcl = fcl->next)
  881. X   {
  882. X      if (fcl->ClassStore==t)
  883. X      {
  884. X     if (last) last->next = fcl->next; else next = fcl->next;
  885. X     delete fcl; return;
  886. X      }
  887. X      last = fcl;
  888. X   }
  889. X   cout << "\nRequest to delete non-existent object of class and location:\n";
  890. X   cout << "   " << name << "   " << (unsigned long)t << "\n";
  891. X   Exception::PrintTrace(TRUE);
  892. X   cout << "\n";
  893. X}
  894. X
  895. Xvoid FreeCheck::DeRegisterR(void* t, char* o, int s)
  896. X{
  897. X   FreeCheckLink* last = 0;
  898. X//   cout << o << "   " << s << "   " << (unsigned long)t << "\n";
  899. X   for (FreeCheckLink* fcl = next; fcl; fcl = fcl->next)
  900. X   {
  901. X      if (fcl->ClassStore==t)
  902. X      {
  903. X     if (last) last->next = fcl->next; else next = fcl->next;
  904. X     if (((FCLRealArray*)fcl)->size != s)
  905. X     {
  906. X        cout << "\nArray sizes don't agree:\n";
  907. X        cout << "   " << o << "   " << (unsigned long)t
  908. X           << "   " << s << "\n";
  909. X        Exception::PrintTrace(TRUE);
  910. X        cout << "\n";
  911. X     }
  912. X     delete fcl; return;
  913. X      }
  914. X      last = fcl;
  915. X   }
  916. X   cout << "\nRequest to delete non-existent real array:\n";
  917. X   cout << "   " << o << "   " << (unsigned long)t << "   " << s << "\n";
  918. X   Exception::PrintTrace(TRUE);
  919. X   cout << "\n";
  920. X}
  921. X
  922. Xvoid FreeCheck::DeRegisterI(void* t, char* o, int s)
  923. X{
  924. X   FreeCheckLink* last = 0;
  925. X//   cout << o << "   " << s << "   " << (unsigned long)t << "\n";
  926. X   for (FreeCheckLink* fcl = next; fcl; fcl = fcl->next)
  927. X   {
  928. X      if (fcl->ClassStore==t)
  929. X      {
  930. X     if (last) last->next = fcl->next; else next = fcl->next;
  931. X     if (((FCLIntArray*)fcl)->size != s)
  932. X     {
  933. X        cout << "\nArray sizes don't agree:\n";
  934. X        cout << "   " << o << "   " << (unsigned long)t
  935. X           << "   " << s << "\n";
  936. X        Exception::PrintTrace(TRUE);
  937. X        cout << "\n";
  938. X     }
  939. X     delete fcl; return;
  940. X      }
  941. X      last = fcl;
  942. X   }
  943. X   cout << "\nRequest to delete non-existent int array:\n";
  944. X   cout << "   " << o << "   " << (unsigned long)t << "   " << s << "\n";
  945. X   Exception::PrintTrace(TRUE);
  946. X   cout << "\n";
  947. X}
  948. X
  949. Xvoid FreeCheck::Status()
  950. X{
  951. X   if (next)
  952. X   {
  953. X      cout << "\nObjects of the following classes remain undeleted:\n";
  954. X      for (FreeCheckLink* fcl = next; fcl; fcl = fcl->next) fcl->Report();
  955. X      cout << "\n";
  956. X   }
  957. X   else cout << "\nNo objects remain undeleted\n";
  958. X}
  959. X
  960. X#endif
  961. X
  962. X
  963. END_OF_FILE
  964. if test 5504 -ne `wc -c <'except.cxx'`; then
  965.     echo shar: \"'except.cxx'\" unpacked with wrong size!
  966. fi
  967. # end of 'except.cxx'
  968. fi
  969. if test -f 'fft.cxx' -a "${1}" != "-c" ; then 
  970.   echo shar: Will not clobber existing file \"'fft.cxx'\"
  971. else
  972. echo shar: Extracting \"'fft.cxx'\" \(4000 characters\)
  973. sed "s/^X//" >'fft.cxx' <<'END_OF_FILE'
  974. X//$$ fft.cxx                         Fast fourier transform
  975. X
  976. X// Copyright (C) 1991,2: R B Davies
  977. X
  978. X
  979. X#define WANT_MATH
  980. X
  981. X#include "include.h"
  982. X
  983. X#include "newmatap.h"
  984. X
  985. X
  986. Xstatic void cossin(int n, int d, Real& c, Real& s)
  987. X// calculate cos(twopi*n/d) and sin(twopi*n/d)
  988. X// minimise roundoff error
  989. X{
  990. X   long n4 = n * 4; int sector = (int)floor( (Real)n4 / (Real)d + 0.5 );
  991. X   n4 -= sector * d;
  992. X   if (sector < 0) sector = 3 - (3 - sector) % 4; else sector %= 4;
  993. X   Real ratio = 1.5707963267948966192 * (Real)n4 / (Real)d;
  994. X
  995. X   switch (sector)
  996. X   {
  997. X   case 0: c =  cos(ratio); s =  sin(ratio); break;
  998. X   case 1: c = -sin(ratio); s =  cos(ratio); break;
  999. X   case 2: c = -cos(ratio); s = -sin(ratio); break;
  1000. X   case 3: c =  sin(ratio); s = -cos(ratio); break;
  1001. X   }
  1002. X}
  1003. X
  1004. Xstatic void fftstep(ColumnVector& A, ColumnVector& B, ColumnVector& X,
  1005. X   ColumnVector& Y, int after, int now, int before)
  1006. X{
  1007. X   Tracer trace("FFT(step)");
  1008. X   // const Real twopi = 6.2831853071795864769;
  1009. X   const int gamma = after * before;  const int delta = now * after;
  1010. X   // const Real angle = twopi / delta;  Real temp;
  1011. X   // Real r_omega = cos(angle);  Real i_omega = -sin(angle);
  1012. X   Real r_arg = 1.0;  Real i_arg = 0.0;
  1013. X   Real* x = X.Store();  Real* y = Y.Store();   // pointers to array storage
  1014. X   const int m = A.Nrows() - gamma;
  1015. X
  1016. X   for (int j = 0; j < now; j++)
  1017. X   {
  1018. X      Real* a = A.Store(); Real* b = B.Store(); // pointers to array storage
  1019. X      Real* x1 = x; Real* y1 = y; x += after; y += after;
  1020. X      for (int ia = 0; ia < after; ia++)
  1021. X      {
  1022. X     // generate sins & cosines explicitly rather than iteratively
  1023. X     // for more accuracy; but slower
  1024. X     cossin(-(j*after+ia), delta, r_arg, i_arg);
  1025. X
  1026. X     Real* a1 = a++; Real* b1 = b++; Real* x2 = x1++; Real* y2 = y1++;
  1027. X     if (now==2)
  1028. X     {
  1029. X        int ib = before; while (ib--)
  1030. X        {
  1031. X           Real* a2 = m + a1; Real* b2 = m + b1; a1 += after; b1 += after;
  1032. X           Real r_value = *a2; Real i_value = *b2;
  1033. X           *x2 = r_value * r_arg - i_value * i_arg + *(a2-gamma);
  1034. X           *y2 = r_value * i_arg + i_value * r_arg + *(b2-gamma);
  1035. X           x2 += delta; y2 += delta;
  1036. X        }
  1037. X     }
  1038. X     else
  1039. X     {
  1040. X        int ib = before; while (ib--)
  1041. X        {
  1042. X           Real* a2 = m + a1; Real* b2 = m + b1; a1 += after; b1 += after;
  1043. X           Real r_value = *a2; Real i_value = *b2;
  1044. X           int in = now-1; while (in--)
  1045. X           {
  1046. X          // it should be possible to make this faster
  1047. X          // hand code for now = 2,3,4,5,8
  1048. X          // use symmetry to halve number of operations
  1049. X          a2 -= gamma; b2 -= gamma;  Real temp = r_value;
  1050. X          r_value = r_value * r_arg - i_value * i_arg + *a2;
  1051. X          i_value = temp    * i_arg + i_value * r_arg + *b2;
  1052. X           }
  1053. X           *x2 = r_value; *y2 = i_value;   x2 += delta; y2 += delta;
  1054. X        }
  1055. X     }
  1056. X
  1057. X         // temp = r_arg;
  1058. X         // r_arg = r_arg * r_omega - i_arg * i_omega;
  1059. X         // i_arg = temp  * i_omega + i_arg * r_omega;
  1060. X
  1061. X      }
  1062. X   }
  1063. X}
  1064. X
  1065. X
  1066. Xvoid FFT(const ColumnVector& U, const ColumnVector& V,
  1067. X   ColumnVector& X, ColumnVector& Y)
  1068. X{
  1069. X   // from Carl de Boor (1980), Siam J Sci Stat Comput, 1 173-8
  1070. X   Tracer trace("FFT");
  1071. X   const int n = U.Nrows();                     // length of arrays
  1072. X   if (n != V.Nrows() || n == 0)
  1073. X      Throw(ProgramException("Vector lengths unequal or zero", U, V));
  1074. X   ColumnVector A = U; ColumnVector B = V;
  1075. X   X.ReDimension(n); Y.ReDimension(n);
  1076. X   const int nextmx = 8;
  1077. X#ifndef ATandT
  1078. X   int prime[8] = { 2,3,5,7,11,13,17,19 };
  1079. X#else
  1080. X   int prime[8];
  1081. X   prime[0]=2; prime[1]=3; prime[2]=5; prime[3]=7;
  1082. X   prime[4]=11; prime[5]=13; prime[6]=17; prime[7]=19;
  1083. X#endif
  1084. X   int after = 1; int before = n; int next = 0; Boolean inzee = TRUE;
  1085. X
  1086. X   do
  1087. X   {
  1088. X      int now, b1;
  1089. X      for (;;)
  1090. X      {
  1091. X     if (next < nextmx) now = prime[next];
  1092. X     b1 = before / now;  if (b1 * now == before) break;
  1093. X     next++; now += 2;
  1094. X      }
  1095. X      before = b1;
  1096. X
  1097. X      if (inzee) fftstep(A, B, X, Y, after, now, before);
  1098. X      else fftstep(X, Y, A, B, after, now, before);
  1099. X
  1100. X      inzee = !inzee; after *= now;
  1101. X   }
  1102. X   while (before != 1);
  1103. X
  1104. X   if (inzee) { A.Release(); X = A; B.Release(); Y = B; }
  1105. X}
  1106. X
  1107. X
  1108. END_OF_FILE
  1109. if test 4000 -ne `wc -c <'fft.cxx'`; then
  1110.     echo shar: \"'fft.cxx'\" unpacked with wrong size!
  1111. fi
  1112. # end of 'fft.cxx'
  1113. fi
  1114. if test -f 'hholder.cxx' -a "${1}" != "-c" ; then 
  1115.   echo shar: Will not clobber existing file \"'hholder.cxx'\"
  1116. else
  1117. echo shar: Extracting \"'hholder.cxx'\" \(1553 characters\)
  1118. sed "s/^X//" >'hholder.cxx' <<'END_OF_FILE'
  1119. X//$$ hholder.cxx                       Householder triangularisation
  1120. X
  1121. X// Copyright (C) 1991,2: R B Davies
  1122. X
  1123. X#define WANT_MATH
  1124. X
  1125. X#include "include.h"
  1126. X
  1127. X#include "newmatap.h"
  1128. X
  1129. X
  1130. X/********************** householder triangularisation *********************/
  1131. X
  1132. Xinline Real square(Real x) { return x*x; }
  1133. X
  1134. Xvoid HHDecompose(Matrix& X, LowerTriangularMatrix& L)
  1135. X{
  1136. X   Tracer et("HHDecompose(1)");
  1137. X   int n = X.Ncols(); int s = X.Nrows(); L.ReDimension(s);
  1138. X   Real* xi = X.Store(); int k;
  1139. X   for (int i=0; i<s; i++)
  1140. X   {
  1141. X      Real sum = 0.0;
  1142. X      Real* xi0=xi; k=n; while(k--) { sum += square(*xi++); }
  1143. X      sum = sqrt(sum);
  1144. X      L.element(i,i) = sum;
  1145. X      if (sum==0.0) Throw(SingularException(L));
  1146. X      Real* xj0=xi0; k=n; while(k--) { *xj0++ /= sum; }
  1147. X      for (int j=i+1; j<s; j++)
  1148. X      {
  1149. X         sum=0.0;
  1150. X     xi=xi0; Real* xj=xj0; k=n; while(k--) { sum += *xi++ * *xj++; }
  1151. X     xi=xi0; k=n; while(k--) { *xj0++ -= sum * *xi++; }
  1152. X     L.element(j,i) = sum;
  1153. X      }
  1154. X   }
  1155. X}
  1156. X
  1157. Xvoid HHDecompose(const Matrix& X, Matrix& Y, Matrix& M)
  1158. X{
  1159. X   Tracer et("HHDecompose(1)");
  1160. X   int n = X.Ncols(); int s = X.Nrows(); int t = Y.Nrows();
  1161. X   if (Y.Ncols() != n)
  1162. X   { Throw(ProgramException("Unequal row lengths",X,Y)); }
  1163. X   M.ReDimension(t,s);
  1164. X   Real* xi = X.Store(); int k;
  1165. X   for (int i=0; i<s; i++)
  1166. X   {
  1167. X      Real* xj0 = Y.Store(); Real* xi0 = xi;
  1168. X      for (int j=0; j<t; j++)
  1169. X      {
  1170. X         Real sum=0.0;
  1171. X         xi=xi0; Real* xj=xj0; k=n; while(k--) { sum += *xi++ * *xj++; }
  1172. X     xi=xi0; k=n; while(k--) { *xj0++ -= sum * *xi++; }
  1173. X     M.element(j,i) = sum;
  1174. X      }
  1175. X   }
  1176. X}
  1177. X
  1178. END_OF_FILE
  1179. if test 1553 -ne `wc -c <'hholder.cxx'`; then
  1180.     echo shar: \"'hholder.cxx'\" unpacked with wrong size!
  1181. fi
  1182. # end of 'hholder.cxx'
  1183. fi
  1184. if test -f 'jacobi.cxx' -a "${1}" != "-c" ; then 
  1185.   echo shar: Will not clobber existing file \"'jacobi.cxx'\"
  1186. else
  1187. echo shar: Extracting \"'jacobi.cxx'\" \(2779 characters\)
  1188. sed "s/^X//" >'jacobi.cxx' <<'END_OF_FILE'
  1189. X//$$jacobi.cxx                           jacobi eigenvalue analysis
  1190. X
  1191. X// Copyright (C) 1991,2: R B Davies
  1192. X
  1193. X#define WANT_MATH
  1194. X
  1195. X#include "include.h"
  1196. X#include "newmat.h"
  1197. X#include "precisio.h"
  1198. X#include "newmatrm.h"
  1199. X
  1200. X
  1201. X
  1202. Xvoid Jacobi(const SymmetricMatrix& X, DiagonalMatrix& D, SymmetricMatrix& A,
  1203. X   Matrix& V, Boolean eivec)
  1204. X{
  1205. X   Tracer et("Jacobi");
  1206. X   int n = X.Nrows(); DiagonalMatrix B(n), Z(n); D.ReDimension(n); A = X;
  1207. X   if (eivec) { V.ReDimension(n,n); D = 1.0; V = D; }
  1208. X   B << A; D = B; Z = 0.0; A.Inject(Z);
  1209. X   for (int i=1; i<=50; i++)
  1210. X   {
  1211. X      Real sm=0.0; Real* a = A.Store(); int p = A.Storage();
  1212. X      while (p--) sm += fabs(*a++);            // have previously zeroed diags
  1213. X      if (sm==0.0) return;
  1214. X      Real tresh = (i<4) ? 0.2 * sm / square(n) : 0.0; a = A.Store();
  1215. X      for (p = 0; p < n; p++)
  1216. X      {
  1217. X     Real* ap1 = a + (p*(p+1))/2;
  1218. X         Real& zp = Z.element(p); Real& dp = D.element(p);
  1219. X     for (int q = p+1; q < n; q++)
  1220. X     {
  1221. X        Real* ap = ap1; Real* aq = a + (q*(q+1))/2;
  1222. X            Real& zq = Z.element(q); Real& dq = D.element(q);
  1223. X            Real& apq = A.element(q,p);
  1224. X            Real g = 100 * fabs(apq); Real adp = fabs(dp); Real adq = fabs(dq);
  1225. X        if (i>4 && adp+g==adp && adq+g==adq) apq = 0.0;
  1226. X        else if (fabs(apq) > tresh)
  1227. X        {
  1228. X           Real t; Real h = dq - dp; Real ah = fabs(h);
  1229. X           if (ah+g==ah) t = apq / h;
  1230. X           else
  1231. X           {
  1232. X          Real theta = 0.5 * h / apq;
  1233. X          t = 1.0 / ( fabs(theta) + sqrt(1.0 + square(theta)) );
  1234. X          if (theta<0.0) t = -t;
  1235. X           }
  1236. X           Real c = 1.0 / sqrt(1.0 + square(t)); Real s = t * c;
  1237. X           Real tau = s / (1.0 + c); h = t * apq;
  1238. X               zp -= h; zq += h; dp -= h; dq += h; apq = 0.0;
  1239. X           int j = p;
  1240. X           while (j--)
  1241. X           {
  1242. X          g = *ap; h = *aq;
  1243. X          *ap++ = g-s*(h+g*tau); *aq++ = h+s*(g-h*tau);
  1244. X           }
  1245. X           int ip = p+1; j = q-ip; ap += ip++; aq++;
  1246. X           while (j--)
  1247. X           {
  1248. X          g = *ap; h = *aq;
  1249. X                  *ap = g-s*(h+g*tau); *aq++ = h+s*(g-h*tau);
  1250. X          ap += ip++;
  1251. X           }
  1252. X           int iq = q+1; j = n-iq; ap += ip++; aq += iq++;
  1253. X           while (j--)
  1254. X           {
  1255. X          g = *ap; h = *aq;
  1256. X          *ap = g-s*(h+g*tau); *aq = h+s*(g-h*tau);
  1257. X          ap += ip++; aq += iq++;
  1258. X           }
  1259. X           if (eivec)
  1260. X           {
  1261. X                  RectMatrixCol VP(V,p); RectMatrixCol VQ(V,q);
  1262. X                  Rotate(VP, VQ, tau, s);
  1263. X           }
  1264. X        }
  1265. X     }
  1266. X      }
  1267. X      B = B + Z; D = B; Z = 0.0;
  1268. X   }
  1269. X   Throw(ConvergenceException(X));
  1270. X}
  1271. X
  1272. Xvoid Jacobi(const SymmetricMatrix& X, DiagonalMatrix& D)
  1273. X{ SymmetricMatrix A; Matrix V; Jacobi(X,D,A,V,FALSE); }
  1274. X
  1275. Xvoid Jacobi(const SymmetricMatrix& X, DiagonalMatrix& D, SymmetricMatrix& A)
  1276. X{ Matrix V; Jacobi(X,D,A,V,FALSE); }
  1277. X
  1278. Xvoid Jacobi(const SymmetricMatrix& X, DiagonalMatrix& D, Matrix& V)
  1279. X{ SymmetricMatrix A; Jacobi(X,D,A,V,TRUE); }
  1280. X
  1281. X
  1282. END_OF_FILE
  1283. if test 2779 -ne `wc -c <'jacobi.cxx'`; then
  1284.     echo shar: \"'jacobi.cxx'\" unpacked with wrong size!
  1285. fi
  1286. # end of 'jacobi.cxx'
  1287. fi
  1288. if test -f 'sort.cxx' -a "${1}" != "-c" ; then 
  1289.   echo shar: Will not clobber existing file \"'sort.cxx'\"
  1290. else
  1291. echo shar: Extracting \"'sort.cxx'\" \(1368 characters\)
  1292. sed "s/^X//" >'sort.cxx' <<'END_OF_FILE'
  1293. X//$$ sort.cxx                            Sorting
  1294. X
  1295. X// Copyright (C) 1991,2: R B Davies
  1296. X
  1297. X#define WANT_MATH
  1298. X
  1299. X#include "include.h"
  1300. X
  1301. X#include "newmatap.h"
  1302. X
  1303. X
  1304. X/******************************** Shell sort ********************************/
  1305. X
  1306. Xvoid SortAscending(GeneralMatrix& GM)
  1307. X{
  1308. X   // from numerical recipies in C - Shell sort
  1309. X   Tracer et("Sort-ascending");
  1310. X
  1311. X   const double aln2i = 1.442695022; const double tiny = 1.0e-5;
  1312. X   Real* gm = GM.Store(); int n = GM.Storage(); int m = n;
  1313. X   int lognb2 = (int)(aln2i * log((double)n) + tiny);
  1314. X   while (lognb2--)
  1315. X   {
  1316. X      m >>= 1;
  1317. X      for (int j = m; j<n; j++)
  1318. X      {
  1319. X         Real* gmj = gm+j; int i = j-m; Real* gmi = gmj-m; Real t = *gmj;
  1320. X         while (i>=0 && *gmi>t)  { *gmj = *gmi; gmj = gmi; gmi -= m; i -= m; }
  1321. X         *gmj = t;
  1322. X      }
  1323. X   }
  1324. X}
  1325. X
  1326. Xvoid SortDescending(GeneralMatrix& GM)
  1327. X{
  1328. X   // from numerical recipies in C - Shell sort
  1329. X   Tracer et("Sort-descending");
  1330. X
  1331. X   const double aln2i = 1.442695022; const double tiny = 1.0e-5;
  1332. X   Real* gm = GM.Store(); int n = GM.Storage(); int m = n;
  1333. X   int lognb2 = (int)(aln2i * log((double)n) + tiny);
  1334. X   while (lognb2--)
  1335. X   {
  1336. X      m >>= 1;
  1337. X      for (int j = m; j<n; j++)
  1338. X      {
  1339. X         Real* gmj = gm+j; int i = j-m; Real* gmi = gmj-m; Real t = *gmj;
  1340. X         while (i>=0 && *gmi<t)  { *gmj = *gmi; gmj = gmi; gmi -= m; i -= m; }
  1341. X         *gmj = t;
  1342. X      }
  1343. X   }
  1344. X}
  1345. X
  1346. END_OF_FILE
  1347. if test 1368 -ne `wc -c <'sort.cxx'`; then
  1348.     echo shar: \"'sort.cxx'\" unpacked with wrong size!
  1349. fi
  1350. # end of 'sort.cxx'
  1351. fi
  1352. if test -f 'submat.cxx' -a "${1}" != "-c" ; then 
  1353.   echo shar: Will not clobber existing file \"'submat.cxx'\"
  1354. else
  1355. echo shar: Extracting \"'submat.cxx'\" \(6534 characters\)
  1356. sed "s/^X//" >'submat.cxx' <<'END_OF_FILE'
  1357. X//$$ submat.cxx                         submatrices
  1358. X
  1359. X// Copyright (C) 1991,2: R B Davies
  1360. X
  1361. X#include "include.h"
  1362. X
  1363. X#include "newmat.h"
  1364. X#include "newmatrc.h"
  1365. X
  1366. X
  1367. X//#define REPORT { static ExeCounter ExeCount(__LINE__,6); ExeCount++; }
  1368. X
  1369. X#define REPORT {}
  1370. X
  1371. X
  1372. X/****************************** submatrices *********************************/
  1373. X
  1374. X#ifdef TEMPS_DESTROYED_QUICKLY
  1375. XGetSubMatrix& BaseMatrix::SubMatrix(int first_row, int last_row, int first_col,
  1376. X   int last_col) const
  1377. X#else
  1378. XGetSubMatrix BaseMatrix::SubMatrix(int first_row, int last_row, int first_col,
  1379. X   int last_col) const
  1380. X#endif
  1381. X{
  1382. X   REPORT
  1383. X   Tracer tr("SubMatrix");
  1384. X   int a = first_row - 1; int b = last_row - first_row + 1;
  1385. X   int c = first_col - 1; int d = last_col - first_col + 1;
  1386. X   if (a<0 || b<=0 || c<0 || d<=0) Throw(SubMatrixDimensionException());
  1387. X#ifdef TEMPS_DESTROYED_QUICKLY
  1388. X   GetSubMatrix* x = new GetSubMatrix(this, a, b, c, d, Type().sub());
  1389. X   MatrixErrorNoSpace(x);
  1390. X   return *x;
  1391. X#else
  1392. X   return GetSubMatrix(this, a, b, c, d, Type().sub());
  1393. X#endif
  1394. X}
  1395. X
  1396. X#ifdef TEMPS_DESTROYED_QUICKLY
  1397. XGetSubMatrix& BaseMatrix::SymSubMatrix(int first_row, int last_row) const
  1398. X#else
  1399. XGetSubMatrix BaseMatrix::SymSubMatrix(int first_row, int last_row) const
  1400. X#endif
  1401. X{
  1402. X   REPORT
  1403. X   Tracer tr("SubMatrix(symmetric)");
  1404. X   int a = first_row - 1; int b = last_row - first_row + 1;
  1405. X   if (a<0 || b<=0) Throw(SubMatrixDimensionException());
  1406. X#ifdef TEMPS_DESTROYED_QUICKLY
  1407. X   GetSubMatrix* x = new GetSubMatrix(this, a, b, a, b, Type().ssub());
  1408. X   MatrixErrorNoSpace(x);
  1409. X   return *x;
  1410. X#else
  1411. X   return GetSubMatrix( this, a, b, a, b, Type().ssub());
  1412. X#endif
  1413. X}
  1414. X
  1415. X#ifdef TEMPS_DESTROYED_QUICKLY
  1416. XGetSubMatrix& BaseMatrix::Row(int first_row) const
  1417. X#else
  1418. XGetSubMatrix BaseMatrix::Row(int first_row) const
  1419. X#endif
  1420. X{
  1421. X   REPORT
  1422. X   Tracer tr("SubMatrix(row)");
  1423. X   int a = first_row - 1;
  1424. X   if (a<0) Throw(SubMatrixDimensionException());
  1425. X#ifdef TEMPS_DESTROYED_QUICKLY
  1426. X   GetSubMatrix* x = new GetSubMatrix(this, a, 1, 0, -1, MatrixType::RV);
  1427. X   MatrixErrorNoSpace(x);
  1428. X   return *x;
  1429. X#else
  1430. X   return GetSubMatrix(this, a, 1, 0, -1, MatrixType::RV);
  1431. X#endif
  1432. X}
  1433. X
  1434. X#ifdef TEMPS_DESTROYED_QUICKLY
  1435. XGetSubMatrix& BaseMatrix::Rows(int first_row, int last_row) const
  1436. X#else
  1437. XGetSubMatrix BaseMatrix::Rows(int first_row, int last_row) const
  1438. X#endif
  1439. X{
  1440. X   REPORT
  1441. X   Tracer tr("SubMatrix(rows)");
  1442. X   int a = first_row - 1; int b = last_row - first_row + 1;
  1443. X   if (a<0 || b<=0) Throw(SubMatrixDimensionException());
  1444. X#ifdef TEMPS_DESTROYED_QUICKLY
  1445. X   GetSubMatrix* x = new GetSubMatrix(this, a, b, 0, -1, MatrixType::RV);
  1446. X   MatrixErrorNoSpace(x);
  1447. X   return *x;
  1448. X#else
  1449. X   return GetSubMatrix(this, a, b, 0, -1, MatrixType::RV);
  1450. X#endif
  1451. X}
  1452. X
  1453. X#ifdef TEMPS_DESTROYED_QUICKLY
  1454. XGetSubMatrix& BaseMatrix::Column(int first_col) const
  1455. X#else
  1456. XGetSubMatrix BaseMatrix::Column(int first_col) const
  1457. X#endif
  1458. X{
  1459. X   REPORT
  1460. X   Tracer tr("SubMatrix(column)");
  1461. X   int c = first_col - 1;
  1462. X   if (c<0) Throw(SubMatrixDimensionException());
  1463. X#ifdef TEMPS_DESTROYED_QUICKLY
  1464. X   GetSubMatrix* x = new GetSubMatrix(this, 0, -1, c, 1, MatrixType::CV);
  1465. X   MatrixErrorNoSpace(x);
  1466. X   return *x;
  1467. X#else
  1468. X   return GetSubMatrix(this, 0, -1, c, 1, MatrixType::CV);
  1469. X#endif
  1470. X}
  1471. X
  1472. X#ifdef TEMPS_DESTROYED_QUICKLY
  1473. XGetSubMatrix& BaseMatrix::Columns(int first_col, int last_col) const
  1474. X#else
  1475. XGetSubMatrix BaseMatrix::Columns(int first_col, int last_col) const
  1476. X#endif
  1477. X{
  1478. X   REPORT
  1479. X   Tracer tr("SubMatrix(columns)");
  1480. X   int c = first_col - 1; int d = last_col - first_col + 1;
  1481. X   if (c<0 || d<=0) Throw(SubMatrixDimensionException());
  1482. X#ifdef TEMPS_DESTROYED_QUICKLY
  1483. X   GetSubMatrix* x = new GetSubMatrix(this, 0, -1, c, d, MatrixType::CV);
  1484. X   MatrixErrorNoSpace(x);
  1485. X   return *x;
  1486. X#else
  1487. X   return GetSubMatrix(this, 0, -1, c, d, MatrixType::CV);
  1488. X#endif
  1489. X}
  1490. X
  1491. Xvoid GetSubMatrix::SetUpLHS()
  1492. X{
  1493. X   REPORT
  1494. X   Tracer tr("SubMatrix(LHS)");
  1495. X   const BaseMatrix* bm1 = bm;
  1496. X   GeneralMatrix* gm = ((BaseMatrix*&)bm)->Evaluate();
  1497. X   if ((BaseMatrix*)gm!=bm1)
  1498. X      Throw(ProgramException("Invalid LHS"));
  1499. X   if (row_number < 0) row_number = gm->Nrows();
  1500. X   if (col_number < 0) col_number = gm->Ncols();
  1501. X   if (row_skip+row_number > gm->Nrows() || col_skip+col_number > gm->Ncols())
  1502. X      Throw(SubMatrixDimensionException());
  1503. X}
  1504. X
  1505. Xvoid GetSubMatrix::operator<<(const BaseMatrix& bmx)
  1506. X{
  1507. X   REPORT
  1508. X   Tracer tr("SubMatrix(<<)");
  1509. X   SetUpLHS(); GeneralMatrix* gmx = ((BaseMatrix&)bmx).Evaluate();
  1510. X   if (row_number != gmx->Nrows() || col_number != gmx->Ncols())
  1511. X      Throw(IncompatibleDimensionsException());
  1512. X   MatrixRow mrx(gmx, LoadOnEntry); 
  1513. X   MatrixRow mr(gm, LoadOnEntry+StoreOnExit+DirectPart, row_skip);
  1514. X                                  // do need LoadOnEntry
  1515. X   MatrixRowCol sub; int i = row_number;
  1516. X   while (i--)
  1517. X   {
  1518. X      mr.SubRowCol(sub, col_skip, col_number);   // put values in sub
  1519. X      sub.Copy(mrx); mr.Next(); mrx.Next();
  1520. X   }
  1521. X   gmx->tDelete();
  1522. X#ifdef TEMPS_DESTROYED_QUICKLY
  1523. X   delete this;
  1524. X#endif
  1525. X}   
  1526. X
  1527. Xvoid GetSubMatrix::operator<<(const Real* r)
  1528. X{
  1529. X   REPORT
  1530. X   Tracer tr("SubMatrix(<<Real*)");
  1531. X   SetUpLHS();
  1532. X   if (row_skip+row_number > gm->Nrows() || col_skip+col_number > gm->Ncols())
  1533. X      Throw(SubMatrixDimensionException());
  1534. X   MatrixRow mr(gm, LoadOnEntry+StoreOnExit+DirectPart, row_skip);
  1535. X                                  // do need LoadOnEntry
  1536. X   MatrixRowCol sub; int i = row_number;
  1537. X   while (i--)
  1538. X   {
  1539. X      mr.SubRowCol(sub, col_skip, col_number);   // put values in sub
  1540. X      sub.Copy(r); mr.Next();
  1541. X   }
  1542. X#ifdef TEMPS_DESTROYED_QUICKLY
  1543. X   delete this;
  1544. X#endif
  1545. X}   
  1546. X
  1547. Xvoid GetSubMatrix::operator<<(Real r)
  1548. X{
  1549. X   REPORT
  1550. X   Tracer tr("SubMatrix(<<Real)");
  1551. X   SetUpLHS();
  1552. X   MatrixRow mr(gm, LoadOnEntry+StoreOnExit+DirectPart, row_skip);
  1553. X                                  // do need LoadOnEntry
  1554. X   MatrixRowCol sub; int i = row_number;
  1555. X   while (i--)
  1556. X   {
  1557. X      mr.SubRowCol(sub, col_skip, col_number);   // put values in sub
  1558. X      sub.Copy(r); mr.Next();
  1559. X   }
  1560. X#ifdef TEMPS_DESTROYED_QUICKLY
  1561. X   delete this;
  1562. X#endif
  1563. X}   
  1564. X
  1565. Xvoid GetSubMatrix::Inject(const GeneralMatrix& gmx)
  1566. X{
  1567. X   REPORT
  1568. X   Tracer tr("SubMatrix(inject)");
  1569. X   SetUpLHS();
  1570. X   if (row_number != gmx.Nrows() || col_number != gmx.Ncols())
  1571. X      Throw(IncompatibleDimensionsException());
  1572. X   MatrixRow mrx((GeneralMatrix*)(&gmx), LoadOnEntry);
  1573. X   MatrixRow mr(gm, LoadOnEntry+StoreOnExit+DirectPart, row_skip);
  1574. X                                  // do need LoadOnEntry
  1575. X   MatrixRowCol sub; int i = row_number;
  1576. X   while (i--)
  1577. X   {
  1578. X      mr.SubRowCol(sub, col_skip, col_number);   // put values in sub
  1579. X      sub.Inject(mrx); mr.Next(); mrx.Next();
  1580. X   }
  1581. X#ifdef TEMPS_DESTROYED_QUICKLY
  1582. X   delete this;
  1583. X#endif
  1584. X}
  1585. X
  1586. END_OF_FILE
  1587. if test 6534 -ne `wc -c <'submat.cxx'`; then
  1588.     echo shar: \"'submat.cxx'\" unpacked with wrong size!
  1589. fi
  1590. # end of 'submat.cxx'
  1591. fi
  1592. if test -f 'svd.cxx' -a "${1}" != "-c" ; then 
  1593.   echo shar: Will not clobber existing file \"'svd.cxx'\"
  1594. else
  1595. echo shar: Extracting \"'svd.cxx'\" \(4480 characters\)
  1596. sed "s/^X//" >'svd.cxx' <<'END_OF_FILE'
  1597. X//$$svd.cxx                           singular value decomposition
  1598. X
  1599. X// Copyright (C) 1991,2: R B Davies
  1600. X
  1601. X#define WANT_MATH
  1602. X
  1603. X#include "include.h"
  1604. X#include "newmat.h"
  1605. X#include "newmatrm.h"
  1606. X#include "precisio.h"
  1607. X
  1608. X
  1609. Xvoid SVD(const Matrix& A, DiagonalMatrix& Q, Matrix& U, Matrix& V,
  1610. X   Boolean withU, Boolean withV)
  1611. X// from Wilkinson and Reinsch: "Handbook of Automatic Computation"
  1612. X{
  1613. X   Tracer trace("SVD");
  1614. X   Real eps = FloatingPointPrecision::Epsilon();
  1615. X   Real tol = FloatingPointPrecision::Minimum()/eps;
  1616. X
  1617. X   int m = A.Nrows(); int n = A.Ncols();
  1618. X   if (m<n) 
  1619. X      Throw(ProgramException("Want no. Rows >= no. Cols", A));
  1620. X
  1621. X   U = A; Real g = 0.0; Real f,h; Real x = 0.0;
  1622. X   RowVector E(n); RectMatrixRow EI(E,0); Q.ReDimension(n);
  1623. X   RectMatrixCol UCI(U,0); RectMatrixRow URI(U,0,1,n-1);
  1624. X
  1625. X   for (int i=0; i<n; i++)
  1626. X   {
  1627. X      EI.First() = g; Real ei = g; EI.Right(); Real s = UCI.SumSquare();
  1628. X      if (s<tol) Q.element(i) = 0.0;
  1629. X      else
  1630. X      {
  1631. X     f = UCI.First(); g = -sign(sqrt(s), f); h = f*g-s; UCI.First() = f-g;
  1632. X     Q.element(i) = g; RectMatrixCol UCJ = UCI; int j=n-i;
  1633. X     while (--j) { UCJ.Right(); UCJ.AddScaled(UCI, (UCI*UCJ)/h); }
  1634. X      }
  1635. X
  1636. X      s = URI.SumSquare();
  1637. X      if (s<tol) g = 0.0;
  1638. X      else
  1639. X      {
  1640. X     f = URI.First(); g = -sign(sqrt(s), f); URI.First() = f-g;
  1641. X     EI.Divide(URI,f*g-s); RectMatrixRow URJ = URI; int j=m-i;
  1642. X     while (--j) { URJ.Down(); URJ.AddScaled(EI, URI*URJ); }
  1643. X      }
  1644. X
  1645. X      Real y = fabs(Q.element(i)) + fabs(ei); if (x<y) x = y;
  1646. X      UCI.DownDiag(); URI.DownDiag();
  1647. X   }
  1648. X
  1649. X   if (withV)
  1650. X   {
  1651. X      V.ReDimension(n,n); V = 0.0; RectMatrixCol VCI(V,n,n,0);
  1652. X      for (i=n-1; i>=0; i--)
  1653. X      {
  1654. X     URI.UpDiag(); VCI.Left();
  1655. X     if (g!=0.0)
  1656. X     {
  1657. X        VCI.Divide(URI, URI.First()*g); int j = n-i;
  1658. X        RectMatrixCol VCJ = VCI;
  1659. X        while (--j) { VCJ.Right(); VCJ.AddScaled( VCI, (URI*VCJ) ); }
  1660. X     }
  1661. X     VCI.Zero(); VCI.Up(); VCI.First() = 1.0; g=E.element(i);
  1662. X      }
  1663. X   }
  1664. X
  1665. X   if (withU)
  1666. X   {
  1667. X      for (i=n-1; i>=0; i--)
  1668. X      {
  1669. X     UCI.UpDiag(); g = Q.element(i); URI.Reset(U,i,i+1,n-i-1); URI.Zero();
  1670. X         if (g!=0.0)
  1671. X         {
  1672. X        h=UCI.First()*g; int j=n-i; RectMatrixCol UCJ = UCI;
  1673. X        while (--j)
  1674. X            {
  1675. X           UCJ.Right(); UCI.Down(); UCJ.Down(); Real s = UCI*UCJ;
  1676. X               UCI.Up(); UCJ.Up(); UCJ.AddScaled(UCI,s/h);
  1677. X            }
  1678. X        UCI.Divide(g);
  1679. X         }
  1680. X         else UCI.Zero();
  1681. X         UCI.First() += 1.0;
  1682. X      }
  1683. X   }
  1684. X
  1685. X   eps *= x;
  1686. X   for (int k=n-1; k>=0; k--)
  1687. X   {
  1688. X      Real z; Real y; int limit = 50; int l;
  1689. X      while (limit--)
  1690. X      {
  1691. X     Real c=0.0; Real s=1.0; int i; int l1=k; Boolean tfc=FALSE;
  1692. X     for (l=k; l>=0; l--)
  1693. X     {
  1694. X//          if (fabs(E.element(l))<=eps) goto test_f_convergence;
  1695. X        if (fabs(E.element(l))<=eps) { tfc=TRUE; break; }
  1696. X        if (fabs(Q.element(l-1))<=eps) { l1=l; break; }
  1697. X     }
  1698. X         if (!tfc)
  1699. X         {
  1700. X        l=l1; l1=l-1;
  1701. X        for (i=l; i<=k; i++)
  1702. X        {
  1703. X           f = s * E.element(i); E.element(i) *= c;
  1704. X//             if (fabs(f)<=eps) goto test_f_convergence;
  1705. X           if (fabs(f)<=eps) break;
  1706. X           g = Q.element(i); h = sqrt(f*f + g*g); Q.element(i) = h;
  1707. X           if (withU)
  1708. X           {
  1709. X              RectMatrixCol UCI(U,i); RectMatrixCol UCJ(U,l1);
  1710. X              ComplexScale(UCI, UCJ, g/h, -f/h);
  1711. X               }
  1712. X            }
  1713. X     }
  1714. X//    test_f_convergence: z = Q.element(k); if (l==k) goto convergence;
  1715. X     z = Q.element(k);  if (l==k) break;
  1716. X
  1717. X     x = Q.element(l); y = Q.element(k-1);
  1718. X     g = E.element(k-1); h = E.element(k);
  1719. X     f = ((y-z)*(y+z) + (g-h)*(g+h)) / (2*h*y); g = sqrt(f*f + 1);
  1720. X     f = ((x-z)*(x+z) + h*(y / ((f<0.0) ? f-g : f+g)-h)) / x;
  1721. X
  1722. X     c = 1.0; s = 1.0;
  1723. X     for (i=l+1; i<=k; i++)
  1724. X     {
  1725. X        g = E.element(i); y = Q.element(i); h = s*g; g *= c;
  1726. X        z = sqrt(f*f + h*h); E.element(i-1) = z; c = f/z; s = h/z;
  1727. X        f = x*c + g*s; g = -x*s + g*c; h = y*s; y *= c;
  1728. X        if (withV)
  1729. X        {
  1730. X           RectMatrixCol VCI(V,i); RectMatrixCol VCJ(V,i-1);
  1731. X           ComplexScale(VCI, VCJ, c, s);
  1732. X        }
  1733. X        z = sqrt(f*f + h*h); Q.element(i-1) = z; c = f/z; s = h/z;
  1734. X        f = c*g + s*y; x = -s*g + c*y;
  1735. X        if (withU)
  1736. X        {
  1737. X           RectMatrixCol UCI(U,i); RectMatrixCol UCJ(U,i-1);
  1738. X           ComplexScale(UCI, UCJ, c, s);
  1739. X        }
  1740. X     }
  1741. X     E.element(l) = 0.0; E.element(k) = f; Q.element(k) = x;
  1742. X      }
  1743. X      if (l!=k) { Throw(ConvergenceException(A)); }
  1744. X// convergence:
  1745. X      if (z < 0.0)
  1746. X      {
  1747. X     Q.element(k) = -z;
  1748. X     if (withV) { RectMatrixCol VCI(V,k); VCI.Negate(); }
  1749. X      }
  1750. X   }
  1751. X}
  1752. X
  1753. Xvoid SVD(const Matrix& A, DiagonalMatrix& D)
  1754. X{ Matrix U; SVD(A, D, U, U, FALSE, FALSE); }
  1755. X
  1756. X
  1757. END_OF_FILE
  1758. if test 4480 -ne `wc -c <'svd.cxx'`; then
  1759.     echo shar: \"'svd.cxx'\" unpacked with wrong size!
  1760. fi
  1761. # end of 'svd.cxx'
  1762. fi
  1763. echo shar: End of archive 6 \(of 7\).
  1764. cp /dev/null ark6isdone
  1765. MISSING=""
  1766. for I in 1 2 3 4 5 6 7 ; do
  1767.     if test ! -f ark${I}isdone ; then
  1768.     MISSING="${MISSING} ${I}"
  1769.     fi
  1770. done
  1771. if test "${MISSING}" = "" ; then
  1772.     echo You have unpacked all 7 archives.
  1773.     rm -f ark[1-9]isdone
  1774. else
  1775.     echo You still need to unpack the following archives:
  1776.     echo "        " ${MISSING}
  1777. fi
  1778. ##  End of shell archive.
  1779. exit 0
  1780.  
  1781. exit 0 # Just in case...
  1782.