Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <script>
- 'use strict';
- // define Group objects..
- // The parameter m should be a square 2D array, being the multiplication table of the group,
- // including row and column headings
- // we do not check m for validity
- // but we have checks for associativity etc
- function Group(m)
- {
- // constructor
- this.order = m.length - 1;
- this.elements = []; // the elements of the group
- // get them from the column headings
- for (var i = 1; i < m.length; i++)
- this.elements.push(m[0][i]);
- // get entries from m into table
- // avoiding headings
- this.table = []; //empty array
- for (var i = 1; i < m.length; i++)
- { // for each row except the first
- var temp = []; // empty row
- for (var j = 1; j < m.length; j++)
- { // for each column except the first
- var item = m[i][j];
- temp.push(item); // add to row
- }
- this.table.push(temp);// add row to array
- }
- // Methods..
- //
- // List elements
- this.list = function ()
- {
- document.write("Group elements: ");
- for (var x = 0; x < this.order; x++)
- document.write(this.elements[x] + " ");
- document.writeln("<br><br>");
- };
- // Display the table (no headings)
- this.showTable = function ()
- {
- document.write("Group table:<br>");
- for (var i = 0; i < this.order; i++) // go through the table
- {
- for (var j = 0; j < this.order; j++)
- {
- document.write(this.table[i][j] + " ");
- }
- document.writeln("<br>");
- }
- document.writeln("<br>");
- };
- // apply table to find x.y
- this.mult = function (x, y)
- {
- var i = this.elements.indexOf(x);
- var j = this.elements.indexOf(y);
- return this.table[i][j];
- };
- // is this associative?
- this.isAssoc = function ()
- { // get all possible triplets
- for (var i = 0; i < this.order; i++)
- for (var j = 0; j < this.order; j++)
- for (var k = 0; k < this.order; k++)
- { // for all x,y,z, x.(y.z) = (x.y).z
- var x = this.elements[i];
- var y = this.elements[j];
- var z = this.elements[k];
- if (this.mult(x, this.mult(y, z)) !== this.mult(this.mult(x, y), z))
- return false;
- }
- // none false, so is true
- return true;
- };
- // identity element
- this.identity = function ()
- {
- // return the identity element, or
- // false if there is none
- for (var i = 0; i < this.order; i++)
- { // check all possible candidates
- var candidate = this.elements[i];
- // is this the identity?
- var isIdentity = true;
- for (var j = 0; j < this.order; j++)
- {
- var other = this.elements[i];
- // is candidate.other=other?
- if (this.mult(candidate, other) !== other || this.mult(other, candidate) !== other)
- isCandidate = false;
- }
- if (isIdentity) // works for all
- return candidate;
- }
- return false; // failed to find one
- };
- // return inverse of an element x,
- // or false if there is no inverse
- this.inverse = function (x)
- {
- var id = this.identity();
- if (id === false) // no identity = no inverse
- return false;
- for (var i = 0; i < this.order; i++)
- { // check all possibilities
- var item = this.elements[i];
- if (this.mult(x, item) === id && this.mult(item, x) === id)
- return item;
- }
- return false; // there is no inverse
- };
- // Does every element have an inverse?
- this.allHasInverse = function ()
- {
- for (var i = 0; i < this.order; i++)
- {
- var item = this.elements[i]; // for each element
- if (this.inverse(item) === false)
- return false;
- }
- return true;
- };
- this.isAbelian = function ()
- {
- for (var i = 0; i < this.order; i++)
- for (var j = 0; j < this.order; j++)
- { // for all x,y, x.y = y.x
- var x = this.elements[i];
- var y = this.elements[j];
- if (this.mult(x, y) !== this.mult(y, x))
- return false;
- }
- return true;
- };
- }
- var m = [
- [" ", "A", "B", "C"],
- ["A", "A", "B", "C"],
- ["B", "B", "C", "A"],
- ["C", "C", "A", "B"]
- ];
- var G3 = new Group(m);
- G3.list();
- G3.showTable();
- document.write("Is it associative? " + G3.isAssoc() + "<br>");
- document.write("The identity is? " + G3.identity() + "<br>");
- document.write("Does every element have an inverse? " + G3.allHasInverse() + "<br>");
- document.write("Is it Abelian? " + G3.isAbelian() + "<br>");
- </script>
- <head>
- <title>Group</title>
- </head>
- <body>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment