Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /////////////////gloabal variables
- //rows and columns in individual matrices
- var ROWS_A = 0;
- var COLUMNS_A = 0;
- var ROWS_B = 0;
- var COLUMNS_B = 0;
- //arrays to store the given matrices
- var MATRIX_A = [];
- var MATRIX_B = [];
- //array to store the resultant matrix
- var MATRIX_C = [];
- /////////////////////////////////
- function main(){
- ROWS_A = get_rows("matrix_a"); //get number of rows in A
- COLUMNS_A = get_cols("matrix_a"); // number of columns in A
- ROWS_B = get_rows("matrix_b");//number of rows in B
- COLUMNS_B = get_cols("matrix_b");//number of columns in B
- //convert matrices into arrays
- MATRIX_A = toArray("matrix_a");
- MATRIX_B = toArray("matrix_b");
- }
- //gets number of rows in the matrix
- function get_rows(matrix){
- var inp = $(matrix).value;
- if (inp == ""){exit();}
- var row = 0;
- //go through the matrix string
- for (i=0;i<inp.length;i++){
- //if the character is newline, add 1 to row count
- if((i != (inp.length))&&(inp.charAt(i) == "\n")){
- row += 1;
- }
- }
- return (row += 1);
- }
- //returns number of columns in the given array
- function get_cols(matrix){
- var inp = $(matrix).value;
- var count = 0;
- //goes through the string finds number of spaces in the FIRST LINE
- for (i=0;i<inp.length;i++){
- if(inp.charAt(i) == " " && i != 0){
- count += 1;
- }
- if(inp.charAt(i) == "\n"){
- break;
- }
- }
- //since number of columns = spaces + 1, we return that
- return (count+1);
- }
- function toArray(matrix){
- //get the string
- var temp_string = $(matrix).value;
- // This - "/ |\n/" matches both newline and space characters
- var ret_array = temp_string.split(/ |\n/);
- return ret_array;
- }
- //used for matrix multiplication
- function to_twod_Array(matrix){
- //get the string
- var temp_string = $(matrix).value;
- //split into an array of lines/rows
- var retArray = temp_string.split(/\n/);
- //new array with rows of A * columns of B elements
- var newArray = new Array(ROWS_A*COLUMNS_B);
- //split the lines into two dimensional new array
- for(i=0;i<retArray.length;i++){
- newArray[i] = retArray[i].split(" ");
- }
- return newArray;
- }
- //adds two matrices
- function add(){
- main();
- verifyInput();
- for(i=0;i<MATRIX_A.length;i++){
- MATRIX_C[i]= (MATRIX_A[i]/1) + (MATRIX_B[i]/1);
- }
- disp_matrix();
- }
- //subtracts two matrices
- function subtract(){
- main();
- verifyInput();
- for(i=0;i<MATRIX_A.length;i++){
- MATRIX_C[i]= (MATRIX_A[i]/1) - (MATRIX_B[i]/1);
- }
- disp_matrix();
- }
- //multiplication handler
- function multiply(){
- ROWS_A = get_rows("matrix_a"); //get number of rows in A
- COLUMNS_A = get_cols("matrix_a"); // number of columns in A
- ROWS_B = get_rows("matrix_b");//number of rows in B
- COLUMNS_B = get_cols("matrix_b");//number of columns in B
- verifyMult();//verify input for multiplication
- //gets two dimensional arrays
- MATRIX_A = to_twod_Array("matrix_a");
- MATRIX_B = to_twod_Array("matrix_b");
- var temp = 0; //used to save single elements of the resultant matrix inside the following loops
- var elem = 0; //keeps track of the element number
- for(i=0;i<ROWS_B;i++){
- for(j=0;j<COLUMNS_B;j++){
- for(k=0;k<ROWS_B;k++){
- temp += MATRIX_A[i][k]*MATRIX_B[k][j];
- MATRIX_C[elem] = temp;
- //alert("MATRIX_A[" + i + "][" + k + "]*MATRIX_B[" + k + "][" + j + "]=" + (MATRIX_A[i][k]*MATRIX_B[k][j]));
- }
- temp = 0;
- elem += 1;
- }
- if(elem == (ROWS_A*COLUMNS_B)){
- break;
- }
- }
- disp_matrix();
- }
- function transpose(){
- ROWS_A = get_rows("matrix_a"); //get number of rows in A
- COLUMNS_A = get_cols("matrix_a"); // number of columns in A
- MATRIX_A = to_twod_Array("matrix_a");//convert into 2d array
- //create a new array with as many elements as the columns of matrix A
- var newArray = new Array(COLUMNS_A);
- //make it multidimensional
- for(i=0;i<newArray.length;i++){
- newArray[i] = new Array(ROWS_A);
- }
- //transpose process... like transposed_array[1][0] = original_array[0][1]...
- for(i=0;i<ROWS_A;i++){
- for(j=0;j<COLUMNS_A;j++){
- newArray[j][i] = MATRIX_A[i][j];
- }
- }
- //just a bunch of printing loops
- var result=$("result");
- result.innerHTML = "";
- result.innerHTML = "<h3>Result - Here's what I got:</h3>";
- for(i=0;i<newArray.length;i++){
- for(j=0;j<ROWS_A;j++){
- result.innerHTML += newArray[i][j];
- result.innerHTML += " ";
- }
- result.innerHTML += "<br />";
- }
- if((result.innerHTML.search(/NaN|undefined/)) != (-1)){
- 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>"
- }else{
- exit();
- }
- }
- function determinant(){
- ROWS_A = get_rows("matrix_a"); //get number of rows in A
- COLUMNS_A = get_cols("matrix_a"); // number of columns in A
- if (ROWS_A != COLUMNS_A){
- alert("Determinant can only be found out for square matrices. ie. having dimesions of 2X2, 3X3 etc. This tool only allows dimensions upto 3X3");
- exit();
- }
- MATRIX_A = to_twod_Array("matrix_a");//convert into 2d array
- switch(COLUMNS_A){
- case 2:
- alert((MATRIX_A[0][0]*MATRIX_A[1][1])-(MATRIX_A[0][1]*MATRIX_A[1][0]));
- break;
- case 3:
- var temp1 = ( MATRIX_A[0][0]*((MATRIX_A[1][1]*MATRIX_A[2][2]) - (MATRIX_A[1][2]*MATRIX_A[2][1]) )) ;
- var temp2 = ( MATRIX_A[0][1]*((MATRIX_A[1][0]*MATRIX_A[2][2]) - (MATRIX_A[1][2]*MATRIX_A[2][0]) ));
- var temp3 = ( MATRIX_A[0][2]*((MATRIX_A[1][0]*MATRIX_A[2][1]) - (MATRIX_A[1][1]*MATRIX_A[2][0]) ));
- var ans = (temp1 - temp2 + temp3);
- var result = $("result");
- result.innerHTML = " ";
- result.innerHTML += "<h3>Result - Here's what I got:</h3>";
- result.innerHTML += "<p style='word-spacing:3px'>Determinant of matrix A = " + ans + "</p>";
- //result.innerHTML += ans;
- break;
- default:
- alert("Unknown dimensions. Cannot calculate");
- }
- }
- /////////////////////////utility functions
- //simplifies accessing DOM
- function $(name) {
- return document.getElementById(name);
- }
- //displays the resultant matrix(resultant is always MATRIX C) in the page
- function disp_matrix(){
- var result = $("result");
- result.style.display = "block";
- result.innerHTML = "";
- result.innerHTML = "<h3>Result - Here's what I got:</h3>";
- for(i=0;i<MATRIX_C.length;i++){
- result.innerHTML += MATRIX_C[i];
- result.innerHTML += " ";
- //if its time for a linebreak, put it
- if( ((i+1)%(COLUMNS_B) ) == 0){
- result.innerHTML += "<br />";
- }
- }
- if((result.innerHTML.search(/NaN|undefined/)) != (-1)){
- 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>"
- }else{
- exit();
- }
- }
- function swap(){
- var a = $("matrix_a")
- var b = $("matrix_b");
- matrixA= a.value;
- matrixB= b.value;
- var temp = matrixA;;
- a.value = matrixB;
- b.value = temp;
- }
- //////////////////////////error handler and input verfication
- function verifyInput(){
- if(ROWS_A != ROWS_B || COLUMNS_A != COLUMNS_B){
- alert("The dimesions of the matrices are not equal. Try again.");
- exit();
- }
- if((MATRIX_A[0] == 'undefined') || (MATRIX_B[0] == 'undefined')){
- alert("Looks like one or both of the input boxes is or are left blank! Try again.");
- }
- }
- function verifyMult(){
- if(COLUMNS_A != ROWS_B){
- alert("To multiply matrices, the number of columns in A should be equal to the number of rows in B. Try again.");
- exit();
- }
- }
Add Comment
Please, Sign In to add comment