rust_tensors/
addressable.rs1use std::hash::Hash;
2
3pub trait Addressable: PartialEq + Ord + Sized + Copy + Hash {
4 fn get_dimension_count() -> u32;
5 fn new_from_value_vec(values: Vec<i64>) -> Self;
6 fn get_item_at_dimension_index(&self, dimension_index: u32) -> &i64;
7 fn get_mut_item_at_dimension_index(&mut self, dimension_index: u32) -> &mut i64;
8 fn add_in_place(&mut self, other: &Self) {
9 for dimension_index in 0..Self::get_dimension_count() {
10 *self.get_mut_item_at_dimension_index(dimension_index) +=
11 other.get_item_at_dimension_index(dimension_index);
12 }
13 }
14 fn subtract_in_place(&mut self, other: &Self) {
15 for dimension_index in 0..Self::get_dimension_count() {
16 *self.get_mut_item_at_dimension_index(dimension_index) -=
17 other.get_item_at_dimension_index(dimension_index);
18 }
19 }
20 fn scale_in_place(&mut self, scalar: f64) {
21 for dimension_index in 0..Self::get_dimension_count() {
22 let value = self.get_mut_item_at_dimension_index(dimension_index);
23 *value = (*value as f64 * scalar) as i64;
24 }
25 }
26 fn abs_in_place(&mut self) {
27 for dimension_index in 0..Self::get_dimension_count() {
28 let value = self.get_mut_item_at_dimension_index(dimension_index);
29 *value = value.abs()
30 }
31 }
32 fn difference_in_place(&mut self, other: &Self) {
33 self.subtract_in_place(other);
34 self.abs_in_place();
35 }
36 fn add(&self, other: &Self) -> Self {
37 let mut out = self.clone();
38 out.add_in_place(other);
39 out
40 }
41 fn subtract(&self, other: &Self) -> Self {
42 let mut out = self.clone();
43 out.subtract_in_place(other);
44 out
45 }
46 fn difference(&self, other: &Self) -> Self {
47 let mut out = self.clone();
48 out.difference_in_place(other);
49 out
50 }
51 fn scale(&self, scalar: f64) -> Self {
52 let mut out = self.clone();
53 out.scale_in_place(scalar);
54 out
55 }
56 fn distance(&self, other: &Self) -> f64 {
57 let mut sum: i64 = 0;
58 for dimension_index in 0..Self::get_dimension_count() {
59 sum += (self.get_item_at_dimension_index(dimension_index) - other.get_item_at_dimension_index(dimension_index)).pow(2);
60 }
61 (sum as f64).sqrt()
62 }
63}