gpt4 book ai didi

java - 用 Java 定义抽象数学

转载 作者:搜寻专家 更新时间:2023-11-01 03:07:42 24 4
gpt4 key购买 nike

我试图在 Java 中定义一些基本的数学概念,但总是遇到泛型错误。考虑下面的例子。在这个例子中,我尝试将零映射(f(x) = 0 for all x)定义为constant mapping(f(x) = c for all x) x), 和一个线性映射 (f(a*x + b*y) = a*f(x) + b*f(y))。编译器不允许我这样做,因为语句“零映射是一个 vector ”是模棱两可的(就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>>
{
}

最佳答案

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

为了解决这个问题,我们更改了 LinearMapping 的通用参数 V和 ConstMapping到各自类别的下限,以便他们可以扩展通用 Vector<V> ,允许更大的灵 active 。希望这能捕获您的意图,使 LinearMapping 既是映射又是 vector (仅自身)。

编辑:在这种情况下,您可能需要使用 3 个通用参数:2 个用于捕获“映射”,1 个用于捕获与其他相同类型的 LinearMapping 的“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) { ... }
}

关于java - 用 Java 定义抽象数学,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16759864/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com