<commit_before>//! The matrix module.
//!
//! Currently contains all code
//! relating to the matrix linear algebra struct.

use std::ops::{Mul, Add, Div, Sub, Index, Neg};
use libnum::{One, Zero, Float, FromPrimitive};
use std::cmp::PartialEq;
use linalg::Metric;
use linalg::vector::Vector;
use linalg::utils;

/// The Matrix struct.
///
/// Can be instantiated with any type.
pub struct Matrix<T> {
    pub cols: usize,
    pub rows: usize,
    pub data: Vec<T>,
}

impl<T> Matrix<T> {
    /// Constructor for Matrix struct.
    ///
    /// Requires both the row and column dimensions.
    ///
    /// # Examples
    ///
    /// ```
    /// use rusty_machine::linalg::matrix::Matrix;
    ///
    /// let mat = Matrix::new(2,2, vec![1.0,2.0,3.0,4.0]);
    /// ```
    pub fn new(rows: usize, cols: usize, data: Vec<T>) -> Matrix<T> {

        assert_eq!(cols * rows, data.len());
        Matrix {
            cols: cols,
            rows: rows,
            data: data,<commit_msg>Adding row and column selection.<commit_after>//! The matrix module.
//!
//! Currently contains all code
//! relating to the matrix linear algebra struct.

use std::ops::{Mul, Add, Div, Sub, Index, Neg};
use libnum::{One, Zero, Float, FromPrimitive};
use std::cmp::PartialEq;
use linalg::Metric;
use linalg::vector::Vector;
use linalg::utils;

/// The Matrix struct.
///
/// Can be instantiated with any type.
pub struct Matrix<T> {
    pub cols: usize,
    pub rows: usize,
    pub data: Vec<T>,
}

impl<T> Matrix<T> {
    /// Constructor for Matrix struct.
    ///
    /// Requires both the row and column dimensions.
    ///
    /// # Examples
    ///
    /// ```
    /// use rusty_machine::linalg::matrix::Matrix;
    ///
    /// let mat = Matrix::new(2,2, vec![1.0,2.0,3.0,4.0]);
    /// ```
    pub fn new(rows: usize, cols: usize, data: Vec<T>) -> Matrix<T> {

        assert_eq!(cols * rows, data.len());
        Matrix {
            cols: cols,
            rows: rows,
            data: data,
        }
    }
}

impl<T: Copy> Matrix<T> {
    /// Select rows from matrix
    ///
    /// # Examples
    ///
    /// ```
    /// use rusty_machine::linalg::matrix::Matrix;
    ///
    /// let a = Matrix::<f64>::ones(3,3);
    ///
    /// let b = &a.select_rows(&[2]);
    /// assert_eq!(b.rows, 1);
    /// assert_eq!(b.cols, 3);
    ///
    /// let c = &a.select_rows(&[1,2]);
    /// assert_eq!(c.rows, 2);
    /// assert_eq!(c.cols, 3);
    /// ```
    pub fn select_rows(&self, rows: &[usize]) -> Matrix<T> {

        let mut mat_vec = Vec::new();

        for row in rows {
            assert!(*row < self.rows);
        }

        unsafe {
            for row in rows {
                for i in 0..self.cols {
                    mat_vec.push(*self.data.get_unchecked(*row * self.cols + i));
                }
            }
        }
        Matrix {
            cols: self.cols,
            rows: rows.len(),
            data: mat_vec,
        }
    }

    /// Select columns from matrix
    ///
    /// # Examples
    ///
    /// ```
    /// use rusty_machine::linalg::matrix::Matrix;
    ///
    /// let a = Matrix::<f64>::ones(3,3);
    /// let b = &a.select_cols(&[2]);
    /// assert_eq!(b.rows, 3);
    /// assert_eq!(b.cols, 1);
    ///
    /// let c = &a.select_cols(&[1,2]);
    /// assert_eq!(c.rows, 3);
    /// assert_eq!(c.cols, 2);
    /// ```
    pub fn select_cols(&self, cols: &[usize]) -> Matrix<T> {

        let mut mat_vec = Vec::new();

        for col in cols {
            assert!(*col < self.cols);
        }

        unsafe {
            for i in 0..self.rows {
                for col in cols.iter() {
                    mat_vec.push(*self.data.get_unchecked(i * self.cols + col));
                }
            }
        }

        Matrix {
            cols: cols.len(),
            rows: self.rows,
            data: mat_vec,
{'fraction_non_alphanumeric': 0.1888802286308132, 'fraction_numerical': 0.011431540659911666, 'mean_word_length': 1.638793694311172, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6795981', 'n_tokens_mistral': 1315, 'n_tokens_neox': 1295, 'n_words': 314}