onemkl 0.3.8

Safe, idiomatic Rust wrapper over Intel oneAPI Math Kernel Library (oneMKL).
Documentation
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
//! Extended Eigensolver — the FEAST contour-integration eigensolver.
//!
//! FEAST finds all eigenvalues and eigenvectors of a Hermitian /
//! symmetric matrix that lie inside a user-specified search interval
//! `[emin, emax]`. It is competitive with traditional QR-based solvers
//! when only a sub-range of the spectrum is needed.
//!
//! The wrappers below cover the standard problem `A x = λ x` for
//! dense, sparse-CSR, and banded `A`, plus the generalized problem
//! `A x = λ B x` for dense `A` / `B`. The reverse-communication
//! interface (`?feast_srci` / `?feast_hrci`) is not yet wrapped.

use core::ffi::{c_char, c_int};

use num_complex::{Complex32, Complex64};
use onemkl_sys as sys;

use crate::enums::UpLo;
use crate::error::{Error, Result};

/// Result of a FEAST solve.
#[derive(Debug, Clone)]
pub struct FeastResult<T, R> {
    /// Eigenvalues in `[emin, emax]`. Length is the number of
    /// converged eigenvalues `m`.
    pub eigenvalues: Vec<R>,
    /// Eigenvectors corresponding to `eigenvalues`. Each column is one
    /// eigenvector; the matrix is column-major with leading dimension
    /// `n` (the matrix order). Total length: `n * m`.
    pub eigenvectors: Vec<T>,
    /// Number of converged eigenvalues.
    pub m: usize,
    /// Relative trace error reported by FEAST.
    pub epsout: R,
    /// Number of refinement loops used.
    pub loop_count: i32,
    /// Per-eigenpair residuals. Length matches eigenvalues.
    pub residuals: Vec<R>,
}

/// Real symmetric dense FEAST solver: returns eigenvalues of `A` in
/// `[emin, emax]`.
///
/// `m0_estimate` is an upper bound on the number of eigenvalues
/// expected in the interval; the algorithm may use less. A common
/// choice is `m0_estimate = 2 * estimate`. The buffers below are
/// sized to `m0_estimate` and trimmed to the actual count on return.
pub fn eigh_real_dense<T: FeastReal>(
    uplo: UpLo,
    a: &[T],
    n: usize,
    lda: usize,
    emin: T,
    emax: T,
    m0_estimate: usize,
) -> Result<FeastResult<T, T>> {
    let n_i: c_int = n.try_into().map_err(|_| Error::DimensionOverflow)?;
    let lda_i: c_int = lda.try_into().map_err(|_| Error::DimensionOverflow)?;
    if a.len() < lda * n {
        return Err(Error::InvalidArgument(
            "A buffer is smaller than lda * n",
        ));
    }
    let m0_i: c_int = m0_estimate
        .try_into()
        .map_err(|_| Error::DimensionOverflow)?;

    let mut fpm: [c_int; 128] = [0; 128];
    unsafe { sys::feastinit(fpm.as_mut_ptr()) };

    let uplo_c: c_char = uplo.as_char() as c_char;
    let mut epsout = T::zero();
    let mut loop_count: c_int = 0;
    let mut m_actual: c_int = 0;
    let mut info: c_int = 0;
    let mut m0_inout: c_int = m0_i;
    let mut eigenvalues: Vec<T> = vec![T::zero(); m0_estimate];
    let mut eigenvectors: Vec<T> = vec![T::zero(); n * m0_estimate];
    let mut residuals: Vec<T> = vec![T::zero(); m0_estimate];

    unsafe {
        T::feast_dense_real(
            &uplo_c,
            &n_i,
            a.as_ptr(),
            &lda_i,
            fpm.as_mut_ptr(),
            &mut epsout,
            &mut loop_count,
            &emin,
            &emax,
            &mut m0_inout,
            eigenvalues.as_mut_ptr(),
            eigenvectors.as_mut_ptr(),
            &mut m_actual,
            residuals.as_mut_ptr(),
            &mut info,
        );
    }

    // info == 0 → success.
    // info == 1 → "no eigenvalue found" (warning); treat as success-with-empty.
    // info > 1 → genuine failure.
    if info > 1 || info < 0 {
        return Err(Error::LapackComputationFailure { info });
    }
    let m = m_actual.max(0) as usize;
    eigenvalues.truncate(m);
    residuals.truncate(m);
    eigenvectors.truncate(n * m);
    Ok(FeastResult {
        eigenvalues,
        eigenvectors,
        m,
        epsout,
        loop_count,
        residuals,
    })
}

