Guest User

Untitled

a guest
Feb 20th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. /////////////////gloabal variables
  3.  
  4. //rows and columns in individual matrices
  5. var ROWS_A = 0;
  6. var COLUMNS_A = 0;
  7. var ROWS_B = 0;
  8. var COLUMNS_B = 0;
  9.  
  10. //arrays to store the given matrices
  11. var MATRIX_A = [];
  12. var MATRIX_B = [];
  13.  
  14. //array to store the resultant matrix
  15. var MATRIX_C = [];
  16. /////////////////////////////////
  17.  
  18. function main(){
  19.     ROWS_A = get_rows("matrix_a"); //get number of rows in A
  20.     COLUMNS_A = get_cols("matrix_a"); // number of columns in A
  21.     ROWS_B = get_rows("matrix_b");//number of rows in B
  22.     COLUMNS_B = get_cols("matrix_b");//number of columns in B
  23.    
  24.     //convert matrices into arrays
  25.     MATRIX_A = toArray("matrix_a");
  26.     MATRIX_B = toArray("matrix_b");
  27.  
  28. }
  29.  
  30. //gets number of rows in the matrix
  31. function get_rows(matrix){
  32. var inp = $(matrix).value;
  33. if (inp == ""){exit();}
  34.     var row = 0;
  35.     //go through the matrix string
  36.         for (i=0;i<inp.length;i++){
  37.         //if the character is newline, add 1 to row count
  38.             if((i != (inp.length))&&(inp.charAt(i) == "\n")){
  39.                 row += 1;
  40.             }
  41.         }
  42.     return (row += 1);
  43. }
  44.  
  45. //returns number of columns in the given array
  46. function get_cols(matrix){
  47. var inp = $(matrix).value;
  48. var count = 0;
  49. //goes through the string finds number of spaces in the FIRST LINE
  50.     for (i=0;i<inp.length;i++){
  51.         if(inp.charAt(i) == " " && i != 0){
  52.                 count += 1;
  53.         }
  54.         if(inp.charAt(i) == "\n"){
  55.                 break;
  56.         }
  57.     }
  58.     //since number of columns = spaces + 1, we return that
  59.     return (count+1);
  60. }
  61.  
  62. function toArray(matrix){
  63. //get the string
  64.     var temp_string = $(matrix).value;
  65.     // This - "/ |\n/" matches both newline and space characters
  66.     var ret_array = temp_string.split(/ |\n/);
  67.     return ret_array;
  68. }
  69.  
  70. //used for matrix multiplication
  71. function to_twod_Array(matrix){
  72. //get the string
  73.     var temp_string = $(matrix).value;
  74.     //split into an array of lines/rows
  75.     var retArray = temp_string.split(/\n/);
  76.     //new array with rows of A * columns of B elements
  77.     var newArray = new Array(ROWS_A*COLUMNS_B);
  78.     //split the lines into two dimensional new array
  79.     for(i=0;i<retArray.length;i++){
  80.         newArray[i] = retArray[i].split(" ");
  81.     }
  82.     return newArray;
  83. }
  84.  
  85. //adds two matrices
  86. function add(){
  87.     main();
  88.     verifyInput();
  89.     for(i=0;i<MATRIX_A.length;i++){
  90.         MATRIX_C[i]= (MATRIX_A[i]/1) + (MATRIX_B[i]/1);
  91.        
  92.     }
  93.     disp_matrix();
  94. }
  95.  
  96. //subtracts two matrices
  97. function subtract(){
  98.     main();
  99.     verifyInput();
  100.     for(i=0;i<MATRIX_A.length;i++){
  101.         MATRIX_C[i]= (MATRIX_A[i]/1) - (MATRIX_B[i]/1);
  102.     }
  103.     disp_matrix();
  104. }
  105.  
  106. //multiplication handler
  107. function multiply(){
  108.     ROWS_A = get_rows("matrix_a"); //get number of rows in A
  109.     COLUMNS_A = get_cols("matrix_a"); // number of columns in A
  110.     ROWS_B = get_rows("matrix_b");//number of rows in B
  111.     COLUMNS_B = get_cols("matrix_b");//number of columns in B
  112.     verifyMult();//verify input for multiplication
  113.    
  114.     //gets two dimensional arrays
  115.     MATRIX_A = to_twod_Array("matrix_a");
  116.     MATRIX_B = to_twod_Array("matrix_b");
  117.    
  118.     var temp = 0; //used to save single elements of the resultant matrix inside the following loops
  119.     var elem = 0; //keeps track of the element number
  120.    
  121.     for(i=0;i<ROWS_B;i++){
  122.         for(j=0;j<COLUMNS_B;j++){
  123.             for(k=0;k<ROWS_B;k++){
  124.                 temp += MATRIX_A[i][k]*MATRIX_B[k][j];
  125.                 MATRIX_C[elem] = temp;
  126.                 //alert("MATRIX_A[" + i + "][" + k + "]*MATRIX_B[" + k + "][" + j + "]=" + (MATRIX_A[i][k]*MATRIX_B[k][j]));
  127.             }
  128.             temp = 0;
  129.             elem += 1;
  130.         }
  131.         if(elem == (ROWS_A*COLUMNS_B)){
  132.             break;
  133.         }
  134.     }
  135.     disp_matrix();
  136. }
  137.  
  138. function transpose(){
  139.     ROWS_A = get_rows("matrix_a"); //get number of rows in A
  140.     COLUMNS_A = get_cols("matrix_a"); // number of columns in A
  141.  
  142.     MATRIX_A = to_twod_Array("matrix_a");//convert into 2d array
  143.    
  144.     //create a new array with as many elements as the columns of matrix A
  145.     var newArray = new Array(COLUMNS_A);
  146.        
  147.         //make it multidimensional
  148.         for(i=0;i<newArray.length;i++){
  149.             newArray[i] = new Array(ROWS_A);
  150.         }
  151.         //transpose process... like transposed_array[1][0] = original_array[0][1]...
  152.         for(i=0;i<ROWS_A;i++){
  153.             for(j=0;j<COLUMNS_A;j++){
  154.                 newArray[j][i] = MATRIX_A[i][j];
  155.             }
  156.         }
  157.        
  158.         //just a bunch of printing loops
  159.         var result=$("result");
  160.         result.innerHTML = "";
  161.         result.innerHTML = "<h3>Result - Here's what I   got:</h3>";
  162.         for(i=0;i<newArray.length;i++){
  163.             for(j=0;j<ROWS_A;j++){
  164.                 result.innerHTML += newArray[i][j];
  165.                 result.innerHTML += " ";
  166.             }
  167.             result.innerHTML += "<br />";
  168.         }
  169.         if((result.innerHTML.search(/NaN|undefined/)) != (-1)){
  170.         result.innerHTML = "<h3 style='word-spacing:2px'>Result - Something went wrong</h3><p style='word-spacing:3px'>Make sure you entered a valid input. This may happen even when an extra space or newline is added in the input..</p>"
  171.         }else{
  172.             exit();
  173.         }
  174. }
  175.  
  176. function determinant(){
  177.     ROWS_A = get_rows("matrix_a"); //get number of rows in A
  178.     COLUMNS_A = get_cols("matrix_a"); // number of columns in A
  179.    
  180.     if (ROWS_A != COLUMNS_A){
  181.         alert("Determinant can only be found out for square matrices. ie. having dimesions of 2X2, 3X3 etc. This tool only allows dimensions upto 3X3");
  182.         exit();
  183.     }
  184.     MATRIX_A = to_twod_Array("matrix_a");//convert into 2d array
  185.    
  186.     switch(COLUMNS_A){
  187.     case 2:
  188.         alert((MATRIX_A[0][0]*MATRIX_A[1][1])-(MATRIX_A[0][1]*MATRIX_A[1][0]));
  189.         break;
  190.     case 3:
  191.         var temp1 = ( MATRIX_A[0][0]*((MATRIX_A[1][1]*MATRIX_A[2][2]) - (MATRIX_A[1][2]*MATRIX_A[2][1]) )) ;
  192.         var temp2 = ( MATRIX_A[0][1]*((MATRIX_A[1][0]*MATRIX_A[2][2]) - (MATRIX_A[1][2]*MATRIX_A[2][0]) ));
  193.         var temp3 = ( MATRIX_A[0][2]*((MATRIX_A[1][0]*MATRIX_A[2][1]) - (MATRIX_A[1][1]*MATRIX_A[2][0]) ));
  194.         var ans = (temp1 - temp2 + temp3);
  195.         var result = $("result");
  196.         result.innerHTML = " ";
  197.         result.innerHTML += "<h3>Result - Here's what I  got:</h3>";
  198.         result.innerHTML += "<p style='word-spacing:3px'>Determinant of matrix A = " + ans + "</p>";
  199.         //result.innerHTML += ans;
  200.         break;
  201.     default:
  202.         alert("Unknown dimensions. Cannot calculate");
  203.     }
  204.    
  205.    
  206. }
  207. /////////////////////////utility functions
  208.  
  209. //simplifies accessing DOM
  210. function $(name) {
  211.     return document.getElementById(name);
  212. }
  213.  
  214. //displays the resultant matrix(resultant is always MATRIX C) in the page
  215. function disp_matrix(){
  216.     var result = $("result");
  217.     result.style.display = "block";
  218.     result.innerHTML = "";
  219.     result.innerHTML = "<h3>Result - Here's what I   got:</h3>";
  220.     for(i=0;i<MATRIX_C.length;i++){
  221.        
  222.                 result.innerHTML += MATRIX_C[i];
  223.                 result.innerHTML += " ";
  224.                 //if its time for a linebreak, put it
  225.                 if( ((i+1)%(COLUMNS_B) ) == 0){
  226.                     result.innerHTML += "<br />";
  227.                 }
  228.            
  229.     }
  230.     if((result.innerHTML.search(/NaN|undefined/)) != (-1)){
  231.         result.innerHTML = "<h3 style='word-spacing:2px'>Result - Something went wrong</h3><p style='word-spacing:3px'>Make sure you entered a valid input. This may happen even when an extra space or newline is added in the input..</p>"
  232.     }else{
  233.         exit();
  234.     }
  235. }
  236.  
  237. function swap(){
  238.     var a = $("matrix_a")
  239.     var b = $("matrix_b");
  240.    
  241.     matrixA= a.value;
  242.     matrixB= b.value;
  243.     var temp = matrixA;;
  244.    
  245.     a.value = matrixB;
  246.     b.value = temp;
  247.    
  248.    
  249. }
  250.  
  251. //////////////////////////error handler and input verfication
  252. function verifyInput(){
  253.     if(ROWS_A != ROWS_B || COLUMNS_A != COLUMNS_B){
  254.         alert("The dimesions of the matrices are not equal. Try again.");
  255.         exit();
  256.     }
  257.     if((MATRIX_A[0] == 'undefined') || (MATRIX_B[0] == 'undefined')){
  258.         alert("Looks like one or both of the input boxes is or are left blank! Try again.");
  259.     }
  260. }
  261.  
  262. function verifyMult(){
  263.     if(COLUMNS_A != ROWS_B){
  264.         alert("To multiply matrices, the number of columns in A should be equal to the number of rows in B. Try again.");
  265.         exit();
  266.     }
  267. }
Add Comment
Please, Sign In to add comment