Migrate over to num crate
Uzaaft opened this issue · comments
There's a lot of code where methods are implemented on different type of numbers(f64, i64, u64, f32, etc...)
Example of file: https://github.com/avhz/RustQuant/blob/main/src/utilities/sequence.rs
Wouldn't it make sense to reduce the amount of code by adding some library, like https://github.com/rust-num/num, that has a collection of numeric types and traits which could be used.
Yes I've wanted to use the num-traits
for a while, especially for the Variable
type in the autodiff
module, but that seems difficult or impossible currently.
If you want to use num
in another part like for the sequences go ahead, that would be awesome, thank you!
And if you know how to impl Zero
and One
for Variable
that would be even better !
Opened #81 with refactoring of https://github.com/avhz/RustQuant/blob/main/src/utilities/sequence.rs
Disclaimer: I'm not a math wiz at all. So my understanding might be lacking.
For the Variable
struct, isn't it just to do:
use num::traits::{One, Zero};
// other parts of your code...
impl<'v> Zero for Variable<'v> {
fn zero() -> Self {
Variable {
graph: &Graph::new(), // you need to provide a way to get a default graph
index: 0, // assuming 0 is a sensible default index
value: 0.0,
}
}
fn is_zero(&self) -> bool {
self.value == 0.0
}
}
impl<'v> One for Variable<'v> {
fn one() -> Self {
Variable {
graph: &Graph::new(), // you need to provide a way to get a default graph
index: 0, // assuming 0 is a sensible default index
value: 1.0,
}
}
fn is_one(&self) -> bool {
self.value == 1.0
}
}
How is this @Uzaaft? It seems like a good idea. Any way I can help?
Checking in again