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
// Non-conventional component-wise operators.

use num::{Signed, Zero};
use std::ops::{Add, Mul};

use alga::general::{ClosedDiv, ClosedMul};

use crate::base::allocator::{Allocator, SameShapeAllocator};
use crate::base::constraint::{SameNumberOfColumns, SameNumberOfRows, ShapeConstraint};
use crate::base::dimension::Dim;
use crate::base::storage::{Storage, StorageMut};
use crate::base::{DefaultAllocator, Matrix, MatrixMN, MatrixSum, Scalar};

/// The type of the result of a matrix component-wise operation.
pub type MatrixComponentOp<N, R1, C1, R2, C2> = MatrixSum<N, R1, C1, R2, C2>;

impl<N: Scalar, R: Dim, C: Dim, S: Storage<N, R, C>> Matrix<N, R, C, S> {
    /// Computes the component-wise absolute value.
    ///
    /// # Example
    ///
    /// ```
    /// # use nalgebra::Matrix2;
    /// let a = Matrix2::new(0.0, 1.0,
    ///                      -2.0, -3.0);
    /// assert_eq!(a.abs(), Matrix2::new(0.0, 1.0, 2.0, 3.0))
    /// ```
    #[inline]
    pub fn abs(&self) -> MatrixMN<N, R, C>
    where
        N: Signed,
        DefaultAllocator: Allocator<N, R, C>,
    {
        let mut res = self.clone_owned();

        for e in res.iter_mut() {
            *e = e.abs();
        }

        res
    }

    // FIXME: add other operators like component_ln, component_pow, etc. ?
}

macro_rules! component_binop_impl(
    ($($binop: ident, $binop_mut: ident, $binop_assign: ident, $cmpy: ident, $Trait: ident . $op: ident . $op_assign: ident, $desc:expr, $desc_cmpy:expr, $desc_mut:expr);* $(;)*) => {$(
        impl<N: Scalar, R1: Dim, C1: Dim, SA: Storage<N, R1, C1>> Matrix<N, R1, C1, SA> {
            #[doc = $desc]
            #[inline]
            pub fn $binop<R2, C2, SB>(&self, rhs: &Matrix<N, R2, C2, SB>) -> MatrixComponentOp<N, R1, C1, R2, C2>
                where N: $Trait,
                      R2: Dim, C2: Dim,
                      SB: Storage<N, R2, C2>,
                      DefaultAllocator: SameShapeAllocator<N, R1, C1, R2, C2>,
                      ShapeConstraint:  SameNumberOfRows<R1, R2> + SameNumberOfColumns<C1, C2> {

                assert_eq!(self.shape(), rhs.shape(), "Componentwise mul/div: mismatched matrix dimensions.");
                let mut res = self.clone_owned_sum();

                for j in 0 .. res.ncols() {
                    for i in 0 .. res.nrows() {
                        unsafe {
                            res.get_unchecked_mut((i, j)).$op_assign(*rhs.get_unchecked((i, j)));
                        }
                    }
                }

                res
            }
        }

        impl<N: Scalar, R1: Dim, C1: Dim, SA: StorageMut<N, R1, C1>> Matrix<N, R1, C1, SA> {
            // componentwise binop plus Y.
            #[doc = $desc_cmpy]
            #[inline]
            pub fn $cmpy<R2, C2, SB, R3, C3, SC>(&mut self, alpha: N, a: &Matrix<N, R2, C2, SB>, b: &Matrix<N, R3, C3, SC>, beta: N)
                where N: $Trait + Zero + Mul<N, Output = N> + Add<N, Output = N>,
                      R2: Dim, C2: Dim,
                      R3: Dim, C3: Dim,
                      SB: Storage<N, R2, C2>,
                      SC: Storage<N, R3, C3>,
                      ShapeConstraint: SameNumberOfRows<R1, R2> + SameNumberOfColumns<C1, C2> +
                                       SameNumberOfRows<R1, R3> + SameNumberOfColumns<C1, C3> {
                assert_eq!(self.shape(), a.shape(), "Componentwise mul/div: mismatched matrix dimensions.");
                assert_eq!(self.shape(), b.shape(), "Componentwise mul/div: mismatched matrix dimensions.");

                if beta.is_zero() {
                    for j in 0 .. self.ncols() {
                        for i in 0 .. self.nrows() {
                            unsafe {
                                let res = alpha * a.get_unchecked((i, j)).$op(*b.get_unchecked((i, j)));
                                *self.get_unchecked_mut((i, j)) = res;
                            }
                        }
                    }
                }
                else {
                    for j in 0 .. self.ncols() {
                        for i in 0 .. self.nrows() {
                            unsafe {
                                let res = alpha * a.get_unchecked((i, j)).$op(*b.get_unchecked((i, j)));
                                *self.get_unchecked_mut((i, j)) = beta * *self.get_unchecked((i, j)) + res;
                            }
                        }
                    }
                }
            }

            #[doc = $desc_mut]
            #[inline]
            pub fn $binop_assign<R2, C2, SB>(&mut self, rhs: &Matrix<N, R2, C2, SB>)
                where N: $Trait,
                      R2: Dim,
                      C2: Dim,
                      SB: Storage<N, R2, C2>,
                      ShapeConstraint: SameNumberOfRows<R1, R2> + SameNumberOfColumns<C1, C2> {

                assert_eq!(self.shape(), rhs.shape(), "Componentwise mul/div: mismatched matrix dimensions.");

                for j in 0 .. self.ncols() {
                    for i in 0 .. self.nrows() {
                        unsafe {
                            self.get_unchecked_mut((i, j)).$op_assign(*rhs.get_unchecked((i, j)));
                        }
                    }
                }
            }

            #[doc = $desc_mut]
            #[inline]
            #[deprecated(note = "This is renamed using the `_assign` suffix instead of the `_mut` suffix.")]
            pub fn $binop_mut<R2, C2, SB>(&mut self, rhs: &Matrix<N, R2, C2, SB>)
                where N: $Trait,
                      R2: Dim,
                      C2: Dim,
                      SB: Storage<N, R2, C2>,
                      ShapeConstraint: SameNumberOfRows<R1, R2> + SameNumberOfColumns<C1, C2> {
                self.$binop_assign(rhs)
            }
        }
    )*}
);