/// Complex Hermitian dense FEAST solver.
pub fn eigh_complex_dense<T: FeastComplex>(
    uplo: UpLo,
    a: &[T],
    n: usize,
    lda: usize,
    emin: T::Real,
    emax: T::Real,
    m0_estimate: usize,
) -> Result<FeastResult<T, T::Real>> {
    let n_i: c_int = n.try_into().map_err(|_| Error::DimensionOverflow)?;
    let lda_i: c_int = lda.try_into().map_err(|_| Error::DimensionOverflow)?;
    if a.len() < lda * n {
        return Err(Error::InvalidArgument(
            "A buffer is smaller than lda * n",
        ));
    }
    let m0_i: c_int = m0_estimate
        .try_into()
        .map_err(|_| Error::DimensionOverflow)?;

    let mut fpm: [c_int; 128] = [0; 128];
    unsafe { sys::feastinit(fpm.as_mut_ptr()) };

    let uplo_c: c_char = uplo.as_char() as c_char;
    let mut epsout = T::Real::zero();
    let mut loop_count: c_int = 0;
    let mut m_actual: c_int = 0;
    let mut info: c_int = 0;
    let mut m0_inout: c_int = m0_i;
    let mut eigenvalues: Vec<T::Real> = vec![T::Real::zero(); m0_estimate];
    let mut eigenvectors: Vec<T> = vec![T::zero_complex(); n * m0_estimate];
    let mut residuals: Vec<T::Real> = vec![T::Real::zero(); m0_estimate];

    unsafe {
        T::feast_dense_complex(
            &uplo_c,
            &n_i,
            a.as_ptr(),
            &lda_i,
            fpm.as_mut_ptr(),
            &mut epsout,
            &mut loop_count,
            &emin,
            &emax,
            &mut m0_inout,
            eigenvalues.as_mut_ptr(),
            eigenvectors.as_mut_ptr(),
            &mut m_actual,
            residuals.as_mut_ptr(),
            &mut info,
        );
    }

    // info == 0 → success.
    // info == 1 → "no eigenvalue found" (warning); treat as success-with-empty.
    // info > 1 → genuine failure.
    if info > 1 || info < 0 {
        return Err(Error::LapackComputationFailure { info });
    }
    let m = m_actual.max(0) as usize;
    eigenvalues.truncate(m);
    residuals.truncate(m);
    eigenvectors.truncate(n * m);
    Ok(FeastResult {
        eigenvalues,
        eigenvectors,
        m,
        epsout,
        loop_count,
        residuals,
    })
}

/// Real symmetric CSR FEAST solver. Wraps `?feast_scsrev`.
///
/// `sa`, `isa`, `jsa` are 1-based CSR storage of `A`. The wrapper
/// stores only the upper or lower triangle as selected by `uplo`.
#[allow(clippy::too_many_arguments)]
pub fn eigh_real_csr<T: FeastReal>(
    uplo: UpLo,
    n: usize,
    sa: &[T],
    isa: &[i32],
    jsa: &[i32],
    emin: T,
    emax: T,
    m0_estimate: usize,
) -> Result<FeastResult<T, T>> {
    if isa.len() != n + 1 {
        return Err(Error::InvalidArgument("isa must have length n + 1"));
    }
    if jsa.len() != sa.len() {
        return Err(Error::InvalidArgument(
            "jsa and sa must have the same length",
        ));
    }
    let n_i: c_int = n.try_into().map_err(|_| Error::DimensionOverflow)?;
    let m0_i: c_int = m0_estimate
        .try_into()
        .map_err(|_| Error::DimensionOverflow)?;
    let mut fpm: [c_int; 128] = [0; 128];
    unsafe { sys::feastinit(fpm.as_mut_ptr()) };
    let uplo_c: c_char = uplo.as_char() as c_char;
    let mut epsout = T::zero();
    let mut loop_count: c_int = 0;
    let mut m_actual: c_int = 0;
    let mut info: c_int = 0;
    let mut m0_inout: c_int = m0_i;
    let mut eigenvalues: Vec<T> = vec![T::zero(); m0_estimate];
    let mut eigenvectors: Vec<T> = vec![T::zero(); n * m0_estimate];
    let mut residuals: Vec<T> = vec![T::zero(); m0_estimate];

    unsafe {
        T::feast_csr_real(
            &uplo_c, &n_i, sa.as_ptr(), isa.as_ptr(), jsa.as_ptr(),
            fpm.as_mut_ptr(), &mut epsout, &mut loop_count,
            &emin, &emax, &mut m0_inout,
            eigenvalues.as_mut_ptr(), eigenvectors.as_mut_ptr(),
            &mut m_actual, residuals.as_mut_ptr(), &mut info,
        );
    }
    finish_real_result(info, m_actual, n, eigenvalues, eigenvectors, residuals,
        epsout, loop_count)
}

