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
use ndarray::*;
use crate::convert::*;
use crate::error::*;
use crate::layout::*;
use crate::tridiagonal::Tridiagonal;
use crate::types::*;
pub use crate::lapack::NormType;
pub trait OperationNorm {
type Output: Scalar;
fn opnorm(&self, t: NormType) -> Result<Self::Output>;
fn opnorm_one(&self) -> Result<Self::Output> {
self.opnorm(NormType::One)
}
fn opnorm_inf(&self) -> Result<Self::Output> {
self.opnorm(NormType::Infinity)
}
fn opnorm_fro(&self) -> Result<Self::Output> {
self.opnorm(NormType::Frobenius)
}
}
impl<A, S> OperationNorm for ArrayBase<S, Ix2>
where
A: Scalar + Lapack,
S: Data<Elem = A>,
{
type Output = A::Real;
fn opnorm(&self, t: NormType) -> Result<Self::Output> {
let l = self.layout()?;
let a = self.as_allocated()?;
Ok(unsafe { A::opnorm(t, l, a) })
}
}
impl<A> OperationNorm for Tridiagonal<A>
where
A: Scalar + Lapack,
{
type Output = A::Real;
fn opnorm(&self, t: NormType) -> Result<Self::Output> {
let arr = match t {
NormType::One => {
let zl: Array1<A> = Array::zeros(1);
let zu: Array1<A> = Array::zeros(1);
let dl = stack![Axis(0), self.dl.to_owned(), zl];
let du = stack![Axis(0), zu, self.du.to_owned()];
let arr = stack![Axis(0), into_row(du), into_row(arr1(&self.d)), into_row(dl)];
arr
}
NormType::Infinity => {
let zl: Array1<A> = Array::zeros(1);
let zu: Array1<A> = Array::zeros(1);
let dl = stack![Axis(0), zl, self.dl.to_owned()];
let du = stack![Axis(0), self.du.to_owned(), zu];
let arr = stack![Axis(1), into_col(dl), into_col(arr1(&self.d)), into_col(du)];
arr
}
NormType::Frobenius => {
let arr = stack![
Axis(1),
into_row(arr1(&self.dl)),
into_row(arr1(&self.d)),
into_row(arr1(&self.du))
];
arr
}
};
let l = arr.layout()?;
let a = arr.as_allocated()?;
Ok(unsafe { A::opnorm(t, l, a) })
}
}