File: linalg.i

package info (click to toggle)
yorick-yutils 1.3.0-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 604 kB
  • ctags: 17
  • sloc: makefile: 105; python: 12
file content (444 lines) | stat: -rw-r--r-- 13,698 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*
 * linalg.i -
 *
 *	Linear Algebra functions for Yorick.
 *
 *-----------------------------------------------------------------------------
 *
 *	Copyright (C) 2003-2004 Eric THIEBAUT.
 *
 *	This file is part of OptimPack.
 *
 *	OptimPack is  free software; you can redistribute  it and/or modify
 *	it under the  terms of the GNU General  Public License as published
 *	by the Free  Software Foundation; either version 2  of the License,
 *	or (at your option) any later version.
 *
 *	OptimPack is  distributed in the hope  that it will  be useful, but
 *	WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
 *	MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
 *	General Public License for more details.
 *
 *	You should have  received a copy of the  GNU General Public License
 *	along with  Yeti (file "COPYING"  in the top source  directory); if
 *	not, write to the Free  Software Foundation, Inc., 59 Temple Place,
 *	Suite 330,  Boston, MA  02111-1307 USA.
 *
 *-----------------------------------------------------------------------------
 *
 * History:
 *	$Id: linalg.i,v 1.1.1.1 2007/12/11 23:55:10 frigaut Exp $
 *	$Log: linalg.i,v $
 *	Revision 1.1.1.1  2007/12/11 23:55:10  frigaut
 *	Initial Import - yorick-yutils
 *	
 */

func gram_schmidt_orthonormalization(b)
/* DOCUMENT gram_schmidt_orthonormalization, b;
       -or- gram_schmidt_orthonormalization(b)
     Performs Gram-Schmidt orthonormalization of basis functions B.  If
     B is an array of pointers, then the input basis vectors are *B(i)
     for i=1,..,numberof(B); otherwise, the input basis vectors are B(..,i)
     for i=1,..,dimsof(B)(0).  When called as a subroutine, the operation
     is done "in-place".
     
   SEE ALSO: SVdec. */
{
  local b_i, b_j;

  /* NOTE: type conversion of array X, e.g. double(X), is a no-operation
     if X already of given type. */
  if (! is_array(b)) error, "expecting array parameter";
  type = double;
  if ((s = structof(b)) == pointer) {
    ptr = 1;
    n = numberof(b);
    if (! am_subroutine()) b = b; /* make a private copy */
    for (i=1 ; i<=n ; ++i) {
      if ((s = structof(*b(i))) == complex) type = complex;
      else if (s != double && s != float && s != long && s != int &&
               s != short && s != char) error, "bad data type";
    }
  } else {
    ptr = 0;
    n = dimsof(b)(0);
    if ((s = structof(b)) == complex) type = complex;
    else if (s != double && s != float && s != long && s != int &&
             s != short && s != char) error, "bad data type";
    if (s != type) {
      if (am_subroutine()) error, "bad data type for in-place conversion";
      b = type(b);
    }
  }
  if (type == complex) error, "complex data type not yet implemented";
  
  /* Gram-Schmidt Orthonormalization. */
  for (i=1 ; i<=n ; ++i) {
    /* get i-th basis vector */
    if (prt) eq_nocopy, b_i, type(*b(i));
    else     b_i = b(.., i);
    
    /* make the i-th vector othogonal to previous ones */
    if (i > 1) {
      if (prt) eq_nocopy, b_j, type(*b(1));
      else     b_j = b(.., 1);
      s = sum(b_i*b_j)*b_j;
      for (j=2 ; j<i ; ++j) {
        if (prt) eq_nocopy, b_j, type(*b(j));
        else     b_j = b(.., j);
        s += sum(b_i*b_j)*b_j;
      }
      b_i -= s;
    }

    /* normalize i-th vector */
    s = sum(b_i*b_i);
    if (ptr) {
      b(i) = &((s > 0.0 ? (1.0/sqrt(s))*b_i : array(double, dimsof(b_i))));
    } else {
      b(..,i) = (s > 0.0 ? (1.0/sqrt(s))*b_i : array(double, dimsof(b_i)));
    }
  }
  return b;
}

