yalal 0.0.1

Yet Another Linear Algebra Library
Documentation
use matrix::Matrix;
use matrix::MatrixErr;

#[test]
fn test_from() {
    let test_matrix = Matrix::from(1, 2, &[1.0, 2.0, 3.0, 4.0, 5.0]);
    assert_eq!(test_matrix, Matrix::from(1, 2, &[1.0, 2.0]));
    assert!(Matrix::try_from(2, 2, &[1.0, 2.0, 3.0]).is_none());
}

#[test]
fn test_replacement() {
    // 3.0 5.0 1.0
    // 2.0 4.0 1.5
    let mut test_matrix = Matrix::from(2, 3, &[3.0, 5.0, 1.0, 2.0, 4.0, 1.5]);
    assert_eq!(test_matrix.row_add(0, 2, 1.0), Err(MatrixErr::RowOverflow));
    assert_eq!(test_matrix.row_add(2, 0, 1.0), Err(MatrixErr::RowOverflow));
    test_matrix.row_add(0, 1, -1.0).unwrap();
    assert_eq!(
        test_matrix,
        Matrix::from(2, 3, &[1.0, 1.0, -0.5, 2.0, 4.0, 1.5])
    );
    test_matrix.row_add(1, 0, 2.0).unwrap();
    assert_eq!(
        test_matrix,
        Matrix::from(2, 3, &[1.0, 1.0, -0.5, 4.0, 6.0, 0.5])
    );
}

#[test]
fn test_interchange() {
    // 3.0 5.0 1.0
    // 2.0 4.0 1.5
    let mut test_matrix = Matrix::from(2, 3, &[3.0, 5.0, 1.0, 2.0, 4.0, 1.5]);
    test_matrix.row_switch(0, 1).unwrap();
    assert_eq!(
        test_matrix,
        Matrix::from(2, 3, &[2.0, 4.0, 1.5, 3.0, 5.0, 1.0])
    );
    test_matrix.row_switch(1, 1).unwrap();
    assert_eq!(
        test_matrix,
        Matrix::from(2, 3, &[2.0, 4.0, 1.5, 3.0, 5.0, 1.0])
    );
}

#[test]
fn test_scaling() {
    // 3.0 5.0 1.0
    // 2.0 4.0 1.5
    let mut test_matrix = Matrix::from(2, 3, &[3.0, 5.0, 1.0, 2.0, 4.0, 1.5]);
    test_matrix.row_multiply(1, 0.5).unwrap();
    assert_eq!(
        test_matrix,
        Matrix::from(2, 3, &[3.0, 5.0, 1.0, 1.0, 2.0, 0.75])
    );
}

#[test]
fn test_leading_entry_index() {
    let m = Matrix::from(3, 4,
        &[0.0, 0.0, 0.0, 0.0,
          1.0, 0.0, 0.0, 0.0,
          0.0, 0.0, 1e-3, 0.0]);
    assert_eq!(m.leading_entry_column(0), Err(MatrixErr::NotExists));
    assert_eq!(m.leading_entry_column(1), Ok(0usize));
    assert_eq!(m.leading_entry_column(2), Ok(2usize));
    assert_eq!(m.leading_entry_column(3), Err(MatrixErr::RowOverflow));
}

#[test]
fn test_mul() {
    let m1 = Matrix::from(2, 3, &[1.0, 1.0, 2.0, 2.0, 1.0, 1.0]);
    let m2 = Matrix::from(2, 3, &[2.0, 3.0, 1.0, 2.0, 3.0, 3.0]);
    let result = Matrix::from(2, 3, &[2.0, 3.0, 2.0, 4.0, 3.0, 3.0]);

    assert_eq!(m1.mul(&m2), Ok(result.clone()));
    assert_eq!(
        m1.mul(&Matrix::from(2, 2, &[0.0, 0.0, 0.0, 0.0])),
        Err(MatrixErr::NotAligned)
    );
    assert_eq!(
        m1.mul(&Matrix::from(1, 3, &[0.0, 0.0, 0.0, 0.0])),
        Err(MatrixErr::NotAligned)
    );
    assert_eq!(&m1 * &m2, result);
}

#[test]
fn test_mul_assign() {
    let mut m = Matrix::from(2, 3, &[1.0, 1.0, 2.0, 2.0, 1.0, 1.0]);
    assert_eq!(m.mul_assign(&Matrix::from(2, 1, &[0.0, 0.0])), Err(MatrixErr::NotAligned));
    assert_eq!(m.mul_assign(&Matrix::from(1, 3, &[0.0, 0.0, 0.0])), Err(MatrixErr::NotAligned));
    assert!(m.mul_assign(&Matrix::from(2, 3, &[2.0, 3.0, 1.0, 2.0, 3.0, 3.0])).is_ok());
    assert_eq!(m, Matrix::from(2, 3, &[2.0, 3.0, 2.0, 4.0, 3.0, 3.0]));

    m *= &Matrix::from(2, 3, &[0.0, 0.0, 0.0, 0.0, 0.0, 0.0]);
    assert_eq!(m, Matrix::from(2, 3, &[0.0, 0.0, 0.0, 0.0, 0.0, 0.0]));
}

#[test]
fn test_dot() {
    let m1 = Matrix::from(2, 3, &[1.0, 2.0, 3.0, 1.5, 3.5, 2.5]);
    let m3 = Matrix::from(2, 2, &[0.0, 0.0, 0.0, 0.0]);
    assert_eq!(m1.dot(&m3), Err(MatrixErr::NotAligned));
}