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
use num_traits::{Float, One, Zero};
use std::fmt;
use std::iter::Flatten;
use std::ops::{
    Add, AddAssign, Div, DivAssign, Index, IndexMut, Mul, MulAssign, Neg, Rem, RemAssign, Sub,
    SubAssign,
};
use std::slice::Iter;

/// A statically allocated MxN matrix. The struct is used in the vector (hyper) dual numbers
/// and provides utilities for the calculation of Jacobians.
#[derive(PartialEq, Clone, Copy, Debug)]
pub struct StaticMat<T, const M: usize, const N: usize>([[T; N]; M]);

pub type StaticVec<T, const N: usize> = StaticMat<T, 1, N>;

impl<T, const M: usize, const N: usize> StaticMat<T, M, N> {
    /// Create a new StaticMat from an array of arrays.
    pub fn new(mat: [[T; N]; M]) -> Self {
        Self(mat)
    }

    /// Return a reference to the raw data in the StaticMat.
    pub fn raw_data(&self) -> &[[T; N]; M] {
        &self.0
    }
}

impl<T, const N: usize> StaticVec<T, N> {
    /// Create a new StaticVec from an array.
    pub fn new_vec(vec: [T; N]) -> Self {
        Self([vec])
    }

    /// Return a reference to the raw data in the StaticVec.
    pub fn raw_array(&self) -> &[T; N] {
        &self.0[0]
    }
}

impl<T: Copy + Zero + One + AddAssign, const N: usize> StaticMat<T, N, N> {
    /// Create a NxN unity matrix.
    pub fn eye() -> Self {
        let mut res = Self::zero();
        for i in 0..N {
            res[(i, i)] = T::one()
        }
        res
    }
}

macro_rules! impl_op {
    ($trt:ident, $mth:ident, $trt_assign:ident, $op_assign:tt, $mth_assign:ident) => {
        impl<T: Copy + $trt_assign, const M: usize, const N: usize> $trt for StaticMat<T, M, N> {
            type Output = StaticMat<T, M, N>;
            fn $mth(mut self, other: Self) -> Self {
                for i in 0..M {
                    for j in 0..N {
                        self.0[i][j] $op_assign other.0[i][j];
                    }
                }
                self
            }
        }

        impl<T: Copy + $trt_assign, const M: usize, const N: usize> $trt_assign for StaticMat<T, M, N> {
            fn $mth_assign(&mut self, other: Self) {
                for i in 0..M {
                    for j in 0..N {
                        self.0[i][j] $op_assign other.0[i][j];
                    }
                }
            }
        }

        impl<T: Copy + $trt_assign, const M: usize, const N: usize> $trt<T> for StaticMat<T, M, N> {
            type Output = StaticMat<T, M, N>;
            fn $mth(mut self, other: T) -> Self {
                for i in 0..M {
                    for j in 0..N {
                        self.0[i][j] $op_assign other;
                    }
                }
                self
            }
        }

        impl<T: Copy + $trt_assign, const M: usize, const N: usize> $trt_assign<T> for StaticMat<T, M, N> {
            fn $mth_assign(&mut self, other: T) {
                for i in 0..M {
                    for j in 0..N {
                        self.0[i][j] $op_assign other;
                    }
                }
            }
        }
    };
}

impl_op!(Add, add, AddAssign, +=, add_assign);
impl_op!(Sub, sub, SubAssign, -=, sub_assign);
impl_op!(Mul, mul, MulAssign, *=, mul_assign);
impl_op!(Div, div, DivAssign, /=, div_assign);
impl_op!(Rem, rem, RemAssign, %=, rem_assign);

impl<T: Zero + Copy, const N: usize> StaticVec<T, N> {
    /// sum over all elements in the vector.
    pub fn sum(&self) -> T {
        self.0[0].iter().fold(T::zero(), |acc, &x| acc + x)
    }
}

impl<T: Float, const N: usize> StaticVec<T, N> {
    /// Calculate the Euclidian norm of the vector
    pub fn norm(&self) -> T {
        self.0[0]
            .iter()
            .fold(T::zero(), |acc, &x| acc + x.powi(2))
            .sqrt()
    }
}

