waltermilner

Untitled

Apr 8th, 2016
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3.  
  4.   <script>
  5.     'use strict';
  6.  
  7. // define Group objects..
  8. // The parameter m should be a square 2D array, being the multiplication table of the group,
  9. // including row and column headings
  10. // we do not check m for validity
  11. // but we have checks for associativity etc
  12.     function Group(m)
  13.     {
  14.         // constructor
  15.         this.order = m.length - 1;
  16.         this.elements = []; // the elements of the group
  17.         // get them from the column headings
  18.         for (var i = 1; i < m.length; i++)
  19.             this.elements.push(m[0][i]);
  20.  
  21.         // get entries from m into table
  22.         // avoiding headings
  23.         this.table = []; //empty array
  24.         for (var i = 1; i < m.length; i++)
  25.         { // for each row except the first
  26.             var temp = []; // empty row
  27.             for (var j = 1; j < m.length; j++)
  28.             { // for each column except the first
  29.                 var item = m[i][j];
  30.                 temp.push(item); // add to row
  31.             }
  32.             this.table.push(temp);// add row to array
  33.         }
  34.  
  35.  
  36.  
  37.         // Methods..
  38.         //
  39.         // List elements
  40.         this.list = function ()
  41.         {
  42.             document.write("Group elements: ");
  43.             for (var x = 0; x < this.order; x++)
  44.                 document.write(this.elements[x] + " ");
  45.             document.writeln("<br><br>");
  46.         };
  47.  
  48.         // Display the table (no headings)
  49.         this.showTable = function ()
  50.         {
  51.             document.write("Group table:<br>");
  52.             for (var i = 0; i < this.order; i++) // go through the table
  53.             {
  54.                 for (var j = 0; j < this.order; j++)
  55.                 {
  56.                     document.write(this.table[i][j] + " ");
  57.  
  58.                 }
  59.                 document.writeln("<br>");
  60.             }
  61.             document.writeln("<br>");
  62.         };
  63.  
  64.         // apply table to find x.y
  65.         this.mult = function (x, y)
  66.         {
  67.             var i = this.elements.indexOf(x);
  68.             var j = this.elements.indexOf(y);
  69.             return this.table[i][j];
  70.         };
  71.  
  72.  
  73.         // is this associative?
  74.         this.isAssoc = function ()
  75.         { // get all possible triplets
  76.             for (var i = 0; i < this.order; i++)
  77.                 for (var j = 0; j < this.order; j++)
  78.                     for (var k = 0; k < this.order; k++)
  79.                     { // for all x,y,z, x.(y.z) = (x.y).z
  80.                         var x = this.elements[i];
  81.                         var y = this.elements[j];
  82.                         var z = this.elements[k];
  83.                         if (this.mult(x, this.mult(y, z)) !== this.mult(this.mult(x, y), z))
  84.                             return false;
  85.                     }
  86.             // none false, so is true
  87.             return true;
  88.         };
  89.  
  90.         // identity element
  91.         this.identity = function ()
  92.         {
  93.             // return the identity element, or
  94.             // false if there is none
  95.             for (var i = 0; i < this.order; i++)
  96.             { // check all possible candidates
  97.                 var candidate = this.elements[i];
  98.                 // is this the identity?
  99.                 var isIdentity = true;
  100.                 for (var j = 0; j < this.order; j++)
  101.                 {
  102.                     var other = this.elements[i];
  103.                     // is candidate.other=other?
  104.                     if (this.mult(candidate, other) !== other || this.mult(other, candidate) !== other)
  105.                         isCandidate = false;
  106.                 }
  107.                 if (isIdentity) // works for all
  108.                     return candidate;
  109.             }
  110.             return false; // failed to find one
  111.         };
  112.  
  113.         // return inverse of an element x,
  114.         // or false if there is no inverse
  115.         this.inverse = function (x)
  116.         {
  117.             var id = this.identity();
  118.             if (id === false) // no identity = no inverse
  119.                 return false;
  120.             for (var i = 0; i < this.order; i++)
  121.             { // check all possibilities
  122.                 var item = this.elements[i];
  123.                 if (this.mult(x, item) === id && this.mult(item, x) === id)
  124.                     return item;
  125.             }
  126.             return false; // there is no inverse
  127.         };
  128.  
  129.         // Does every element have an inverse?
  130.         this.allHasInverse = function ()
  131.         {
  132.             for (var i = 0; i < this.order; i++)
  133.             {
  134.                 var item = this.elements[i]; // for each element
  135.                 if (this.inverse(item) === false)
  136.                     return false;
  137.             }
  138.             return true;
  139.         };
  140.  
  141.         this.isAbelian = function ()
  142.         {
  143.             for (var i = 0; i < this.order; i++)
  144.                 for (var j = 0; j < this.order; j++)
  145.                 { // for all x,y, x.y = y.x
  146.                     var x = this.elements[i];
  147.                     var y = this.elements[j];
  148.                     if (this.mult(x, y) !== this.mult(y, x))
  149.                         return false;
  150.                 }
  151.             return true;
  152.         };
  153.     }
  154.  
  155.     var m = [
  156.         [" ", "A", "B", "C"],
  157.         ["A", "A", "B", "C"],
  158.         ["B", "B", "C", "A"],
  159.         ["C", "C", "A", "B"]
  160.     ];
  161.     var G3 = new Group(m);
  162.  
  163.     G3.list();
  164.     G3.showTable();
  165.  
  166.     document.write("Is it associative? " + G3.isAssoc() + "<br>");
  167.     document.write("The identity is? " + G3.identity() + "<br>");
  168.     document.write("Does every element have an inverse? " + G3.allHasInverse() + "<br>");
  169.     document.write("Is it Abelian? " + G3.isAbelian() + "<br>");
  170.  
  171.   </script>
  172.   <head>
  173.     <title>Group</title>
  174.   </head>
  175.   <body>
  176.   </body>
  177. </html>
Advertisement
Add Comment
Please, Sign In to add comment