Skip to content

Commit

Permalink
adding stuff/fixing broken stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Devarth123 committed Jun 14, 2022
1 parent 6f15b54 commit a0a9d8a
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 199 deletions.
22 changes: 2 additions & 20 deletions src/ml/f64/gernerate.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
extern crate rand;
<<<<<<< HEAD
use rand::Rng;
=======

>>>>>>> parent of 7d001fc (formated)
use std::collections::HashMap;
use rand::Rng;

<<<<<<< HEAD

pub fn storing_labels<'a>(formated_data: &'a Vec<f32>, labels: &mut HashMap<u16, &'a [f32]>) {
labels.insert(formated_data[0] as u16, &formated_data[1..]);
}
Expand All @@ -23,18 +18,5 @@ pub fn gernerate_imgs<'a>(
match labels.get(&random_img) {
Some(vec) => *vec,
None => panic!("the random gernerate_imgs() method is not working at line 49"),
=======

pub fn storing_labels<'a>(formated_data: &'a Vec<f32>, labels: &mut HashMap<u16, &'a[f32]>){
labels.insert(formated_data[0] as u16, &formated_data[1..]);
}
pub fn gernerate_imgs<'a>(labels: HashMap<u16, &'a Vec<Vec<f32>>>, range: &u32) -> &'a Vec<Vec<f32>>{
let random_img: u16 = rand::thread_rng().gen_range(0, *range + 1) as u16;
// let random_img: u16 = crate::Rng.get_range(0, range);
match labels.get(&random_img){
Some(vec) => *vec,
None => panic!("the random gernerate_imgs() method is not working at line 49")
}
>>>>>>> parent of 7d001fc (formated)
}

}
13 changes: 10 additions & 3 deletions src/ml/f64/img.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
use crate::ml::f64::matrix;

pub struct Img {
label: u8,
matrix: matrix::MatrixStruct,
pub label: u8,
pub matrix: matrix::MatrixStruct,
}


