Group.js
Summary
This file defines the Group class, instance methods, and static objects/methods. It depends on the Set class, but as long as easyload.js is loaded first, Set.js will automatically be included with this file.
dependencies = ['Set'];
require(dependencies);
function Group(elements, operator) {
if(elements instanceof Array) {
this.set = new Set(elements);
}
if(elements instanceof Set) {
this.set = elements;
}
this.operation = operator;
this.hasClosure = false;
this.hasAssociativity = false;
this.hasIdentity = false;
this.hasInverse = false;
this.identity;
}
Group.prototype = {
testClosure: function() {
for(i = 0; i < this.set.cardinality; i++) {
for(j = 0; j < this.set.cardinality;) {
if(!(this.set.inSet(this.operand(this.set.elements[i],this.set.elements[j])))) {
this.hasClosure = false;
return this.hasClosure;
}
}
}
this.hasClosure = true;
return this.hasClosure;
},
setIdentity: function() {
for(i = 0; i < this.set.cardinality; i++) {
if(this.operand(this.set.elements[i],this.set.elements[i])==this.set.elements[i]) {
this.hasIdentity = true;
this.identity = this.set.elements[i]
}
}
},
getIdentity: function() {
if(this.hasIdentity) {
return this.identity;
} else {
return false;
}
},
testIdentity: function() {
return this.hasIdentity;
},
testAssociativity: function() {
for(i = 0; i < this.set.cardinality; i++) {
for(j = 0; j < this.set.cardinality; j++) {
for(k = 0; k < this.set.cardinality; k++) {
if(this.operand(this.operand(this.set.elements[i],this.set.elements[j]),this.set.elements[k]) != this.operand(this.set.elements[i],this.operand(this.set.elements[j],this.set.elements[k]))) {
this.hasAssociativity = false;
return this.hasAssociativity;
}
}
}
}
this.hasAssociativity = true;
return this.hasAssociativity;
},
testInverse: function() {
var numInverses = 0;
var temp;
if(this.hasIdentity) {
for(i = 0; i < this.set.cardinality; i++) {
temp = false;
for(j = 0; j < this.set.cardinality; j++) {
if(this.operand(this.set.elements[i],this.set.elements[j]) == this.identity && this.operand(this.set.elements[j],this.set.elements[i]) == this.identity) {
temp = true;
}
}
if(temp) {
numInverses++;
}
}
if(numInverses >= this.set.cardinality) {
this.hasInverse = true;
}
return this.hasInverse;
} else {
return this.hasInverse;
}
},
isGroup: function() {
if(this.hasClosure && this.hasAssociativity && this.hasIdentity && this.hasInverse) {
return true;
} else {
return false;
}
},
testCycle: function(array) {
var tempvec = new Array();
var cycle2 = new Array();
var userCycle = new Array();
userCycle = array;
if(userCycle.length == 1) {
if(this.set.elements[userCycle[0]] == userCycle[0]) {
return true;
} else {
return false;
}
}
for(i = 0; i < userCycle.length; i++) {
cycle2[i] = this.set.elements[userCycle[i]];
}
var temp = cycle2[userCycle.length - 1];
for(i = 0; i < cycle2.length; i++) {
tempvec[i] = cycle2[i];
}
cycle2[0] = temp;
for(i = 1; i < userCycle.length; i++) {
cycle2[i] = tempvec[i-1];
}
if(cycle2.compareArray(userCycle)) {
return true;
} else {
return false;
}
},
operand: function(el1, el2) {
if(typeof this.operation == 'function') {
return this.operation(el1, el2);
} else {
if(this.operation == 'multiplication') {
return el1 * el2;
}
if(this.operation == 'addition') {
return el1 + el2;
}
}
}
}
Documentation generated by
JSDoc on Tue Aug 5 10:22:22 2008