package org.wlld.MatrixTools;
import java.util.ArrayList;
import java.util.List;
public class MatrixOperation {
private MatrixOperation() {
}
public static Matrix add(Matrix matrix1, Matrix matrix2) throws Exception {//矩阵相加
if (matrix1.getX() == matrix2.getX() && matrix1.getY() == matrix2.getY()) {
Matrix matrix = new Matrix(matrix1.getX(), matrix1.getY());
int x = matrix1.getX();
int y = matrix1.getY();
for (int i = 0; i < x; i++) {//遍历行
for (int j = 0; j < y; j++) {//遍历列
matrix.setNub(i, j, matrix1.getNumber(i, j) + matrix2.getNumber(i, j));
}
}
return matrix;
} else {
throw new Exception("matrix is not equals");
}
}
//矩阵相减
public static Matrix sub(Matrix matrix1, Matrix matrix2) throws Exception {//
if (matrix1.getX() == matrix2.getX() && matrix1.getY() == matrix2.getY()) {
Matrix matrix = new Matrix(matrix1.getX(), matrix1.getY());
int x = matrix1.getX();
int y = matrix1.getY();
for (int i = 0; i < x; i++) {//遍历行
for (int j = 0; j < y; j++) {//遍历列
matrix.setNub(i, j, matrix1.getNumber(i, j) - matrix2.getNumber(i, j));
}
}
return matrix;
} else {
throw new Exception("matrix is not equals");
}
}
//多元线性回归
public static Matrix getLinearRegression(Matrix parameter, Matrix out) throws Exception {
if (parameter.getX() == out.getX() && out.isVector()) {
//将参数矩阵转置
Matrix matrix1 = transPosition(parameter);
//转置的参数矩阵乘以参数矩阵
Matrix matrix2 = mulMatrix(matrix1, parameter);
//求上一步的逆矩阵 这一步需要矩阵非奇异,若出现奇异矩阵,则返回0矩阵,意味失败
Matrix matrix3 = getInverseMatrixs(matrix2);
if (matrix3.getX() == 1 && matrix3.getY() == 1) {
return matrix3;
} else {
//逆矩阵乘以转置矩阵
Matrix matrix4 = mulMatrix(matrix3, matrix1);
//最后乘以输出矩阵,生成权重矩阵并返回
return mulMatrix(matrix4, out);
}
} else {
throw new Exception("invalid regression matrix");
}
}
public static double getEDistByMatrix(Matrix matrix1, Matrix matrix2) throws Exception {
if (matrix1.getX() == matrix2.getX() && matrix1.getY() == matrix2.getY()) {
int x = matrix1.getX();
int y = matrix1.getY();
double sigma = 0;
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
double sub = matrix1.getNumber(i, j) - matrix2.getNumber(i, j);
sigma = sigma + Math.pow(sub, 2);
}
}
return sigma / (x * y);
} else {
throw new Exception("two matrixes is not equals");
}
}
//返回两个向量之间的欧氏距离的平方
public static double getEDist(Matrix matrix1, Matrix matrix2) throws Exception {
if (matrix1.isRowVector() && matrix2.isRowVector() && matrix1.getY() == matrix2.getY()) {
Matrix matrix = sub(matrix1, matrix2);
return getNorm(matrix);
} else {
throw new Exception("this matrix is not rowVector or length different");
}
}
public static double errorNub(Matrix matrix, Matrix avgMatrix) throws Exception {//求均方误差
int y = matrix.getY();
if (matrix.isRowVector() && avgMatrix.isRowVector() && y == avgMatrix.getY()) {
double[] subAll = new double[y];
for (int j = 0; j < y; j++) {
double mySelf = matrix.getNumber(0, j);
double avg = avgMatrix.getNumber(0, j);
double sub = Math.pow(avg - mySelf, 2);
subAll[j] = sub;
}
double sigma = 0;
for (int i = 0; i < y; i++) {
sigma = sigma + subAll[i];
}
return sigma / y;
} else {
throw new Exception("this matrix is not rowVector or length different");
}
}
public static Matrix pushVector(Matrix myMatrix, Matrix matrix, boolean addRow) throws Exception {
//向一个矩阵里合并一个行向量或者列向量到矩阵行或者列的末尾
if (matrix.getX() == 1 || matrix.getY() == 1) {
Matrix addMatrix;
if (addRow) {//增加一行
if (matrix.getY() != myMatrix.getY()) {
throw new Exception("this matrix column is not equals");
}
addMatrix = new Matrix(myMatrix.getX() + 1, myMatrix.getY());
} else {//增加一列
if (matrix.getX() != myMatrix.getX()) {
throw new Exception("this matrix row is not equals");
}
addMatrix = new Matrix(myMatrix.getX(), myMatrix.getY() + 1);
}
for (int i = 0; i < addMatrix.getX(); i++) {
for (int j = 0; j < addMatrix.getY(); j++) {
if (addRow) {
if (i == addMatrix.getX() - 1) {//最后一行
addMatrix.setNub(i, j, matrix.getNumber(0, j));
} else {
addMatrix.setNub(i, j, myMatrix.getNumber(i, j));
}
} else {
if (j == addMatrix.getY() - 1) {//最后一列
addMatrix.setNub(i, j, matrix.getNumber(i, 0));
} else {
addMatrix.setNub(i, j, myMatrix.getNumber(i, j));
}
}
}
}
return addMatrix;
} else {
throw new Exception("this matrix is not a vector");
}
}
public static Matrix push(Matrix matrix, double nub, boolean isRow) throws Exception {//向一个向量里PUSH一个值
if (matrix.getX() == 1 || matrix.getY() == 1) {
Matrix myMatrix;
int nubs;
if (isRow) {//行向量
nubs = matrix.getY() + 1;
myMatrix = new Matrix(1, nubs);
} else {//列向量
nubs = matrix.getX() + 1;
myMatrix = new Matrix(nubs, 1);
}
for (int i = 0; i < nubs; i++) {
if (i == nubs - 1) {
if (isRow) {
myMatrix.setNub(0, i, nub);
} else {
myMatrix.setNub(i, 0, nub);
}
} else {
if (isRow) {//行向量
myMatrix.setNub(0, i, matrix.getNumber(0, i));
} else {//列向量
myMatrix.setNub(i, 0, matrix.getNumber(i, 0));
}
}
}
return myMatrix;
} else {
throw new Exception("this matrix is not a vector");
}
}
public static Matrix getPoolVector(Matrix matrix) throws Exception {
if (matrix.getX() == 1 || matrix.getY() == 1) {
Matrix vector;
int nub;
boolean isRow = false;
if (matrix.getX() == 1) {//行向量
isRow = true;
nub = matrix.getY() / 4;
vector = new Matrix(1, nub);
} else {//列向量
nub = matrix.getX() / 4;
vector = new Matrix(nub, 1);
}
int k = 0;
for (int i = 0; i < nub * 4 - 3; i += 4) {