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
/*!
linear algebra operations for [nalgebra][1] matrices using [LAPACK][2].

Functions to compute the singular value decomposition (SVD) and eigensystem are
implemented.

# Examples

```rust
extern crate nalgebra_lapack;
extern crate nalgebra as na;

use nalgebra_lapack::{HasSVD, HasEigensystem};

fn main() {
    // Create an input matrix
    let m = na::DMat::from_row_vec(3,5,&[
        -1.01,   0.86,  -4.60,   3.31,  -4.81,
         3.98,   0.53,  -7.04,   5.29,   3.55,
         3.30,   8.26,  -3.89,   8.20,  -1.51]);

    // Now perform SVD
    let (u,s,vt) = m.svd().unwrap();
    println!("u {:?}",u);
    println!("s {:?}",s);
    println!("vt {:?}",vt);

    // Create an input matrix
    let m = na::DMat::from_row_vec(2,2,&[
        2.0, 1.0,
        1.0, 2.0]);

    // Now get the eigensystem
    let (vals, vecs) = m.eigensystem().unwrap();
    println!("eigenvalues {:?}",vals);
    println!("eigenvectors {:?}",vecs);

}
```

[1]: https://crates.io/crates/nalgebra
[2]: https://crates.io/crates/lapack
*/

extern crate nalgebra as na;
extern crate lapack;
extern crate num;

use std::error::Error;
use std::fmt::{self, Display};
use num::complex::Complex;

use na::{DMat, DVec, Iterable};

#[derive(Debug)]
pub struct NalgebraLapackError {
  pub desc: String,
}

pub type NalgebraLapackResult<T> = Result<T, NalgebraLapackError>;

impl Error for NalgebraLapackError {
  fn description(&self) -> &str { &self.desc }
}


impl Display for NalgebraLapackError {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    Display::fmt(&self.desc, f)
  }
}

impl From<String> for NalgebraLapackError {
  fn from(err: String) -> NalgebraLapackError {
    NalgebraLapackError { desc: format!(
                  "String ({})",
                  err
                  ),
    }
  }
}

macro_rules! eigensystem_impl(
    ($t: ty, $lapack_func: path) => (
        impl HasEigensystem<Complex<$t>> for DMat<$t> {
            fn eigensystem(mut self) -> NalgebraLapackResult<(DVec<Complex<$t>>, DMat<Complex<$t>>)> {
                let jobvl = b'N';
                let jobvr = b'V';

                if self.ncols() != self.nrows() {
                    return Err(NalgebraLapackError { desc: "argument to eigen must be square.".to_owned() } );
                }
                let n = self.ncols();

                let lda = n;
                let ldvl = 1;
                let ldvr = n;

                let mut wr: DVec<$t> = DVec::from_elem( n, 0.0);
                let mut wi: DVec<$t> = DVec::from_elem( n, 0.0);

                let mut vl: DVec<$t> = DVec::from_elem( ldvl*ldvl, 0.0);
                let mut vr: DVec<$t> = DVec::from_elem( n*n, 0.0);

                let mut work = vec![0.0];
                let mut lwork = -1;
                let mut info = 0;

                $lapack_func(jobvl, jobvr, n, self.as_mut_vec(), lda, wr.as_mut_slice(),
                    wi.as_mut_slice(), vl.as_mut_slice(), ldvl, vr.as_mut_slice(), ldvr,
                    &mut work, lwork, &mut info);

                if info < 0 {
                  return Err(NalgebraLapackError { desc: "illegal argument in eigensystem.".to_owned() } );
                }

                lwork = work[0] as isize;
                let mut work = vec![0.0; lwork as usize];

                $lapack_func(jobvl, jobvr, n, self.as_mut_vec(), lda, wr.as_mut_slice(),
                    wi.as_mut_slice(), vl.as_mut_slice(), ldvl, vr.as_mut_slice(), ldvr,
                    &mut work, lwork, &mut info);

                if info < 0 {
                    return Err(NalgebraLapackError { desc: format!(
                                  "illegal argument {} to eigensystem()",
                                  -info
                                  ) } );
                }
                if info > 0 {
                    // TODO: should figure out how to return the correct eigenvalues.
                    return Err(NalgebraLapackError { desc: format!(
                                  "The QR algorithm failed to compute all the eigenvalues. {} were computed.",
                                  -info
                                  ) } );
                }

                let x: Vec<Complex<$t>> = wr.iter().zip(wi.iter()).map( |(r,i)| {Complex{re:*r,im:*i}} ).collect();
                let eigen_values = DVec{at:x};
                let mut result: Vec<Complex<$t>> = Vec::with_capacity(n*n);
                for i in (0..n) {
                    let mut j = 0;
                    while j < n {
                        if eigen_values[j].im == 0.0 {
                            result.push( Complex{ re: vr[i+j*ldvr], im: 0.0 });
                            j += 1;
                        } else {
                            result.push( Complex{ re: vr[i+j*ldvr], im: vr[i+(j+1)*ldvr] });
                            result.push( Complex{ re: vr[i+j*ldvr], im: -vr[i+(j+1)*ldvr] });
                            j += 2;
                        }
                    }
                }
                let right_eigen_vectors = DMat::from_row_vec(n,n,&result);
                Ok((eigen_values,right_eigen_vectors))
            }
        }
    );
);

