Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Reduced Row Echelon Form for augmented matrices

807569Jul 26 2006 — edited Jul 26 2006
The Matrix class is suppose to encapsulate a 2D array as an matrix, and provide methods to find the row echelon form of the matrix. The code is complete, but there is a bug: once in a while I can end up with an Nah or -Infinity for an the value of an entire row after a numerator coincidentally ends up being zero when it is used to find the multiple. (See the comments for more on how I plan to make this work). Any suggestions? By the way, what would be a good start for finding the Reduced Row Echelon Form of a matrix?

Matrix.java
/*
 *The Matrix Class Encapsulates a 2 Dimensional Array
 *For The Storage Of Elements Belonging To A Matrix.
 *An Matrix Object Provides Methods For Converting The Matrix
 *To It's Row Echelon Form Using The Gaussian Elimination Method.
 */
public class Matrix
{
	private double[][] matrix; //The Array That Will Store Values Of The Matrix
	
	/*
	 *Constructor Takes In Two Integer Arguments; Which Specify The Dimension
	 *Of The Matrix
	 */
	public Matrix(int row, int col)
	{
		matrix = new double[row][col];
	}
	
	/*
	 *This Method Allows User To Manually Set A Specific Element Located At
	 *[row]col] to a Given Value of type double Precision.
	 */
	public void fill(int row, int col, double value)
	{
		matrix[row][col] = value;
	}
	
	/*
	 *This Method Allows User To Replace All Elements In The Matrix By
	 *Passing As an Argument A New double 2D Array.
	 */
	public void fill(double[][] values)
	{
		matrix = values;
	}	
	
	/*
	 *reduce() Performs an series of Operation To a given row 
	 *In a Matrix so that the target column of that Row
	 *ends up being 0 after the method returns.
	 */
	private void reduce(int row, int target)
	{
		//Two additional arrays are created to hold the value 
		//of two rows from the matrix; 1)'ref' represents the row that will
		//be reduced by the end of the method. 
		//'from' represents the row from which the 'ref' row eliminates its leading
		//coefficent
		double[] ref = new double[matrix[row].length];
		double[] from = new double[matrix[target].length];
		
		//values of both rows are copied to the new arrays to clarify purposes
		for (int i = 0; i < ref.length; i++)
		{
			ref[i] = matrix[row];
from[i] = matrix[target][i];
}

//the process works like this: 1st Find the lowest rational multiple of
//the leading coefficents of each row. Then multiply each element in the
//'ref' row by that multiple. lastly subtract the 'from' row by the 'ref'
//row to obtain an row of the matrix with it's leading coefficent removed
//then the updated 'ref' row is copied back to its position in the matrix
double multiple = from[target] / ref[target];

for (int i = 0; i < ref.length; i++)
ref[i]*=multiple;

for (int i = 0; i < ref.length; i++)
ref[i] = from[i] - ref[i];

for (int i = 0; i < ref.length; i++)
matrix[row][i] = ref[i];

}

//All following methods work perfectly, their purpose (besides toString()
//is to swap the rows in the matrix so that rows with the most consecutive
//leading zeros are swaped toward the buttom of the matrix.
private void swapRow(int rowA, int rowB)
{
double[] a = matrix[rowA];
double[] b = matrix[rowB];
double[] temp = new double[matrix[0].length];

for (int i = 0; i < a.length; i++)
temp[i] = a[i];

for (int i = 0; i < b.length; i++)
a[i] = b[i];

for (int i = 0; i < a.length; i++)
b[i] = temp[i];
}

public void rearrange(int initial, int end)
{
int i;
int[] tally = new int[end-initial + 1];
for (i = initial; i <= end; i++)
{
int k = 0;
while(k < (matrix[0].length) - 1 && matrix[i][k] == 0)
{
tally[i]++;
k++;
}
}

int max = max(tally);

swapRow(end,max);

if (initial == end)
return;
else
rearrange(initial, end - 1);
}

//returns the index of the max value in tally
private int max(int[] data)
{
int max = 0;
for (int i = 1; i < data.length; i++)
if (data[i] > data [max])
max = i;
return max;
}

public void ref()
{
rearrange(0,matrix.length-1);
for (int i = 1; i < matrix.length; i++) //i = row k = col
for (int k = 0; k < i; k++)
if (matrix[i][k]!=0)
reduce(i,k);
}

public String toString()
{
String str = "[[";
for (int i = 0; i < matrix.length; i++)
{
for (int j = 0; j < matrix[i].length; j++)
{
str+=matrix[i][j];
if (j != matrix[i].length-1)
str+=" , ";
else if (i != matrix.length - 1)
str+="]\n [";

}
if (i == matrix.length - 1)
str+="]]";
}
return str;
}
}

A Test Class:
import java.util.Random;
public class SimpleMatrix
{
	static Random r = new Random();
	
	public static void main(String[] args)
	{		
		Matrix m = new Matrix(4,5);
		m.fill(createMatrix(4,5));
		System.out.println("ORIGINAL MATRIX");
		System.out.println(m);
		
		m.ref();
		
		System.out.println("MATRIX AFTER REF");
		System.out.println(m);
	}
	
	public static double[][] createMatrix(int row, int col)
	{
		double[][] matrix = new double[row][col];
		for (int i = 0; i < matrix.length; i++)
		{
			for (int j = 0; j < matrix.length; j++)
{
matrix[i][j] = r.nextInt(19);
}
}
return matrix;
}

public static double[][] add(double[][] a, double[][] b)
{
double[][] sum = new double[a.length][a[0].length];
for (int i = 0; i < sum.length; i++)
for (int j = 0; j < sum[i].length; j++)
sum[i][j] = a[i][j] + b[i][j];
return sum;
}

public static void displayMatrix(double[][] matrix)
{
for (int i = 0; i < matrix.length; i++)
{
for (int j = 0; j < matrix[i].length; j++)
System.out.print(matrix[i][j] + " ");
System.out.println();
}
}
}


**If you run the program, you will see that it pretty much work as intended under most circumstances. Except that the final matrix doesn't have 1s as each row's leading coefficent. Just another problem I'm having.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 23 2006
Added on Jul 26 2006
3 comments
716 views