Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![allow(dead_code)]
- #![allow(unused)]
- fn main(){
- let matrix: Vec<Vec<f64>> = vec![
- vec![1.1, 2.2, 3.6],
- vec![4.1, 1.2, 6.6],
- vec![7.1, 8.2, 9.6]
- ];
- // no. of times det2 is called
- // while finding determinant
- // of matrices of following sizes:
- // 1 -> 0
- // 2 -> 1
- // 3 -> 3
- // 4 -> 12
- // 5 -> 60
- // 6 -> 360
- // 7 -> 2520
- // 8 -> 20160
- // 9 -> 181440
- // 10 -> 1814400
- println!("{}", determinant(&matrix));
- }
- fn matrix_print(matrix: &Vec<Vec<f64>>){
- for i in matrix.iter(){
- print!("( ");
- for j in i.iter(){
- print!("{} ", j)
- }
- println!(")")
- }
- println!("")
- }
- fn get_order(matrix: &Vec<Vec<f64>>) -> (usize, usize) {
- (matrix.len(), matrix[0].len())
- }
- fn trim_around(matrix: &Vec<Vec<f64>>, row: usize, col: usize) -> Vec<Vec<f64>> {
- let mut out_mat: Vec<Vec<f64>> = vec![];
- for (i1, j1) in matrix.iter().enumerate(){
- if i1 == row {
- continue;
- }
- let mut tmp_mat: Vec<f64> = vec![];
- for (i2, j2) in j1.iter().enumerate(){
- if i2 == col {
- continue;
- }
- tmp_mat.push(*j2);
- }
- out_mat.push(tmp_mat.clone());
- }
- out_mat
- }
- fn reduce(matrix: &Vec<Vec<f64>>) -> Vec<Vec<Vec<f64>>> {
- let mut out_mat: Vec<Vec<Vec<f64>>> = vec![];
- for i in 0..matrix.len() {
- for j in 0..matrix[0].len() {
- out_mat.push(trim_around(matrix, i, j))
- }
- }
- out_mat
- }
- fn inner_reduce(matrix: &Vec<Vec<f64>>) -> Vec<Vec<Vec<f64>>>{
- let mut out_mat: Vec<Vec<Vec<f64>>> = vec![];
- for i in 0..matrix[0].len() {
- out_mat.push(trim_around(matrix, 0, i))
- }
- out_mat
- }
- fn inner_reduce_indexed(matrix: &Vec<Vec<f64>>, row: usize) -> Vec<Vec<Vec<f64>>>{
- let mut out_mat: Vec<Vec<Vec<f64>>> = vec![];
- for i in 0..matrix[row].len() {
- out_mat.push(trim_around(matrix, row, i))
- }
- out_mat
- }
- fn flatten(matrix: &Vec<Vec<f64>>) -> Vec<f64> {
- let mut out_vec: Vec<f64> = vec![];
- let mut times: f64 = 1.0;
- for i in matrix.iter(){
- for j in i.iter(){
- out_vec.push(*j * times);
- times *= -1.0;
- }
- }
- out_vec
- }
- fn inner_flatten(matrix: &Vec<Vec<f64>>, row: usize) -> Vec<f64> {
- let mut out_vec: Vec<f64> = vec![];
- let mut times: f64 = 1.0;
- for i in matrix[0].iter(){
- out_vec.push(*i* times);
- times *= -1.0;
- }
- out_vec
- }
- fn inner_flatten_indexed(matrix: &Vec<Vec<f64>>, row: usize) -> Vec<f64> {
- let mut out_vec: Vec<f64> = vec![];
- let mut times: f64 = 1.0;
- for i in matrix[row].iter(){
- out_vec.push(*i* times);
- times *= -1.0;
- }
- out_vec
- }
- fn det2(matrix: &Vec<Vec<f64>>) -> f64 {
- matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0]
- }
- fn determinant(matrix: &Vec<Vec<f64>>) -> f64 {
- let order: (usize, usize) = get_order(matrix);
- if order.0 != order.1 {
- return 0.0;
- }
- if order.0 == 1 {
- return matrix[0][0];
- }
- if order.0 == 2 {
- return det2(matrix);
- }
- let reduced: Vec<Vec<Vec<f64>>> = inner_reduce(matrix);
- let falttened: Vec<f64> = flatten(matrix);
- let mut tmp_vec: Vec<f64> = vec![];
- for i in 0..order.0 {
- tmp_vec.push(falttened[i] * inner_determinant(&reduced[i])); //spawns mulmtiple little demons
- }
- return tmp_vec.iter().sum();
- }
- fn determinant_indexed(matrix: &Vec<Vec<f64>>, row: usize) -> f64 {
- let order: (usize, usize) = get_order(matrix);
- if row >= order.0 {
- return 0.0;
- }
- if order.0 != order.1 {
- return 0.0;
- }
- if order.0 == 1 {
- return matrix[0][0];
- }
- if order.0 == 2 {
- return det2(matrix);
- }
- let reduced: Vec<Vec<Vec<f64>>> = inner_reduce_indexed(matrix, row);
- let falttened: Vec<f64> = inner_flatten_indexed(matrix, row);
- let mut tmp_vec: Vec<f64> = vec![];
- for i in 0..order.0 {
- tmp_vec.push(falttened[i] * inner_determinant(&reduced[i])); //spawns mulmtiple little demons
- }
- return tmp_vec.iter().sum::<f64>() * (-1.0_f64).powi(row as i32);
- }
- fn inner_determinant(matrix: &Vec<Vec<f64>>) -> f64 {
- let order_r = matrix.len();
- if order_r == 2 {
- return det2(matrix);
- }
- let reduced: Vec<Vec<Vec<f64>>> = inner_reduce(matrix);
- let falttened: Vec<f64> = flatten(matrix);
- let mut tmp_vec: Vec<f64> = vec![];
- for i in 0..order_r {
- tmp_vec.push(falttened[i] * inner_determinant(&reduced[i])); //spawns mulmtiple little demons
- }
- return tmp_vec.iter().sum();
- }
- fn inner_determinant_indexed(matrix: &Vec<Vec<f64>>, row: usize) -> f64 {
- let order_r = matrix.len();
- if order_r == 2 {
- return det2(matrix);
- }
- let reduced: Vec<Vec<Vec<f64>>> = inner_reduce_indexed(matrix, row);
- let falttened: Vec<f64> = inner_flatten_indexed(matrix, row);
- let mut tmp_vec: Vec<f64> = vec![];
- for i in 0..order_r {
- tmp_vec.push(falttened[i] * inner_determinant_indexed(&reduced[i], row)); //spawns mulmtiple little demons
- }
- return tmp_vec.iter().sum();
- }
Add Comment
Please, Sign In to add comment