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);
}
}
Java 数组常用函数(2)
最新推荐文章于 2024-07-20 23:59:39 发布