/// Complex Hermitian CSR FEAST solver. Wraps `?feast_hcsrev`.
#[allow(clippy::too_many_arguments)]
pub fn eigh_complex_csr<T: FeastComplex>(
    uplo: UpLo,
    n: usize,
    sa: &[T],
    isa: &[i32],
    jsa: &[i32],
    emin: T::Real,
    emax: T::Real,
    m0_estimate: usize,
) -> Result<FeastResult<T, T::Real>> {
    if isa.len() != n + 1 {
        return Err(Error::InvalidArgument("isa must have length n + 1"));
    }
    if jsa.len() != sa.len() {
        return Err(Error::InvalidArgument(
            "jsa and sa must have the same length",
        ));
    }
    let n_i: c_int = n.try_into().map_err(|_| Error::DimensionOverflow)?;
    let m0_i: c_int = m0_estimate
        .try_into()
        .map_err(|_| Error::DimensionOverflow)?;
    let mut fpm: [c_int; 128] = [0; 128];
    unsafe { sys::feastinit(fpm.as_mut_ptr()) };
    let uplo_c: c_char = uplo.as_char() as c_char;
    let mut epsout = T::Real::zero();
    let mut loop_count: c_int = 0;
    let mut m_actual: c_int = 0;
    let mut info: c_int = 0;
    let mut m0_inout: c_int = m0_i;
    let mut eigenvalues: Vec<T::Real> = vec![T::Real::zero(); m0_estimate];
    let mut eigenvectors: Vec<T> = vec![T::zero_complex(); n * m0_estimate];
    let mut residuals: Vec<T::Real> = vec![T::Real::zero(); m0_estimate];

    unsafe {
        T::feast_csr_complex(
            &uplo_c, &n_i, sa.as_ptr(), isa.as_ptr(), jsa.as_ptr(),
            fpm.as_mut_ptr(), &mut epsout, &mut loop_count,
            &emin, &emax, &mut m0_inout,
            eigenvalues.as_mut_ptr(), eigenvectors.as_mut_ptr(),
            &mut m_actual, residuals.as_mut_ptr(), &mut info,
        );
    }
    finish_complex_result(info, m_actual, n, eigenvalues, eigenvectors, residuals,
        epsout, loop_count)
}

/// Real symmetric banded FEAST solver. Wraps `?feast_sbev`.
///
/// `kla` is the number of subdiagonals (or superdiagonals — must be
/// equal because `A` is symmetric). The matrix `a` is stored in LAPACK
/// band format with leading dimension `lda >= kla + 1`.
#[allow(clippy::too_many_arguments)]
pub fn eigh_real_banded<T: FeastReal>(
    uplo: UpLo,
    n: usize,
    kla: usize,
    a: &[T],
    lda: usize,
    emin: T,
    emax: T,
    m0_estimate: usize,
) -> Result<FeastResult<T, T>> {
    if a.len() < lda * n {
        return Err(Error::InvalidArgument("A buffer is smaller than lda * n"));
    }
    let n_i: c_int = n.try_into().map_err(|_| Error::DimensionOverflow)?;
    let kla_i: c_int = kla.try_into().map_err(|_| Error::DimensionOverflow)?;
    let lda_i: c_int = lda.try_into().map_err(|_| Error::DimensionOverflow)?;
    let m0_i: c_int = m0_estimate
        .try_into()
        .map_err(|_| Error::DimensionOverflow)?;
    let mut fpm: [c_int; 128] = [0; 128];
    unsafe { sys::feastinit(fpm.as_mut_ptr()) };
    let uplo_c: c_char = uplo.as_char() as c_char;
    let mut epsout = T::zero();
    let mut loop_count: c_int = 0;
    let mut m_actual: c_int = 0;
    let mut info: c_int = 0;
    let mut m0_inout: c_int = m0_i;
    let mut eigenvalues: Vec<T> = vec![T::zero(); m0_estimate];
    let mut eigenvectors: Vec<T> = vec![T::zero(); n * m0_estimate];
    let mut residuals: Vec<T> = vec![T::zero(); m0_estimate];

    unsafe {
        T::feast_banded_real(
            &uplo_c, &n_i, &kla_i, a.as_ptr(), &lda_i,
            fpm.as_mut_ptr(), &mut epsout, &mut loop_count,
            &emin, &emax, &mut m0_inout,
            eigenvalues.as_mut_ptr(), eigenvectors.as_mut_ptr(),
            &mut m_actual, residuals.as_mut_ptr(), &mut info,
        );
    }
    finish_real_result(info, m_actual, n, eigenvalues, eigenvectors, residuals,
        epsout, loop_count)
}

