Interval is an interface shared by Numeric and Triple types allowing mixed mode arithmetic between them.
package strathclyde.cs.relational.atomic; public interface Interval{ public double inf(); public double main(); public double sup(); }
package strathclyde.cs.relational.atomic; /*import com.ms.com.Variant;*/
Numeric PartofMbaseimplementedinJava AuthorPaulCockshott StartedAug Copyright (c) University of strathclyde
The D of type Number is the union of all the Ds below it. The implementation will when possible, use the most compact binary representation available for numeric values. Its S is inherited from universal and extended by { main, inf, sup }. These are used for compatibbility with triplex arithmetic their meanings of these are defined below.
Numerics will be held approximately using IEEE floating point representation.
Operator | Type | Explanation
|
a < b | ( NumberÄNumber ® B) | Returns 1 if a is less than
b
|
|
a = b | ( NumberÄNumber ® B) | Returns 1 if a equals
b
|
z¬ a + b | (NumberÄ Number ® Number) | z is the algebraic
sum of
a and b
|
z¬ a - b | (NumberÄ Number ® Number) | z is the algebraic
difference of a and b
|
z¬ a ×b | (NumberÄ Number ® Number) | z is the
algebraic product of a and b
|
z¬ a ¸b | (NumberÄ Number ® Number) | z is the quotient
of
a and b
| |
import strathclyde.cs.relational.*; import strathclyde.cs.relational.structured.*; public class Numeric extends Number implements Universal,Interval { public static final double epsilon =2.22044604925e-16; protected double value; public Numeric(){value = Double.NaN;} public Numeric(double x){ value = x; } public Numeric(float x){ value = x; } public Numeric(int x){ value = (double)x; } public byte byteValue(){return (byte)value;} public double doubleValue(){return value;} public float floatValue(){return (float)value;} public int intValue(){return (int)value;} public long longValue(){return (long)value;} public short shortValue(){return (short)value;} public int hashCode(){return intValue();}The next set of methods are used to make the type Numeric compatible with the type Triple, for interval arithmetic
public double inf(){ if (value>0) return value - value * epsilon; else if (value<=0) return value + value * epsilon; throw new RuntimeException("Error in inf of Triplex"); } //==================================================== public double main(){return value;} public double sup(){ if (value>0) return value + value * epsilon; else if (value<=0) return value - value * epsilon; throw new RuntimeException("Error in sup of Triplex"); } //====================================================== public Universal plus(Universal b) { if(b instanceof Triple) return (new Triple(this)).plus(b); else if (b instanceof Numeric ) return new Numeric(value+((Numeric)b).value); else return undefinedValue; } public Universal concat(Universal b){ return new Text(toString()+b);} public Universal minus( Universal b) { if(b instanceof Triple) return (new Triple(this)).minus(b); else if (b instanceof Numeric ) return new Numeric(value-((Numeric)b).value); else return undefinedValue; } public Universal times( Universal b) { if (b instanceof Triple) return (new Triple(this)).times(b); else if(b instanceof Numeric ) return new Numeric(value*((Numeric)b).value); else return undefinedValue; } public Universal divide( Universal b) { if (b instanceof Triple) return (new Triple(this)).divide(b); else if(b instanceof Numeric ) return new Numeric(value/((Numeric)b).value); else return undefinedValue; } public boolean equals( Object b) { if (b instanceof Numeric ) return (value ==((Numeric)b).value); else return false; } public boolean lessthan( Universal b) { if (b instanceof Numeric ) return (value >((Numeric)b).value); else return false; } public Universal max (Universal b){ if (lessthan(b)){ return b; } else { return this;} } public Universal min (Universal b){ if (lessthan(b)){ return this; } else { return b;} } public String toString (){ if (Double.isNaN(value)) return "?"; return value + ""; } }
/* * * divide * Part of MdBase implemented in Java Author Nikos Kotsis Started Aug 1997 Copyright (c) University of strathclyde */ package strathclyde.cs.relational.atomic; import strathclyde.cs.relational.*; import strathclyde.cs.relational.structured.*; public class Triple extends Numeric { private double inf,sup; public Triple (Numeric b){ inf=b.inf(); value=b.main(); sup=b.sup(); } public Triple (double e1,double e2,double e3) throws ArithmeticException{ if (e1>e2 || e2>e3) throw new ArithmeticException("Not a valid Interval"); else{ inf=e1; value=e2; sup=e3; } } public double inf(){return inf;} public double sup(){return sup;} public Universal plus (Universal b){ if(b instanceof Interval){ Interval t=(Interval)b; return new Triple(inf + t.inf(), value + t.main(), sup + t.sup()); }else{ return undefinedValue; } } public Universal minus (Universal b){ if(b instanceof Interval){ Interval t=(Interval)b; return new Triple(inf - t.sup(), value - t.main(), sup-t.inf()); }else{ return undefinedValue; } } public Universal times (Universal b){ if(b instanceof Interval){ Interval t=(Interval)b; if (inf>=0 && sup>=0 && t.inf()>=0 && t.sup()>=0) return new Triple(inf*t.inf(), value*t.main(), sup*t.sup()); if (inf>=0 && sup>=0 && t.inf()<0 && t.sup()>=0) return new Triple(sup*t.inf(), value*t.main(), sup*t.sup()); if (inf>=0 && sup>=0 && t.inf()<0 && t.sup()<0) return new Triple(sup*t.inf(), value*t.main(), inf*t.sup()); if (inf<0 && sup>=0 && t.inf()>=0 && t.sup()>=0) return new Triple(inf*t.sup(), value*t.main(), sup*t.inf()); /*if (inf<0 && sup>=0 && t.inf()<0 && t.sup()>=0) if inf*t.inf()>sup*sup() return max=inf*t.inf() else max=sup*sup() if inf*t.sup()>sup*t.inf() return return new Triple(min(inf*t.sup(), value*t.main(), sup*t.inf())); */ if (inf<0 && sup>=0 && t.inf()<0 && t.sup()<0) return new Triple(sup*t.inf(), value*t.main(), inf*t.inf()); if (inf<0 && sup<0 && t.inf()>=0 && t.sup()>=0) return new Triple(inf*t.sup(), value*t.main(), sup*t.inf()); if (inf<0 && sup<0 && t.inf()<0 && t.sup()>=0) return new Triple(inf*t.sup(), value*t.main(), inf*t.inf()); if (inf<0 && sup<0 && t.inf()<0 && t.sup()<0) return new Triple(sup*t.sup(), value*t.main(), inf*t.inf()); throw new RuntimeException("Error in Times operation of Triplex"); }else{ return undefinedValue; } } public Universal divide (Universal b){ if(b instanceof Interval){ Interval t=(Interval)b; if (inf>=0 && sup>=0 && t.inf()>0 && t.sup()>0) return new Triple(inf/t.sup(), value/t.main(), sup/t.inf()); if (inf>=0 && sup>=0 && t.inf()<0 && t.sup()<0) return new Triple(sup/t.sup(), value/t.main(), inf/t.inf()); if (inf<0 && sup>=0 && t.inf()>0 && t.sup()>0) return new Triple(inf/t.inf(), value/t.main(), sup/t.inf()); if (inf<0 && sup>=0 && t.inf()<0 && t.sup()<0) return new Triple(sup/t.sup(), value/t.main(),inf/t.sup()); if (inf<0 && sup<0 && t.inf()>0 && t.sup()>0) return new Triple(inf/t.inf(), value/t.main(),sup/t.sup()); if (inf<0 && sup<0 && t.inf()<0 && t.sup()<0) return new Triple(sup/t.inf(), value/t.main(),inf/t.sup()); throw new RuntimeException("Error in Divide operation of Triplex"); }else{ return undefinedValue; } } public boolean equals (Object b){ if(b instanceof Interval){ Interval t=(Interval)b; return (inf==t.inf() && value==t.main() && sup==t.sup()); } else { return false; } } public boolean lessthan (Universal b){ if(b instanceof Interval){ Interval t=(Interval)b; return (sup<t.inf()); } else { return false; } } public Universal divmod (Universal b){return undefinedValue;} public String toString (){ // System.out.print("" ); return " "+ inf + "«" + value + "«"+ sup + " " ; } }
The class Bool is a subclass of universal that extends the Java class number. It does this to allow booleans to be treated as a special case of numbers in the range 0 to 1.
package strathclyde.cs.relational.atomic; import strathclyde.cs.relational.operators.*; import strathclyde.cs.relational.*; public class Bool extends Number implements Universal { public boolean value; public static final Bool False = new Bool(false); public static final Bool True = new Bool(true); public byte byteValue(){return (byte)(value?1:0);} public double doubleValue(){return (value?1.0:0.0);} public float floatValue(){return (float)(value?1.0:0.0);} public int intValue(){return (int)(value?1:0);} public long longValue(){return (long)(value?1:0);} public short shortValue(){return (short)(value?1:0);} public int hashCode(){return intValue();} public Bool(boolean v){value=v; } public boolean equals(Object b){ if(b instanceof Number){ return (intValue()==((Number)b).intValue()); } else { return false; } } public boolean lessthan(Universal b){ if(b instanceof Number){ int bValue=((Number)b).intValue(); return(intValue()< bValue); }else{ return false; } } public Universal plus (Universal b)following Bool this is treated as or
{ if (value) return this; if(b instanceof Bool) return b; if(b instanceof Number) return new Bool(((Number)b).intValue()==1); return undefinedValue; } public Universal concat(Universal b){return new Text(toString()+b);} public Universal times (Universal b)
Following the standard boolean approach multiplication is equivalent to the and function
{ if(!value)return this;if false return false
if(b instanceof Bool) return b;otherwide return the other element
if(b instanceof Number) return new Bool(1==((Bool)b).intValue()); return undefinedValue; } public Universal minus (Universal b){return undefinedValue;} public Universal divide (Universal b){return undefinedValue;} //******************************************* public Universal max (Universal b){ if (lessthan(b)){ return b; } else { return this;} } public Universal min (Universal b){ if (lessthan(b)){ return this; } else { return b;} } //******************************************* public String toString (){return value + "" ; } }
Text19988 implements the string type for mbase.
Strings of characters are an atomic data type. Since strings of characters represent a valid denotation of numbers, mixed operations involving a number and string whose format is that of a valid number will return a numeric result. In other circumstances addition of strings is treated as concatenation.
package strathclyde.cs.relational.atomic; import strathclyde.cs.relational.*; import strathclyde.cs.relational.operators.*; import strathclyde.cs.relational.structured.*; import java.util.Enumeration;The class stupit (string tuple itterator) is an Enumeration used for going through strings.
class stupit implements Enumeration{ int i; Text t; public boolean hasMoreElements(){return i<t.arity();} public Object nextElement(){return t.subscript(i++);} public stupit(Text t1){i=0;t=t1;} } public class Text extends Structured implements Subscriptable { protected String thetext; public Text(String s){ thetext=s;}Operations inherited from Universal
public Universal concat(Universal b){return new Text(thetext+b.toString());} public Universal plus(Universal b){return concat(b);} public Universal max (Universal b){return(lessthan(b)?b:this);} public Universal min (Universal b){return (lessthan(b)?this:b);} public boolean lessthan(Universal b) { return (b instanceof Text?thetext.compareTo(((Text)b).thetext)<0:false);} public Universal times(Universal b){return undefinedValue;} public Universal minus(Universal b){return undefinedValue;} public Universal divide(Universal b){return undefinedValue;}Operations inherited from structured
public Enumeration members(){return new stupit(this);} public int arity(){ return thetext.length();} public float cardinality(){return arity();} public Structured image(Moperator op) { int i; String res =""; for(i=0;i<thetext.length(); i++) res=res+charof(op.apply(subscript(i))); return new Text(res); }Operations inherited from Subscriptable
public Universal subscript(int I) {return (I<arity()&&I>=0 ? subscript(I):undefinedValue );} public Universal subscript(Universal i) { if(i instanceof Numeric) { int I = ((Numeric)i).intValue(); return subscript(I); } if (i instanceof Tuple) { Tuple T=(Tuple)i; int j,N=T.arity(); String t="" ; for(j=0;j<N;j++) t=t+charof(subscript(T.subscript(j))); return new Text(t); } return undefinedValue; }Operations inherited from the java Object class
public String toString(){ return thetext;} public int hashCode(){return thetext.hashCode();} public boolean equals(Object b){ return((b instanceof Text)?((Text)b).thetext.compareTo(thetext)==0:false); }A private procedure used in the subscript function
private char charof(Universal a) { if(a==undefinedValue) return '\0'; if(a instanceof Numeric) return (char)((Numeric)a).intValue(); return '\0'; } }