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
// Copyright 2018-2020 argmin developers
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

//! # Math
//!
//! Mathematics related traits which some solvers require. This provides an abstraction over
//! different types of parameter vectors. The idea is, that it does not matter whether you would
//! like to use simple `Vec`s, `ndarray`, `nalgebra` or custom defined types: As long as the traits
//! required by the solver are implemented, you should be fine. In this module several of these
//! traits are defined and implemented. These will be extended as needed. They are also already
//! implemented for basic `Vec`s, and will in the future also be implemented for types defined by
//! `ndarray` and `nalgebra`.
//!
//! # TODO
//!
//! * Implement tests for Complex<T> impls

mod add;
#[cfg(feature = "nalgebral")]
mod add_nalgebra;
#[cfg(feature = "ndarrayl")]
mod add_ndarray;
mod add_vec;
mod conj;
#[cfg(feature = "nalgebral")]
mod conj_nalgebra;
#[cfg(feature = "ndarrayl")]
mod conj_ndarray;
mod conj_vec;
mod div;
#[cfg(feature = "nalgebral")]
mod div_nalgebra;
#[cfg(feature = "ndarrayl")]
mod div_ndarray;
mod div_vec;
mod dot;
#[cfg(feature = "nalgebral")]
mod dot_nalgebra;
#[cfg(feature = "ndarrayl")]
mod dot_ndarray;
mod dot_vec;
#[cfg(feature = "nalgebral")]
mod eye_nalgebra;
#[cfg(feature = "ndarrayl")]
mod eye_ndarray;
mod eye_vec;
#[cfg(feature = "nalgebral")]
mod inv_nalgebra;
#[cfg(feature = "ndarrayl")]
mod inv_ndarray;
mod mul;
#[cfg(feature = "nalgebral")]
mod mul_nalgebra;
#[cfg(feature = "ndarrayl")]
mod mul_ndarray;
mod mul_vec;
mod norm;
#[cfg(feature = "nalgebral")]
mod norm_nalgebra;
#[cfg(feature = "ndarrayl")]
mod norm_ndarray;
mod norm_vec;
mod scaledadd;
#[cfg(feature = "nalgebral")]
mod scaledadd_nalgebra;
#[cfg(feature = "ndarrayl")]
mod scaledadd_ndarray;
mod scaledadd_vec;
mod scaledsub;
#[cfg(feature = "nalgebral")]
mod scaledsub_nalgebra;
#[cfg(feature = "ndarrayl")]
mod scaledsub_ndarray;
mod scaledsub_vec;
mod sub;
#[cfg(feature = "nalgebral")]
mod sub_nalgebra;
#[cfg(feature = "ndarrayl")]
mod sub_ndarray;
mod sub_vec;
mod transpose;
#[cfg(feature = "nalgebral")]
mod transpose_nalgebra;
#[cfg(feature = "ndarrayl")]
mod transpose_ndarray;
mod transpose_vec;
mod weighteddot;
mod zero;
#[cfg(feature = "nalgebral")]
mod zero_nalgebra;
#[cfg(feature = "ndarrayl")]
mod zero_ndarray;
mod zero_vec;
// #[cfg(feature = "ndarrayl")]
// mod random_ndarray; // TODO
mod random_vec;
// #[cfg(feature = "ndarrayl")]
// mod minmax_ndarray; // TODO
mod minmax_vec;
pub use crate::core::math::add::*;
#[cfg(feature = "ndarrayl")]
pub use crate::core::math::add_ndarray::*;
pub use crate::core::math::add_vec::*;
pub use crate::core::math::conj::*;
#[cfg(feature = "ndarrayl")]
pub use crate::core::math::conj_ndarray::*;
pub use crate::core::math::conj_vec::*;
#[cfg(feature = "ndarrayl")]
pub use crate::core::math::div_ndarray::*;
pub use crate::core::math::dot::*;
#[cfg(feature = "ndarrayl")]
pub use crate::core::math::dot_ndarray::*;
pub use crate::core::math::dot_vec::*;
#[cfg(feature = "ndarrayl")]
pub use crate::core::math::eye_ndarray::*;
pub use crate::core::math::eye_vec::*;
#[cfg(feature = "ndarrayl")]
pub use crate::core::math::inv_ndarray::*;
pub use crate::core::math::mul::*;
#[cfg(feature = "ndarrayl")]
pub use crate::core::math::mul_ndarray::*;
pub use crate::core::math::mul_vec::*;
pub use crate::core::math::norm::*;
#[cfg(feature = "ndarrayl")]
pub use crate::core::math::norm_ndarray::*;
pub use crate::core::math::norm_vec::*;
pub use crate::core::math::scaledadd::*;
#[cfg(feature = "ndarrayl")]
pub use crate::core::math::scaledadd_ndarray::*;
pub use crate::core::math::scaledadd_vec::*;
pub use crate::core::math::scaledsub::*;
#[cfg(feature = "ndarrayl")]
pub use crate::core::math::scaledsub_ndarray::*;
pub use crate::core::math::scaledsub_vec::*;
pub use crate::core::math::sub::*;
#[cfg(feature = "ndarrayl")]
pub use crate::core::math::sub_ndarray::*;
pub use crate::core::math::sub_vec::*;
pub use crate::core::math::transpose::*;
#[cfg(feature = "ndarrayl")]
pub use crate::core::math::transpose_ndarray::*;
pub use crate::core::math::transpose_vec::*;
pub use crate::core::math::weighteddot::*;
pub use crate::core::math::zero::*;
#[cfg(feature = "ndarrayl")]
pub use crate::core::math::zero_ndarray::*;
pub use crate::core::math::zero_vec::*;
// #[cfg(feature = "ndarrayl")] // TODO
// pub use crate::core::math::random_ndarray::*;
pub use crate::core::math::random_vec::*;
// #[cfg(feature = "ndarrayl")] // TODO
// pub use crate::core::math::minmax_ndarray::*;
pub use crate::core::math::minmax_vec::*;

