RustQuant icon indicating copy to clipboard operation
RustQuant copied to clipboard

Migrate over to num crate

Open Uzaaft opened this issue 2 years ago • 6 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.

Uzaaft avatar Jul 23 '23 13:07 Uzaaft

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 !

avhz avatar Jul 23 '23 17:07 avhz

Opened #81 with refactoring of https://github.com/avhz/RustQuant/blob/main/src/utilities/sequence.rs

Uzaaft avatar Jul 23 '23 17:07 Uzaaft

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
    }
}

Uzaaft avatar Jul 23 '23 17:07 Uzaaft

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.

avhz avatar Jul 23 '23 22:07 avhz

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

Autoparallel avatar Aug 25 '23 11:08 Autoparallel

Checking in again

Autoparallel avatar Feb 25 '24 02:02 Autoparallel