JSci.maths.categories.Preorder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jsci Show documentation
Show all versions of jsci Show documentation
JSci is a set of open source Java packages. The aim is to encapsulate scientific methods/principles in the most natural way possible. As such they should greatly aid the development of scientific based software.
It offers: abstract math interfaces, linear algebra (support for various matrix and vector types), statistics (including probability distributions), wavelets, newtonian mechanics, chart/graph components (AWT and Swing), MathML DOM implementation, ...
Note: some packages, like javax.comm, for the astro and instruments package aren't listed as dependencies (not available).
The newest version!
package JSci.maths.categories;
/**
* The Preorder class encapsulates preorders as categories.
* @version 1.0
* @author Mark Hale
*/
public final class Preorder extends Object implements Category {
private final int N;
/**
* Constructs a preorder category.
*/
public Preorder(int n) {
N=n;
}
/**
* Returns the identity morphism for an object.
* @param a an Integer.
*/
public Category.Morphism identity(Object a) {
return new Relation((Integer)a, (Integer)a);
}
/**
* Returns the cardinality of an object.
* @param a an Integer.
* @return an Integer.
*/
public Object cardinality(Object a) {
return a;
}
/**
* Returns a hom-set.
*/
public Category.HomSet hom(Object a,Object b) {
final Integer i=(Integer)a;
final Integer j=(Integer)b;
if(i.compareTo(j)<=0)
return new RelationSet(i,j);
else
return new RelationSet();
}
public Object initial() {
return new Integer(0);
}
public Object terminal() {
return new Integer(N-1);
}
/**
* Returns the ordinal that this category represents.
*/
public int ordinal() {
return N;
}
public class RelationSet implements Category.HomSet {
public final Relation morphism;
public RelationSet() {
morphism=null;
}
public RelationSet(Integer a,Integer b) {
morphism=new Relation(a,b);
}
}
public class Relation implements Category.Morphism {
private final Integer from,to;
public Relation(Integer a,Integer b) {
from=a;
to=b;
}
public Object domain() {
return from;
}
public Object codomain() {
return to;
}
public Object map(Object o) {
return to;
}
public Category.Morphism compose(Category.Morphism m) {
if(m instanceof Relation) {
Relation r=(Relation)m;
if(to.equals(r.from))
return new Relation(from,r.to);
else
throw new UndefinedCompositionException();
} else
throw new IllegalArgumentException("Morphism is not a Relation.");
}
}
}