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
/// Test approx equality of two arrays element-wise
///
/// # Panics
/// Panics when difference is larger than 1e-3.
#[allow(dead_code)]
pub fn approx_eq<A>(vec_a: &[A], vec_b: &[A])
where
    A: crate::types::FloatNum + std::fmt::Display,
{
    let tol = A::from_f64(1e-3).unwrap();
    for (a, b) in vec_a.iter().zip(vec_b.iter()) {
        assert!(
            ((*a - *b).abs() < tol),
            "Large difference of values, got {} expected {}.",
            b,
            a
        );
    }
}

/// Test approx equality of two arrays element-wise
///
/// # Panics
/// Panics when difference is larger than 1e-3.
#[allow(dead_code)]
pub fn approx_eq_complex<A>(vec_a: &[num_complex::Complex<A>], vec_b: &[num_complex::Complex<A>])
where
    A: crate::types::FloatNum + std::fmt::Display,
{
    let tol = A::from_f64(1e-3).unwrap();
    for (a, b) in vec_a.iter().zip(vec_b.iter()) {
        assert!(
            ((a.re - b.re).abs() < tol || (a.im - b.im).abs() < tol),
            "Large difference of values, got {} expected {}.",
            b,
            a
        );
    }
}

/// Test approx equality of two arrays element-wise
///
/// # Panics
/// Panics when difference is larger than 1e-3.
pub fn approx_eq_ndarray<A, S, D>(
    result: &ndarray::ArrayBase<S, D>,
    expected: &ndarray::ArrayBase<S, D>,
) where
    A: crate::types::FloatNum + std::fmt::Display,
    S: ndarray::Data<Elem = A>,
    D: ndarray::Dimension,
{
    let tol = A::from_f64(1e-3).unwrap();
    for (a, b) in expected.iter().zip(result.iter()) {
        assert!(
            ((*a - *b).abs() < tol),
            "Large difference of values, got {} expected {}.",
            b,
            a
        );
    }
}

/// Test approx equality of two arrays element-wise
///
/// # Panics
/// Panics when difference is larger than 1e-3.
pub fn approx_eq_complex_ndarray<A, S, D>(
    result: &ndarray::ArrayBase<S, D>,
    expected: &ndarray::ArrayBase<S, D>,
) where
    A: crate::types::FloatNum + std::fmt::Display,
    S: ndarray::Data<Elem = num_complex::Complex<A>>,
    D: ndarray::Dimension,
{
    let tol = A::from_f64(1e-3).unwrap();
    for (a, b) in expected.iter().zip(result.iter()) {
        assert!(
            ((a.re - b.re).abs() < tol || (a.im - b.im).abs() < tol),
            "Large difference of values, got {} expected {}.",
            b,
            a
        );
    }
}

/// Returns a new array with same dimensionality
/// but different size *n* along the specified *axis*.
///
/// # Example
/// ```
/// use funspace::utils::array_resized_axis;
/// let array = ndarray::Array2::<f64>::zeros((5, 3));
/// let resized: ndarray::Array2<f64> = array_resized_axis(&array, 2, 1);
/// assert!(resized == ndarray::Array2::zeros((5, 2)));
/// ```
pub fn array_resized_axis<A, S, D, T>(
    input: &ndarray::ArrayBase<S, D>,
    size: usize,
    axis: usize,
) -> ndarray::Array<T, D>
where
    T: num_traits::Zero + std::clone::Clone,
    S: ndarray::Data<Elem = A>,
    D: ndarray::Dimension,
{
    // Get dim
    let mut dim = input.raw_dim();

    // Replace position in dim
    dim[axis] = size;

    // Return
    ndarray::Array::<T, D>::zeros(dim)
}

/// Checks size of axis.
///
/// # Panics
/// Panics when inputs shape does not match
/// axis' size
///
/// # Example
/// ```should_panic
/// use funspace::utils::check_array_axis;
/// let array = ndarray::Array2::<f64>::zeros((5, 3));
/// check_array_axis(&array, 3, 0, "");
/// ```
pub fn check_array_axis<A, S, D>(
    input: &ndarray::ArrayBase<S, D>,
    size: usize,
    axis: usize,
    function_name: &str,
) where
    S: ndarray::Data<Elem = A>,
    D: ndarray::Dimension,
{
    // Arrays size
    let m = input.shape()[axis];

    assert!(
        input.shape()[axis] == size,
        "Size mismatch in {}, got {} expected {} along axis {}",
        function_name,
        size,
        m,
        axis
    );
}

// /// Checks size of axis.
// ///
// /// # Panics
// /// Panics when inputs shape does not match
// /// axis' size
// ///
// /// # Example
// /// ```should_panic
// /// use funspace::utils::check_array_axis;
// /// let array = ndarray::Array2::<f64>::zeros((5, 3));
// /// check_array_axis(&array, 3, 0, None);
// /// ```
// pub fn check_array_axis<A, S, D>(
//     input: &ndarray::ArrayBase<S, D>,
//     size: usize,
//     axis: usize,
//     function_name: Option<&str>,
// ) where
//     A: ndarray::LinalgScalar,
//     S: ndarray::Data<Elem = A>,
//     D: ndarray::Dimension,
// {
//     // Arrays size
//     let m = input.shape()[axis];
//
//     // Panic
//     if size != m {
//         if let Some(name) = function_name {
//             panic!(
//                 "Size mismatch in {}, got {} expected {} along axis {}",
//                 name, size, m, axis
//             );
//         } else {
//             panic!(
//                 "Size mismatch, got {} expected {} along axis {}",
//                 size, m, axis
//             );
//         };
//     }
// }