use crate::core::Error;

/// Dot/scalar product of `T` and `self`
pub trait ArgminDot<T, U> {
    /// Dot/scalar product of `T` and `self`
    fn dot(&self, other: &T) -> U;
}

/// Dot/scalar product of `T` and `self` weighted by W (p^TWv)
pub trait ArgminWeightedDot<T, U, V> {
    /// Dot/scalar product of `T` and `self`
    fn weighted_dot(&self, w: &V, vec: &T) -> U;
}

/// Return param vector of all zeros (for now, this is a hack. It should be done better)
pub trait ArgminZero {
    /// Return zero(s)
    fn zero() -> Self;
}

/// Return the conjugate
pub trait ArgminConj {
    /// Return conjugate
    fn conj(&self) -> Self;
}

/// Zero for dynamically sized objects
pub trait ArgminZeroLike {
    /// Return zero(s)
    fn zero_like(&self) -> Self;
}

/// Identity matrix
pub trait ArgminEye {
    /// Identity matrix of size `n`
    fn eye(n: usize) -> Self;
    /// Identity matrix of same size as `self`
    fn eye_like(&self) -> Self;
}

/// Add a `T` to `self`
pub trait ArgminAdd<T, U> {
    /// Add a `T` to `self`
    fn add(&self, other: &T) -> U;
}

/// Subtract a `T` from `self`
pub trait ArgminSub<T, U> {
    /// Subtract a `T` from `self`
    fn sub(&self, other: &T) -> U;
}

/// (Pointwise) Multiply a `T` with `self`
pub trait ArgminMul<T, U> {
    /// (Pointwise) Multiply a `T` with `self`
    fn mul(&self, other: &T) -> U;
}

/// (Pointwise) Divide a `T` by `self`
pub trait ArgminDiv<T, U> {
    /// (Pointwise) Divide a `T` by `self`
    fn div(&self, other: &T) -> U;
}

/// Add a `T` scaled by an `U` to `self`
pub trait ArgminScaledAdd<T, U, V> {
    /// Add a `T` scaled by an `U` to `self`
    fn scaled_add(&self, factor: &U, vec: &T) -> V;
}

/// Subtract a `T` scaled by an `U` from `self`
pub trait ArgminScaledSub<T, U, V> {
    /// Subtract a `T` scaled by an `U` from `self`
    fn scaled_sub(&self, factor: &U, vec: &T) -> V;
}

/// Compute the l2-norm (`U`) of `self`
pub trait ArgminNorm<U> {
    /// Compute the l2-norm (`U`) of `self`
    fn norm(&self) -> U;
}

// Suboptimal: self is moved. ndarray however offers array views...
/// Return the transpose (`U`) of `self`
pub trait ArgminTranspose<U> {
    /// Transpose
    fn t(self) -> U;
}

/// Compute the inverse (`T`) of `self`
pub trait ArgminInv<T> {
    /// Compute the inverse
    fn inv(&self) -> Result<T, Error>;
}

/// Create a random number
pub trait ArgminRandom {
    /// Get a random element between min and max,
    fn rand_from_range(min: &Self, max: &Self) -> Self;
}

/// Minimum and Maximum of type `T`
pub trait ArgminMinMax {
    /// Select piecewise minimum
    fn min(x: &Self, y: &Self) -> Self;
    /// Select piecewise maximum
    fn max(x: &Self, y: &Self) -> Self;
}