Generic matrix Classes

Source: Internet
Author: User

1, Genericmatrix class

Public abstract class Genericmatrix<e extends Number> {

/**
* Abstract method for adding elements of the matrices
* @param O1
* @param O2
* @return
*/
Protected abstract e Add (e O1, e O2);

/**
* Abstract method for myltiplying elements of the matrices
* @param O1
* @param O2
* @return
*/
Protected abstract e Multiply (e O1, e O2);

/**
* Abstract method for defining zero for the matrix element
* @return
*/
protected abstract E Zero ();

/**
* Add matrices
* @param matrix1
* @param matrix2
* @return
*/
Public e[][] Addmatrix (e[][] matrix1, e[][] matrix2) {

if (matrix1.length! = Matrix2.length
|| Matrix1[0].length! = matrix2[0].length) {
throw new RuntimeException ("The matrices does not have the save size");
}

E[][] result = (e[][]) new number[matrix1.length][matrix1[0].length];
for (int i = 0; i < matrix1.length; i++) {
for (int j = 0; J < Matrix1[i].length; J + +) {
RESULT[I][J] = Add (Matrix1[i][j], matrix2[i][j]);
}
}
return result;
}

/**
* Myltiply-matrices
* @param matrix1
* @param matrix2
* @return
*/
Public e[][] Multiplymatrix (e[][] matrix1, e[][] matrix2) {

if (matrix1[0].length! = matrix2.length) {
throw New RuntimeException (
"The matrices does not have compatible size");
}

E[][] result = (e[][]) new number[matrix1.length][matrix2[0].length];
for (int i = 0; i < result.length; i++) {
for (int j = 0; J < Result[0].length; J + +) {
RESULT[I][J] = zero ();
for (int k = 0; k < matrix1[i].length; k++) {
RESULT[I][J] = Add (Result[i][j],
Multiply (Matrix1[i][k], matrix2[k][j]));
}
}
}
return result;
}

/**
* Print matrices, the operator, and their operation result
* @param M1
* @param m2
* @param m3
* @param op
*/
public static void Printresult (Number[][] M1, number[][] m2, number[][] m3,
Char op) {

for (int i = 0; i < m1.length; i++) {
for (int j = 0; J < M1[0].length; J + +) {
System.out.print ("" + m1[i][j]);
}
if (i = = M1.LENGTH/2) {
System.out.print ("+ op +");
}
else {
System.out.print ("");
}

for (int j = 0; J < M2.length; J + +) {
System.out.print ("" + m2[i][j]);
}
if (i = = M1.LENGTH/2) {
System.out.print ("=");
}
else {
System.out.print ("");
}
for (int j = 0; J < M3.length; J + +) {
System.out.print (M3[i][j] + "");
}
System.out.println ();
}
}
}

2, Integermatrix class

public class Integermatrix extends Genericmatrix<integer> {

@Override
protected integer Add (integer O1, integer o2) {

TODO auto-generated Method Stub
return O1 + O2;
}

@Override
protected integer Multiply (integer o1, integer o2) {

TODO auto-generated Method Stub
return O1 * O2;
}

@Override
Protected Integer Zero () {

TODO auto-generated Method Stub
return 0;
}

}

3, Rationalmatrix class

public class Rationalmatrix extends Genericmatrix<rational> {

@Override
Protected rational Add (rational O1, rational O2) {

TODO auto-generated Method Stub
Return O1.add (O2);
}

@Override
protected rational multiply (rational o1, rational O2) {

TODO auto-generated Method Stub
Return O1.multiple (O2);
}

@Override
Protected Rational Zero () {

TODO auto-generated Method Stub
return new Rational (0, 1);
}

}

4. Rational class

