- /**
- * java的矩阵类以及相关性质
- * */
- import java.util.Arrays;
- public final class Matrix {
- private int rownums;
- private int colnums;
- private Vector[] vectorArrays=null;
- // private double MatrixElements;
-
- /***
- * basic initialization...
- * */
- public Matrix(){}
- /**
- * initialization....
- * */
- public Matrix(int rownums,int colnums){
- if(isInit(rownums,colnums)){
- this.rownums=rownums;
- this.colnums=colnums;
- vectorArrays=new Vector[rownums];
- init();
- System.out.println(" Matrix initilized succeed!");
- }else{
- try{
- throw new Exception("Error,please check this matrix");
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- }
-
- /**
- * initializing the value of specified position in this matrix...
- * */
- public double setMatrixValue(int rows,int cols,double MatrixElements){
- if(isOutofBounds(rows,cols)){
- try{
- throw new Exception("Error,please check the Matrix");
- }catch(Exception e){
- e.printStackTrace();
- }
- }
-
- return setValue(rows,cols,MatrixElements);
-
-
- }
- /**
- * get the specified position of the matrix
- * */
- public double getMatrixValue(int rows,int cols){
- if(isOutofBounds(rows,cols)){
- try{
- throw new Exception("Error,please check the Matrix");
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- return vectorArrays[rows-1].peek(cols-1);
-
- }
- /**
- * construct the Matrix contains the value
- * */
- private double setValue(int rows,int cols,double MatrixElements){
- return vectorArrays[rows-1].add(cols-1,MatrixElements);
- }
- /**
- * the default value in each cell of this Matrix is 0;
- * */
- public Vector[] init(){
- for(int i=1;i<=this.rownums;i++){
- vectorArrays[i-1]=new Vector(colnums);
- for(int j=1;j<=this.colnums;j++){
- vectorArrays[i-1].add(j-1, 0);
- }
- }
- return vectorArrays ;
- }
- /**
- * toString
- * */
- public String toString(){
- return Arrays.toString(vectorArrays);
- }
- public boolean isInit(int rows,int cols){
- if(rows*cols<=0){
- return false;
- }else{
- return true;
- }
- }
- public boolean isOutofBounds(int rows,int cols){
- if(rows<=0||rows>this.rownums&&cols<=0||cols>this.colnums){
- return true;
- }else{
- return false;
- }
-
- }
-
- /**
- * transpose the matrix
- * */
- public Matrix tranposeMatrix(){
- int rowsT=colnums;
- int colsT=rownums;
- Matrix matrixTranspose=new Matrix(rowsT,colsT);
- for(int i=1;i<=rowsT;i++){
- for(int j=1;j<=colsT;j++){
- matrixTranspose.setMatrixValue(i, j, getMatrixValue(j,i));
- }
- }
- return matrixTranspose;
- }
- /**
- * the multiple of the matrix
- * */
- public Matrix mutiple(Matrix matrix){
- int rows=matrix.getRownums();
- int cols=matrix.getColnums();
- Matrix matrixA=null;
- if(this.colnums==rows){
- // matrixA.setRownums(rownums);
- // matrixA.setColnums(cols);
- matrixA=new Matrix(rownums,cols);
- for(int i=1;i<=rownums;i++){
- double d=0;
- for(int j=1;j<=colnums;j++){
- d+=this.getMatrixValue(i, j)*matrix.getMatrixValue(j, i);
- }
- for(int j=1;j<=cols;j++){
- matrixA.setMatrixValue(i, j, d);
- }
- }
- }else{
- try{
- throw new Exception("计算失败,请确认该矩阵是否正确");
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- return matrixA;
- }
- // /**
- // * two matrix multiply
- // * */
- // public Matrix mutiple(Matrix matrixA,Matrix matrixB){
- // Matrix matrix=null;
- // if(isMultiple(matrixA,matrixB)){
- // matrix=new Matrix(matrixA.getRownums(),matrixB.getColnums());
- // for(int i=1;i<=matrixA.getRownums();i++){
- // double d=0;
- // for(int j=1;j<=matrixA.getColnums();j++){
- // d+=matrixA.getMatrixValue(i, j)*matrixB.getMatrixValue(j, i);
- // }
- // for(int j=1;j<=matrixB.getColnums();j++){
- // matrix.setMatrixValue(i, j, d);
- // }
- // }
- // }else{
- // try{
- // throw new Exception("计算失败,请确认矩阵是否正确");
- // }catch(Exception e){
- // e.printStackTrace();
- // }
- // }
- // return matrix;
- // }
- /**
- * the matrix equals
- * @param precisions
- * */
- public boolean isEqual(Matrix matrix,double eps){
- if(this.rownums==matrix.getRownums()&&this.colnums==matrix.getColnums()){
- for(int i=1;i<=rownums;i++){
- for(int j=1;j<=colnums;j++){
- if((this.getMatrixValue(i, j)-matrix.getMatrixValue(i, j))<=eps){
- return true;
- }else{
- return false;
- }
- }
- }
- }
- return false;
- }
- /**
- * the plus of matrix
- *
- * */
- public Matrix plusMatrix(Matrix matrix){
- Matrix matrixSum=new Matrix(this.rownums,this.colnums);
- if(this.rownums==matrix.rownums&&this.colnums==matrix.colnums){
- for(int i=1;i<=this.rownums;i++){
- for(int j=1;j<this.colnums;j++){
- matrixSum.setMatrixValue(i, j, this.getMatrixValue(i, j)+matrix.getMatrixValue(i, j));
- }
- }
- }else{
- try{
- throw new Exception("请注意,该矩阵无法相加");
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- return matrixSum;
- }
- public Matrix minusMatrix(Matrix matrix){
- Matrix matrixminus=new Matrix(this.rownums,this.colnums);
- if(this.rownums==matrix.rownums&&this.colnums==matrix.colnums){
- for(int i=1;i<=this.rownums;i++){
- for(int j=1;j<this.colnums;j++){
- matrixminus.setMatrixValue(i, j, this.getMatrixValue(i, j)-matrix.getMatrixValue(i, j));
- }
- }
- }else{
- try{
- throw new Exception("请注意,该矩阵无法相加");
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- return matrixminus;
- }
- // /***
- // * @param eps the precisions
- // * */
- // public boolean isEqual(Matrix matrixA,Matrix matrixB,double eps){
- // if(matrixA.isEqual(matrixB, eps)){
- // return true;
- // }else{
- // return false;
- // }
- //
- // }
- /**
- * return the boolean of two matrix,multiply by each matrix
- * */
- private boolean isMultiple(Matrix matrix){
- int rows=matrix.getRownums();
- int cols=matrix.getColnums();
- if(this.colnums==rows){
- return true;
- }else{
- return false;
- }
- }
- // private boolean isMultiple(Matrix matrixA,Matrix matrixB){
- // if(matrixA.isMultiple(matrixB)){
- // return true;
- // }else{
- // return false;
- // }
- // }
- /**
- * multiple the constant
- * */
- public Matrix multipleConstant(double K){
- for(int i=1;i<=this.rownums;i++){
- for(int j=1;j<=this.colnums;j++){
- double d=this.getMatrixValue(i, j)*K;
- this.setMatrixValue(i, j, d);
- }
- }
- return this;
- }
- /**
- * vector mutiple by the constant
- * */
- public Vector mutipleConstantV(double K,int cols){
- if(cols>colnums||cols<=0){
- try{
- throw new Exception("请确认是否有误");
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- for(int i=1;i<=rownums;i++){
- this.setMatrixValue(i, cols, K*this.getMatrixValue(i, cols));
- }
- return this.getVerticalVector(cols);
- }
- /**
- * vector mutiple by the constant
- * */
- public Vector mutipleConstantH(double K,int rows){
- if(rows>rownums||rows<=0){
- try{
- throw new Exception("请确认是否有误");
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- for(int i=1;i<=colnums;i++){
- this.setMatrixValue(rows, i, K*this.getMatrixValue(rows, i));
- }
- return this.getHorizontalVector(rows);
- }
- /**
- * get the egenValue of this Matrix
- * how? Gaussian 消元法
- * */
- public double egenValue(){
- double value=1;
-
- if(this.rownums==this.colnums){
- for(int j=1;j<=colnums;j++){
- double max=this.getMaxAllRowValue(j);
- if(Math.abs(max)!=0){
- int rows=this.getMaxValueRow(max, j);
- for(int i=1;i<this.getRownums();i++){
- this.SwapMatrixValue(i, j, rows, j);
- Vector vector=this.getHorizontalVector(i+1);
- Vector vectorH=mutipleConstantH((1/max),i); //归一化
- Vector vectorU=vector.vecMinus(vectorH.vecMutipleConstant(vectorH.peek(j-1)));
- this.setMatrixHVector(i, vectorU);
- }
- }
- }
- for(int i=1;i<=rownums;i++){
- value*=this.getMatrixValue(i, i);
- }
- }else{
- try{
- throw new Exception("不是行列式,请确认");
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- return value;
- }
- /**
- * return the max of the specified cols given the rows
- *
- * @param cols指定列的所有行的最大值
- * */
-
- public double getMaxAllRowValue(int cols){
- double max=this.getMatrixValue(1, cols);
- for(int i=1;i<=this.rownums;i++){
- if(this.getMatrixValue(i, cols)>max){
- max=this.getMatrixValue(i, cols);
- }
- }
-
- return max;
- }
- /**
- * 获取指定列最大值的行值
- * */
- public int getMaxValueRow(double max,int cols){
- int positions=1;
- for(int i=1;i<=this.rownums;i++){
- if(this.getMatrixValue(i, cols)==max){
- positions=i;
- break;
- }
- }
- return positions;
- }
- /**
- *the following method is to Swap the values of the specified positions
- * */
- public Matrix SwapMatrixValue(int rows1,int cols1,int row2,int cols2){
- double a=this.getMatrixValue(rows1, cols1);
- double b=this.getMatrixValue(row2, cols2);
- this.setMatrixValue(rows1, cols1, b);
- this.setMatrixValue(row2, cols2, a);
- return this;
- }
- /**
- * the following method is to get the horizontal vectors;
- * */
- public Vector getHorizontalVector(int rows){
- Vector vector=new Vector(this.colnums);
- for(int i=1;i<=vector.length();i++){
- vector.add(i-1, this.getMatrixValue(rows, i));
- }
- return vector;
- }
- public Vector getVerticalVector(int cols){
- Vector vector=new Vector(this.rownums);
- for(int i=1;i<=vector.length();i++){
- vector.add(i-1, this.getMatrixValue(i, cols));
- }
- return vector;
- }
- /**
- * 以下设置矩阵列向量
- * */
- public Matrix setMatrixVVector(int cols,Vector vector){
- if(cols>colnums||vector.length()>rownums){
- try{
- throw new Exception("请确认该向量");
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- for(int i=0;i<vector.length();i++){
- this.setMatrixValue(i+1, cols, vector.peek(i));
- }
- return this;
- }
- /**
- * 以下设置矩阵行向量
- * @param rows 指定行
- * */
- public Matrix setMatrixHVector(int rows,Vector vector){
- if(rows>rownums||vector.length()>colnums){
- try{
- throw new Exception("请确认该向量");
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- for(int i=0;i<vector.length();i++){
- this.setMatrixValue(rows, i+1, vector.peek(i));
- }
- return this;
- }
- public int getRownums() {
- return rownums;
- }
- public void setRownums(int rownums) {
- this.rownums = rownums;
- }
- public int getColnums() {
- return colnums;
- }
- public void setColnums(int colnums) {
- this.colnums = colnums;
- }
- }
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.Map;
- public class Vector {
- private int MaxSize;
- private double[] vectorArray;
- private int cursor;
- public Vector(int MaxSize){
- this.MaxSize=MaxSize;
- vectorArray=new double[this.MaxSize];
- this.cursor=-1;
- }
- public double[] add(double s){
- vectorArray[++cursor]=s;
- return vectorArray;
- }
- public double add(int cursors,double s){
- return vectorArray[cursors]=s;
- }
- public double peek(int cursor){
- return vectorArray[cursor];
- }
- public double[] remove(double s){
- Map<Double,Integer> map=new HashMap<Double,Integer>();
- for(int i=0;i<vectorArray.length;i++){
- int count=0;
- for(int j=0;j<vectorArray.length;j++){
- if(vectorArray[j]==s){
- map.put((double)i, count++);
- }
- }
- }
- for(int i=0;i<vectorArray.length;i++){
- for(int j=0;j<i;j++){
- if(vectorArray[j]==s){
- double temp=vectorArray[i];
- vectorArray[i]=vectorArray[j];
- vectorArray[j]=temp;
- }
- }
- }
- double[] newvectorArray=new double[map.size()];
- System.out.println(map.size());
- for(int i=0;i<newvectorArray.length;i++){
- newvectorArray[i]=vectorArray[i];
- }
- System.out.println(Arrays.toString(vectorArray));
- return newvectorArray;
- }
- public Vector vecAdd(Vector vector){
- Vector newVector=new Vector(this.length());
- if(this.length()==vector.length()){
- for(int i=0;i<vector.length();i++){
- newVector.add(i, (this.peek(i)+vector.peek(i)));
- }
- }else{
- try{
- throw new Exception("请确认该向量的长度");
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- return newVector;
- }
- public Vector vecMinus(Vector vector){
- Vector newVector=new Vector(this.length());
- if(this.length()==vector.length()){
- for(int i=0;i<vector.length();i++){
- newVector.add(i, (this.peek(i)-vector.peek(i)));
- }
- }else{
- try{
- throw new Exception("请确认该向量的长度");
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- return newVector;
- }
- /**
- * mutiple
- * */
- public Vector vecMutipleConstant(double K){
- Vector vector=new Vector(this.length());
- for(int i=0;i<this.length();i++){
- vector.add(i, this.peek(i));
- }
- return vector;
- }
- /**
- * mutiple by vector
- * */
- public double vecMutipleVector(Vector vector){
- double d=0;
- if(this.length()==vector.length()){
- for(int i=0;i<this.length();i++){
- d+=this.peek(i)*vector.peek(i);
- }
- }
- return d;
- }
- public boolean isFull(){
- return (MaxSize==cursor);
- }
- public boolean isEmpty(){
- return (cursor==-1);
- }
- public String toString(){
- return Arrays.toString(vectorArray)+"\n";
- }
- public int length(){
- return vectorArray.length;
- }
- }
有人做了向量机。我也来写。后面发。