pub trait HasEigensystem<T> {
    /// `eigensystem` computes eigenvalues and right eigenvectors
    ///
    /// Because the input matrix may be overwritten or destroyed, it is consumed.
    ///
    /// # Returns
    ///
    /// * `eigen_values` - The eigenvalues, normalized to have Euclidean norm equal to 1 and largest component real.
    /// * `right_eigen_vectors` - The right eigenvectors. They are contained as columns of this matrix.
    fn eigensystem(mut self) -> NalgebraLapackResult<(DVec<T>, DMat<T>)>;
}

macro_rules! eigensystem_complex_impl(
    ($t: ty, $lapack_func: path) => (
        impl HasEigensystem<Complex<$t>> for DMat<Complex<$t>> {
            fn eigensystem(mut self) -> NalgebraLapackResult<(DVec<Complex<$t>>, DMat<Complex<$t>>)> {
                let jobvl = b'N';
                let jobvr = b'V';

                if self.ncols() != self.nrows() {
                    return Err(NalgebraLapackError { desc: "argument to eigen must be square.".to_owned() } );
                }
                let n = self.ncols();

                let lda = n;
                let ldvl = 1;
                let ldvr = n;

                let mut w: DVec<Complex<$t>> = DVec::from_elem( n, Complex{re:0.0, im:0.0});

                let mut vl: DVec<Complex<$t>> = DVec::from_elem( ldvl*ldvl, Complex{re:0.0, im:0.0});
                let mut vr: DVec<Complex<$t>> = DVec::from_elem( n*n, Complex{re:0.0, im:0.0});

                let mut work = vec![Complex{re:0.0, im:0.0}];
                let mut lwork = -1;
                let mut rwork: Vec<$t> = vec![0.0; (2*n) as usize];

                let mut info = 0;

                $lapack_func(jobvl, jobvr, n, self.as_mut_vec(), lda, w.as_mut_slice(),
                    vl.as_mut_slice(), ldvl, vr.as_mut_slice(), ldvr,
                    & mut work, lwork, & mut rwork, &mut info);

                if info < 0 {
                  return Err(NalgebraLapackError { desc: "illegal argument in eigensystem.".to_owned() } );
                }

                lwork = work[0].re as isize;
                let mut work = vec![Complex{re:0.0, im:0.0}; lwork as usize];

                $lapack_func(jobvl, jobvr, n, self.as_mut_vec(), lda, w.as_mut_slice(),
                    vl.as_mut_slice(), ldvl, vr.as_mut_slice(), ldvr,
                    & mut work, lwork, & mut rwork, &mut info);

                if info < 0 {
                    return Err(NalgebraLapackError { desc: format!(
                                  "illegal argument {} to eigensystem()",
                                  -info
                                  ) } );
                }
                if info > 0 {
                    // TODO: should figure out how to return the correct eigenvalues.
                    return Err(NalgebraLapackError { desc: format!(
                                  "The QR algorithm failed to compute all the eigenvalues. {} were computed.",
                                  -info
                                  ) } );
                }

                let eigen_values = w;
                let right_eigen_vectors = DMat::from_row_vec(n,n,&vr.at);
                Ok((eigen_values,right_eigen_vectors))
            }
        }
    );
);

pub trait HasSVD<T,U> {
    /// `svd` computes the singular value decomposition (SVD). Returns full
    /// matrices.
    ///
    /// Because the input matrix may be overwritten or destroyed, it is consumed.
    ///
    /// # Returns
    ///
    /// * `u` - The left-singular vectors.
    /// * `s` - The singular values.
    /// * `vt` - The right-singular vectors.
    fn svd(mut self) -> NalgebraLapackResult<(DMat<T>, DVec<U>, DMat<T>)>;
}

