dnsl48 / fraction

[Rust] Lossless fractions and decimals; drop-in float replacement
Apache License 2.0
83 stars 25 forks source link

Trait bounds were not satisfied with nalgebra #99

Closed chen-qingyu closed 9 months ago

chen-qingyu commented 9 months ago

When I want to use Fraction in nalgebra (a linear algebra library), there are some traits that do not meet the requirements:

fn main() {
    type Fraction = fraction::Fraction;
    type Matrix = nalgebra::Matrix2<Fraction>;

    let a = Matrix::new(
        Fraction::from(1),
        Fraction::from(2),
        Fraction::from(3),
        Fraction::from(4),
    );

    println!("{:.3}", a.try_inverse().unwrap());
}

Error message:

  --> src\main.rs:15:25
   |
15 |     println!("{:.3}", a.try_inverse().unwrap());
   |                         ^^^^^^^^^^^ method cannot be called due to unsatisfied trait bounds
53 | pub enum GenericFraction<T>
   | ---------------------------
   | |
   | doesn't satisfy `<_ as SimdValue>::Element = GenericFraction<u64>`
   | doesn't satisfy `<_ as SimdValue>::SimdBool = bool`
   | doesn't satisfy `GenericFraction<u64>: ComplexField`
   | doesn't satisfy `GenericFraction<u64>: Field`
   | doesn't satisfy `GenericFraction<u64>: FromPrimitive`
   | doesn't satisfy `GenericFraction<u64>: SimdValue`
   | doesn't satisfy `_: SubsetOf<GenericFraction<u64>>`
   | doesn't satisfy `_: SupersetOf<f64>`
   |
   = note: the following trait bounds were not satisfied:
           `GenericFraction<u64>: ComplexField`
           `<GenericFraction<u64> as SimdValue>::SimdBool = bool`
           which is required by `GenericFraction<u64>: ComplexField`
           `<GenericFraction<u64> as SimdValue>::Element = GenericFraction<u64>`
           which is required by `GenericFraction<u64>: ComplexField`
           `GenericFraction<u64>: Field`
           which is required by `GenericFraction<u64>: ComplexField`
           `GenericFraction<u64>: SimdValue`
           which is required by `GenericFraction<u64>: ComplexField`
           `GenericFraction<u64>: FromPrimitive`
           which is required by `GenericFraction<u64>: ComplexField`
           `GenericFraction<u64>: simba::scalar::subset::SupersetOf<f64>`
           which is required by `GenericFraction<u64>: ComplexField`
           `GenericFraction<u64>: simba::scalar::subset::SubsetOf<GenericFraction<u64>>`
           which is required by `GenericFraction<u64>: ComplexField`

Is there any way to solve this problem?

dnsl48 commented 9 months ago

Hi @chen-qingyu, sorry to hear you have some troubles. Unfortunately, it doesn't look like this would work, as nalgebra has a bunch of its own traits that it implements for most of the primitive types. Given that, Fraction is not among those implementations and also does not have integration with nalgebra at this stage.

I am not saying the integration between the two projects would not be possible. However, it is not implemented at the moment and won't work out of the box without you providing those missing trait implementations.

chen-qingyu commented 9 months ago

Thank you for your reply.