Java 数组常用函数(2)

package Mat;

public class Matrix {
	//打印boolean数组,空格表示false,*表示true
	//前置条件:有一个二维boolean数组
	//后置条件:以矩阵形式进行图形显示
	public static void printBoolMatrix(boolean array[][]) {
		int rowLength = array[0].length;
		int couLength = array.length;
		for(int i=0;i != couLength;i++) {
			for(int j=0;j != rowLength;j++) {
				if(array[i][j] == true) 
					System.out.print("*"+" ");
				else
					System.out.print(" "+" ");
			}
			System.out.print("\n");
		}
	}
	//编写一个matrix库
	//向量点承
	//前置条件:有两个一维数组
	//后置条件:返回一个点承值
	public static double dotVector(double[] a,double[] b) {
		double Dot = 0.0;
		for(int i=0;i != a.length;i++) {
			Dot += a[i]*b[i];
		}
		return Dot;
	}
	//编写一个matrix库
	//矩阵转置
	public static double[][] transMatrix(double[][] a){
		int couLength = a.length;
		int rowLength = a[0].length;
		double[][] c = new double[rowLength][couLength];
		for(int i=0;i != couLength;i++) {
			for(int j=0;j != rowLength;j++) {
				c[j][i] = a[i][j];
			}
		}
		return c;
	}
	//编写一个matrix库
	//矩阵和矩阵相乘
	//前置条件:有两个数组
	//后置条件:返回一个数组为两个矩阵之积
	public static double[][] multMatrix(double[][] a,double[][] b){
		int couLength_a = a.length;
		int couLength_b = b.length;
		int rowLength_a = a[0].length;
		int rowLength_b = b[0].length;
		
		if(rowLength_a != couLength_b)
			return null;
		
		double[][] b_trans = transMatrix(b);
		double[][] result = new double[couLength_a][rowLength_b];
		
		for(int i=0;i != couLength_a;i++) {
			for(int j=0;j != rowLength_b;j++) {
				result[i][j] = dotVector(a[i], b_trans[j]);
			}
		}
		return result;
	}
	
	//编写一个matrix库
	//矩阵和向量的积
	//前置条件:有一个二维数组,有一个一维数组
	//后置条件:返回一个一维数组
	public static double[] multMatVec(double[][] a,double[] b) {
		int couLength = a.length;
		if(a[0].length != b.length)
			return null;
		double[] c = new double[b.length];
		for(int i=0;i != couLength; i++) {
			c[i] = dotVector(a[i], b);
		}
		return c;
	}
	//编写一个matrix库
	//向量和矩阵的积
	//前置条件:有一个二维数组,有一个一维数组
	//后置条件:返回一个一维数组
	public static double[] multVecMat(double[][] a,double[] b) {
		int couLength = a.length;
		int rowLength = a[0].length;
		
		double[] c = new double[b.length];
		double[][] aTrans = transMatrix(a);
		
		if(couLength != b.length)
			return null;
		
		for(int i=0;i != rowLength; i++) {
			c[i] = dotVector(aTrans[i], b);
		}
		
		return c;
		
	}
	
	public static void main(String[] args) {
		boolean boolTest[][] ={{true,false,true},
				{false,true,true}};
		printBoolMatrix(boolTest);

	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值