-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathmultinomial.rs
57 lines (50 loc) · 1.56 KB
/
multinomial.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#[allow(unused_imports)]
use crate::structure::matrix::*;
use crate::traits::math::InnerProduct;
use crate::util::useful::*;
use std::fmt;
#[derive(Debug, Clone)]
pub struct Multinomial {
coef: Vec<f64>,
}
impl fmt::Display for Multinomial {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut result = String::new();
let l = self.coef.len();
if l == 1 {
let value = self.coef[0];
let target = choose_shorter_string(format!("{}x_0", value), format!("{:.4}x_0", value));
return write!(f, "{}", target);
}
let first_value = self.coef[0];
result.push_str(&choose_shorter_string(
format!("{}x_0", first_value),
format!("{:.4}x_0", first_value),
));
for i in 1..l {
let value = self.coef[i];
if value > 0. {
let target = choose_shorter_string(
format!(" + {}x_{}", value, i),
format!(" + {:.4}x_{}", value, i),
);
result.push_str(&target);
} else if value < 0. {
let target = choose_shorter_string(
format!(" - {}x_{}", value, i),
format!(" - {:.4}x_{}", value, i),
);
result.push_str(&target);
}
}
write!(f, "{}", result)
}
}
impl Multinomial {
pub fn new(coef: Vec<f64>) -> Self {
Self { coef }
}
pub fn eval(&self, values: &Vec<f64>) -> f64 {
self.coef.dot(values)
}
}