impl<T: Copy + Zero + AddAssign, const M: usize, const N: usize> Zero for StaticMat<T, M, N> {
    fn zero() -> Self {
        Self([[T::zero(); N]; M])
    }

    fn is_zero(&self) -> bool {
        self.0.iter().all(|r| r.iter().all(T::is_zero))
    }
}

impl<T: Copy + One + MulAssign, const M: usize, const N: usize> One for StaticMat<T, M, N> {
    fn one() -> Self {
        Self([[T::one(); N]; M])
    }
}

impl<T: Copy + Neg<Output = T>, const M: usize, const N: usize> Neg for StaticMat<T, M, N> {
    type Output = Self;
    fn neg(mut self) -> Self {
        for i in 0..M {
            for j in 0..N {
                self.0[i][j] = -self.0[i][j];
            }
        }
        self
    }
}

impl<T: fmt::Display, const M: usize, const N: usize> fmt::Display for StaticMat<T, M, N> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        if M > 1 {
            write!(f, "[")?;
        }
        for i in 0..M {
            write!(f, "[")?;
            for j in 0..N {
                write!(f, "{}", self.0[i][j])?;
                if j != N - 1 {
                    write!(f, ", ")?;
                }
            }
            write!(f, "]")?;
            if i != M - 1 {
                write!(f, ", ")?;
            }
        }
        if M > 1 {
            write!(f, "]")?;
        }
        Ok(())
    }
}

impl<T, const M: usize, const N: usize> Index<(usize, usize)> for StaticMat<T, M, N> {
    type Output = T;
    fn index(&self, i: (usize, usize)) -> &Self::Output {
        &self.0[i.0][i.1]
    }
}

impl<T, const N: usize> Index<usize> for StaticVec<T, N> {
    type Output = T;
    fn index(&self, i: usize) -> &Self::Output {
        &self.0[0][i]
    }
}

impl<T, const M: usize, const N: usize> IndexMut<(usize, usize)> for StaticMat<T, M, N> {
    fn index_mut(&mut self, i: (usize, usize)) -> &mut Self::Output {
        &mut self.0[i.0][i.1]
    }
}

impl<T, const N: usize> IndexMut<usize> for StaticVec<T, N> {
    fn index_mut(&mut self, i: usize) -> &mut Self::Output {
        &mut self.0[0][i]
    }
}

impl<T: Copy, const M: usize, const N: usize> StaticMat<T, M, N> {
    /// Apply a function elementwise to all elements of the matrix and return a new matrix with the results.
    pub fn map<B, F>(&self, f: F) -> StaticMat<B, M, N>
    where
        B: Copy + Zero,
        F: Fn(T) -> B,
    {
        let mut res = [[B::zero(); N]; M];
        for i in 0..M {
            for j in 0..N {
                res[i][j] = f(self.0[i][j])
            }
        }
        StaticMat(res)
    }
    /// Apply a function elementwise to all elements of the matrix and a second matrix.
    /// Return a new matrix with the results.
    pub fn map_zip<B, C, F>(&self, other: &StaticMat<B, M, N>, f: F) -> StaticMat<C, M, N>
    where
        B: Copy,
        C: Copy + Zero,
        F: Fn(T, B) -> C,
    {
        let mut res = [[C::zero(); N]; M];
        for i in 0..M {
            for j in 0..N {
                res[i][j] = f(self.0[i][j], other.0[i][j])
            }
        }
        StaticMat(res)
    }

