Matrix Elementary Row Operation
The matrix in algebra has three row operations are called Matrix Elementary Row Operation. They are
- Swapping any two rows
- Multiply a row by constant
- Adding any two rows
Swap two rows
The matrix A has 3 rows and 3 columns. Row R1 and R2 swapped - Both rows interchanged their elements. i.e R1 becomes R2 and vice versa.
Add two rows
The matrix A has 3 rows and 3 columns. Add two rows R1 and R2 - R1's each elements is added to R2's corresponding elements. resultant row replace row R2 i.e R2 -> R1 + R2 .
Multiply a row by constant
The matrix A has 3 rows and 3 columns. Multiply a row R1 by constant value, 2 - R1's each elements is multiplied by constant 2 and resultant row replace row R1 i.e R1 -> 2 x R1 .
Java program Elementary Row Operation
public class RowOperation {
public static void swap(Matrix mat,int r1,int r2) {
for (int n=0;n<mat.getNcol();n++) {
double tmp= mat.getElement(r1, n);
mat.setElement(r1, n, mat.getElement(r2, n));
mat.setElement(r2, n, tmp);
}
}
public static void multiply(Matrix mat,int r1,double val) {
for (int n=0;n<mat.getNcol();n++) {
mat.setElement(r1, n, val*mat.getElement(r1, n));
}
}
public static void add(Matrix mat,int r1,int r2) {
for (int n=0;n<mat.getNcol();n++)
{
mat.setElement(r2, n, mat.getElement(r1, n)
+ mat.getElement(r2, n));
}
}
public static void subtract(Matrix mat,int r1,int r2) {
for (int n=0;n<mat.getNcol();n++)
{
mat.setElement(r2, n, mat.getElement(r1, n)
- mat.getElement(r2, n));
}
}
public static void add(Matrix mat,int r2,int r1,double val) {
for (int n=0;n<mat.getNcol();n++) {
double tmp= val*mat.getElement(r1, n);
mat.setElement(r2, n, mat.getElement(r2, n)+tmp);
}
}
public static void subtract(Matrix mat,int r2,int r1,double val) {
for (int n=0;n<mat.getNcol();n++) {
double tmp= val*mat.getElement(r1, n);
mat.setElement(r2, n, mat.getElement(r2, n)- tmp);
}
}
public static void main(String[] args) {
double vals[][]={{3,1,2},{2,-1,1},{1,3,-1}};
Matrix A =new Matrix(vals);
System.out.println("Elementary Row Operation");
System.out.println("Matrix A");
System.out.println(A.toString());
System.out.println("Swapping two rows ");
System.out.println("R0 <-> R1 ");
RowOperation.swap(A, 0, 1);
System.out.println(A.toString());
System.out.println("Multiply a value with a rows ");
System.out.println("R2 <- 2*R2 ");
RowOperation.multiply(A, 2, 2.0);
System.out.println(A.toString());
System.out.println("Adding two rows ");
System.out.println("R1 <- R0 + R1 ");
RowOperation.add(A, 0, 1);
System.out.println(A.toString());
System.out.println("add rows R1 and 2 times R0 ");
System.out.println("R1 <- R1 + 2R0 ");
RowOperation.add(A, 0,1, 2.0);
System.out.println(A.toString());
}
}
Java program Elementary Row Operation Output
Elementary Row Operation Matrix A 3.0 1.0 2.0 2.0 -1.0 1.0 1.0 3.0 -1.0 Swapping two rows R0,R1 R0 <-> R1 2.0 -1.0 1.0 3.0 1.0 2.0 1.0 3.0 -1.0 Multiply a value with a row R2 R2 <- 2*R2 2.0 -1.0 1.0 3.0 1.0 2.0 2.0 6.0 -2.0 Adding two rows R0,R1 R1 <- R0 + R1 2.0 -1.0 1.0 5.0 0.0 3.0 2.0 6.0 -2.0 add row R1 and 2 times R0 R1 <- R1 + 2R0 2.0 -1.0 1.0 9.0 -2.0 5.0 2.0 6.0 -2.0
Comments
Post a Comment