KickJava   Java API By Example, From Geeks To Geeks.

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


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

10 public final class Simplicial extends Object JavaDoc implements Category {
11         public final Bifunctor ADDITION=new Addition();
12         /**
13         * Constructs a simplicial category.
14         */

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

19         public Category.Morphism identity(Object JavaDoc a) {
20                 Preorder p=(Preorder)a;
21                 Integer JavaDoc id[]=new Integer JavaDoc[p.ordinal()];
22                 for(int i=0;i<id.length;i++)
23                         id[i]=new Integer JavaDoc(i);
24                 return new IncreasingMap(p,id);
25         }
26         /**
27         * Returns the cardinality of an object.
28         */

29         public Object JavaDoc cardinality(Object JavaDoc a) {
30                 return new MathInteger(((Preorder)a).ordinal());
31         }
32         /**
33         * Returns a hom-set.
34         */

35         public Category.HomSet hom(Object JavaDoc a,Object JavaDoc b) {
36                 return new FunctionSet((Preorder)a,(Preorder)b);
37         }
38         public Object JavaDoc terminal() {
39                 return new Preorder(1);
40         }
41         public class FunctionSet implements Category.HomSet {
42                 private final Preorder from,to;
43                 public FunctionSet(Preorder a,Preorder b) {
44                         from=a;
45                         to=b;
46                 }
47                 public int cardinality() {
48                         return (int)ExtraMath.binomial(from.ordinal()+to.ordinal()-1,from.ordinal());
49                 }
50         }
51         public class IncreasingMap implements Functor {
52                 protected final Preorder from,to;
53                 protected final Integer JavaDoc out[];
54                 public IncreasingMap(Preorder toObj,Integer JavaDoc toImg[]) {
55                         from=new Preorder(toImg.length);
56                         to=toObj;
57                         out=toImg;
58                 }
59                 public Object JavaDoc domain() {
60                         return from;
61                 }
62                 public Object JavaDoc codomain() {
63                         return to;
64                 }
65                 public Object JavaDoc map(Object JavaDoc o) {
66                         return out[((Integer JavaDoc)o).intValue()];
67                 }
68                 public Category.Morphism map(Category.Morphism m) {
69                         return ((Preorder.RelationSet)to.hom(map(m.domain()),map(m.codomain()))).morphism;
70                 }
71                 public Category.Morphism compose(Category.Morphism m) {
72                         return compose((Functor)m);
73                 }
74                 public Functor compose(Functor f) {
75                         if(f instanceof IncreasingMap) {
76                                 IncreasingMap im=(IncreasingMap)f;
77                                 if(to.equals(im.from)) {
78                                         Integer JavaDoc outImg[]=new Integer JavaDoc[out.length];
79                                         for(int i=0;i<outImg.length;i++)
80                                                 outImg[i]=(Integer JavaDoc)im.map(out[i]);
81                                         return new IncreasingMap(im.to,outImg);
82                                 } else
83                                         throw new UndefinedCompositionException();
84                         } else
85                                 throw new IllegalArgumentException JavaDoc("Morphism is not an IncreasingMap.");
86                 }
87         }
88         public final class FaceMap extends IncreasingMap {
89                 private final int skip;
90                 public FaceMap(Preorder toObj,int i) {
91                         super(toObj,new Integer JavaDoc[toObj.ordinal()-1]);
92                         skip=i;
93                         for(i=0;i<skip;i++)
94                                 out[i]=new Integer JavaDoc(i);
95                         for(;i<out.length;i++)
96                                 out[i]=new Integer JavaDoc(i+1);
97                 }
98         }
99         public final class DegeneracyMap extends IncreasingMap {
100                 private final int repeat;
101                 public DegeneracyMap(Preorder toObj,int i) {
102                         super(toObj,new Integer JavaDoc[toObj.ordinal()+1]);
103                         repeat=i;
104                         for(i=0;i<=repeat;i++)
105                                 out[i]=new Integer JavaDoc(i);
106                         for(;i<out.length;i++)
107                                 out[i]=new Integer JavaDoc(i-1);
108                 }
109         }
110         public final class Addition implements Bifunctor {
111                 public Addition() {}
112                 public Object JavaDoc map(Object JavaDoc a,Object JavaDoc b) {
113                         return new Preorder(((Preorder)a).ordinal()+((Preorder)b).ordinal());
114                 }
115                 public Category.Morphism map(Category.Morphism m, Category.Morphism n) {
116                         IncreasingMap im=(IncreasingMap)m;
117                         IncreasingMap in=(IncreasingMap)n;
118                         Preorder to=new Preorder(im.to.ordinal()+in.to.ordinal());
119                         Integer JavaDoc toObj[]=new Integer JavaDoc[im.out.length+in.out.length];
120                         int i;
121                         for(i=0;i<im.out.length;i++)
122                                 toObj[i]=im.out[i];
123                         for(;i<toObj.length;i++)
124                                 toObj[i]=in.out[i];
125                         return new IncreasingMap(to,toObj);
126                 }
127         }
128 }
129
130
Popular Tags