    /// Iterate over all matrix elements.
    pub fn iter(&self) -> Flatten<Iter<'_, [T; N]>> {
        self.0.iter().flatten()
    }

    /// Perform a matrix-matrix multiplication.
    /// ```
    /// use num_dual::StaticMat;
    /// let a = StaticMat::new([[1, 2], [3, 4]]);
    /// let b = StaticMat::new([[2, 1], [4, 3]]);
    /// let x = a.matmul(&b);
    /// assert_eq!(x[(0,0)], 10);
    /// assert_eq!(x[(0,1)], 7);
    /// assert_eq!(x[(1,0)], 22);
    /// assert_eq!(x[(1,1)], 15);
    /// ```
    pub fn matmul<B: Copy, C, const O: usize>(
        &self,
        other: &StaticMat<B, N, O>,
    ) -> StaticMat<C, M, O>
    where
        C: Copy + Zero + AddAssign,
        T: Mul<B, Output = C>,
    {
        let mut res = [[C::zero(); O]; M];
        for i in 0..M {
            for j in 0..N {
                for k in 0..O {
                    res[i][k] += self.0[i][j] * other.0[j][k];
                }
            }
        }
        StaticMat(res)
    }

    /// Perform a matrix-matrix multiplication in which the first matrix is transposed.
    /// ```
    /// use num_dual::StaticVec;
    /// let a = StaticVec::new_vec([1, 2]);
    /// let b = StaticVec::new_vec([2, 1]);
    /// let x = a.transpose_matmul(&b);
    /// assert_eq!(x[(0,0)], 2);
    /// assert_eq!(x[(0,1)], 1);
    /// assert_eq!(x[(1,0)], 4);
    /// assert_eq!(x[(1,1)], 2);
    /// ```
    pub fn transpose_matmul<B: Copy, C, const O: usize>(
        &self,
        other: &StaticMat<B, M, O>,
    ) -> StaticMat<C, N, O>
    where
        C: Copy + Zero + AddAssign,
        T: Mul<B, Output = C>,
    {
        let mut res = [[C::zero(); O]; N];
        for i in 0..M {
            for j in 0..N {
                for k in 0..O {
                    res[j][k] += self.0[i][j] * other.0[i][k];
                }
            }
        }
        StaticMat(res)
    }

    /// Perform a matrix-matrix multiplication in which the second matrix is transposed.
    /// ```
    /// use num_dual::{StaticMat, StaticVec};
    /// let a = StaticMat::new([[1, 2], [3, 4]]);
    /// let b = StaticVec::new_vec([2, 1]);
    /// let x = a.matmul_transpose(&b);
    /// assert_eq!(x[(0, 0)], 4);
    /// assert_eq!(x[(1, 0)], 10);
    /// ```
    pub fn matmul_transpose<B: Copy, C, const O: usize>(
        &self,
        other: &StaticMat<B, O, N>,
    ) -> StaticMat<C, M, O>
    where
        C: Copy + Zero + AddAssign,
        T: Mul<B, Output = C>,
    {
        let mut res = [[C::zero(); O]; M];
        for i in 0..M {
            for j in 0..N {
                for k in 0..O {
                    res[i][k] += self.0[i][j] * other.0[k][j];
                }
            }
        }
        StaticMat(res)
    }

    /// Transpose the matrix.
    pub fn t(&self) -> StaticMat<T, N, M>
    where
        T: Zero,
    {
        let mut res = [[T::zero(); M]; N];
        for i in 0..M {
            for j in 0..N {
                res[j][i] = self.0[i][j];
            }
        }
        StaticMat(res)
    }
}

impl<T: Copy + Zero, const N: usize> StaticVec<T, N> {
    /// Return a new vector containing the first M elements of self.
    pub fn first<const M: usize>(&self) -> StaticVec<T, M> {
        let mut res = [[T::zero(); M]; 1];
        for i in 0..M {
            res[0][i] = self[i];
        }
        StaticMat(res)
    }
    /// Return a new vector containing the last M elements of self.
    pub fn last<const M: usize>(&self) -> StaticVec<T, M> {
        let mut res = [[T::zero(); M]; 1];
        for i in 0..M {
            res[0][i] = self[i + N - M];
        }
        StaticMat(res)
    }

    /// Calculate the dot product of two vectors.
    /// ```
    /// use num_dual::StaticVec;
    /// let a = StaticVec::new_vec([1, 2, 3, 4]);
    /// let b = StaticVec::new_vec([2, 1, 4, 3]);
    /// assert_eq!(a.dot(&b), 28);
    /// ```
    pub fn dot(&self, other: &StaticVec<T, N>) -> T
    where
        T: Mul<Output = T> + AddAssign,
    {
        self.matmul_transpose(other)[0]
    }
}

impl<T, const M: usize, const N: usize> StaticMat<T, M, N> {
    /// multiply each matrix element with f in place.
    pub fn scale<F: Copy>(&mut self, f: F)
    where
        T: MulAssign<F>,
    {
        for i in 0..M {
            for j in 0..N {
                self.0[i][j] *= f;
            }
        }
    }
}