我试图在java中定义一些基本的数学概念,但不断陷入泛型错误。考虑下面的例子。 在这个例子中,我尝试将零映射(所有x的f(x)= 0)定义为常量映射(f(x)= c for all x)和线性映射(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>>
{
}
答案 0 :(得分:3)
问题似乎是ZeroMapping正在扩展Vector<LinearMapping>
和Vector<ConstMapping>
。
为了解决这个问题,我们将LinearMapping
和ConstMapping
的通用参数V更改为各个类的下限,以便它们可以扩展通用Vector<V>
,从而允许更多灵活性。希望这可以捕获您将LinearMapping设置为Mapping和Vector(仅限其本身)的意图。
编辑:在这种情况下,您可能需要使用3个通用参数:2用于捕获“Mapping”,1用于捕获与其他相同类型的LinearMappings的“Vector”关系。
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) { ... }
}