/// Complex Hermitian banded FEAST solver. Wraps `?feast_hbev`.
#[allow(clippy::too_many_arguments)]
pub fn eigh_complex_banded<T: FeastComplex>(
    uplo: UpLo,
    n: usize,
    kla: usize,
    a: &[T],
    lda: usize,
    emin: T::Real,
    emax: T::Real,
    m0_estimate: usize,
) -> Result<FeastResult<T, T::Real>> {
    if a.len() < lda * n {
        return Err(Error::InvalidArgument("A buffer is smaller than lda * n"));
    }
    let n_i: c_int = n.try_into().map_err(|_| Error::DimensionOverflow)?;
    let kla_i: c_int = kla.try_into().map_err(|_| Error::DimensionOverflow)?;
    let lda_i: c_int = lda.try_into().map_err(|_| Error::DimensionOverflow)?;
    let m0_i: c_int = m0_estimate
        .try_into()
        .map_err(|_| Error::DimensionOverflow)?;
    let mut fpm: [c_int; 128] = [0; 128];
    unsafe { sys::feastinit(fpm.as_mut_ptr()) };
    let uplo_c: c_char = uplo.as_char() as c_char;
    let mut epsout = T::Real::zero();
    let mut loop_count: c_int = 0;
    let mut m_actual: c_int = 0;
    let mut info: c_int = 0;
    let mut m0_inout: c_int = m0_i;
    let mut eigenvalues: Vec<T::Real> = vec![T::Real::zero(); m0_estimate];
    let mut eigenvectors: Vec<T> = vec![T::zero_complex(); n * m0_estimate];
    let mut residuals: Vec<T::Real> = vec![T::Real::zero(); m0_estimate];

    unsafe {
        T::feast_banded_complex(
            &uplo_c, &n_i, &kla_i, a.as_ptr(), &lda_i,
            fpm.as_mut_ptr(), &mut epsout, &mut loop_count,
            &emin, &emax, &mut m0_inout,
            eigenvalues.as_mut_ptr(), eigenvectors.as_mut_ptr(),
            &mut m_actual, residuals.as_mut_ptr(), &mut info,
        );
    }
    finish_complex_result(info, m_actual, n, eigenvalues, eigenvectors, residuals,
        epsout, loop_count)
}

