首页 文章

我可以选择特征边界吗?

提问于
浏览
2

给定一个模拟条件概率分布的特征:

trait Distribution {
    type T;
    fn sample<U>(&self, x: U) -> Self::T;
}

我想为两个结构实现特征, ConditionalNormalMultivariateConditionalNormal 分别为标量和向量值分布建模 .

这样的实现看起来像这样:

struct ConditionalNormal;

impl Distribution for ConditionalNormal {
    type T = f64;

    fn sample<U>(&self, x: U) -> Self::T {
        0.0
    }
}

struct MultivariateConditionalNormal;

impl Distribution for MultivariateConditionalNormal {
    type T = f64;

    fn sample<U>(&self, x: U) -> Self::T {
        0.0 + x[0]
    }
}

playground

但是, MultivariateConditionalNormal 的实现无效,因为通用 x[0] 不可索引 . 如果我添加特征边界 std::ops::Index<usize> ConditionalNormal 的实现无效,因为标量 f64 不可索引 .

我听说过,例如 Sized trait通过 ?Sized 接受可选的特征边界;我可以做类似的事吗?有什么方法可以解决这个问题吗?

2 回答

  • 3

    您可以将特征的定义更改为

    trait Distribution<U> {
        type T;
        fn sample(&self, x: U) -> Self::T;
    }
    

    这允许您在具有不同特征边界的各种类型上实现它 .

    impl<U> Distribution<U> for ConditionalNormal {
        // ...
    }
    
    impl<U> Distribution<U> for MultivariateConditionalNormal
    where
        U: std::ops::Index<usize, Output = f64>,
    {
        // ...
    }
    

    Playground

  • 3

    您可以添加新特征以指定 U 的功能:

    trait Distribution {
        type T;
        fn sample<U>(&self, x: U) -> Self::T
        where
            U: Samplable;
    }
    
    struct ConditionalNormal;
    
    impl Distribution for ConditionalNormal {
        type T = f64;
    
        fn sample<U>(&self, x: U) -> Self::T
        where
            U: Samplable,
        {
            0.0.value()
        }
    }
    
    struct MultivariateConditionalNormal;
    
    impl Distribution for MultivariateConditionalNormal {
        type T = f64;
    
        fn sample<U>(&self, x: U) -> Self::T
        where
            U: Samplable,
        {
            0.0 + x.value()
        }
    }
    
    trait Samplable {
        fn value(&self) -> f64;
    }
    
    impl Samplable for f64 {
        fn value(&self) -> f64 {
            *self
        }
    }
    
    impl Samplable for Vec<f64> {
        fn value(&self) -> f64 {
            self[0]
        }
    }
    
    fn main() {}
    

相关问题