macro_rules! svd_impl(
    ($t: ty, $lapack_func: path) => (
        impl HasSVD<$t,$t> for DMat<$t> {
            fn svd(mut self) -> NalgebraLapackResult<(DMat<$t>, DVec<$t>, DMat<$t>)> {
                let m = self.nrows();
                let n = self.ncols();

                let jobu = b'A';
                let jobvt = b'A';

                let lda = m;
                let min_mn = if m <= n { m } else {n};
                let mut s: DVec<$t> = DVec::from_elem( min_mn, 0.0);
                let ldu = m;
                let mut u: DMat<$t> = DMat::new_zeros(ldu, m);
                let ldvt = n;
                let mut vt: DMat<$t> = DMat::new_zeros(ldvt, n);
                let mut work = vec![0.0];
                let mut lwork = -1;
                let mut info = 0;

                $lapack_func(jobu, jobvt, m, n, self.as_mut_vec(), lda, &mut s.as_mut_slice(),
                               u.as_mut_vec(), ldu, vt.as_mut_vec(),
                               ldvt, &mut work, lwork, &mut info);
                if info < 0 {
                  return Err(NalgebraLapackError { desc: "illegal argument to svd.".to_owned() } );
                }

                lwork = work[0] as isize;
                work = vec![0.0; lwork as usize];

                $lapack_func(jobu, jobvt, m, n, self.as_mut_vec(), lda, &mut s.as_mut_slice(),
                               u.as_mut_vec(), ldu, vt.as_mut_vec(), ldvt, &mut work,
                               lwork, &mut info);

                if info < 0 {
                    return Err(NalgebraLapackError { desc: format!(
                                  "illegal argument {} in svd",
                                  -info
                                  ) } );
                }
                if info > 0 {
                    return Err(NalgebraLapackError { desc: format!(
                                  "{} superdiagonals did not converge.",
                                  info
                                  ) } );
                }

                Ok((u, s, vt))
            }
        }
    );
);

macro_rules! svd_complex_impl(
    ($t: ty, $lapack_func: path) => (
        impl HasSVD<Complex<$t>,$t> for DMat<Complex<$t>> {
            fn svd(mut self) -> NalgebraLapackResult<(DMat<Complex<$t>>, DVec<$t>, DMat<Complex<$t>>)> {
                let m = self.nrows();
                let n = self.ncols();

                let jobu = b'A';
                let jobvt = b'A';

                let lda = m;
                let min_mn = if m <= n { m } else {n};
                let mut s: DVec<$t> = DVec::from_elem( min_mn, 0.0);
                let ldu = m;
                let mut u: DMat<Complex<$t>> = DMat::new_zeros(ldu, m);
                let ldvt = n;
                let mut vt: DMat<Complex<$t>> = DMat::new_zeros(ldvt, n);
                let mut work: Vec<Complex<$t>> = vec![Complex{re:0.0, im:0.0}];
                let mut lwork = -1;
                let mut rwork: Vec<$t> = vec![0.0; (5*min_mn as usize)];
                let mut info = 0;

                $lapack_func(jobu, jobvt, m, n, self.as_mut_vec(), lda, &mut s.as_mut_slice(),
                             u.as_mut_vec(), ldu, vt.as_mut_vec(), ldvt, &mut work,
                             lwork, &mut rwork, &mut info);

                if info < 0 {
                  return Err(NalgebraLapackError { desc: "illegal argument to svd.".to_owned() } );
                }

                lwork = work[0].re as isize;
                let mut work: Vec<Complex<$t>> = vec![Complex{re:0.0, im:0.0}; lwork as usize];

                $lapack_func(jobu, jobvt, m, n, self.as_mut_vec(), lda, &mut s.as_mut_slice(),
                             u.as_mut_vec(), ldu, vt.as_mut_vec(), ldvt, &mut work,
                             lwork, &mut rwork, &mut info);

                if info < 0 {
                    return Err(NalgebraLapackError { desc: format!(
                                  "illegal argument {} in svd",
                                  -info
                                  ) } );
                }
                if info > 0 {
                    return Err(NalgebraLapackError { desc: format!(
                                  "{} superdiagonals did not converge.",
                                  info
                                  ) } );
                }

                Ok((u, s, vt))
            }
        }
    );
);

eigensystem_impl!(f32, lapack::sgeev);
eigensystem_impl!(f64, lapack::dgeev);
eigensystem_complex_impl!(f32, lapack::cgeev);
eigensystem_complex_impl!(f64, lapack::zgeev);

svd_impl!(f32, lapack::sgesvd);
svd_impl!(f64, lapack::dgesvd);
svd_complex_impl!(f32, lapack::cgesvd);
svd_complex_impl!(f64, lapack::zgesvd);