component_binop_impl!(
    component_mul, component_mul_mut, component_mul_assign, cmpy, ClosedMul.mul.mul_assign,
    r"
    Componentwise matrix or vector multiplication.

    # Example

    ```
    # use nalgebra::Matrix2;
    let a = Matrix2::new(0.0, 1.0, 2.0, 3.0);
    let b = Matrix2::new(4.0, 5.0, 6.0, 7.0);
    let expected = Matrix2::new(0.0, 5.0, 12.0, 21.0);

    assert_eq!(a.component_mul(&b), expected);
    ```
    ",
    r"
    Computes componentwise `self[i] = alpha * a[i] * b[i] + beta * self[i]`.

    # Example
    ```
    # use nalgebra::Matrix2;
    let mut m = Matrix2::new(0.0, 1.0, 2.0, 3.0);
    let a = Matrix2::new(0.0, 1.0, 2.0, 3.0);
    let b = Matrix2::new(4.0, 5.0, 6.0, 7.0);
    let expected = (a.component_mul(&b) * 5.0) + m * 10.0;

    m.cmpy(5.0, &a, &b, 10.0);
    assert_eq!(m, expected);
    ```
    ",
    r"
    Inplace componentwise matrix or vector multiplication.

    # Example
    ```
    # use nalgebra::Matrix2;
    let mut a = Matrix2::new(0.0, 1.0, 2.0, 3.0);
    let b = Matrix2::new(4.0, 5.0, 6.0, 7.0);
    let expected = Matrix2::new(0.0, 5.0, 12.0, 21.0);

    a.component_mul_assign(&b);

    assert_eq!(a, expected);
    ```
    ";
    component_div, component_div_mut, component_div_assign, cdpy, ClosedDiv.div.div_assign,
    r"
    Componentwise matrix or vector division.

    # Example

    ```
    # use nalgebra::Matrix2;
    let a = Matrix2::new(0.0, 1.0, 2.0, 3.0);
    let b = Matrix2::new(4.0, 5.0, 6.0, 7.0);
    let expected = Matrix2::new(0.0, 1.0 / 5.0, 2.0 / 6.0, 3.0 / 7.0);

    assert_eq!(a.component_div(&b), expected);
    ```
    ",
    r"
    Computes componentwise `self[i] = alpha * a[i] / b[i] + beta * self[i]`.

    # Example
    ```
    # use nalgebra::Matrix2;
    let mut m = Matrix2::new(0.0, 1.0, 2.0, 3.0);
    let a = Matrix2::new(4.0, 5.0, 6.0, 7.0);
    let b = Matrix2::new(4.0, 5.0, 6.0, 7.0);
    let expected = (a.component_div(&b) * 5.0) + m * 10.0;

    m.cdpy(5.0, &a, &b, 10.0);
    assert_eq!(m, expected);
    ```
    ",
    r"
    Inplace componentwise matrix or vector division.

    # Example
    ```
    # use nalgebra::Matrix2;
    let mut a = Matrix2::new(0.0, 1.0, 2.0, 3.0);
    let b = Matrix2::new(4.0, 5.0, 6.0, 7.0);
    let expected = Matrix2::new(0.0, 1.0 / 5.0, 2.0 / 6.0, 3.0 / 7.0);

    a.component_div_assign(&b);

    assert_eq!(a, expected);
    ```
    ";
    // FIXME: add other operators like bitshift, etc. ?
);