KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > maths > categories > FinSet


1 package JSci.maths.categories;
2
3 import JSci.maths.*;
4
5 /**
6 * The FinSet class encapsulates the category <b>FinSet</b>.
7 * @version 1.0
8 * @author Mark Hale
9 */

10 public class FinSet extends Object JavaDoc implements Category {
11         /**
12         * Constructs a <b>FinSet</b> category.
13         */

14         public FinSet() {}
15         /**
16         * Returns the identity morphism for an object.
17         */

18         public Category.Morphism identity(Object JavaDoc a) {
19                 return new IdentityFunction((MathSet)a);
20         }
21         /**
22         * Returns the cardinality of an object.
23         */

24         public Object JavaDoc cardinality(Object JavaDoc a) {
25                 return new MathInteger(((MathSet)a).cardinality());
26         }
27         /**
28         * Returns a hom-set.
29         */

30         public Category.HomSet hom(Object JavaDoc a,Object JavaDoc 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                 /**
42                 * Returns an element of this hom-set.
43                 */

44                 public Function getElement(Object JavaDoc in[],Object JavaDoc 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 JavaDoc in[],out[];
60                 public Function(MathSet a,MathSet b,Object JavaDoc inObjs[],Object JavaDoc outObjs[]) {
61                         from=a;
62                         to=b;
63                         in=inObjs;
64                         out=outObjs;
65                 }
66                 public Object JavaDoc domain() {
67                         return from;
68                 }
69                 public Object JavaDoc codomain() {
70                         return to;
71                 }
72                 public Object JavaDoc map(Object JavaDoc 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 JavaDoc outObjs[]=new Object JavaDoc[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 JavaDoc("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 JavaDoc map(Object JavaDoc 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 JavaDoc("Morphism is not a Function.");
105                 }
106         }
107 }
108
109
Popular Tags