/// Real symmetric generalized dense FEAST solver: solves
/// `A x = λ B x` with `A` symmetric and `B` symmetric positive
/// definite. Wraps `?feast_sygv`.
#[allow(clippy::too_many_arguments)]
pub fn gen_eigh_real_dense<T: FeastReal>(
    uplo: UpLo,
    n: usize,
    a: &[T],
    lda: usize,
    b: &[T],
    ldb: usize,
    emin: T,
    emax: T,
    m0_estimate: usize,
) -> Result<FeastResult<T, T>> {
    if a.len() < lda * n || b.len() < ldb * n {
        return Err(Error::InvalidArgument(
            "A or B buffer is smaller than its leading dimension * n",
        ));
    }
    let n_i: c_int = n.try_into().map_err(|_| Error::DimensionOverflow)?;
    let lda_i: c_int = lda.try_into().map_err(|_| Error::DimensionOverflow)?;
    let ldb_i: c_int = ldb.try_into().map_err(|_| Error::DimensionOverflow)?;
    let m0_i: c_int = m0_estimate
        .try_into()
        .map_err(|_| Error::DimensionOverflow)?;
    let mut fpm: [c_int; 128] = [0; 128];
    unsafe { sys::feastinit(fpm.as_mut_ptr()) };
    let uplo_c: c_char = uplo.as_char() as c_char;
    let mut epsout = T::zero();
    let mut loop_count: c_int = 0;
    let mut m_actual: c_int = 0;
    let mut info: c_int = 0;
    let mut m0_inout: c_int = m0_i;
    let mut eigenvalues: Vec<T> = vec![T::zero(); m0_estimate];
    let mut eigenvectors: Vec<T> = vec![T::zero(); n * m0_estimate];
    let mut residuals: Vec<T> = vec![T::zero(); m0_estimate];

    unsafe {
        T::feast_dense_gen_real(
            &uplo_c, &n_i, a.as_ptr(), &lda_i, b.as_ptr(), &ldb_i,
            fpm.as_mut_ptr(), &mut epsout, &mut loop_count,
            &emin, &emax, &mut m0_inout,
            eigenvalues.as_mut_ptr(), eigenvectors.as_mut_ptr(),
            &mut m_actual, residuals.as_mut_ptr(), &mut info,
        );
    }
    finish_real_result(info, m_actual, n, eigenvalues, eigenvectors, residuals,
        epsout, loop_count)
}

/// Complex Hermitian generalized dense FEAST solver: solves
/// `A x = λ B x` with `A` Hermitian and `B` Hermitian positive
/// definite. Wraps `?feast_hegv`.
#[allow(clippy::too_many_arguments)]
pub fn gen_eigh_complex_dense<T: FeastComplex>(
    uplo: UpLo,
    n: usize,
    a: &[T],
    lda: usize,
    b: &[T],
    ldb: usize,
    emin: T::Real,
    emax: T::Real,
    m0_estimate: usize,
) -> Result<FeastResult<T, T::Real>> {
    if a.len() < lda * n || b.len() < ldb * n {
        return Err(Error::InvalidArgument(
            "A or B buffer is smaller than its leading dimension * n",
        ));
    }
    let n_i: c_int = n.try_into().map_err(|_| Error::DimensionOverflow)?;
    let lda_i: c_int = lda.try_into().map_err(|_| Error::DimensionOverflow)?;
    let ldb_i: c_int = ldb.try_into().map_err(|_| Error::DimensionOverflow)?;
    let m0_i: c_int = m0_estimate
        .try_into()
        .map_err(|_| Error::DimensionOverflow)?;
    let mut fpm: [c_int; 128] = [0; 128];
    unsafe { sys::feastinit(fpm.as_mut_ptr()) };
    let uplo_c: c_char = uplo.as_char() as c_char;
    let mut epsout = T::Real::zero();
    let mut loop_count: c_int = 0;
    let mut m_actual: c_int = 0;
    let mut info: c_int = 0;
    let mut m0_inout: c_int = m0_i;
    let mut eigenvalues: Vec<T::Real> = vec![T::Real::zero(); m0_estimate];
    let mut eigenvectors: Vec<T> = vec![T::zero_complex(); n * m0_estimate];
    let mut residuals: Vec<T::Real> = vec![T::Real::zero(); m0_estimate];

    unsafe {
        T::feast_dense_gen_complex(
            &uplo_c, &n_i, a.as_ptr(), &lda_i, b.as_ptr(), &ldb_i,
            fpm.as_mut_ptr(), &mut epsout, &mut loop_count,
            &emin, &emax, &mut m0_inout,
            eigenvalues.as_mut_ptr(), eigenvectors.as_mut_ptr(),
            &mut m_actual, residuals.as_mut_ptr(), &mut info,
        );
    }
    finish_complex_result(info, m_actual, n, eigenvalues, eigenvectors, residuals,
        epsout, loop_count)
}

