rust_linear_algebra/vector/
angle_cos.rs

1use crate::vector::Vector;
2use std::ops::{Add, Mul};
3
4pub fn angle_cos<K>(u: &Vector<K>, v: &Vector<K>) -> f32
5where
6    K: Copy + Default + Mul<f32, Output = K> + Into<f32> + Add<Output = K> + Mul<Output = K>,
7{
8    if u.elements.len() != v.elements.len() {
9        panic!("The vector need to be on the same plan");
10    }
11
12    let u_norm = u.norm();
13    let v_norm = v.norm();
14
15    if u_norm == 0.0 || v_norm == 0.0 {
16        panic!("The vector can't be 0");
17    }
18
19    let dot_product: f32 = u.dot(v.clone()).into();
20    dot_product / (u_norm * v_norm)
21}