func trace(a)
/* DOCUMENT trace(a)
     Returns the trace of matrix A.
     
   SEE ALSO: diag. */
{
  if (! is_array(a) || (dims = dimsof(a))(1) != 2)
    error, "expecting a 2D array";
  m = dims(2);
  n = dims(3);
  return a(sum:1:m*min(m,n):m+1);
}

func diag(a)
/* DOCUMENT diag(a)
     Returns the diagonal of matrix A (if A is a 2D array) or a square
     diagonal matrix with diagonal elements equal to those of vector A (if
     A is a 1D array).
     
   SEE ALSO: trace, unit. */
{
  if (is_array(a)) {
    if ((dims = dimsof(a))(1) == 1) {
      m = dims(2);
      (mx = array(structof(a), m, m))(1:m*m:m+1) = a;
      return mx;
    } else if (dims(1) == 2) {
      m = dims(2);
      n = dims(3);
      return a(1:m*min(m,n):m+1);
    }
  }
  error, "expecting a 1D or 2D array";
}

func euclidean_norm(x)
/* DOCUMENT euclidean_norm(x)
    Returns the Euclidian norm of X: sqrt(sum(X*X)), taking care of overflows.
*/
{
  if (! (s = max(-min(x), max(x)))) return 0.0;
  x *= 1.0/s;
  return s*sqrt(sum(x*x));
}

func pm(a, ndigits=, file=, format=)
/* DOCUMENT pm, a;
     Prints matrix A.  Keyword FILE can be set to the name/stream of output
     file; the default is to use standard output.  If keyword NDIGITS is
     set, floating-point values get printed with that number of significant
     digits; alternatively, keyword FORMAT may be set with the format for
     each element of A -- for complex numbers, the real and imaginary parts
     use the same format.
     
   SEE ALSO: write. */
{
  if (! is_array(a) || (dims = dimsof(a))(1) != 2) {
    error, "expecting a 2-dimensional array";
  }
  m = dims(2);
  n = dims(3);
  s = structof(a);
  if (s == complex) {
    if (is_void(format)) {
      if (is_void(ndigits)) ndigits = 5;
      format = swrite(format="%%.%dg+%%.%dgi", ndigits, ndigits);
    } else {
      format += "+" + format + "i";
    }
    a = swrite(format=format, double(a), a.im);
  } else if (s == pointer) {
    a = swrite(a);
  } else {
    if (is_void(format)) {
      if (s == double) {
        if (is_void(ndigits)) ndigits = 5;
        format = swrite(format="%%.%dg", ndigits);
      } else if (s == float) {
        if (is_void(ndigits)) ndigits = 3;
        format = swrite(format="%%.%dg", ndigits);
      } else if (s == long || s == int || s == short) {
        format = "%d";    
      } else if (s == char) {
        format = "0x%02x";
      } else if (s == string) {
        /* should escape quotes in input strings */
        format = "\"%s\"";
      } else {
        error, "bad data type";
      }
    }
    a = swrite(format=format, a);
  }
  if (structof(file) == string) file = open(file, "w");
  cols = swrite(format="(,%d)",indgen(n));
  rows = swrite(format="(%d,)",indgen(m));
  fmt0 = swrite(format="%%%ds", max(strlen(rows)));
  fmt1 = swrite(format=" %%-%ds", max(max(strlen(cols)), max(strlen(a))));
  fmt2 = " %s\n";
  write, file, format=fmt0, "";
  for (j=1 ; j<n ; ++j) write, file, format=fmt1, cols(j);
  write, file, format=fmt2, cols(n);
  for (i=1 ; i<=m ; ++i) {
    write, file, format=fmt0, rows(i);
    for (j=1 ; j<n ; ++j) write, file, format=fmt1, a(i,j);
    write, file, format=fmt2, a(i,n);
  }
}


/*---------------------------------------------------------------------------*/
/* Cholesky Decomposition */

