Giter Site home page Giter Site logo

Comments (3)

sebcrozet avatar sebcrozet commented on August 26, 2024

Are you referring to the Scalar trait from nalgebra there? If so, it is automatically implemented for any type that already implements Copy + PartialEq + Debug + Any.

from alga.

ChristopherRabotin avatar ChristopherRabotin commented on August 26, 2024

Yes, I am referring to that Scalar trait.

Allow me to get to the details. I'd like to be able to multiply vectors of Dual<T: Scalar+Num>, but I currently cannot seem to be able to do this (note that the following code requires the latest master from dual_num which isn't published on crates.io yet):

let x = Vector2::new(Dual::from(1.0f64), Dual::from(-2.0f64));
println!("{}", Dual::from(2.0f64) * x);

Which leads to the following error:

Compiling dual_num v0.2.0 (file:///home/chris/Workspace/rust/dual_num)
error[E0277]: cannot multiply `na::Matrix<dual_num::Dual<f64>, na::U2, na::U1, na::MatrixArray<dual_num::Dual<f64>, na::U2, na::U1>>` to `dual_num::Dual<f64>`
   --> tests/lib.rs:247:39
    |
247 |     println!("{}", Dual::from(2.0f64) * x);
    |                                       ^ no implementation for `dual_num::Dual<f64> * na::Matrix<dual_num::Dual<f64>, na::U2, na::U1, na::MatrixArray<dual_num::Dual<f64>, na::U2, na::U1>>`
    |
    = help: the trait `std::ops::Mul<na::Matrix<dual_num::Dual<f64>, na::U2, na::U1, na::MatrixArray<dual_num::Dual<f64>, na::U2, na::U1>>>` is not implemented for `dual_num::Dual<f64>`

error: aborting due to previous error

So I assumed, perhaps incorrectly, that the Scalar trait wasn't implemented because from the following code, I think that PartialEq, Debug and Copy are all implemented.

Note that the Dual struct is defined as follows:

#[derive(Clone, Copy)]
pub struct Dual<T: Scalar>(na::Vector2<T>);
/// ...
impl<T: Scalar + PartialEq> PartialEq<Self> for Dual<T> {
    fn eq(&self, rhs: &Self) -> bool {
        self.0 == rhs.0
    }
}
// ...
impl<T: Scalar + PartialEq> PartialEq<T> for Dual<T> {
    fn eq(&self, rhs: &T) -> bool {
        *self.real_ref() == *rhs
    }
}
/// ...
impl<T: Scalar> Debug for Dual<T> {
    fn fmt(&self, f: &mut Formatter) -> FmtResult {
        f.debug_tuple("Dual").field(self.real_ref()).field(self.dual_ref()).finish()
    }
}

Any hint is appreciated, thanks

from alga.

ChristopherRabotin avatar ChristopherRabotin commented on August 26, 2024

For the record:
The solution was trivial (as pointed out by sebcrozet on IRC). The scalar multiplication of a vector must be as vector * x due to the limitations of the compilers (which prevents implementing traits on structs defined by other traits).

Thanks for the support!

from alga.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.