1extern crate rand;
2
3use rand::prelude::*;
4
5#[derive(Clone)]
6pub struct Point {
7 pub cs: Vec<f64> }
9
10impl Point {
11 pub fn new_default(dim: usize) -> Self {
12 Self{cs: vec![0f64; dim]}
13 }
14
15 pub fn new(cs: Vec<f64>) -> Self {
16 Self{cs}
17 }
18
19 pub fn new_random(rng: &mut ThreadRng, dim: usize, rad: f64) -> Self {
20 Self{cs: (0..dim).map(|_| rng.gen_range((-rad)..=rad)).collect::<Vec<f64>>()}
21 }
22
23 pub fn coords(&self) -> Vec<f64> {
24 self.cs.clone()
25 }
26
27 pub fn mul(&self, a: f64) -> Self {
28 Self{cs: self.cs.iter().map(|&c| c * a).collect::<Vec<f64>>()}
29 }
30
31 pub fn add(&self, other: &Self) -> Self {
32 Self{cs: (0..self.cs.len()).map(|i| self.cs[i] + other.cs[i]).collect::<Vec<f64>>()}
33 }
34
35 pub fn sub(&self, other: &Self) -> Self {
36 Self{cs: (0..self.cs.len()).map(|i| self.cs[i] - other.cs[i]).collect::<Vec<f64>>()}
37 }
38
39}
40
41impl std::fmt::Debug for Point {
42 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43 f.debug_list().entries(self.cs.iter()).finish()
44 }
45}