argmin_math/ndarray_m/
inv.rs

1// Copyright 2018-2024 argmin developers
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7
8use crate::ArgminInv;
9use crate::Error;
10use ndarray::Array2;
11use ndarray_linalg::Inverse;
12use num_complex::Complex;
13
14macro_rules! make_inv {
15    ($t:ty) => {
16        impl ArgminInv<Array2<$t>> for Array2<$t>
17        where
18            Array2<$t>: Inverse,
19        {
20            #[inline]
21            fn inv(&self) -> Result<Array2<$t>, Error> {
22                Ok(<Self as Inverse>::inv(&self)?)
23            }
24        }
25
26        // inverse for scalars (1d solvers)
27        impl ArgminInv<$t> for $t {
28            #[inline]
29            fn inv(&self) -> Result<$t, Error> {
30                Ok(1.0 / self)
31            }
32        }
33    };
34}
35
36make_inv!(f32);
37make_inv!(f64);
38make_inv!(Complex<f32>);
39make_inv!(Complex<f64>);
40
41// All code that does not depend on a linked ndarray-linalg backend can still be tested as normal.
42// To avoid dublicating tests and to allow convenient testing of functionality that does not need ndarray-linalg the tests are still included here.
43// The tests expect the name for the crate containing the tested functions to be argmin_math
44#[cfg(test)]
45use crate as argmin_math;
46include!(concat!(
47    env!("CARGO_MANIFEST_DIR"),
48    "/ndarray-tests-src/inv.rs"
49));