#[allow(clippy::too_many_arguments)]
fn finish_real_result<T: FeastReal>(
    info: c_int,
    m_actual: c_int,
    n: usize,
    mut eigenvalues: Vec<T>,
    mut eigenvectors: Vec<T>,
    mut residuals: Vec<T>,
    epsout: T,
    loop_count: c_int,
) -> Result<FeastResult<T, T>> {
    if info > 1 || info < 0 {
        return Err(Error::LapackComputationFailure { info });
    }
    let m = m_actual.max(0) as usize;
    eigenvalues.truncate(m);
    residuals.truncate(m);
    eigenvectors.truncate(n * m);
    Ok(FeastResult {
        eigenvalues,
        eigenvectors,
        m,
        epsout,
        loop_count,
        residuals,
    })
}

#[allow(clippy::too_many_arguments)]
fn finish_complex_result<T: FeastComplex>(
    info: c_int,
    m_actual: c_int,
    n: usize,
    mut eigenvalues: Vec<T::Real>,
    mut eigenvectors: Vec<T>,
    mut residuals: Vec<T::Real>,
    epsout: T::Real,
    loop_count: c_int,
) -> Result<FeastResult<T, T::Real>> {
    if info > 1 || info < 0 {
        return Err(Error::LapackComputationFailure { info });
    }
    let m = m_actual.max(0) as usize;
    eigenvalues.truncate(m);
    residuals.truncate(m);
    eigenvectors.truncate(n * m);
    Ok(FeastResult {
        eigenvalues,
        eigenvectors,
        m,
        epsout,
        loop_count,
        residuals,
    })
}

// =====================================================================
// Trait wiring
// =====================================================================

/// Real scalar types supported by FEAST dense drivers.
#[allow(missing_docs)]
pub trait FeastReal: Copy + 'static {
    fn zero() -> Self;
    #[allow(clippy::too_many_arguments)]
    unsafe fn feast_dense_real(
        uplo: *const c_char,
        n: *const c_int,
        a: *const Self,
        lda: *const c_int,
        fpm: *mut c_int,
        epsout: *mut Self,
        loop_: *mut c_int,
        emin: *const Self,
        emax: *const Self,
        m0: *mut c_int,
        e: *mut Self,
        x: *mut Self,
        m: *mut c_int,
        res: *mut Self,
        info: *mut c_int,
    );

    #[allow(clippy::too_many_arguments)]
    unsafe fn feast_csr_real(
        uplo: *const c_char,
        n: *const c_int,
        sa: *const Self,
        isa: *const c_int,
        jsa: *const c_int,
        fpm: *mut c_int,
        epsout: *mut Self,
        loop_: *mut c_int,
        emin: *const Self,
        emax: *const Self,
        m0: *mut c_int,
        e: *mut Self,
        x: *mut Self,
        m: *mut c_int,
        res: *mut Self,
        info: *mut c_int,
    );

    #[allow(clippy::too_many_arguments)]
    unsafe fn feast_banded_real(
        uplo: *const c_char,
        n: *const c_int,
        kla: *const c_int,
        a: *const Self,
        lda: *const c_int,
        fpm: *mut c_int,
        epsout: *mut Self,
        loop_: *mut c_int,
        emin: *const Self,
        emax: *const Self,
        m0: *mut c_int,
        e: *mut Self,
        x: *mut Self,
        m: *mut c_int,
        res: *mut Self,
        info: *mut c_int,
    );

    #[allow(clippy::too_many_arguments)]
    unsafe fn feast_dense_gen_real(
        uplo: *const c_char,
        n: *const c_int,
        a: *const Self,
        lda: *const c_int,
        b: *const Self,
        ldb: *const c_int,
        fpm: *mut c_int,
        epsout: *mut Self,
        loop_: *mut c_int,
        emin: *const Self,
        emax: *const Self,
        m0: *mut c_int,
        e: *mut Self,
        x: *mut Self,
        m: *mut c_int,
        res: *mut Self,
        info: *mut c_int,
    );
}

