diff --git a/src/ml/f64/gernerate.rs b/src/ml/f64/gernerate.rs index 528d891..6d711e8 100644 --- a/src/ml/f64/gernerate.rs +++ b/src/ml/f64/gernerate.rs @@ -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, labels: &mut HashMap) { labels.insert(formated_data[0] as u16, &formated_data[1..]); } @@ -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, labels: &mut HashMap){ - labels.insert(formated_data[0] as u16, &formated_data[1..]); - } -pub fn gernerate_imgs<'a>(labels: HashMap>>, range: &u32) -> &'a Vec>{ - 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) } - +} diff --git a/src/ml/f64/img.rs b/src/ml/f64/img.rs index c41d0a9..4123cfd 100644 --- a/src/ml/f64/img.rs +++ b/src/ml/f64/img.rs @@ -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) + } + } +} diff --git a/src/ml/f64/matrix.rs b/src/ml/f64/matrix.rs index f5edce9..42ad471 100644 --- a/src/ml/f64/matrix.rs +++ b/src/ml/f64/matrix.rs @@ -1,128 +1,141 @@ -pub struct MatrixStruct{ - pub rows: u16, - pub columns: u16, - pub matrix: Vec>, +use crate::ml::f64::read; + +pub struct MatrixStruct { + pub rows: u16, + pub columns: u16, + pub matrix: Vec>, } -pub fn matrix_create<'a>(row: &'a u16, column: &'a u16) -> MatrixStruct{ - let vec: Vec> = 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![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 } diff --git a/src/ml/f64/read.rs b/src/ml/f64/read.rs index 20238c6..0f6e6a0 100644 --- a/src/ml/f64/read.rs +++ b/src/ml/f64/read.rs @@ -1,21 +1,7 @@ use crate::ml::f64::img; -use crate::ml::f64::matrix; -use std::fs; +use std::fs::File; +use std::io::prelude::Read; -// pub fn breaker(not_formated_string: &str) -> Vec { -// // print!("{not_formated_string}"); -// let mut formated_string: Vec = Vec::new(); -// let mut push_str = String::new(); -// for i in not_formated_string.chars() { -// if i == '\n' { -// formated_string.push(push_str.clone()); -// } else { -// push_str.push(i); -// } -// } -// formated_string -// } -// pub fn count_new_lines(string: &str) -> u16 { let mut new_lines = 0; for i in string.chars() { @@ -25,63 +11,34 @@ pub fn count_new_lines(string: &str) -> u16 { } new_lines } -// pub fn matrix_fit(unformated_data: &Vec) -> matrix::MatrixStruct{ -// -// } -// pub fn matrix_fit(unformated_data: &Vec) -> matrix::MatrixStruct{ -// let mut matrix = matrix::matrix_create(&28, &28); -// for i in 0..28{ -// let mut multiplier = 0; -// for j in 0..28{ -// matrix.matrix[i][j] = unformated_data[j+multiplier]; -// } -// multiplier += 28; -// } -// matrix -// } -// pub fn read_csv(path_of_the_file: &str, how_many_lines_do_want: &u8) -> Vec> { -// let data = breaker(&fs::read_to_string(&path_of_the_file).unwrap()); -// let mut counter = 0; -// let mut str_to_f32: Vec> = Vec::new(); -// for i in &data { -// let mut push_vec: Vec = vec![0.0; 28]; -// for j in i.chars() { -// match j { -// j if j != ',' => push_vec.push( -// j.to_string() -// .parse::() -// .expect("the method at line 64 is not working as expected"), -// ), -// '\n' => counter += 1, -// _ => (), -// } -// if counter == *how_many_lines_do_want { -// break; -// } -// } -// str_to_f32.push(push_vec); -// } -// str_to_f32 -// } -pub fn line(line_no: &u16, lines: &std::str::Lines) -> String{ - let mut str = ""; - for i in 0..*line_no{ - if i == line_no -1 { - str = lines.next().unwrap(); - }else { - lines.next(); + + +pub fn format_data(string: &[u8], lines: &usize) -> Vec { + let mut imgs: Vec = Vec::with_capacity(*lines); + let mut a: [f64; LEN] = [0.0; LEN]; + let stupid = 0; + for i in 0..string.len() { + if string[i] != b',' { + if string[i] != b'\n' { + a[i] = string[i].to_string().parse::().unwrap(); + } else { + let mut img = img::Img::new(); + for j in 0..28{ + img.matrix.matrix[j] = a[stupid..i].to_vec(); + } + imgs.push(img); + let stupid = stupid + 28; + } } - } - str.to_string() + } + imgs } -pub fn read_csv(path_of_the_file: &str, lines: &u8) -> matrix::MatrixStruct { - let imgs: Vec> = Vec::with_capacity(*lines as usize); - static MAXCHAR: u16 = 10000; - let lines: String = (fs::read_to_string(path_of_the_file).unwrap()).lines(); - let mut i: u16 = 0; - while i < *lines { - let j = 0; - x - i += 1; +pub fn read_csv(path_of_the_file: &str, lines: &usize) -> Vec { + const MAXCHAR: usize = 10000; //this numbr cuz each linne has basically 10000 bytes + let mut string: [u8; MAXCHAR] = [0; MAXCHAR]; + { + let mut file = File::open(path_of_the_file).unwrap(); + File::read_exact(&mut file, &mut string).unwrap(); } + format_data::(&string, lines) }