Defining abstract math with java

I am trying to define some basic math concepts in java, but keep getting stuck with generic errors. Consider the example below. In this example, I am trying to define a null mapping (f (x) = 0 for all x) as for a constant mapping (f (x) = c for all x) and a linear mapping (f (a * x + b * y) = a * f (x) + b * f (y)). The compiler does not allow me to do this because the operator "null mapping is a vector" is ambiguous (from the point of view of java, but not mathematically).

Can you suggest a clean solution to this problem?

// 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
source share
1 answer

The problem is that ZeroMapping extends both Vector<LinearMapping> and Vector<ConstMapping> .

To fix this, we will change the generic parameter V from LinearMapping and ConstMapping to the lower bounds of their individual classes so that they can extend the generic Vector<V> , which provides more flexibility. I hope this will record your intention to do LinearMapping both Mapping and Vector (for yourself only).

EDIT : in this case, you may need 3 general parameters: 2 for capturing the Mapping and 1 for attaching the Vector relationship to other LinearMappings of the same type.

Then ZeroMapping follows in the same format.

I included the @LuiggiMendoza suggestion in the answer below.

 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> { } 

An example of how the class will be implemented can be:

 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) { ... } } 
+3
source

Source: https://habr.com/ru/post/1482822/