impl Img{
pub fn new() -> Img{
Img{
label: 0,
matrix: matrix::matrix_create(&28, &28)
}
}
}
221 changes: 117 additions & 104 deletions src/ml/f64/matrix.rs
Original file line number Diff line number Diff line change
@@ -1,128 +1,141 @@
pub struct MatrixStruct{
pub rows: u16,
pub columns: u16,
pub matrix: Vec<Vec<f64>>,
use crate::ml::f64::read;

pub struct MatrixStruct {
pub rows: u16,
pub columns: u16,
pub matrix: Vec<Vec<f64>>,
}
pub fn matrix_create<'a>(row: &'a u16, column: &'a u16) -> MatrixStruct{
let vec: Vec<Vec<f64>> = vec![vec![0.0; *row as usize]; *column as usize];
MatrixStruct{
rows: *row,
columns: *column,
matrix: vec,
}
pub fn matrix_create<'a>(row: &'a u16, column: &'a u16) -> MatrixStruct {
let vec: Vec<Vec<f64>> = vec![vec![0.0; *row as usize]; *column as usize];
MatrixStruct {
rows: *row,
columns: *column,
matrix: vec,
}
}
pub fn matrix_display(m: &MatrixStruct){
for i in 0..m.rows as usize{
for j in 0..m.columns as usize{
print!("{}", m.matrix[i][j]);
}
println!();

//dev
pub fn matrix_display(m: &MatrixStruct) {
for i in 0..m.rows as usize {
for j in 0..m.columns as usize {
print!("{} ", m.matrix[i][j]);
}
println!();
}
}
pub fn positive(f: &f64) -> f64{
if *f<0.0{
return *f * -1_f64;
}
*f
pub fn positive(f: &f64) -> f64 {
if *f < 0.0 {
return *f * -1_f64;
}
*f
}
pub fn dot(m1: &MatrixStruct, m2: &MatrixStruct) -> MatrixStruct{
assert_eq!(m1.rows, m2.columns, "the dimensions dont match matrix1.row = {} \t matrix2.column = {}", m1.rows, m2.columns);
let mut matrix: MatrixStruct = matrix_create(&m1.rows, &m2.columns);
for i in 0..m1.rows as usize{
for j in 0..m2.columns as usize{
let mut product: f64 = 0.0;
for k in 0..m2.rows as usize{
product+=m1.matrix[i][k] * m2.matrix[j][k];
}
matrix.matrix[i][j] = product;
}

pub fn dot(m1: &MatrixStruct, m2: &MatrixStruct) -> MatrixStruct {
assert_eq!(
m1.rows, m2.columns,
"the dimensions dont match matrix1.row = {} \t matrix2.column = {}",
m1.rows, m2.columns
);
let mut matrix: MatrixStruct = matrix_create(&m1.rows, &m2.columns);
for i in 0..m1.rows as usize {
for j in 0..m2.columns as usize {
let mut product: f64 = 0.0;
for k in 0..m2.rows as usize {
product += m1.matrix[i][k] * m2.matrix[j][k];
}
matrix.matrix[i][j] = product;
}
matrix
}
matrix
}
pub fn add(m1: &MatrixStruct, m2: &MatrixStruct) -> MatrixStruct{
assert!(m1.rows == m2.rows && m1.columns == m2.columns, "the dimensions dont match matrix1.rows = {}, matrix1.columns = {}, matrix2.rows = {}, matrix2.columns = {}", m1.rows, m1.columns, m2.rows, m2.columns);
let mut matrix: MatrixStruct = matrix_create(&m1.rows, &m1.columns);
// if (m1.rows == m2.columns) && (m1.columns == m2.columns) {
for i in 0..m1.rows as usize{
for j in 0..m1.columns as usize{
matrix.matrix[i][j] = m1.matrix[i][j] + m2.matrix[i][j];
}
}
matrix
pub fn add(m1: &MatrixStruct, m2: &MatrixStruct) -> MatrixStruct {
assert!(m1.rows == m2.rows && m1.columns == m2.columns, "the dimensions dont match matrix1.rows = {}, matrix1.columns = {}, matrix2.rows = {}, matrix2.columns = {}", m1.rows, m1.columns, m2.rows, m2.columns);
let mut matrix: MatrixStruct = matrix_create(&m1.rows, &m1.columns);
// if (m1.rows == m2.columns) && (m1.columns == m2.columns) {
for i in 0..m1.rows as usize {
for j in 0..m1.columns as usize {
matrix.matrix[i][j] = m1.matrix[i][j] + m2.matrix[i][j];
}
}
matrix
}
pub fn subtract(m1: &MatrixStruct, m2: &MatrixStruct) -> MatrixStruct{
assert!(m1.rows == m2.rows && m1.columns == m2.columns, "the dimensions dont match matrix1.rows = {}, matrix1.columns = {}, matrix2.rows = {}, matrix2.columns = {}", m1.rows, m1.columns, m2.rows, m2.columns);
let mut matrix = matrix_create(&m1.rows, &m1.columns);
for i in 0..m1.rows as usize{
for j in 0..m1.columns as usize{
matrix.matrix[i][j] = positive(&(m1.matrix[i][j] - m2.matrix[i][j]));
}
pub fn subtract(m1: &MatrixStruct, m2: &MatrixStruct) -> MatrixStruct {
assert!(m1.rows == m2.rows && m1.columns == m2.columns, "the dimensions dont match matrix1.rows = {}, matrix1.columns = {}, matrix2.rows = {}, matrix2.columns = {}", m1.rows, m1.columns, m2.rows, m2.columns);
let mut matrix = matrix_create(&m1.rows, &m1.columns);
for i in 0..m1.rows as usize {
for j in 0..m1.columns as usize {
matrix.matrix[i][j] = positive(&(m1.matrix[i][j] - m2.matrix[i][j]));
}
matrix
}
matrix
}

pub fn multiply(m1: &MatrixStruct, m2: &MatrixStruct) -> MatrixStruct{
assert!(m1.rows == m2.rows && m1.columns == m2.columns, "the dimensions dont match matrix1.rows = {}, matrix1.columns = {}, matrix2.rows = {}, matrix2.columns = {}", m1.rows, m1.columns, m2.rows, m2.columns);
let mut matrix = matrix_create(&m1.rows, &m1.columns);
for i in 0..m1.rows as usize{
for j in 0..m2.columns as usize {
matrix.matrix[i][j] = m1.matrix[i][j] * m2.matrix[i][j];
}
pub fn multiply(m1: &MatrixStruct, m2: &MatrixStruct) -> MatrixStruct {
assert!(m1.rows == m2.rows && m1.columns == m2.columns, "the dimensions dont match matrix1.rows = {}, matrix1.columns = {}, matrix2.rows = {}, matrix2.columns = {}", m1.rows, m1.columns, m2.rows, m2.columns);
let mut matrix = matrix_create(&m1.rows, &m1.columns);
for i in 0..m1.rows as usize {
for j in 0..m2.columns as usize {
matrix.matrix[i][j] = m1.matrix[i][j] * m2.matrix[i][j];
}
matrix
}
matrix
}
pub fn add_by_scalar(m1: &MatrixStruct, num: &f64) -> MatrixStruct {
let mut matrix = matrix_create(&m1.rows, &m1.columns);
for i in 0..m1.rows as usize{
for j in 0..m1.columns as usize{
matrix.matrix[i][j] += *num;
}
}
matrix
let mut matrix = matrix_create(&m1.rows, &m1.columns);
for i in 0..m1.rows as usize {
for j in 0..m1.columns as usize {
matrix.matrix[i][j] += *num;
}
}
matrix
}
pub fn multiply_by_scalar(m: &MatrixStruct, num: &f64) -> MatrixStruct{
let mut matrix = matrix_create(&m.rows, &m.columns);
for i in 0..m.rows as usize{
for j in 0..m.columns as usize{
matrix.matrix[i][j] = m.matrix[i][j] * *num;

}
pub fn multiply_by_scalar(m: &MatrixStruct, num: &f64) -> MatrixStruct {
let mut matrix = matrix_create(&m.rows, &m.columns);
for i in 0..m.rows as usize {
for j in 0..m.columns as usize {
matrix.matrix[i][j] = m.matrix[i][j] * *num;
}
matrix
}
matrix
}
pub fn transpose(m: &MatrixStruct) -> MatrixStruct{
let mut matrix = matrix_create(&m.columns, &m.rows);
for i in 0..m.rows as usize{
for j in 0..m.columns as usize{
matrix.matrix[j][i] = m.matrix[i][j];
}
pub fn transpose(m: &MatrixStruct) -> MatrixStruct {
assert_ne!(m.rows, m.columns);
let mut matrix = matrix_create(&m.columns, &m.rows);
for i in 0..m.rows as usize {
for j in 0..m.columns as usize {
matrix.matrix[j][i] = m.matrix[i][j];
}
matrix
}
matrix
}
pub fn copy_matrix(m: &MatrixStruct) -> MatrixStruct{
let mut cp_matrix = matrix_create(&m.rows, &m.columns);
for i in 0..m.rows as usize{
for j in 0..m.columns as usize{
cp_matrix.matrix[i][j] = m.matrix[i][j];
}
pub fn copy_matrix(m: &MatrixStruct) -> MatrixStruct {
let mut cp_matrix = matrix_create(&m.rows, &m.columns);
for i in 0..m.rows as usize {
for j in 0..m.columns as usize {
cp_matrix.matrix[i][j] = m.matrix[i][j];
}
cp_matrix
}
cp_matrix
}
//sometimes the rust is not in the mood
pub fn scale(num: &f64, m1: &MatrixStruct) -> MatrixStruct{
let mut m = copy_matrix(m1);
for i in 0..m.rows as usize{
for j in 0..m.columns as usize{
m.matrix[i][j] *= num;
}
pub fn scale(num: &f64, m1: &MatrixStruct) -> MatrixStruct {
let mut m = copy_matrix(m1);
for i in 0..m.rows as usize {
for j in 0..m.columns as usize {
m.matrix[i][j] *= num;
}
m
}
pub fn sigmoid_matrix<'a>(r: &'a u16, c: &'a u16) -> MatrixStruct{
MatrixStruct{
rows: *r,
columns: *c,
matrix: vec![vec![1.0; *r as usize]; *c as usize]
}
}
m
}
pub fn sigmoid_matrix<'a>(r: &'a u16, c: &'a u16) -> MatrixStruct {
MatrixStruct {
rows: *r,
columns: *c,
matrix: vec![vec![1.0; *r as usize]; *c as usize],
}
}

pub fn conv_matrix(slice: &mut [u8]) -> MatrixStruct {
let matrix = matrix_create(&28, &28);

matrix
}
Loading

0 comments on commit a0a9d8a

Please sign in to comment.