avhz / RustQuant

Rust library for quantitative finance.

Home Page:https://docs.rs/RustQuant

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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.

commented

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 !

Disclaimer: I'm not a math wiz at all. So my understanding might be lacking.
For the Variablestruct, 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
    }
}
commented

This won't work since multiple Variables won't refer to the same Graph so performing operations on them such as x + y won't work.

The reason it is needed is to enable nalgebra matrices to contain Variable. See #63, #64, #72.

How is this @Uzaaft? It seems like a good idea. Any way I can help?

Checking in again