1 package JSci.maths.categories; 2 3 import JSci.maths.*; 4 5 10 public class FinSet extends Object implements Category { 11 14 public FinSet() {} 15 18 public Category.Morphism identity(Object a) { 19 return new IdentityFunction((MathSet)a); 20 } 21 24 public Object cardinality(Object a) { 25 return new MathInteger(((MathSet)a).cardinality()); 26 } 27 30 public Category.HomSet hom(Object a,Object b) { 31 return new FunctionSet((MathSet)a,(MathSet)b); 32 } 33 public class FunctionSet implements MathSet, Category.HomSet { 34 private final MathSet from,to; 35 private final int size; 36 public FunctionSet(MathSet a,MathSet b) { 37 from=a; 38 to=b; 39 size=ExtraMath.pow(b.cardinality(),a.cardinality()); 40 } 41 44 public Function getElement(Object in[],Object out[]) { 45 return new Function(from,to,in,out); 46 } 47 public int cardinality() { 48 return size; 49 } 50 public MathSet union(MathSet set) { 51 return set.union(this); 52 } 53 public MathSet intersect(MathSet set) { 54 return set.intersect(this); 55 } 56 } 57 public class Function implements Category.Morphism { 58 private final MathSet from,to; 59 private final Object in[],out[]; 60 public Function(MathSet a,MathSet b,Object inObjs[],Object outObjs[]) { 61 from=a; 62 to=b; 63 in=inObjs; 64 out=outObjs; 65 } 66 public Object domain() { 67 return from; 68 } 69 public Object codomain() { 70 return to; 71 } 72 public Object map(Object o) { 73 for(int i=0;i<in.length;i++) { 74 if(o.equals(in[i])) 75 return out[i]; 76 } 77 return null; 78 } 79 public Category.Morphism compose(Category.Morphism m) { 80 if(m instanceof Function) { 81 Function f=(Function)m; 82 if(to.equals(f.from)) { 83 Object outObjs[]=new Object [in.length]; 84 for(int i=0;i<outObjs.length;i++) 85 outObjs[i]=f.map(out[i]); 86 return new Function(from,f.to,in,outObjs); 87 } else 88 throw new UndefinedCompositionException(); 89 } else 90 throw new IllegalArgumentException ("Morphism is not a Function."); 91 } 92 } 93 private class IdentityFunction extends Function { 94 public IdentityFunction(MathSet s) { 95 super(s,s,null,null); 96 } 97 public Object map(Object o) { 98 return o; 99 } 100 public Category.Morphism compose(Category.Morphism m) { 101 if(m instanceof Function) { 102 return m; 103 } else 104 throw new IllegalArgumentException ("Morphism is not a Function."); 105 } 106 } 107 } 108 109 | Popular Tags |