1、余子式
public double[][] confactor(double[][] matrix,int h,int v){
double[][] result = new double[matrix.length - 1][matrix[0].length - 1];
for (int i = 0;i < result.length;i++){
if(i < h - 1){
for (int j = 0;j < result[i].length;j++){
if(j < v - 1){
result[i][j] = matrix[i][j];
}
else {
result[i][j] = matrix[i][j + 1];
}
}
}
else {
for (int j = 0;j < result[i].length;j++){
if(j < v - 1){
result[i][j] = matrix[i + 1][j];
}
else {
result[i][j] = matrix[i + 1][j + 1];
}
}
}
}
return result;
}
2、行列式
public double det(double[][] matrix){
if(matrix.length == 2) {
return matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
}
double result = 0;
int num = matrix.length;
double[] nums = new double[num];
for(int i = 0; i < matrix.length; i++) {
if(i % 2 == 0) {
nums[i] = matrix[0][i] * det(confactor(matrix, 1, i+1));
}else {
nums[i] = -matrix[0][i] * det(confactor(matrix, 1, i+1));
}
}
for(int i = 0; i < matrix.length; i++) {
result += nums[i];
}
return result;
}
3、转置
public double[][] transpose(double[][] matrix){
double[][] result = new double[matrix[0].length][matrix.length];
for (int i = 0;i < matrix.length;i++){
for (int j = 0;j < matrix[i].length;j++){
result[j][i] = matrix[i][j];
}
}
return result;
}
4、逆
public double[][] inverse(double[][] matrix){
double[][] result = new double[matrix.length][matrix[0].length];
double detA = det(matrix);
for(int i = 0; i < matrix.length; i++) {
for(int j = 0; j < matrix[0].length; j++) {
if((i + j) % 2 == 0) {
result[i][j] = det(confactor(matrix, i+1, j+1)) / detA;
}else {
result[i][j] = -det(confactor(matrix, i+1, j+1)) / detA;
}
}
}
result = transpose(result);
return result;
}
5、减法
public double[][] subtract(double[][] matrix1,double[][] matrix2){
int m1 = matrix1.length;
int n1 = matrix1[0].length;
int m2 = matrix2.length;
int n2 = matrix2[0].length;
if(m1 != m2 || n1 != n2){
return null;
}
else {
double[][] result = new double[m1][n1];
for (int i = 0;i < m1;i++){
for(int j = 0; j < n1;j++){
result[i][j] = matrix1[i][j] - matrix2[i][j];
}
}
return result;
}
}
6、数乘
public double[][] scalarMult(double[][] matrix,double a){
double[][] result = new double[matrix.length][matrix[0].length];
for (int i = 0;i < matrix.length;i++){
for (int j = 0;j < matrix[i].length;j++){
result[i][j] = a * matrix[i][j];
}
}
return result;
}
7、乘法
public double[][] vectorMult(double[][] matrix1,double[][] matrix2){
int m1 = matrix1.length;
int n1 = matrix1[0].length;
int m2 = matrix2.length;
int n2 = matrix2[0].length;
if(n1 != m2){
return null;
}
else {
double[][] result = new double[m1][n2];
for (int i = 0;i < m1;i++){
for (int j = 0;j < n2;j++){
for (int k = 0;k < n1;k++){
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
return result;
}
}
8、邻接矩阵的入度
public double[] inDegree(double[][] matrix){
int len = matrix.length;
double[] result = new double[len];
for (int i = 0;i < len;i++){
double degree = 0.0;
for (int j = 0;j<matrix[i].length;j++){
if(matrix[j][i] != 0.0){
degree++;
}
}
result[i] = degree;
}
return result;
}
9、邻接矩阵的出度
public double[] outDegree(double[][] matrix){
int len = matrix.length;
double[] result = new double[len];
for (int i = 0;i < len;i++){
double degree = 0.0;
for (int j = 0;j < matrix[i].length;j++){
if(matrix[i][j] != 0.0){
degree++;
}
}
result[i] = degree;
}
return result;
}
10、求矩阵的最大特征值和其对应的特征向量
import java.math.BigDecimal;
public class EigenValue {
private double[] RI = {
0.00, 0.00, 0.52, 0.89, 1.12,
1.26, 1.36, 1.41, 1.46, 1.49,
1.52, 1.24, 1.56, 1.58, 1.59,
1.5943,1.6064,1.6133,1.6207,1.6292,
1.6385,1.6403,1.6462,1.6497,1.6556,
1.6587,1.6631,1.6670,1.6693,1.6724
};
private double CR = 0.0;
private double lamta = 0.0;
public double[] weight(double[][] matrix) {
int len = matrix.length;
double[] w0 = new double[len];
for (int i = 0; i < len; i++) {
w0[i] = 1.0 / len;
}
double[] w1 = new double[len];
double[] w2 = new double[len];
double sum = 1.0;
double d = 1.0;
double delt = 0.00001;
while (d > delt) {
d = 0.0;
sum = 0;
for (int i = 0; i < len; i++) {
double temp = 0.0;
for (int j = 0; j < len; j++) {
temp += matrix[i][j] * w0[j];
}
w1[i] = temp;
sum += w1[i];
}
for (int i = 0; i < len; i++) {
w2[i] = w1[i] / sum;
d = Math.max(Math.abs(w2[i] - w0[i]), d);
w0[i] = w2[i];
}
}
lamta = 0.0;
for (int i = 0; i < len; i++) {
if(w1[i] != 0 && w0[i] != 0) {
lamta += w1[i] / (len * w0[i]);
}
}
double CI = (lamta - len) / (len - 1);
if (RI[len - 1] != 0) {
CR = CI / RI[len - 1];
}
lamta = round(lamta, 3);
CI = Math.abs(round(CI, 3));
CR = Math.abs(round(CR, 3));
for (int i = 0; i < len; i++) {
w0[i] = round(w0[i], 18);
w1[i] = round(w1[i], 18);
w2[i] = round(w2[i], 18);
}
return w2;
}
public double getLamta(){
return lamta;
}
private double round(double value, int scale) {
if (scale < 0) {
throw new IllegalArgumentException(
"The scale must be a positive integer or zero");
}
BigDecimal b = new BigDecimal(Double.toString(value));
BigDecimal one = new BigDecimal("1");
return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
}
}