func cholesky(a, raw)
/* DOCUMENT cholesky(a)
       -or- cholesky(a, 0/1)
       
     Given  a  symmetric  positive  definite  matrix  A,  returns  a  lower
     triangular  matrix C  which is  the Cholesky  decomposition of  A such
     that:

       A = C(+,)*C(+,);

     If  optional  second argument  is  true  (non-nil  and non-zero),  the
     scratch values in  the upper triangular part of  C are left unchanged;
     otherwise (the  default behavior), the  upper triangular part of  C is
     filled with zeros.
*/
{
  if (! is_array(a) || structof(a) == complex ||
      (dims = dimsof(a))(1) != 2 || (n = dims(2)) != dims(3))
    error, "expecting a N  N non-complex array";
  a = double(a);

  if ((s = a(1,1)) <= 0.0) error, "the matrix is not positive definite";
  a(1,1) = sqrt(s);
  for (j=2 ; j<=n ; ++j) {
    a(1,j) = (t = a(1,j)/a(1,1));
    s = t*t;
    for (k=2 ; k<j ; ++k) {
      rng = 1:k-1;
      a(k,j) = (t = (a(k,j) - sum(a(rng,k)*a(rng,j)))/a(k,k));
      s += t*t;
    }
    s = a(j,j) - s;
    if (s <= 0.0) error, "the matrix is not positive definite";
    a(j,j) = sqrt(s);
  }
#if 0 /* slower code (in Yorick) but less obscure */ 
  for (j=1 ; j<=n ; ++j) {
    s = 0.0;
    for (k=1 ; k<j ; ++k) {
      if (k == 1) {
        t = a(1,j) / a(1,1);
      } else {
        rng = 1:k-1;
        t = (a(k,j) - sum(a(rng,k)*a(rng,j))) / a(k,k);
      }
      a(k,j) = t;
      s += t*t;
    }
    s = a(j,j) - s;
    if (s <= 0.0) error, "the matrix is not positive definite";
    a(j,j) = sqrt(s);
  }
#endif
  if (! raw) {
    for (k=1 ; k<n ; ++k) a(k+1, 1:k)=0;
  }
  return a;
}


/*---------------------------------------------------------------------------*/
/* Singular Vlaue Decomposition */

local sv_intro;
/* DOCUMENT sv_intro - introduction to SVD Yorick package
 *
 * Notes about matrix multiplication in Yorick:
 *
 *   A.B  = A(,+)*B(+,)
 *   A'.B = A(+,)*B(+,)  // transpose of A times B
 *   A.B' = A(,+)*B(,+)  // A times transpose of B
 *
 *   diag(s).A  = diag(s)(,+)*A(+,)
 *              = s*A = A*s
 *
 *   diag(s).A' = diag(s)(,+)*A(,+)
 *              = transpose(s(-,)*A) = transpose(A*s(-,))
 *
 *   A.diag(s)  = A(,+)*diag(s)(+,)
 *              = A*s(-,) = s(-,)*A
 *
 *   A'.diag(s) = A(+,)*diag(s)(+,) = A(+,)*diag(s)(,+)
 *              = transpose(A*s) = transpose(s*A)
 *
 * Singular Value Decomposition:
 *
 *   A = U.diag(SIGMA).V' = U.diag(SIGMA).VT
 *     = (U*SIGMA(-,))(,+)*VT(+,)
 *     = U(,+)*(SIGMA*VT)(+,)
 *
 * where:
 *
 *   SIGMA = SVdec(A, U, VT)
 *
 * Columns  of  U  and  V form  an  orthonormal basis  (i.e.  U  and V  are
 * column-orthonormal):
 *
 *   U'.U = V'.V = I
 *
 * in Yorick notation:
 *
 *   U(+,)*U(+,) = V(+,)*V(+,) = VT(,+)*VT(,+) = unit(N)
 *
 * Note (to be verified): if U and/or V are square, they are also
 * row-orthonormal:
 *
 *   U'.U = U.U' = I   (if U is square)
 *   V'.V = V.V' = I   (if V is square)
 *
 * Generalized-inverse of A:
 *
 *   AP = V.diag(1/SIGMA).U' = VT'.diag(1/SIGMA).U'
 *     = VT(+,)*((1.0/SIGMA)(-,)*U)(,+)
 *     = ((1.0/SIGMA)*VT)(+,)*U(,+)
 *
 * Solving a linear problem: A.x ~ b with SVD (taking care of index
 * ordering for faster matrix multiplication):
 *
 *   X = V.diag(W).U'.B
 *     = (W*VT)(+,)*U(,+))(,+)*B(+,..)
 *     = (W*VT)(+,)*(U(+,)*B(+,..))(+,..)  // sum over 1st indices: faster
 *
 * where W is an approximation of 1/SIGMA.
 *
 * SEE ALSO: sv_dcmp, sv_solve_trunc, sv_solve_wiener, SVdec, SVsolve.
 */

