is there a mathematical subset relation in java?

Viewed 126

I want to express a mathematical subset relation in Java. In Mathematics we have nice relations like: the integers are a subset of the real numbers. I try to somehow express that in java with a Point class with generics:

public class Point<T extends Number> {
 protected Number xCoord;

 public T getXCoordinate() {
      // do some stuff
     return (T) this.xCoord;
}

Unfortunately I have to cast here to T, but then I get the usual warning of unchecked conversion, what I want to avoid. Here it would be nice if Number were instanciable, but it is unfortunately abstract. So I wonder if there is a common instanciable Superclass of Integer and Double (with an explicit Zero). I didn't find one. But mathematically we really do have N as a subset of R. How can I reflect this in Java? Here I am at the end of my knowledge according to generics, because I cannot define a subclass (say InstNumber) of Number that is instanciable and superclass of Integer and Double since I am not able to change the java.lang package. So does anyone have an Idea how to "make Points of Integers a subset of Points of Double" or find a common instanciable class InstNumber. However if not, why is the type safety so hard at this point?

In the end I try to implement an algorithm to encrypt with elliptic curves. But to do so you often need that each Point in FpxFp (p prime) c NxN is also a Point in RxR. So it I want to compute with these "Numbers" even if I didn't know the current subtype. Since mathematically a field is interesting in here it would be nice to have a kind of "abstract" but instanciable Subclass called Field of Number which is Parent class of all the children of number. In Field it would be sufficient to have a concrete 0 and 1 which are present in most subclasses of Number. Thus could Field be some "mathematical field" hence a set with explicit 0 and 1 (like Z/2Z which is indeed a mathematical field). Now let for instance Integer be a subclass of Field. The only "additional job" of the Integer class is to map the "abstract" 0 and 1 of Field to the concrete 0 and 1 interpretation of Integer. This could be also implemented for other subclasses.

I know that associativity is not really possible. But for most calculations and decisions in programming it would be nice to test whether a Field element is 0 or 1 (Specifically to invert a Field element). So I want to restrict myself to the calculation of existing numbers in R, not such things as NaN.

I am sorry that I could not give you more code, but I think its too much Code that I have right now to post it here. For short: I have a ProjectivePoint class which has to do the projective addition of points in RxR. But it would be nice if the calculations in this class also are availible on FpxFp c RxR. Which is mathematically more or less trivial, but I see no current solution in Java.

1 Answers

For short: I used polymorphism multiple times to do calculations and used an instanciable fatherclass Z2 of Fp and R.

I have constructed constructed an inheritance tree of Z2 extends Fp and Fp extends R (all instanciable). The calculations inside Z2 are easy to code. Fp in contrast uses integers to calculate different values modulo p (where p is a prime passed to the constructor. R (or RealNumber) is a class that encapsulates a double and uses "doubled methods" (methods with double arguments and return type) for its calculations. I override with each inherited class the calculation methods with different parameters (i.e. Fp.add(int,int) or R.add(double,double)). Because of the instanciable class Z2 I can now define a PrjoectivePoint and do all needed calculations on Z2. The polymorphism is located in my static Calculator class, where for example add(Z2,Z2) is defined. In my first implementation I use the instanceof operator to decide whether the Z2 argument of add is really a Z2 instance, a Fp instance of a R instance. (By really I mean explicitly hence an Fp object is not instance of R) In a better version I think I can replace this by polymorphism.

The methods intValue() and doubleValue() are common from Z2 to R and implemented in the expected way since each class encapsulates it's own value. Now my calculations in Calculator can be "polymorphically" done over Z2. For security my Calculator class has a reference Z2 instanceType = new Z2|Fp|R dependend on which field I want to calculate. With all my add,mult,invert and so on methods implemnted polymorphically in Calcuator I can now do the calculations for the ProjectivePoint on Z2. Again polymorphism decides which calculations are really done in Calculator (because R extends Fp extends Z2). Moreover Z2 is instanciable and that has made the success, since every calculation can be done polymorphically on Z2.

Note that 4x in a formula for x in Z2 can also be written as Calculator.add(Calculator.add(x,x),Calculator.add(x,x));

Related