C01-matrix 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281
  1. Chapter 1
  2. LINEAR ALGEBRA
  3. Summary
  4. The matrix algebra library contains functions that
  5. perform the standard computations of linear algebra.
  6. General areas covered are:
  7. o Solution of Linear Systems
  8. o Matrix Inversion
  9. o Eigensystem Analysis
  10. o Matrix Utility Operations
  11. o Singular Value Decomposition
  12. The operations covered here are fundamental to many
  13. areas of mathematics and statistics. Thus, functions
  14. in this library segment are called by other library
  15. functions. Both real and complex valued matrices
  16. are covered by functions in the first four of these
  17. categories.
  18. Notes on Contents
  19. Functions in this library segment provide the basic operations of
  20. numerical linear algebra and some useful utility functions for operations on
  21. vectors and matrices. The following list describes the functions available for
  22. operations with real-valued matrices.
  23. o Solving and Inverting Linear Systems:
  24. solv --------- solve a general system of real linear equations.
  25. solvps ------- solve a real symmetric linear system.
  26. solvru ------- solve a real right upper triangular linear system.
  27. solvtd ------- solve a tridiagonal real linear system.
  28. minv --------- invert a general real square matrix.
  29. psinv -------- invert a real symmetric matrix.
  30. ruinv -------- invert a right upper triangular matrix.
  31. The solution of a general linear system and efficient algorithms for
  32. solving special systems with symmetric and tridiagonal matrices are provided
  33. by these functions. The general solution function employs a LU factorization
  34. with partial pivoting and it is very robust. It will work efficiently on any
  35. problem that is not ill-conditioned. The symmetric matrix solution is based
  36. on a modified Cholesky factorization. It is best used on positive definite
  37. matrices that do not require pivoting for numeric stability. Tridiagonal
  38. solvers require order-N operations (N = dimension). Thus, they are highly
  39. recommended for this important class of sparse systems. Two matrix inversion
  40. routines are provided. The general inversion function is again LU based. It
  41. is suitable for use on any stable (ie. well-conditioned) problem. The
  42. Cholesky based symmetric matrix inversion is efficient and safe for use on
  43. matrices known to be positive definite, such as the variance matrices
  44. encountered in statistical computations. Both the solver and the inverse
  45. functions are designed to enhance data locality. They are very effective
  46. on modern microprocessors.
  47. o Eigensystem Analysis:
  48. eigen ------ extract all eigen values and vectors of a real
  49. symmetric matrix.
  50. eigval ----- extract the eigen values of a real symmetric matrix.
  51. evmax ------ compute the eigen value of maximum absolute magnitude
  52. and its corresponding vector for a symmetric matrix.
  53. Eigensystem functions operate on real symmetric matrices. Two forms of
  54. the general eigen routine are provided because the computation of eigen values
  55. only is much faster when vectors are not required. The basic algorithms use
  56. a Householder reduction to tridiagonal form followed by QR iterations with
  57. shifts to enhance convergence. This has become the accepted standard for
  58. symmetric eigensystem computation. The evmax function uses an efficient
  59. iterative power method algorithm to extract the eigen value of maximum
  60. absolute size and the corresponding eigenvector.
  61. o Singular Value Decomposition:
  62. svdval ----- compute the singular values of a m by n real matrix.
  63. sv2val ----- compute the singular values of a real matrix
  64. efficiently for m >> n.
  65. svduv ------ compute the singular values and the transformation
  66. matrices u and v for a real m by n matrix.
  67. sv2uv ------ compute the singular values and transformation
  68. matrices efficiently for m >> n.
  69. svdu1v ----- compute the singular values and transformation
  70. matrices u1 and v, where u1 overloads the input
  71. with the first n column vectors of u.
  72. sv2u1v ----- compute the singular values and the transformation
  73. matrices u1 and v efficiently for m >> n.
  74. Singular value decomposition is extremely useful when dealing with linear
  75. systems that may be singular. Singular values with values near zero are flags
  76. of a potential rank deficiency in the system matrix. They can be used to
  77. identify the presence of an ill-conditioned problem and, in some cases, to
  78. deal with the potential instability. They are applied to the linear least
  79. squares problem in this library. Singular values also define some important
  80. matrix norm parameters such as the 2-norm and the condition value. A complete
  81. decomposition provides both singular values and an orthogonal decomposition of
  82. vector spaces related to the matrix identifying the range and null-space.
  83. Fortunately, a highly stable algorithm based on Householder reduction to
  84. bidiagonal form and QR rotations can be used to implement the decomposition.
  85. The library provides two forms with one more efficient when the dimensions
  86. satisfy m > (3/2)n.
  87. o Real Matrix Utilities:
  88. rmmult ----- multiply two compatible real matrices.
  89. mmul ------- multiply two real square matrices.
  90. vmul ------- multiply a vector by a square matrix (transform).
  91. mattr ------ compute the transpose of a general matrix.
  92. trnm ------- transpose a real square matrix in place.
  93. otrma ------ compute orthogonal conjugate of a square matrix.
  94. otrsm ------ compute orthogonal conjugate of a symmetric matrix.
  95. smgen ------ construct a symmetric matrix from its eigen values
  96. and vectors.
  97. ortho ------ generate a general orthogonal matrix (uses random
  98. rotation generator).
  99. mcopy ------ make a copy of an array.
  100. matprt ----- print a matrix in row order with a specified
  101. format for elements to stdout or a file (fmatprt).
  102. The utility functions perform simple matrix operations such as matrix
  103. multiplication, transposition, matrix conjugation, and linear transformation
  104. of a vector. They are used to facilitate the rapid production of test and
  105. application code requiring these operations. The function call overhead
  106. associated with use of these utilities becomes negligible as the dimension of
  107. the matrices increases. The implementation of these routines is designed to
  108. enhance data locality and thus execution performance. A generator for
  109. orthogonal matrices can be used to generate test matrices.
  110. Linear Algebra with Complex Matrices
  111. The complex section of the linear algebra library contains functions that
  112. operate on general complex valued matrices and on Hermitian matrices.
  113. Hermitian matrices are the complex analog of symmetric matrices. Complex
  114. arithmetic is performed in-line in these functions to ensure efficient
  115. execution.
  116. o Solving and Inverting Complex Linear Systems:
  117. csolv ------ solve a general complex system of linear equations.
  118. cminv ------ invert a general complex matrix.
  119. Both these functions are based on the robust LU factorization algorithm
  120. with row pivots. They can be expected to solve or invert any system that is
  121. well conditioned.
  122. o Hermitian Eigensystem Analysis:
  123. heigvec ---- extract the eigen values and vectors of a Hermitian
  124. matrix.
  125. heigval ---- compute the eigenvalues of a Hermitian matrix.
  126. hevmax ----- compute the eigen value of largest absolute magnitude
  127. and the corresponding vector of a Hermitian matrix.
  128. The algorithms used for complex eigensystems are complex generalizations
  129. of those employed in the real systems. The eigen values of Hermitian matrices
  130. are real and their eigenvectors form a unitary matrix. As in the real case,
  131. the function for eigen values only is provided as a time saver. These
  132. routines have important application to many quantum mechanical problems.
  133. o Complex Matrix Utilities:
  134. cmmult ---- multiply two general, size compatible, complex matrices.
  135. cmmul ----- multiply two square complex matrices.
  136. cvmul ----- multiply a complex vector by a complex square matrix.
  137. cmattr ---- transpose a general complex matrix.
  138. trncm ----- transpose a complex square matrix in place.
  139. hconj ----- transform a square complex matrix to its Hermitian
  140. conjugate in place.
  141. utrncm ---- compute the unitary transform of a complex matrix.
  142. utrnhm ---- compute the unitary transform of a Hermitian matrix.
  143. hmgen ----- generate a general Hermitian from its eigen values
  144. and vectors.
  145. unitary --- generate a general unitary matrix (uses a random
  146. rotation generator).
  147. cmcpy ----- copy a complex array.
  148. cmprt ----- print a complex matrix in row order with a specified
  149. format for matrix elements.
  150. These utility operations replicate the utilities available for real
  151. matrices. Matrix computations implemented in a manner that enhances data
  152. locality. This ensures their efficiency on modern computers with a memory
  153. hierarchy.
  154. -------------------------------------------------------------------------------
  155. General Technical Comments
  156. Efficient computation with matrices on modern processors must be
  157. adapted to the storage scheme employed for matrix elements. The functions
  158. of this library segment do not employ the multidimensional array intrinsic
  159. of the C language. Access to elements employs the simple row-major scheme
  160. described here.
  161. Matrices are modeled by the library functions as arrays with elements
  162. stored in row order. Thus, the element in the jth row and kth column of
  163. the n by n matrix M, stored in the array mat[], is addressed by
  164. M[j,k] = mat[n*j+k] , with 0 =< j,k <= n-1 .
  165. (Remember that C employs zero as the starting index.) The storage order has
  166. important implications for data locality.
  167. The algorithms employed here all have excellent numerical stability, and
  168. the default double precision arithmetic of C enhances this. Thus, any
  169. problems encountered in using the matrix algebra functions will almost
  170. certainly be due to an ill-conditioned matrix. (The Hilbert matrices,
  171. H[i,j] = 1/(1+i+j) for i,j < n
  172. form a good example of such ill-conditioned systems.) We remind the reader
  173. that the appropriate response to such ill-conditioning is to seek an
  174. alternative approach to the problem. The option of increasing precision has
  175. already been exploited. Modification of the linear algebra algorithm code is
  176. not normally effective in an ill-conditioned problem.
  177. ------------------------------------------------------------------------------
  178. FUNCTION SYNOPSES
  179. ------------------------------------------------------------------------------
  180. Linear System Solutions:
  181. -----------------------------------------------------------------------------
  182. solv
  183. Solve a general linear system A*x = b.
  184. int solv(double a[],double b[],int n)
  185. a = array containing system matrix A in row order
  186. (altered to L-U factored form by computation)
  187. b = array containing system vector b at entry and
  188. solution vector x at exit
  189. n = dimension of system
  190. return: 0 -> normal exit
  191. -1 -> singular input
  192. -----------------------------------------------------------
  193. solvps
  194. Solve a symmetric positive definite linear system S*x = b.
  195. int solvps(double a[],double b[],int n)
  196. a = array containing system matrix S (altered to
  197. Cholesky upper right factor by computation)
  198. b = array containing system vector b as input and
  199. solution vector x as output
  200. n = dimension of system
  201. return: 0 -> normal exit
  202. 1 -> input matrix not positive definite
  203. --------------------------------------------------------------
  204. solvtd
  205. Solve a tridiagonal linear system M*x = y.
  206. void solvtd(double a[],double b[],double c[],double x[],int m)
  207. a = array containing m+1 diagonal elements of M
  208. b = array of m elements below the main diagonal of M
  209. c = array of m elements above the main diagonal
  210. x = array containing the system vector y initially, and
  211. the solution vector at exit (m+1 elements)
  212. m = dimension parameter ( M is (m+1)x(m+1) )
  213. --------------------------------------------------------------
  214. solvru
  215. Solve an upper right triangular linear system T*x = b.
  216. int solvru(double *a,double *b,int n)
  217. a = pointer to array of upper right triangular matrix T
  218. b = pointer to array of system vector
  219. The computation overloads this with the
  220. solution vector x.
  221. n = dimension (dim(a)=n*n,dim(b)=n)
  222. return value: f = status flag, with 0 -> normal exit
  223. -1 -> system singular
  224. ------------------------------------------------------------------------------
  225. Matrix Inversion:
  226. ------------------------------------------------------------------------------
  227. minv
  228. Invert (in place) a general real matrix A -> Inv(A).
  229. int minv(double a[],int n)
  230. a = array containing the input matrix A
  231. This is converted to the inverse matrix.
  232. n = dimension of the system (i.e. A is n x n )
  233. return: 0 -> normal exit
  234. 1 -> singular input matrix
  235. --------------------------------------------------------------
  236. psinv
  237. Invert (in place) a symmetric real matrix, V -> Inv(V).
  238. int psinv(double v[],int n)
  239. v = array containing a symmetric input matrix
  240. This is converted to the inverse matrix.
  241. n = dimension of the system (dim(v)=n*n)
  242. return: 0 -> normal exit
  243. 1 -> input matrix not positive definite
  244. The input matrix V is symmetric (V[i,j] = V[j,i]).
  245. --------------------------------------------------------------
  246. ruinv
  247. Invert an upper right triangular matrix T -> Inv(T).
  248. int ruinv(double *a,int n)
  249. a = pointer to array of upper right triangular matrix
  250. This is replaced by the inverse matrix.
  251. n = dimension (dim(a)=n*n)
  252. return value: status flag, with 0 -> matrix inverted
  253. -1 -> matrix singular
  254. -----------------------------------------------------------------------------
  255. Symmetric Eigensystem Analysis:
  256. -----------------------------------------------------------------------------
  257. eigval
  258. Compute the eigenvalues of a real symmetric matrix A.
  259. void eigval(double *a,double *ev,int n)
  260. a = pointer to array of symmetric n by n input
  261. matrix A. The computation alters these values.
  262. ev = pointer to array of the output eigenvalues
  263. n = dimension parameter (dim(a)= n*n, dim(ev)= n)
  264. --------------------------------------------------------------
  265. eigen
  266. Compute the eigenvalues and eigenvectors of a real symmetric
  267. matrix A.
  268. void eigen(double *a,double *ev,int n)
  269. double *a,*ev; int n;
  270. a = pointer to store for symmetric n by n input
  271. matrix A. The computation overloads this with an
  272. orthogonal matrix of eigenvectors E.
  273. ev = pointer to the array of the output eigenvalues
  274. n = dimension parameter (dim(a)= n*n, dim(ev)= n)
  275. The input and output matrices are related by
  276. A = E*D*E~ where D is the diagonal matrix of eigenvalues
  277. D[i,j] = ev[i] if i=j and 0 otherwise.
  278. The columns of E are the eigenvectors.
  279. ---------------------------------------------------------------
  280. evmax
  281. Compute the maximum (absolute) eigenvalue and corresponding
  282. eigenvector of a real symmetric matrix A.
  283. double evmax(double a[],double u[],int n)
  284. double a[],u[]; int n;
  285. a = array containing symmetric input matrix A
  286. u = array containing the n components of the eigenvector
  287. at exit (vector normalized to 1)
  288. n = dimension of system
  289. return: ev = eigenvalue of A with maximum absolute value
  290. HUGE -> convergence failure
  291. -----------------------------------------------------------------------------
  292. Eigensystem Auxiliaries:
  293. -----------------------------------------------------------------------------
  294. The following routines are used by the eigensystem functions.
  295. They are not normally called by the user.
  296. house
  297. Transform a real symmetric matrix to tridiagonal form.
  298. void house(double *a,double *d,double *dp,int n)
  299. a = pointer to array of the symmetric input matrix A
  300. These values are altered by the computation.
  301. d = pointer to array of output diagonal elements
  302. dp = pointer to array of n-1 elements neighboring the
  303. diagonal in the symmetric transformed matrix
  304. n = dimension (dim(a)= n*n, dim(d)=dim(dp)=n)
  305. The output arrays are related to the tridiagonal matrix T by
  306. T[i,i+1] = T[i+1,i] = dp[i] for i=0 to n-2, and
  307. T[i,i] = d[i] for i=0 to n-1.
  308. --------------------------------------------------------------
  309. housev
  310. Transform a real symmetric matrix to tridiagonal form and
  311. compute the orthogonal matrix of this transformation.
  312. void housev(double *a,double *d,double *dp,int n)
  313. a = pointer to array of symmetric input matrix A
  314. The computation overloads this array with the
  315. orthogonal transformation matrix O.
  316. d = pointer to array of diagonal output elements
  317. dp = pointer to array of n-1 elements neighboring the
  318. diagonal in the symmetric transformed matrix
  319. n = dimension (dim(a)= n*n, dim(d)=dim(dp)=n)
  320. The orthogonal transformation matrix O satisfies O~*T*O = A.
  321. ----------------------------------------------------------------
  322. qreval
  323. Perform a QR reduction of a real symmetric tridiagonal
  324. matrix to diagonal form.
  325. int qreval(double *ev,double *dp,int n)
  326. ev = pointer to array of input diagonal elements
  327. The computation overloads this array with
  328. eigenvalues.
  329. dp = pointer to array input elements neighboring the
  330. diagonal. This array is altered by the computation.
  331. n = dimension (dim(ev)=dim(dp)= n)
  332. ---------------------------------------------------------------
  333. qrevec
  334. Perform a QR reduction of a real symmetric tridiagonal matrix
  335. to diagonal form and update an orthogonal transformation matrix.
  336. int qrevec(double *ev,double *evec,double *dp,int n)
  337. ev = pointer to array of input diagonal elements
  338. that the computation overloads with eigenvalues
  339. evec = pointer array of orthogonal input matrix
  340. This is updated by the computation to a matrix
  341. of eigenvectors.
  342. dp = pointer to array input elements neighboring the
  343. diagonal. This array is altered by the computation.
  344. n = dimension (dim(ev)=dim(dp)= n)
  345. This function operates on the output of 'housev'.
  346. ------------------------------------------------------------------------------
  347. Matrix Utilities:
  348. ------------------------------------------------------------------------------
  349. mmul
  350. Multiply two real square matrices C = A * B.
  351. void mmul(double *c,double *a,double *b,int n)
  352. double *a,*b,*c; int n;
  353. a = pointer to store for left product matrix
  354. b = pointer to store for right product matrix
  355. c = pointer to store for output matrix
  356. n = dimension (dim(a)=dim(b)=dim(c)=n*n)
  357. -------------------------------------------------------
  358. rmmult
  359. Multiply two matrices Mat = A*B.
  360. void rmmult(double *mat,double *a,double *b,int m,int k,int n)
  361. double mat[],a[],b[]; int m,k,n;
  362. mat = array containing m by n product matrix at exit
  363. a = input array containing m by k matrix
  364. b = input array containing k by n matrix
  365. (all matrices stored in row order)
  366. m,k,n = dimension parameters of arrays
  367. ----------------------------------------------------------
  368. vmul
  369. Multiply a vector by a matrix Vp = Mat*V.
  370. void vmul(double *vp,double *mat,double *v,int n)
  371. vp = pointer to array containing output vector
  372. mat = pointer to array containing input matrix in row order
  373. v = pointer to array containing input vector
  374. n = dimension of vectors (mat is n by n)
  375. ----------------------------------------------------------------
  376. vnrm
  377. Compute the inner product of two real vectors, p = u~*v.
  378. double vnrm(double *u,double *v,int n)
  379. u = pointer to array of input vector u
  380. v = pointer to array of input vector v
  381. n = dimension (dim(u)=dim(v)=n)
  382. return value: p = u~*v (dot product of u and v)
  383. -----------------------------------------------------
  384. trnm
  385. Transpose a real square matrix in place A -> A~.
  386. void trnm(double *a,int n)
  387. a = pointer to array of n by n input matrix A
  388. This is overloaded by the transpose of A.
  389. n = dimension (dim(a)=n*n)
  390. ---------------------------------------------------------
  391. mattr
  392. Transpose an m by n matrix A = B~.
  393. void mattr(double *a,double *b,int m,int n)
  394. a = pointer to array containing output n by m matrix
  395. b = pointer to array containing input m by n matrix
  396. (matrices stored in row order)
  397. m,n = dimension parameters (dim(a)=dim(b)=n*m)
  398. ------------------------------------------------------------
  399. otrma
  400. Perform an orthogonal similarity transform C = A*B*A~.
  401. void otrma(double *c,double *a,double *b,int n)
  402. c = pointer to array of output matrix C
  403. a = pointer to array of transformation A
  404. b = pointer to array of input matrix B
  405. n = dimension (dim(a)=dim(b)=dim(c)=n*n)
  406. -----------------------------------------------------------
  407. otrsm
  408. Perform a similarity transform on a symmetric matrix S = A*B*A~.
  409. void otrsm(double *sm,double *a,double *b,int n)
  410. sm = pointer to array of output matrix S
  411. a = pointer to array of transformation matrix A
  412. b = pointer to array of symmetric input matrix B
  413. n = dimension (dim(a)=dim(b)=dim(sm)=n*n)
  414. ---------------------------------------------------------------
  415. smgen
  416. Construct a symmetric matrix from specified eigenvalues and
  417. eigenvectors.
  418. void smgen(double *a,double *eval,double *evec,int n)
  419. a = pointer to array containing output matrix
  420. eval = pointer to array containing the n eigenvalues
  421. evec = pointer to array containing eigenvectors
  422. (n by n with kth column the vector corresponding
  423. to the kth eigenvalue)
  424. n = system dimension
  425. If D is the diagonal matrix of eigenvalues
  426. and E[i,j] = evec[j+n*i] , then A = E*D*E~.
  427. ----------------------------------------------------------------
  428. ortho
  429. Generate a general orthogonal transformation matrix, E~*E = I.
  430. void ortho(double *e,int n)
  431. e = pointer to array of orthogonal output matrix E
  432. n = dimension of vector space (dim(e)=n*n)
  433. This function calls on the uniform random generator 'unfl' to
  434. produce random rotation angles. Therefore this random generator
  435. should be initialized by a call of 'setunfl' before calling
  436. ortho (see Chapter 7).
  437. -----------------------------------------------------------------
  438. mcopy
  439. Copy an array a = b.
  440. void mcopy(double *a,double *b,int n)
  441. a = array containing output values, identical to input
  442. b at exit
  443. b = input array
  444. n = dimension of arrays
  445. -----------------------------------------------------------
  446. matprt
  447. Print an array in n rows of m columns to stdout.
  448. void matprt(double *a,int n,int m,char *fmt)
  449. a = pointer to input array stored in row order (size = n*m)
  450. n = number of output rows
  451. m = number of output columns
  452. fmt= pointer to character array containing format string
  453. (printf formats eg. " %f")
  454. Long rows may overflow the print line.
  455. ---------------------------------------------------------------
  456. fmatprt
  457. Print formatted array output to a file.
  458. void fmatprt(FILE *fp,double *a,int n,int m,char *fmt)
  459. fp = pointer to file opened for writing
  460. a = pointer to input array stored in row order (size = n*m)
  461. n = number of output rows
  462. m = number of output columns
  463. fmt= pounter to character array containing format string
  464. (printf formats eg. " %f")
  465. ------------------------------------------------------------------------------
  466. Singular Value Decomposition:
  467. ------------------------------------------------------------------------------
  468. A number of versions of the Singular Value Decomposition (SVD)
  469. are implemented in the library. They support the efficient
  470. computation of this important factorization for a real m by n
  471. matrix A. The general form of the SVD is
  472. A = U*S*V~ with S = | D |
  473. | 0 |
  474. where U is an m by m orthogonal matrix, V is an n by n orthogonal matrix,
  475. D is the n by n diagonal matrix of singular value, and S is the singular
  476. m by n matrix produced by the transformation.
  477. The singular values computed by these functions provide important
  478. information on the rank of the matrix A, and on several matrix
  479. norms of A. The number of non-zero singular values d[i] in D
  480. equal to the rank of A. The two norm of A is
  481. ||A|| = max(d[i]) , and the condition number is
  482. k(A) = max(d[i])/min(d[i]) .
  483. The Frobenius norm of the matrix A is
  484. Fn(A) = Sum(i=0 to n-1) d[i]^2 .
  485. Singular values consistent with zero are easily recognized, since
  486. the decomposition algorithms have excellent numerical stability.
  487. The value of a 'zero' d[i] is no larger than a few times the
  488. computational rounding error e.
  489. The matrix U1 is formed from the first n orthonormal column vectors
  490. of U. U1[i,j] = U[i,j] for i = 1 to m and j = 1 to n. A singular
  491. value decomposition of A can also be expressed in terms of the m by\
  492. n matrix U1, with
  493. A = U1*D*V~ .
  494. SVD functions with three forms of output are provided. The first
  495. form computes only the singular values, while the second computes
  496. the singular values and the U and V orthogonal transformation
  497. matrices. The third form of output computes singular values, the
  498. V matrix, and saves space by overloading the input array with
  499. the U1 matrix.
  500. Two forms of decomposition algorithm are available for each of the
  501. three output types. One is computationally efficient when m ~ n.
  502. The second, distinguished by the prefix 'sv2' in the function name,
  503. employs a two stage Householder reduction to accelerate computation
  504. when m substantially exceeds n. Use of functions of the second form
  505. is recommended for m > 2n.
  506. Singular value output from each of the six SVD functions satisfies
  507. d[i] >= 0 for i = 0 to n-1.
  508. -------------------------------------------------------------------------------
  509. svdval
  510. Compute the singular values of a real m by n matrix A.
  511. int svdval(double *d,double *a,int m,int n)
  512. d = pointer to double array of dimension n
  513. (output = singular values of A)
  514. a = pointer to store of the m by n input matrix A
  515. (A is altered by the computation)
  516. m = number of rows in A
  517. n = number of columns in A (m>=n required)
  518. return value: status flag with:
  519. 0 -> success
  520. -1 -> input error m < n
  521. ------------------------------------------------------------
  522. sv2val
  523. Compute singular values when m >> n.
  524. int sv2val(double *d,double *a,int m,int n)
  525. d = pointer to double array of dimension n
  526. (output = singular values of A)
  527. a = pointer to store of the m by n input matrix A
  528. (A is altered by the computation)
  529. m = number of rows in A
  530. n = number of columns in A (m>=n required)
  531. return value: status flag with:
  532. 0 -> success
  533. -1 -> input error m < n
  534. --------------------------------------------------------------
  535. svduv
  536. Compute the singular value transformation S = U~*A*V.
  537. int svduv(double *d,double *a,double *u,int m,double *v,int n)
  538. d = pointer to double array of dimension n
  539. (output = singular values of A)
  540. a = pointer to store of the m by n input matrix A
  541. (A is altered by the computation)
  542. u = pointer to store for m by m orthogonal matrix U
  543. v = pointer to store for n by n orthogonal matrix V
  544. m = number of rows in A
  545. n = number of columns in A (m>=n required)
  546. return value: status flag with:
  547. 0 -> success
  548. -1 -> input error m < n
  549. --------------------------------------------------------------------
  550. sv2uv
  551. Compute the singular value transformation when m >> n.
  552. int sv2uv(double *d,double *a,double *u,int m,double *v,int n)
  553. d = pointer to double array of dimension n
  554. (output = singular values of A)
  555. a = pointer to store of the m by n input matrix A
  556. (A is altered by the computation)
  557. u = pointer to store for m by m orthogonal matrix U
  558. v = pointer to store for n by n orthogonal matrix V
  559. m = number of rows in A
  560. n = number of columns in A (m>=n required)
  561. return value: status flag with:
  562. 0 -> success
  563. -1 -> input error m < n
  564. ----------------------------------------------------------------
  565. svdu1v
  566. Compute the singular value transformation with A overloaded by
  567. the partial U-matrix.
  568. int svdu1v(double *d,double *a,int m,double *v,int n)
  569. d = pointer to double array of dimension n
  570. (output = singular values of A)
  571. a = pointer to store of the m by n input matrix A
  572. (At output a is overloaded by the matrix U1
  573. whose n columns are orthogonal vectors equal to
  574. the first n columns of U.)
  575. v = pointer to store for n by n orthogonal matrix V
  576. m = number of rows in A
  577. n = number of columns in A (m>=n required)
  578. return value: status flag with:
  579. 0 -> success
  580. -1 -> input error m < n
  581. ------------------------------------------------------------------
  582. sv2u1v
  583. Compute the singular value transformation with partial U
  584. matrix U1 efficiently for m >> n.
  585. #include <math.h>
  586. int sv2u1v(d,a,m,v,n)
  587. double *d,*a,*v; int m,n;
  588. d = pointer to double array of dimension n
  589. (output = singular values of A)
  590. a = pointer to store of the m by n input matrix A
  591. (At output a is overloaded by the matrix U1
  592. whose n columns are orthogonal vectors equal to
  593. the first n columns of U.)
  594. v = pointer to store for n by n orthogonal matrix V
  595. m = number of rows in A
  596. n = number of columns in A (m>=n required)
  597. return value: status flag with:
  598. 0 -> success
  599. -1 -> input error m < n
  600. -------------------------------------------------------------------------------
  601. Auxiliary Functions used in SVD Computation:
  602. -------------------------------------------------------------------------------
  603. The following routines are used by the singular value decomposition
  604. functions. They are not normally called by the user.
  605. qrbdi
  606. Perform a QR reduction of a bidiagonal matrix.
  607. int qrbdi(double *d,double *e,int m)
  608. d = pointer to n-dimensional array of diagonal values
  609. (overloaded by diagonal elements of reduced matrix)
  610. e = pointer to store of superdiagonal values (loaded in
  611. first m-1 elements of the array). Values are altered
  612. by the computation.
  613. m = dimension of the d and e arrays
  614. return value: N = number of QR iterations required
  615. -------------------------------------------------------------
  616. qrbdv
  617. Perform a QR reduction of a bidiagonal matrix and update the
  618. the orthogonal transformation matrices U and V.
  619. int qrbdv(double *d,double *e,double *u,int m,double *v,int n)
  620. d = pointer to n-dimensional array of diagonal values
  621. (overloaded by diagonal elements of reduced matrix)
  622. e = pointer to store of superdiagonal values (loaded in
  623. first m-1 elements of the array). Values are altered
  624. by the computation.
  625. u = pointer to store of m by m orthogonal matrix U updated
  626. by the computation
  627. v = pointer to store of n by n orthogonal matrix V updated
  628. by the computation
  629. m = dimension parameter of the U matrix
  630. n = size of the d and e arrays and the number of rows and
  631. columns in the V matrix
  632. return value: N = number of QR iterations required
  633. ---------------------------------------------------------------
  634. qrbd1
  635. Perform a QR reduction of a bidiagonal matrix and update the
  636. transformation matrices U1 and V.
  637. int qrbdu1(double *d,double *e,double *u1,int m,double *v,int n)
  638. d = pointer to n-dimensional array of diagonal values
  639. (overloaded by diagonal elements of reduced matrix)
  640. e = pointer to store of superdiagonal values (loaded in
  641. first m-1 elements of the array). Values are altered
  642. by the computation.
  643. u1 = pointer to store of m by n transformation matrix U1
  644. updated by the computation
  645. v = pointer to store of n by n orthogonal matrix V updated
  646. by the computation
  647. m = number of rows in the U1 matrix
  648. n = size of the d and e arrays, number of columns in the U1
  649. matrix, and the number of rows and columns in the V matrix.
  650. return value: N = number of QR iterations required
  651. ------------------------------------------------------------------
  652. ldumat
  653. Compute a left Householder transform matrix U from the vectors
  654. specifying the Householder reflections.
  655. void ldumat(double *a,double *u,int m,int n)
  656. a = pointer to store of m by n input matrix A. Elements
  657. of A on and below the main diagonal specify the
  658. vectors of n Householder reflections (see note below).
  659. u = pointer to store for the m by m orthogonal output
  660. matrix U.
  661. m = number of rows in A and U, and number of columns in U.
  662. n = number of columns in A
  663. --------------------------------------------------------------
  664. ldvmat
  665. Compute a right Householder transform matrix from the vectors
  666. specifying the Householder reflections.
  667. void ldvmat(double *a,double *v,int n)
  668. a = pointer to store of n by n input matrix A. Elements
  669. of A on and above the superdiagonal specify vectors
  670. of a sequence of Householder reflections (see note below).
  671. v = pointer to store for the n by n orthogonal output
  672. matrix V
  673. n = number of rows and columns in A and V
  674. -----------------------------------------------------------------
  675. atou1
  676. Overload a Householder left-factored matrix A with the first
  677. n columns of the Householder orthogonal matrix.
  678. void atou1(double *a,int m,int n)
  679. a = pointer to store of m by n input matrix A. Elements
  680. of A on and below the main diagonal specify the
  681. vectors of n Householder reflections (see note below).
  682. This array is overloaded by the first n columns of the
  683. Householder transformation matrix.
  684. m = number of rows in A
  685. n = number of columns in A
  686. ---------------------------------------------------------------
  687. atovm
  688. Overload a Householder right-factored square matrix A with the
  689. Householder transformation matrix V.
  690. void atovm(double *v,int n)
  691. v = pointer to store for the n by n orthogonal
  692. output matrix V
  693. n = number of rows and columns in V
  694. -------------------------------------------------------------------------------
  695. Individual Householder reflections are specified by a vector h.
  696. The corresponding orthogonal reflection matrix is given by
  697. H = I - c* h~ .
  698. Input matrices store the vector, normalized to have its leading
  699. coefficient equal to one, and the normalization factor
  700. c = 2/(h~*h) .
  701. Storage for the vectors is by column starting at the diagonal for
  702. a left transform, and by row starting at the superdiagonal for a
  703. right transform. The first location holds c followed by
  704. components 2 to k of the vector.
  705. -------------------------------------------------------------------------------
  706. Complex Linear Algebra
  707. -------------------------------------------------------------------------------
  708. Solution and Inverse:
  709. -------------------------------------------------------------------------------
  710. csolv
  711. Solve a complex linear system A*x = b.
  712. int csolv(Cpx *a,Cpx *b,int n)
  713. a = pointer to array of n by n system matrix A
  714. The computation alters this array to a LU factorization.
  715. b = pointer to input array of system vector b
  716. This is replaced by the solution vector b -> x.
  717. n = dimension of system (dim(a)=n*n, dim(b)=n)
  718. return value: status flag with: 0 -> valid solution
  719. 1 -> system singular
  720. ---------------------------------------------------------------
  721. cminv
  722. Invert a general complex matrix in place A -> Inv(A).
  723. int cminv(Cpx *a,int n)
  724. a = pointer to input array of complex n by n matrix A
  725. The computation replaces A by its inverse.
  726. n = dimension of system (dim(a)=n*n)
  727. return value: status flag with: 0 -> valid solution
  728. 1 -> system singular
  729. ------------------------------------------------------------------------------
  730. Hermitian Eigensystems:
  731. ------------------------------------------------------------------------------
  732. heigval
  733. Compute the eigenvalues of a Hermitian matrix.
  734. void heigval(Cpx *a,double *ev,int n)
  735. a = pointer to array for the Hermitian matrix H
  736. These values are altered by the computation.
  737. ev = pointer to array that is loaded with the
  738. eigenvalues of H by the computation
  739. n = dimension (dim(a)=n*n, dim(ev)=n)
  740. --------------------------------------------------------
  741. heigvec
  742. Compute the eigenvalues and eigenvectors of a Hermitian
  743. matrix.
  744. void heigvec(Cpx *a,double *ev,int n)
  745. a = pointer to array for the hermitian matrix H
  746. This array is loaded with a unitary matrix of
  747. eigenvectors E by the computation.
  748. ev = pointer to array that is loaded with the
  749. eigenvalues of H by the computation
  750. n = dimension (dim(a)=n*n, dim(ev)=n)
  751. The eigen vector matrix output E satisfies
  752. E^*E = I and A = E*D*E^
  753. where D[i,j] = ev[i] for i=j and 0 otherwise
  754. and E^ is the Hermitian conjugate of E.
  755. The columns of E are the eigenvectors of A.
  756. ----------------------------------------------------------
  757. hevmax
  758. Compute the eigenvalue of maximum absolute value and
  759. the corresponding eigenvector of a Hermitian matrix.
  760. double hevmax(Cpx *a,Cpx *u,int n)
  761. Cpx *a,*u; int n;
  762. a = pointer to array for the Hermitian matrix H
  763. u = pointer to array for the eigenvector umax
  764. n = dimension (dim(a)=n*n, dim(u)=n)
  765. return value: emax = eigenvalue of H with largest
  766. absolute value
  767. The eigenvector u and eigenvalue emax are related by u^*A*u = emax.
  768. ------------------------------------------------------------------------------
  769. Hermitian Eigensystem Auxiliaries:
  770. ------------------------------------------------------------------------------
  771. The following routines are called by the Hermitian eigensystem
  772. functions. They are not normally called by the user.
  773. chouse
  774. Transform a Hermitian matrix H to real symmetric tridiagonal
  775. form.
  776. void chouse(Cpx *a,double *d,double *dp,int n)
  777. a = pointer to input array of complex matrix elements of H
  778. This array is altered by the computation
  779. d = pointer to output array of real diagonal elements
  780. dp = pointer to output array of real superdiagonal elements
  781. n = system dimension, with:
  782. dim(a) = n * n, dim(d) = dim(dn) = n;
  783. -----------------------------------------------------------------
  784. chousv
  785. Transform a Hermitian matrix H to real symmetric tridiagonal
  786. form, and compute the unitary matrix of this transformation.
  787. void chousv(Cpx *a,double *d,double *dp,int n)
  788. a = pointer to input array of complex matrix elements of H
  789. The computation replaces this with the unitary matrix
  790. U of the transformation.
  791. d = pointer to output array of real diagonal elements
  792. dp = pointer to output array of real superdiagonal elements
  793. n = system dimension, with:
  794. dim(a) = n*n, dim(d) = dim(dn) = n;
  795. The matrix U satisfies
  796. A = U^*T*U where T is real and tridiagonal,
  797. with T[i,i+1] = T[i+1,i] = dp[i] and T[i,i] = d[i].
  798. ------------------------------------------------------------
  799. qrecvc
  800. Use QR transformations to reduce a real symmetric tridiagonal
  801. matrix to diagonal form, and update a unitary transformation
  802. matrix.
  803. void qrecvc(double *ev,Cpx *evec,double *dp,int n)
  804. ev = pointer to input array of diagonal elements
  805. The computation transforms these to eigenvalues
  806. of the input matrix.
  807. evec = pointer to input array of a unitary transformation
  808. matrix U. The computation applies the QR rotations
  809. to this matrix.
  810. dp = pointer to input array of elements neighboring the
  811. diagonal. These values are altered by the computation.
  812. n = dimension parameter (dim(ev)=dim(dp)=n, dim(evec)=n*n)
  813. This function operates on the output of 'chousv'.
  814. -------------------------------------------------------------------------------
  815. Complex Matrix Utilities:
  816. -------------------------------------------------------------------------------
  817. cvmul
  818. Transform a complex vector u = A*v.
  819. void cvmul(Cpx *u,Cpx *a,Cpx *v,int n)
  820. u = pointer to array of output vector u.
  821. a = pointer to array of transform matrix A.
  822. v = pointer to array of input vector v.
  823. n = dimension (dim(u)=dim(v)=n, dim(a)=n*n)
  824. -----------------------------------------------------------
  825. cvnrm
  826. Compute a Hermitian inner product s = u^*v.
  827. Cpx cvnrm(Cpx *u,Cpx *v,int n)
  828. u = pointer to array of first vector u
  829. v = pointer to array of second vector v
  830. n = dimension (dim(u)=dim(v)=n)
  831. return value: s = complex value of inner product
  832. -----------------------------------------------------------
  833. cmmul
  834. Multiply two square complex matrices C = A * B.
  835. void cmmul(Cpx *c,Cpx *a,Cpx *b,int n)
  836. a = pointer to input array of left matrix factor A
  837. b = pointer to input array of right matrix factor B
  838. c = pointer to array of output product matrix C
  839. n = dimension parameter (dim(c)=dim(a)=dim(b)=n*n)
  840. -------------------------------------------------------------
  841. cmmult
  842. Multiply two complex matrices C = A * B.
  843. void cmmult(Cpx *c,Cpx *a,Cpx *b,int n,int m,int l)
  844. a = pointer to input array of right n by m factor matrix A
  845. b = pointer to input array of left m by l factor matrix B
  846. c = pointer to store for n by l output matrix C
  847. n,m,l = system dimension parameters, with
  848. (dim(c)=n*l, dim(a)=n*m, dim(b)=m*l)
  849. ----------------------------------------------------------------
  850. hconj
  851. Compute the Hermitian conjugate in place, A -> A^.
  852. void hconj(Cpx *a,int n)
  853. a = pointer to input array for the complex matrix A
  854. This is converted to the Hermitian conjugate A^.
  855. n = dimension (dim(a)=n*n)
  856. ----------------------------------------------------------
  857. utrncm
  858. Perform a unitary similarity transformation C = T*B*T^.
  859. void utrncm(Cpx *cm,Cpx *a,Cpx *b,int n)
  860. a = pointer to the array of the transform matrix T
  861. b = pointer to the array of the input matrix B
  862. cm = pointer to output array of the transformed matrix C
  863. n = dimension (dim(cm)=dim(a)=dim(b)=n*n)
  864. ---------------------------------------------------------------
  865. utrnhm
  866. Perform a unitary similarity transformation on a Hermitian
  867. matrix H' = T*H*T^.
  868. void utrnhm(Cpx *hm,Cpx *a,Cpx *b,int n)
  869. a = pointer to the array of the transform matrix T
  870. b = pointer to the array of the Hermitian input matrix H
  871. hm = pointer to array containing Hermitian output matrix H'
  872. n = dimension (dim(cm)=dim(a)=dim(b)=n*n)
  873. -----------------------------------------------------------------
  874. trncm
  875. Transpose a complex square matrix in place A -> A~.
  876. void trncm(Cpx *a,int n)
  877. a = pointer to array of n by n complex matrix A
  878. The computation replaces A by its transpose
  879. n = dimension (dim(a)=n*n)
  880. ---------------------------------------------------------
  881. cmattr
  882. Compute the transpose A = B~ of a complex m by n matrix.
  883. void cmattr(Cpx *a,Cpx *b,int m,int n)
  884. a = pointer to output array of matrix A
  885. b = pointer to input array of matrix B
  886. m, n = matrix dimensions, with B m by n and A n by m
  887. (dim(a)=dim(b)= m*n)
  888. -----------------------------------------------------------------
  889. hmgen
  890. Generate a Hermitian matrix with specified eigen values and
  891. eigenvectors.
  892. void hmgen(Cpx *h,double *ev,Cpx *u,int n)
  893. h = pointer to complex array of output matrix H
  894. ev = pointer to real array of input eigen values
  895. u = pointer to complex array of unitary matrix U
  896. n = dimension (dim(h)=dim(u)=n*n, dim(ev)=n)
  897. If D is a diagonal matrix with D[i,j] = ev[i] for i=j and 0
  898. otherwise. H = U*D*U^. The columns of U are eigenvectors.
  899. -----------------------------------------------------------------
  900. unitary
  901. Generate a random unitary transformation U.
  902. void unitary(Cpx *u,int n)
  903. u = pointer to complex output array for U
  904. n = dimension (dim(u)=n*n)
  905. This function calls on the uniform random generator 'unfl' to
  906. produce random rotation angles. Therefore this random generator
  907. should be initialized by a call of 'setunfl' before calling
  908. 'unitary' (see Chapter 7).
  909. ---------------------------------------------------------------------
  910. cmcpy
  911. Copy a complex array A = B.
  912. void cmcpy(Cpx *a,Cpx *b,int n)
  913. a = pointer to store for output array
  914. b = pointer to store for input array
  915. n = dimension of complex arrays A and B
  916. -----------------------------------------------------------
  917. cmprt
  918. Print rows of a complex matrix in a specified format.
  919. void cmprt(Cpx *a,int m,int n,char *f)
  920. a = pointer to array of complex m by n matrix
  921. m = number of columns
  922. n = number of rows
  923. f = character array holding format for complex number
  924. output (ie., "%f, %f ")
  925. Long rows may overflow the print line.