macro_rules! impl_feast_real {
    ($ty:ty,
        syev=$syev:ident, scsrev=$scsrev:ident,
        sbev=$sbev:ident, sygv=$sygv:ident
    ) => {
        impl FeastReal for $ty {
            fn zero() -> Self {
                0.0
            }
            unsafe fn feast_dense_real(
                uplo: *const c_char, n: *const c_int,
                a: *const Self, lda: *const c_int,
                fpm: *mut c_int, epsout: *mut Self, loop_: *mut c_int,
                emin: *const Self, emax: *const Self,
                m0: *mut c_int, e: *mut Self, x: *mut Self,
                m: *mut c_int, res: *mut Self, info: *mut c_int,
            ) {
                unsafe {
                    sys::$syev(
                        uplo, n, a, lda, fpm, epsout, loop_, emin, emax, m0, e, x, m, res, info,
                    )
                }
            }
            unsafe fn feast_csr_real(
                uplo: *const c_char, n: *const c_int,
                sa: *const Self, isa: *const c_int, jsa: *const c_int,
                fpm: *mut c_int, epsout: *mut Self, loop_: *mut c_int,
                emin: *const Self, emax: *const Self,
                m0: *mut c_int, e: *mut Self, x: *mut Self,
                m: *mut c_int, res: *mut Self, info: *mut c_int,
            ) {
                unsafe {
                    sys::$scsrev(
                        uplo, n, sa, isa, jsa, fpm, epsout, loop_, emin, emax,
                        m0, e, x, m, res, info,
                    )
                }
            }
            unsafe fn feast_banded_real(
                uplo: *const c_char, n: *const c_int, kla: *const c_int,
                a: *const Self, lda: *const c_int,
                fpm: *mut c_int, epsout: *mut Self, loop_: *mut c_int,
                emin: *const Self, emax: *const Self,
                m0: *mut c_int, e: *mut Self, x: *mut Self,
                m: *mut c_int, res: *mut Self, info: *mut c_int,
            ) {
                unsafe {
                    sys::$sbev(
                        uplo, n, kla, a, lda, fpm, epsout, loop_, emin, emax,
                        m0, e, x, m, res, info,
                    )
                }
            }
            unsafe fn feast_dense_gen_real(
                uplo: *const c_char, n: *const c_int,
                a: *const Self, lda: *const c_int,
                b: *const Self, ldb: *const c_int,
                fpm: *mut c_int, epsout: *mut Self, loop_: *mut c_int,
                emin: *const Self, emax: *const Self,
                m0: *mut c_int, e: *mut Self, x: *mut Self,
                m: *mut c_int, res: *mut Self, info: *mut c_int,
            ) {
                unsafe {
                    sys::$sygv(
                        uplo, n, a, lda, b, ldb, fpm, epsout, loop_, emin, emax,
                        m0, e, x, m, res, info,
                    )
                }
            }
        }
    };
}

impl_feast_real!(f32,
    syev=sfeast_syev, scsrev=sfeast_scsrev, sbev=sfeast_sbev, sygv=sfeast_sygv);
impl_feast_real!(f64,
    syev=dfeast_syev, scsrev=dfeast_scsrev, sbev=dfeast_sbev, sygv=dfeast_sygv);

/// Complex scalar types supported by FEAST drivers.
#[allow(missing_docs)]
pub trait FeastComplex: Copy + 'static {
    /// The matching real scalar type (used for eigenvalues, residuals,
    /// and the search interval `[emin, emax]`).
    type Real: FeastReal;
    fn zero_complex() -> Self;
    #[allow(clippy::too_many_arguments)]
    unsafe fn feast_dense_complex(
        uplo: *const c_char, n: *const c_int,
        a: *const Self, lda: *const c_int,
        fpm: *mut c_int, epsout: *mut Self::Real, loop_: *mut c_int,
        emin: *const Self::Real, emax: *const Self::Real,
        m0: *mut c_int, e: *mut Self::Real, x: *mut Self,
        m: *mut c_int, res: *mut Self::Real, info: *mut c_int,
    );

    #[allow(clippy::too_many_arguments)]
    unsafe fn feast_csr_complex(
        uplo: *const c_char, n: *const c_int,
        sa: *const Self, isa: *const c_int, jsa: *const c_int,
        fpm: *mut c_int, epsout: *mut Self::Real, loop_: *mut c_int,
        emin: *const Self::Real, emax: *const Self::Real,
        m0: *mut c_int, e: *mut Self::Real, x: *mut Self,
        m: *mut c_int, res: *mut Self::Real, info: *mut c_int,
    );

    #[allow(clippy::too_many_arguments)]
    unsafe fn feast_banded_complex(
        uplo: *const c_char, n: *const c_int, kla: *const c_int,
        a: *const Self, lda: *const c_int,
        fpm: *mut c_int, epsout: *mut Self::Real, loop_: *mut c_int,
        emin: *const Self::Real, emax: *const Self::Real,
        m0: *mut c_int, e: *mut Self::Real, x: *mut Self,
        m: *mut c_int, res: *mut Self::Real, info: *mut c_int,
    );

    #[allow(clippy::too_many_arguments)]
    unsafe fn feast_dense_gen_complex(
        uplo: *const c_char, n: *const c_int,
        a: *const Self, lda: *const c_int,
        b: *const Self, ldb: *const c_int,
        fpm: *mut c_int, epsout: *mut Self::Real, loop_: *mut c_int,
        emin: *const Self::Real, emax: *const Self::Real,
        m0: *mut c_int, e: *mut Self::Real, x: *mut Self,
        m: *mut c_int, res: *mut Self::Real, info: *mut c_int,
    );
}

