public/subjects/matrix_ops
Michele Sessa 6d2677e0f0 fix(matrix_op): fix package name mismatch 2022-12-12 11:52:17 +00:00
..
README.md fix(matrix_op): fix package name mismatch 2022-12-12 11:52:17 +00:00

README.md

matrix_ops

Instructions

In this exercise, you will define some basic matrix operations, Implement traits for Add and Sub

Remember that two matrices can only be added or subtracted if they have the same dimensions. Therefore, you must handle the possibility of failure by returning an Option<T>.

You will be reusing your Matrix and Scalar structures defined in the matrix and lalgebra_scalar exercises.

Expected Function

use crate::{Matrix, Scalar};
use std::ops::{ Add, Sub };

impl Add for Matrix {

}

impl Sub for Matrix {

}

Usage

Here is a program to test your function

use matrix_ops::*;

fn main() {
	let matrix = Matrix(vec![vec![8, 1], vec![9, 1]]);
	let matrix_2 = Matrix(vec![vec![1, 1], vec![1, 1]]);
	println!("{:?}", matrix + matrix_2);

	let matrix = Matrix(vec![vec![1, 3], vec![2, 5]]);
	let matrix_2 = Matrix(vec![vec![3, 1], vec![1, 1]]);
	println!("{:?}", matrix - matrix_2);

	let matrix = Matrix(vec![vec![1, 1], vec![1, 1]]);
	let matrix_2 = Matrix(vec![vec![1, 1, 3], vec![1, 1]]);
	println!("{:?}", matrix - matrix_2);

	let matrix = Matrix(vec![vec![1, 3], vec![9, 1]]);
	let matrix_2 = Matrix(vec![vec![1, 1, 3], vec![1, 1]]);
	println!("{:?}", matrix + matrix_2);
}

And its output

$ cargo run
Some(Matrix([[9, 2], [10, 2]]))
Some(Matrix([[-2, 2], [1, 4]]))
None
None
$