testing token
This commit is contained in:
23
.gitignore
vendored
Normal file
23
.gitignore
vendored
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
# Generated by Cargo
|
||||||
|
# will have compiled files and executables
|
||||||
|
debug
|
||||||
|
target
|
||||||
|
|
||||||
|
# These are backup files generated by rustfmt
|
||||||
|
**/*.rs.bk
|
||||||
|
|
||||||
|
# MSVC Windows builds of rustc generate these, which store debugging information
|
||||||
|
*.pdb
|
||||||
|
|
||||||
|
# Generated by cargo mutants
|
||||||
|
# Contains mutation testing data
|
||||||
|
**/mutants.out*/
|
||||||
|
|
||||||
|
# RustRover
|
||||||
|
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
||||||
|
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
||||||
|
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
||||||
|
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||||
|
#.idea/
|
||||||
|
.git-credentials
|
||||||
|
.git-credentials
|
||||||
25
Cargo.lock
generated
Normal file
25
Cargo.lock
generated
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 4
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "autocfg"
|
||||||
|
version = "1.5.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "matrix"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"num-traits",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "num-traits"
|
||||||
|
version = "0.2.19"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
|
||||||
|
dependencies = [
|
||||||
|
"autocfg",
|
||||||
|
]
|
||||||
7
Cargo.toml
Normal file
7
Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
[package]
|
||||||
|
name = "matrix"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
num-traits = "0.2.19"
|
||||||
21
LICENSE
Normal file
21
LICENSE
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2025 LaEntropia
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
2
README.md
Normal file
2
README.md
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
# RustMatrix
|
||||||
|
A Rust implementation of a functional mathematical matrix
|
||||||
23
f
Normal file
23
f
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
[33mcommit 918815f5b1a997018bc83b9ec7d65dd3a7ca7bce[m[33m ([m[1;36mHEAD[m[33m -> [m[1;32mmain[m[33m)[m
|
||||||
|
Author: LaEntropiaa <aadrian887mh@gmail.com>
|
||||||
|
Date: Tue Aug 26 11:38:47 2025 -0600
|
||||||
|
|
||||||
|
Added sum and substract function
|
||||||
|
|
||||||
|
[33mcommit a0523d9ccb77f77184429638f33cc4adc1376a48[m
|
||||||
|
Author: LaEntropiaa <aadrian887mh@gmail.com>
|
||||||
|
Date: Thu Aug 21 11:45:42 2025 -0600
|
||||||
|
|
||||||
|
added display trait for matrix struct
|
||||||
|
|
||||||
|
[33mcommit 6b426fa1b0f9ebdbbf5e8c3b2eb2e435a053eca4[m
|
||||||
|
Author: LaEntropiaa <aadrian887mh@gmail.com>
|
||||||
|
Date: Wed Aug 20 10:32:43 2025 -0600
|
||||||
|
|
||||||
|
added get function
|
||||||
|
|
||||||
|
[33mcommit ef914ed0169305a120c16ad1e0c4927049af4c94[m
|
||||||
|
Author: LaEntropiaa <aadrian887mh@gmail.com>
|
||||||
|
Date: Sat Aug 9 16:19:53 2025 -0600
|
||||||
|
|
||||||
|
Initial comit, bases for matrix struct and creation
|
||||||
364
src/lib.rs
Normal file
364
src/lib.rs
Normal file
@@ -0,0 +1,364 @@
|
|||||||
|
use num_traits::{Num, NumAssign};
|
||||||
|
use std::fmt::{self, Debug};
|
||||||
|
use std::ops::Add;
|
||||||
|
use std::ops::Sub;
|
||||||
|
use std::ops::Mul;
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(PartialEq, Eq, Debug)]
|
||||||
|
pub struct Matrix<T: Num + NumAssign + fmt::Display + Copy + Eq + PartialEq + Debug> {
|
||||||
|
rows: usize,
|
||||||
|
columns: usize,
|
||||||
|
data: Vec<T>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Num + NumAssign + fmt::Display + Copy + Eq + PartialEq + Debug> Matrix<T> {
|
||||||
|
pub fn new(rows: usize, columns: usize, default: T) -> Self {
|
||||||
|
Self {
|
||||||
|
rows,
|
||||||
|
columns,
|
||||||
|
data: vec![default; rows * columns],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get(&self, row: usize, column: usize) -> &T {
|
||||||
|
let mut index = 0;
|
||||||
|
index += row * self.columns;
|
||||||
|
index += column;
|
||||||
|
return &self.data[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_row(&self, row: usize) -> Vec<T> {
|
||||||
|
if row >= self.rows {
|
||||||
|
panic!("Row index is out of bounds.");
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut index = 0;
|
||||||
|
let mut data = Vec::new();
|
||||||
|
|
||||||
|
|
||||||
|
index += self.columns * row;
|
||||||
|
for i in 0..self.columns {
|
||||||
|
data.push(self.data[index + i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_column(&self, column: usize) -> Vec<T> {
|
||||||
|
if column >= self.columns {
|
||||||
|
panic!("Column index is out of bounds.");
|
||||||
|
}
|
||||||
|
|
||||||
|
let index = column;
|
||||||
|
let mut data = Vec::new();
|
||||||
|
|
||||||
|
for i in 0..self.rows {
|
||||||
|
data.push(self.data[(i * self.columns) + index])
|
||||||
|
}
|
||||||
|
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set(&mut self, row: usize, column: usize, data: T) -> () {
|
||||||
|
let mut index = 0;
|
||||||
|
index += row * self.columns;
|
||||||
|
index += column;
|
||||||
|
self.data[index] = data;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Num + NumAssign + fmt::Display + Copy + Eq + PartialEq + Debug> Add for Matrix<T> {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn add(self, other: Self) -> Self::Output {
|
||||||
|
if self.data.len() != other.data.len() {
|
||||||
|
panic!("Matrix size is inadecuate.");
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut new_data = Vec::new();
|
||||||
|
for i in 0..self.data.len() {
|
||||||
|
new_data.push(self.data[i] + other.data[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
Matrix {
|
||||||
|
columns: self.columns,
|
||||||
|
rows: self.rows,
|
||||||
|
data: new_data,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Num + NumAssign + fmt::Display + Copy + Eq + PartialEq + Debug> Sub for Matrix<T> {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn sub(self, other: Self) -> Self::Output {
|
||||||
|
if self.data.len() != other.data.len() {
|
||||||
|
panic!("Matrix size is inadecuate.");
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut new_data = Vec::new();
|
||||||
|
for i in 0..self.data.len() {
|
||||||
|
new_data.push(self.data[i] - other.data[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
Matrix {
|
||||||
|
columns: self.columns,
|
||||||
|
rows: self.rows,
|
||||||
|
data: new_data,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Num + NumAssign + fmt::Display + Copy + Eq + PartialEq + Debug> Mul for Matrix<T> {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn mul(self, other: Self) -> Self::Output {
|
||||||
|
if self.columns != other.rows {
|
||||||
|
panic!("Matrix dimentions are inadecuate.");
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut new_data: Vec<T> = Vec::new();
|
||||||
|
for i in 0..self.rows {
|
||||||
|
let current_row = self.get_row(i);
|
||||||
|
|
||||||
|
for k in 0..other.columns {
|
||||||
|
let current_column = other.get_column(k);
|
||||||
|
let mut new_value = T::zero();
|
||||||
|
for (a, b) in current_row.iter().zip(current_column.iter()) {
|
||||||
|
new_value += *a * *b;
|
||||||
|
}
|
||||||
|
new_data.push(new_value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Matrix {
|
||||||
|
rows: self.rows,
|
||||||
|
columns: other.columns,
|
||||||
|
data: new_data,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Num + NumAssign + fmt::Display + Copy + Eq + PartialEq + Debug> fmt::Display for Matrix<T> {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
let mut display = String::new();
|
||||||
|
let mut index = 0;
|
||||||
|
for _i in 0..self.columns {
|
||||||
|
display += "{";
|
||||||
|
for _k in 0..self.rows {
|
||||||
|
display += &format!(" {},", self.data[index]);
|
||||||
|
index += 1;
|
||||||
|
}
|
||||||
|
display += " }\n";
|
||||||
|
}
|
||||||
|
write!(f, "{}", display)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn sum_two_matrix_1() {
|
||||||
|
let mut matrix1 = Matrix::new(4, 4, 2);
|
||||||
|
let matrix2 = Matrix::new(4, 4, 3);
|
||||||
|
let result_matrix = Matrix::new(4, 4, 5);
|
||||||
|
|
||||||
|
matrix1 = matrix1 + matrix2;
|
||||||
|
|
||||||
|
assert_eq!(matrix1, result_matrix);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn sum_two_matrix_2() {
|
||||||
|
let mut matrix1 = Matrix::new(4, 4, 2);
|
||||||
|
let mut matrix2 = Matrix::new(4, 4, 0);
|
||||||
|
let mut result_matrix = Matrix::new(4, 4, 2);
|
||||||
|
|
||||||
|
result_matrix.set(0, 0, 9);
|
||||||
|
matrix2.set(0, 0, 7);
|
||||||
|
matrix1 = matrix1 + matrix2;
|
||||||
|
|
||||||
|
assert_eq!(matrix1, result_matrix);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[should_panic]
|
||||||
|
fn sum_two_matrix_3() {
|
||||||
|
let mut matrix1 = Matrix::new(4, 4, 2);
|
||||||
|
let matrix2 = Matrix::new(4, 5, 0);
|
||||||
|
|
||||||
|
matrix1 = matrix1 + matrix2;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn substract_two_matrix_1() {
|
||||||
|
let mut matrix1 = Matrix::new(4, 4, 2);
|
||||||
|
let matrix2 = Matrix::new(4, 4, 3);
|
||||||
|
let result_matrix = Matrix::new(4, 4, -1);
|
||||||
|
|
||||||
|
matrix1 = matrix1 - matrix2;
|
||||||
|
|
||||||
|
assert_eq!(matrix1, result_matrix);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn substract_two_matrix_2() {
|
||||||
|
let mut matrix1 = Matrix::new(4, 4, 2);
|
||||||
|
let mut matrix2 = Matrix::new(4, 4, 0);
|
||||||
|
let mut result_matrix = Matrix::new(4, 4, 2);
|
||||||
|
|
||||||
|
result_matrix.set(0, 0, -5);
|
||||||
|
matrix2.set(0, 0, 7);
|
||||||
|
matrix1 = matrix1 - matrix2;
|
||||||
|
|
||||||
|
assert_eq!(matrix1, result_matrix);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[should_panic]
|
||||||
|
fn substract_two_matrix_3() {
|
||||||
|
let mut matrix1 = Matrix::new(4, 4, 2);
|
||||||
|
let matrix2 = Matrix::new(4, 5, 0);
|
||||||
|
|
||||||
|
matrix1 = matrix1 - matrix2;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn get_row_1() {
|
||||||
|
let mut matrix1 = Matrix::new(3, 4, 0);
|
||||||
|
matrix1.set(0, 0, 8);
|
||||||
|
matrix1.set(0, 1, 7);
|
||||||
|
matrix1.set(0, 2, 9);
|
||||||
|
matrix1.set(0, 3, 6);
|
||||||
|
|
||||||
|
let data = vec![8, 7 , 9, 6];
|
||||||
|
|
||||||
|
assert_eq!(matrix1.get_row(0), data);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn get_row_2() {
|
||||||
|
let mut matrix1 = Matrix::new(4, 4, 0);
|
||||||
|
|
||||||
|
matrix1.set(3, 0, 8);
|
||||||
|
matrix1.set(3, 1, 7);
|
||||||
|
matrix1.set(3, 2, 9);
|
||||||
|
matrix1.set(3, 3, 6);
|
||||||
|
|
||||||
|
let data = vec![8, 7, 9, 6];
|
||||||
|
|
||||||
|
assert_eq!(matrix1.get_row(3), data);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[should_panic]
|
||||||
|
fn get_row_3() {
|
||||||
|
let matrix1 = Matrix::new(4, 4, 0);
|
||||||
|
|
||||||
|
let _data = matrix1.get_row(4);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn get_column_1() {
|
||||||
|
let mut matrix1 = Matrix::new(4, 3, 0);
|
||||||
|
matrix1.set(0, 0, 8);
|
||||||
|
matrix1.set(1, 0, 7);
|
||||||
|
matrix1.set(2, 0, 9);
|
||||||
|
matrix1.set(3, 0, 6);
|
||||||
|
|
||||||
|
let data = vec![8, 7 , 9, 6];
|
||||||
|
|
||||||
|
assert_eq!(matrix1.get_column(0), data);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn get_column_2() {
|
||||||
|
let mut matrix1 = Matrix::new(4, 4, 0);
|
||||||
|
|
||||||
|
matrix1.set(0, 3, 8);
|
||||||
|
matrix1.set(1, 3, 7);
|
||||||
|
matrix1.set(2, 3, 9);
|
||||||
|
matrix1.set(3, 3, 6);
|
||||||
|
|
||||||
|
let data = vec![8, 7, 9, 6];
|
||||||
|
|
||||||
|
assert_eq!(matrix1.get_column(3), data);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[should_panic]
|
||||||
|
fn get_column_3() {
|
||||||
|
let matrix1 = Matrix::new(4, 4, 0);
|
||||||
|
|
||||||
|
let _data = matrix1.get_column(4);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn mult_matrix_1() {
|
||||||
|
let mut matrix1 = Matrix::new(4, 4, 2);
|
||||||
|
let matrix2 = Matrix::new(4, 4, 2);
|
||||||
|
let result_matrix = Matrix::new(4, 4, 16);
|
||||||
|
|
||||||
|
matrix1 = matrix1 * matrix2;
|
||||||
|
|
||||||
|
assert_eq!(matrix1, result_matrix);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn mult_matrix_2() {
|
||||||
|
let mut matrix1 = Matrix::new(4, 4, 2);
|
||||||
|
let mut matrix2 = Matrix::new(4, 3, 4);
|
||||||
|
let mut result_matrix = Matrix::new(4, 3, 0);
|
||||||
|
|
||||||
|
matrix1.set(0, 0, 6);
|
||||||
|
matrix1.set(0, 1, 8);
|
||||||
|
matrix1.set(0, 2, 9);
|
||||||
|
matrix1.set(0, 3, 5);
|
||||||
|
matrix1.set(1, 0, 3);
|
||||||
|
matrix1.set(1, 1, 8);
|
||||||
|
matrix1.set(1, 2, 4);
|
||||||
|
matrix1.set(1, 3, 7);
|
||||||
|
matrix1.set(2, 0, 4);
|
||||||
|
matrix1.set(2, 1, 5);
|
||||||
|
matrix1.set(2, 2, 6);
|
||||||
|
matrix1.set(2, 3, 4);
|
||||||
|
matrix1.set(3, 0, 6);
|
||||||
|
matrix1.set(3, 1, 2);
|
||||||
|
matrix1.set(3, 2, 2);
|
||||||
|
matrix1.set(3, 3, 9);
|
||||||
|
|
||||||
|
matrix2.set(0, 0, 7);
|
||||||
|
matrix2.set(0, 1, 6);
|
||||||
|
matrix2.set(0, 2, 1);
|
||||||
|
matrix2.set(1, 0, 6);
|
||||||
|
matrix2.set(1, 1, 4);
|
||||||
|
matrix2.set(1, 2, 8);
|
||||||
|
matrix2.set(2, 0, 3);
|
||||||
|
matrix2.set(2, 1, 0);
|
||||||
|
matrix2.set(2, 2, 6);
|
||||||
|
matrix2.set(3, 0, 1);
|
||||||
|
matrix2.set(3, 1, 1);
|
||||||
|
matrix2.set(3, 2, 1);
|
||||||
|
|
||||||
|
result_matrix.set(0, 0, 122);
|
||||||
|
result_matrix.set(0, 1, 73);
|
||||||
|
result_matrix.set(0, 2, 129);
|
||||||
|
result_matrix.set(1, 0, 88);
|
||||||
|
result_matrix.set(1, 1, 57);
|
||||||
|
result_matrix.set(1, 2, 98);
|
||||||
|
result_matrix.set(2, 0, 80);
|
||||||
|
result_matrix.set(2, 1, 48);
|
||||||
|
result_matrix.set(2, 2, 84);
|
||||||
|
result_matrix.set(3, 0, 69);
|
||||||
|
result_matrix.set(3, 1, 53);
|
||||||
|
result_matrix.set(3, 2, 43);
|
||||||
|
|
||||||
|
matrix1 = matrix1 * matrix2;
|
||||||
|
|
||||||
|
assert_eq!(result_matrix, matrix1);
|
||||||
|
}
|
||||||
|
}
|
||||||
8
src/main.rs
Normal file
8
src/main.rs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
use matrix::Matrix;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
println!("Hola mundo que rollo wey");
|
||||||
|
let mut matriz1 = Matrix::new(10, 15, 0);
|
||||||
|
matriz1.set(5, 5, 40);
|
||||||
|
println!("{}", matriz1);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user