macro_rules! impl_feast_complex {
    ($ty:ty, $real:ty,
        heev=$heev:ident, hcsrev=$hcsrev:ident,
        hbev=$hbev:ident, hegv=$hegv:ident
    ) => {
        impl FeastComplex for $ty {
            type Real = $real;
            fn zero_complex() -> Self {
                <$ty>::new(0.0, 0.0)
            }
            unsafe fn feast_dense_complex(
                uplo: *const c_char, n: *const c_int,
                a: *const Self, lda: *const c_int,
                fpm: *mut c_int, epsout: *mut Self::Real, loop_: *mut c_int,
                emin: *const Self::Real, emax: *const Self::Real,
                m0: *mut c_int, e: *mut Self::Real, x: *mut Self,
                m: *mut c_int, res: *mut Self::Real, info: *mut c_int,
            ) {
                unsafe {
                    sys::$heev(
                        uplo, n, a.cast(), lda, fpm, epsout, loop_, emin, emax,
                        m0, e, x.cast(), m, res, info,
                    )
                }
            }
            unsafe fn feast_csr_complex(
                uplo: *const c_char, n: *const c_int,
                sa: *const Self, isa: *const c_int, jsa: *const c_int,
                fpm: *mut c_int, epsout: *mut Self::Real, loop_: *mut c_int,
                emin: *const Self::Real, emax: *const Self::Real,
                m0: *mut c_int, e: *mut Self::Real, x: *mut Self,
                m: *mut c_int, res: *mut Self::Real, info: *mut c_int,
            ) {
                unsafe {
                    sys::$hcsrev(
                        uplo, n, sa.cast(), isa, jsa, fpm, epsout, loop_,
                        emin, emax, m0, e, x.cast(), m, res, info,
                    )
                }
            }
            unsafe fn feast_banded_complex(
                uplo: *const c_char, n: *const c_int, kla: *const c_int,
                a: *const Self, lda: *const c_int,
                fpm: *mut c_int, epsout: *mut Self::Real, loop_: *mut c_int,
                emin: *const Self::Real, emax: *const Self::Real,
                m0: *mut c_int, e: *mut Self::Real, x: *mut Self,
                m: *mut c_int, res: *mut Self::Real, info: *mut c_int,
            ) {
                unsafe {
                    sys::$hbev(
                        uplo, n, kla, a.cast(), lda, fpm, epsout, loop_,
                        emin, emax, m0, e, x.cast(), m, res, info,
                    )
                }
            }
            unsafe fn feast_dense_gen_complex(
                uplo: *const c_char, n: *const c_int,
                a: *const Self, lda: *const c_int,
                b: *const Self, ldb: *const c_int,
                fpm: *mut c_int, epsout: *mut Self::Real, loop_: *mut c_int,
                emin: *const Self::Real, emax: *const Self::Real,
                m0: *mut c_int, e: *mut Self::Real, x: *mut Self,
                m: *mut c_int, res: *mut Self::Real, info: *mut c_int,
            ) {
                unsafe {
                    sys::$hegv(
                        uplo, n, a.cast(), lda, b.cast(), ldb, fpm, epsout, loop_,
                        emin, emax, m0, e, x.cast(), m, res, info,
                    )
                }
            }
        }
    };
}

impl_feast_complex!(Complex32, f32,
    heev=cfeast_heev, hcsrev=cfeast_hcsrev, hbev=cfeast_hbev, hegv=cfeast_hegv);
impl_feast_complex!(Complex64, f64,
    heev=zfeast_heev, hcsrev=zfeast_hcsrev, hbev=zfeast_hbev, hegv=zfeast_hegv);