4

我正在尝试在 java 中定义一些基本的数学概念,但总是遇到泛型错误。考虑下面的例子。在此示例中,我尝试将零映射(所有 x 的 f(x) = 0)定义为常量映射(所有 x 的 f(x) = c)和线性映射(f(a*x + b*y) = a*f(x) + b*f(y))。编译器不允许我这样做,因为“零映射是一个向量”这句话是模棱两可的(在 java 方面,但不是在数学方面)。

你能建议一个干净的解决方案来解决这个问题吗?

// A vector in a finite-dimensional vector space over the real numbers.
interface Vector<V extends Vector<?>>
{
    int dimension();
    V plus(V v);
    V times(double c);
}

interface Mapping<U extends Vector<?>, V extends Vector<?>>
// Does not inherit from Vector because the set of all mappings is an
// infinite-dimensional vector space.
{
    V map(U u);
}

// Linear mappings, on the other hand, from one finite-dimensional vector space
// to another, do form a finite-dimensional vector space.
interface LinearMapping<U extends Vector<?>, V extends Vector<?>>
    extends Mapping<U, V>, Vector<LinearMapping<U, V>>
{
}

// All elements of U are mapped to getImage(). If V is finite-dimensional, then
// the set of constant mappings is also a finite-dimensional vector space.
interface ConstMapping<U extends Vector<?>, V extends Vector<?>>
    extends Mapping<U, V>, Vector<ConstMapping<U, V>>
{
    V getImage();
}

// A zero mapping is both constant and linear, but cannot be defined as such.
interface ZeroMapping<U extends Vector<?>, V extends Vector<?>>
    extends LinearMapping<U, V>, ConstMapping<U, V>
// Error: The interface Vector cannot be implemented more than once with
// different arguments: Vector<ConstMapping<U,V>> and Vector<LinearMapping<U,V>>
{
}
4

1 回答 1

3

问题似乎是 ZeroMapping 正在扩展Vector<LinearMapping>Vector<ConstMapping>.

为了解决这个问题,我们将泛型参数 VLinearMapping和更改ConstMapping为其各个类的下限,以便它们可以扩展泛型Vector<V>,从而提供更大的灵活性。希望这能捕捉到您将 LinearMapping 制作为 Mapping 和 Vector(仅其本身)的意图。

编辑:在这种情况下,您可能必须使用 3 个通用参数:2 捕获“映射”,1 捕获与其他相同类型的线性映射的“向量”关系。

ZeroMapping 然后遵循相同的格式。

我在下面的回复中包含了@LuiggiMendoza 的建议。

interface Vector<V extends Vector<V>>
{
    int dimension();
    V plus(V v);
    V times(double c);
}

interface Mapping<U extends Vector<U>, V extends Vector<V>>
// Does not inherit from Vector because the set of all mappings is an
// infinite-dimensional vector space.
{
    V map(U u);
}

// Linear mappings, on the other hand, from one finite-dimensional vector space
// to another, do form a finite-dimenszional vector space.
interface LinearMapping<U extends Vector<U>, V extends Vector<V>, W extends LinearMapping<U, V, W>>
    extends Mapping<U, V>, Vector<W>
{
}

// All elements of U are mapped to getImage(). If V is finite-dimensional, then
// the set of constant mappings is also a finite-dimensional vector space.
interface ConstMapping<U extends Vector<U>, V extends Vector<V>, W extends ConstMapping<U, V, W>>
    extends Mapping<U, V>, Vector<W>
{
    V getImage();
}

// A zero mapping is both constant and linear, but cannot be defined as such.
interface ZeroMapping<U extends Vector<U>, V extends Vector<V>, W extends ZeroMapping<U, V, W>>
    extends LinearMapping<U, V, W>, ConstMapping<U, V, W>
{
}

如何实现该类的一个示例可能是:

class RealVector implements Vector<RealVector> { // methods here .. // }

class RealLinearMapping implements LinearMapping<RealVector, RealVector, RealLinearMapping>
{
    @Override
    public RealVector map(RealVector u) { ... }

    @Override
    public RealLinearMapping plus(RealLinearMapping v) { ... }
}
于 2013-05-26T14:08:00.067 回答