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


// local imports
use matrix::matrix::Matrix;
use matrix::traits::*;
use algebra::structure::CommutativeMonoidAddPartial;

/// Implements matrix extraction API
impl <T:CommutativeMonoidAddPartial+PartialOrd> MinMax<T> for Matrix<T> {

    /// Returns a column vector consisting of maximum over each row
    fn max_row_wise(&self) -> Matrix<T>{
        // Pick the first column
        let mut result = self.col(0);
        let pd = result.as_mut_ptr();
        let ps = self.as_ptr();
        for r  in 0..self.num_rows(){
            let dst_offset = result.cell_to_offset(r, 0);
            for c in 1..self.num_cols(){
                let src_offset = self.cell_to_offset(r, c);
                unsafe{
                    let s = *ps.offset(src_offset);
                    let d = *pd.offset(dst_offset);
                    *pd.offset(dst_offset) = if s > d { s } else {d};

                }
            }
        }
        result
    }

    /// Returns a column vector consisting of minimum over each row
    fn min_row_wise(&self) -> Matrix<T>{
        // Pick the first column
        let mut result = self.col(0);
        let pd = result.as_mut_ptr();
        let ps = self.as_ptr();
        for r  in 0..self.num_rows(){
            let dst_offset = result.cell_to_offset(r, 0);
            for c in 1..self.num_cols(){
                let src_offset = self.cell_to_offset(r, c);
                unsafe{
                    let s = *ps.offset(src_offset);
                    let d = *pd.offset(dst_offset);
                    *pd.offset(dst_offset) = if s < d { s } else {d};

                }
            }
        }
        result
    }

   /// Returns a row vector consisting of maximum over each column
    fn max_col_wise(&self) -> Matrix<T>{
        // Pick the first row
        let mut result = self.row(0);
        let pd = result.as_mut_ptr();
        let ps = self.as_ptr();
        for c in 0..self.num_cols(){
            let dst_offset = result.cell_to_offset(0, c);
            for r  in 1..self.num_rows(){
                let src_offset = self.cell_to_offset(r, c);
                unsafe{
                    let s = *ps.offset(src_offset);
                    let d = *pd.offset(dst_offset);
                    *pd.offset(dst_offset) = if s > d { s } else {d};

                }
            }
        }
        result
    }



   /// Returns a row vector consisting of minimum over each column
    fn min_col_wise(&self) -> Matrix<T>{
        // Pick the first row
        let mut result = self.row(0);
        let pd = result.as_mut_ptr();
        let ps = self.as_ptr();
        for c in 0..self.num_cols(){
            let dst_offset = result.cell_to_offset(0, c);
            for r  in 1..self.num_rows(){
                let src_offset = self.cell_to_offset(r, c);
                unsafe{
                    let s = *ps.offset(src_offset);
                    let d = *pd.offset(dst_offset);
                    *pd.offset(dst_offset) = if s < d { s } else {d};
                }
            }
        }
        result
    }


}