Showing posts with label Matrix Echelon Form. Show all posts
Showing posts with label Matrix Echelon Form. Show all posts

Sunday, 30 June 2013

Matrix Echelon Form

Convert Matrix into Echelon Form

Java Program

Code:

import java.util.Scanner;

public class echo {

    static int Count;
    public static  void rowSwap(double[][] a)
    {
       
        if (a[0][ 0] == 0)
        {
            Count++;
            double[] primaryRow = new double[a.length];
            for (int i = 0; i < a.length; i++)
            {
                primaryRow[i] = a[0][ i];
            }
            for (int j = 0; j < a.length; j++)
            {
                int i = 1;
                for (i=1; i < a.length; i++)
                {
                    if (a[i][j] != 0)
                    {

                        for (int m = 0; m < primaryRow.length; m++)
                        {
                            a[0][ m] = a[i][m];
                            a[i][ m] = primaryRow[m];
                        }
                        break;
                    }
                }
                if (i != a.length)
                {
                    break;
                }
            }
        }
        rowOperations(a);
           for (int i =0; i <a.length; i++) {
                for (int j = 0; j <a.length; j++) {
                System.out.print(" " + a[i][j]);
                }
                System.out.println("");
                }
        }
     private static  void rowOperations(double[][] a)
        {
            double Variable = 0;
            for (int i = 0, j = 0; i < a.length - 1; )
            {
                if (j == 0)
                {
                    while (j < a.length && a[i][ j] == 0)
                    {
                        j++;
                    }
                }
                if (j == a.length)
                {
                    i++;
                    j = 0;
                }
                else
                {

                    for (int l = i + 1; l < a.length; l++)
                    {

                        for (int m = 0; m < a.length; m++)
                        {
                            if (m == 0)
                            {
                                Variable = ((1 / a[i][ j]) * a[l][ j]);
                                if ((Variable * a[i][ j] > 0 && a[l][ j] > 0)|| (Variable * a[i][ j] < 0 && a[l][ j] < 0))
                                    Variable = Variable * (-1);

                            }

                            a[l][ m] = Math.round(Variable * a[i][ m] + a[l][ m]);
                        

                        }
                    }
                    i++;
                    j = 0;
                }
            }

        }
       
public static void main(String [] args){
    Scanner input = new Scanner(System.in);     
    System.out.println("Enter the number of rows ");     
    int x = input.nextInt(); 
    System.out.print("Enter the number of columns ");     
     int y = input.nextInt();     
     
     double[][]a  =  new double [x][y]; 
 
    System.out.println( "Enter " + a.length + " rows and " + a[0].length + " columns: "); 
                for (int f = 0; f < a.length; f++)  
                    for (int g = 0; g < a[f].length; g++) 
                    a[f][g] = input.nextDouble();
                rowSwap(a);
}}

Program Screenshot: