泛型矩阵类

1、GenericMatrix类

public abstract class GenericMatrix<E extends Number> {

/**
* Abstract method for adding two elements of the matrices
* @param o1
* @param o2
* @return
*/
protected abstract E add(E o1, E o2);

/**
* Abstract method for myltiplying two 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 two 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 do 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 two matrices
* @param matrix1
* @param matrix2
* @return
*/
public E[][] multiplyMatrix(E[][] matrix1, E[][] matrix2) {

if (matrix1[0].length != matrix2.length) {
throw new RuntimeException(
"The matrices do 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类

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类

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类

public class Rational extends Number implements Comparable {

private long numerator;// 分子
private long 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 类

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 类

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), '*');
}

}

转载于:https://www.cnblogs.com/diyishijian/p/5064861.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一、实验目的 熟练掌握Java中一维数组、多维数组的使用方法。 使用Java数组解决一般性的应用问题。 二、实验内容 1、在main方法中创建一个含有10个元素的int型数组,进行以下操作:(1)将数组元素按照从小到大的顺序排列;(2)对排好序的数组使用折半查找(使用递归和非递归两种形式分别实现)查找某一个int元素。 2、使用一维数组编码实现一个栈(Stack),要求提供以下操作:(1)boolean isEmpty():判断栈当前是否为空;(2)入栈操作void push(obj):把数据元素obj插入堆栈;(3)出栈操作Object pop():出栈,并返回删除的数据元素;(4)Object getTop():取堆栈当前栈顶的数据元素并返回。编写代码测试所形成的Stack,然后利用Stack实现以下功能:输入一个正整数,输出该整数所对应的二进制数。 3、按照要求使用Java编码。 以型int[][]声明一个叫matrix的二维数组变量,将矩阵初始化为一个5个元素的数组。 以下列方式为matrix的内部元素赋值:matrix从零开始循环到其长度值;例如索引为i,在每次迭代中,将matrix[i]指向一个新的整数数组,其长度为i。然后用索引变量j,对数组中的每一个元素进行循环。在每次内部循环中,将matrix[i][j]赋值为(i*j)。 通过循环打印matrix中的所有元素,结果为:   <>   <0>   <0 2>   <0 3 6>   <0 4 8 12> 4、利用二维数组实现一个矩阵:Matrix。要求提供以下操作:(1)set(int row, int col, double value):将第row行第col列的元素赋值为value;(2)get(int row,int col):取第row行第col列的元素;(3)width():返回矩阵的列数;(4)height():返回矩阵的行数;(5)Matrix add(Matrix b):返回当前矩阵矩阵b相加后的结果矩阵;(6)Matrix multiply(Matrix b):返回当前矩阵矩阵b相乘后的结果矩阵。(7)print():打印出当前矩阵的值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值