001: package JSci.maths.categories;
002:
003: import JSci.maths.*;
004:
005: /**
006: * The FinSet class encapsulates the category <b>FinSet</b>.
007: * @version 1.0
008: * @author Mark Hale
009: */
010: public class FinSet extends Object implements Category {
011: /**
012: * Constructs a <b>FinSet</b> category.
013: */
014: public FinSet() {
015: }
016:
017: /**
018: * Returns the identity morphism for an object.
019: */
020: public Category.Morphism identity(Object a) {
021: return new IdentityFunction((MathSet) a);
022: }
023:
024: /**
025: * Returns the cardinality of an object.
026: */
027: public Object cardinality(Object a) {
028: return new MathInteger(((MathSet) a).cardinality());
029: }
030:
031: /**
032: * Returns a hom-set.
033: */
034: public Category.HomSet hom(Object a, Object b) {
035: return new FunctionSet((MathSet) a, (MathSet) b);
036: }
037:
038: public class FunctionSet implements MathSet, Category.HomSet {
039: private final MathSet from, to;
040: private final int size;
041:
042: public FunctionSet(MathSet a, MathSet b) {
043: from = a;
044: to = b;
045: size = ExtraMath.pow(b.cardinality(), a.cardinality());
046: }
047:
048: /**
049: * Returns an element of this hom-set.
050: */
051: public Function getElement(Object in[], Object out[]) {
052: return new Function(from, to, in, out);
053: }
054:
055: public int cardinality() {
056: return size;
057: }
058:
059: public MathSet union(MathSet set) {
060: return set.union(this );
061: }
062:
063: public MathSet intersect(MathSet set) {
064: return set.intersect(this );
065: }
066: }
067:
068: public class Function implements Category.Morphism {
069: private final MathSet from, to;
070: private final Object in[], out[];
071:
072: public Function(MathSet a, MathSet b, Object inObjs[],
073: Object outObjs[]) {
074: from = a;
075: to = b;
076: in = inObjs;
077: out = outObjs;
078: }
079:
080: public Object domain() {
081: return from;
082: }
083:
084: public Object codomain() {
085: return to;
086: }
087:
088: public Object map(Object o) {
089: for (int i = 0; i < in.length; i++) {
090: if (o.equals(in[i]))
091: return out[i];
092: }
093: return null;
094: }
095:
096: public Category.Morphism compose(Category.Morphism m) {
097: if (m instanceof Function) {
098: Function f = (Function) m;
099: if (to.equals(f.from)) {
100: Object outObjs[] = new Object[in.length];
101: for (int i = 0; i < outObjs.length; i++)
102: outObjs[i] = f.map(out[i]);
103: return new Function(from, f.to, in, outObjs);
104: } else
105: throw new UndefinedCompositionException();
106: } else
107: throw new IllegalArgumentException(
108: "Morphism is not a Function.");
109: }
110: }
111:
112: private class IdentityFunction extends Function {
113: public IdentityFunction(MathSet s) {
114: super (s, s, null, null);
115: }
116:
117: public Object map(Object o) {
118: return o;
119: }
120:
121: public Category.Morphism compose(Category.Morphism m) {
122: if (m instanceof Function) {
123: return m;
124: } else
125: throw new IllegalArgumentException(
126: "Morphism is not a Function.");
127: }
128: }
129: }
|