func sv_dcmp(a, full)
/* DOCUMENT sv_dcmp(a)
       -or- sv_dcmp(a, full)
     Computes the singular value decomposition of matrix A.  The result
     is an array of pointers:

         [&SIGMA, &U, &VT]

     where SIGMA is the vector of  singular values of A, and the columns of
     U  and  the  rows of  VT  are  the  left  and right  singular  vectors
     (respectively).  Using matrix notation:

         A = U.diag(SIGMA).V' = U.diag(SIGMA).VT

     and using Yorick notation:

         A = (U*SIGMA(-,))(,+)*VT(+,)
           = U(,+)*(SIGMA*VT)(+,)

     FULL has the same meaning as keyword FULL in SVdec (to see).
     
   SEE ALSO: sv_dcmp, sv_solve_trunc, sv_solve_wiener, SVdec. */
{
  local u, vt;
  sigma = SVdec(a, u, vt, full=full);
  return [&sigma, &u, &vt];
}

func _sv_get_dcmp(a, full)
/* DOCUMENT local sigma, u, vt; _sv_get_dcmp(a);
       -or- local sigma, u, vt; _sv_get_dcmp(a, full)
     Private routine used to extract Singular Value Decomposition of A into
     external symbols: SIGMA, U and VT.
     
   SEE ALSO: sv_dcmp, sv_solve_trunc, sv_solve_wiener. */
{
  extern sigma, u, vt;
  if (structof(a) == pointer) {
    eq_nocopy, sigma, *a(1);
    eq_nocopy, u,     *a(2);
    eq_nocopy, vt,    *a(3);
  } else {
    sigma = SVdec(a, u, vt, full=full);
  }
}

func sv_solve_trunc(a, b, eps, full=)
/* DOCUMENT sv_solve_trunc(a, b, eps)
     Solve linear problem A.x = b by truncated singular value method.  A is
     either a matrix or the  singular value value decomposition as returned
     by sv_dcmp  (to see).  B  is the right  hand side vector (or  array to
     solve for  several right  hand sides  at a time).   EPS (in  the range
     [0,1])  is the  minimum  relative  singular value  to  keep, i.e.  all
     singular values  less than EPS*max(SIGMA) get discarded  (SIGMA is the
     vector of singular values of A).  The result is:
     
         (W*VT)(+,)*(U(+,)*B(+,..))(+,..)
       
     where  SIGMA,  U and  VT  are the  components  of  the singular  value
     decomposition of A and W is:

         W(i) = 1/SIGMA(i)   if SIGMA(i) > EPS*max(SIGMA);
                0            otherwise.
     
   SEE ALSO: sv_dcmp, sv_intro, sv_solve_wiener. */
{
  local sigma, u, vt;
  _sv_get_dcmp, a, full;
  if (is_void(eps)) eps = 1e-1;
  w = double(sigma > eps*sigma(1))/(sigma + !sigma);
  return (w*vt)(+,)*(u(+,)*b(+,..))(+,..);
}

func sv_solve_wiener(a, b, eps, full=)
/* DOCUMENT sv_solve_wiener(a, b, eps)
     Solve  linear problem  A.x =  b by  Wiener filtering  of  the singular
     values.   A   is  either  a   matrix  or  the  singular   value  value
     decomposition as  returned by sv_dcmp (to  see).  B is  the right hand
     side  vector (or  array to  solve for  several right  hand sides  at a
     time).  EPS (in the range [0,1]) is the relative singular value filter
     level.  The result is:
     
         (W*VT)(+,)*(U(+,)*B(+,..))(+,..)
       
     where  SIGMA,  U and  VT  are the  components  of  the singular  value
     decomposition of A and W is:

         W = SIGMA/(SIGMA^2 + (EPS*max(SIGMA))^2)
     
   SEE ALSO: sv_dcmp, sv_intro, sv_solve_trunc. */
{
  local sigma, u, vt;
  _sv_get_dcmp, a, full;
  if (is_void(eps)) eps = 1e-1;
  w = sigma/(sigma*sigma + (eps*sigma(1))^2);
  return (w*vt)(+,)*(u(+,)*b(+,..))(+,..);
}

/*---------------------------------------------------------------------------*/