public class Rational extends number implements comparable {

Private Long numerator;//molecule
Private long denominator;//Denominator

/**
* @param args
*/
public static void Main (string[] args) {

TODO auto-generated Method Stub
Rational rational1 = new Rational (1, 10);
Rational rational2 = new Rational (1,-10);
System.out.println (Rational1.add (Rational2));
System.out.println (Rational1.subtract (Rational2));
System.out.println (Rational1.multiple (Rational2));
System.out.println (Rational1.divide (Rational2));
}

Public Rational () {

TODO auto-generated Constructor stub
This (0, 1);

}

Public Rational (long numerator, long denominator) {

Long gcd = gcd (numerator, denominator);
This.numerator = (Denominator > 0? 1:-1) * NUMERATOR/GCD;
This.denominator = Math.Abs (denominator)/gcd;
}

public static long gcd (long A, long B) {

Long N1 = Math.Abs (a);
Long N2 = Math.Abs (b);
Long remainder = n1% N2;
while (Remainder > 0) {
N1 = n2;
N2 = remainder;
remainder = n1% N2;
}
return n2;
}

Public long Getnumerator () {

return numerator;
}

Public long Getdenominator () {

return denominator;
}

Public rational Add (rational secondrational) {

Long n = numerator * Secondrational.getdenominator () + Denominator
* Secondrational.getnumerator ();
Long d = denominator * secondrational.getdenominator ();
return new Rational (n, D);
}

Public rational subtract (rational secondrational) {

Long n = numerator * Secondrational.getdenominator ()-Denominator
* Secondrational.getnumerator ();
Long d = denominator * secondrational.getdenominator ();
return new Rational (n, D);
}

Public rational multiple (rational secondrational) {

Long n = numerator * Secondrational.getnumerator ();
Long d = denominator * secondrational.getdenominator ();
return new Rational (n, D);
}

Public rational divide (rational secondrational) {

Long n = numerator * Secondrational.getdenominator ();
Long d = denominator * secondrational.getnumerator ();
return new Rational (n, D);
}

@Override
public boolean equals (Object obj) {

TODO auto-generated Method Stub
if (this.subtract (Rational) obj). Getnumerator () = = 0) {
return true;
}
else {
return false;
}
}

@Override
Public String toString () {

TODO auto-generated Method Stub
if (denominator = = 1) {
return string.valueof (numerator);
}
else {
return numerator + "/" + denominator;
}
}

@Override
public int intvalue () {

TODO auto-generated Method Stub
return (int) doublevalue ();
}

@Override
Public long Longvalue () {

TODO auto-generated Method Stub
Return (long) doublevalue ();
}

@Override
public float Floatvalue () {

TODO auto-generated Method Stub
return (float) doublevalue ();
}

@Override
Public double Doublevalue () {

TODO auto-generated Method Stub
return numerator * 1.0/denominator;
}

@Override
public int compareTo (Object o) {

TODO auto-generated Method Stub
if (This.subtract (Rational) o). Getnumerator () > 0) {
return 1;
}
else if (this.subtract (Rational) o). Getnumerator () < 0) {
return-1;
}
else {
return 0;
}
}
}

5, Testintegermatrix class

public class Testintegermatrix {

/**
* @param args
*/
public static void Main (string[] args) {

TODO auto-generated Method Stub
Integer[][] M1 = new integer[][] {{1, 2, 3}, {4, 5, 6},
{1, 1, 1}};
Integer[][] m2 = new integer[][] {{1, 1, 1}, {2, 2, 2},
{0, 0, 0}};
Integermatrix Integermatrix = new Integermatrix ();
System.out.println ("\nm1 + m2 is");
Integermatrix.printresult (M1, M2, Integermatrix.addmatrix (M1, M2), ' + ');

SYSTEM.OUT.PRINTLN ("\nm1 * m2 is");
Integermatrix.printresult (M1, M2, Integermatrix.multiplymatrix (M1, M2),
‘*‘);
}

}

6, Testrationalmatrix class

public class Testrationalmatrix {

/**
* @param args
*/
public static void Main (string[] args) {

TODO auto-generated Method Stub
Rational[][] M1 = new RATIONAL[3][3];
Rational[][] m2 = new rational[3][3];
for (int i = 0; i < m1.length; i++) {
for (int j = 0; J < M1[0].length; J + +) {
M1[I][J] = new Rational (i + 1, j + 5);
M2[I][J] = new Rational (i + 1, j + 6);
}
}
Rationalmatrix Rationalmatrix = new Rationalmatrix ();
System.out.println ("\nm1 + m2 is");
Rationalmatrix.printresult (M1, M2, Rationalmatrix.addmatrix (M1, M2),
' + ');

SYSTEM.OUT.PRINTLN ("\nm1 * m2 is");
Rationalmatrix.printresult (M1, M2,
Rationalmatrix.multiplymatrix (M1, m2), ' * ');
}

}

Generic matrix Classes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.