`

矩阵 java实现

阅读更多
package com.shine.matrix;
/*
* 矩阵类
* 直接由2维数组进行构建
*/
public class Matrix {
private int element[][];//存储矩阵元素的二维数组
public Matrix(int m,int n){
this.element = new int[m][n];
}
public Matrix(int n){
this(n,n);
}
public Matrix(int m,int n,int mat[][]){//构造m*n矩阵,由mat提供元素
this(m,n);
for(int i=0;i<mat.length;i++){
for(int j=0;j<mat[i].length;j++)
this.element[i][j] = mat[i][j];
}
}
public int get(int i,int j){
return this.element[i][j];
}
public void set(int i,int j,int value){
this.element[i][j] = value;
}
public String toString() {
String str = "矩阵 Matrix("+this.element.length+"x"+this.element[0].length+"):\n";
for(int i=0;i<this.element.length;i++)
{
for(int j=0;j<this.element[i].length;j++)
str += String.format("%4d", this.element[i][j]);
str += "\n";
}
    return str;
};
public void add(Matrix mat)//矩阵的加法
{
if(mat.element.length!=this.element.length||mat.element[0].length!=this.element[0].length)
throw new IllegalArgumentException("两个矩阵阶数不同,不能进行相加运算!");
for(int i=0;i<this.element.length;i++)
for(int j=0;j<this.element[i].length;j++)
this.element[i][j]+=mat.element[i][j];
}
}



package com.shine.matrix;
/*
* 描述稀疏矩阵非零元素的三元组类
*/
public class Triple implements java.lang.Comparable<Triple>{
int row,column,value;
public Triple(int row,int column,int value)
{
if(row<0||column<0)
throw new IllegalArgumentException("稀疏矩阵元素三元组的行/列序号非正数");
this.row = row;
this.column = column;
this.value = value;
}
//拷贝构造方法,复制一个三元组
public Triple(Triple elem)
{
this(elem.row,elem.column,elem.value);
}
@Override
public String toString() {
return "("+row+","+column+","+value+")";
}
@Override
public boolean equals(Object obj) {
if(!(obj instanceof Triple))
return false;
Triple elem = (Triple)obj;
return this.row==elem.row&&this.column==elem.column&&this.value==elem.value;
}
//根据三元组位置比较两个三元组的大小,与元素值无关,约定三元组排序次序
public int compareTo(Triple elem) {
if(this.row<elem.row||this.row==elem.row&&this.column<elem.column)
return -1;
if(this.row==elem.row&&this.column==elem.column)
return 0;
return 1;
}

}





package com.shine.matrix;
/*
* 下三角矩阵的压缩存储
*/
public class DownTriangleMatrix {
private int rows;
private int element[];
public DownTriangleMatrix(int rows)//构造rows阶下三角矩阵,元素为0
{
if(rows<0)
throw new IllegalArgumentException("矩阵行数非正数:"+rows);
this.rows = rows;
this.element = new int[rows*(rows+1)/2];//rows阶下三角矩阵存储rows*(rows+1)/2个元素
}
//构造rows阶下三角矩阵,初值由mat提供,mat按行主序顺序存储rows阶下三角矩阵元素
public DownTriangleMatrix(int rows,int[] mat)
{
this(rows);
int n = element.length<=mat.length? element.length:mat.length;
for(int i=0;i<n;i++)
this.element[i] = mat[i];
}
public int get(int i,int j)
{
if(i<0||i>=rows||j<0||j>=rows)
throw new IndexOutOfBoundsException("矩阵的行号或列号越界");
return i<j?0:element[i*(i+1)/2+j];//按照线性压缩存储地址寻找矩阵元素
}
public void set(int i,int j,int value)
{
if(i<0||i>=rows||j<0||j>=rows)
throw new IndexOutOfBoundsException("矩阵的行号或列号越界");
this.element[i*(i+1)/2+j]=value;
}
public void add(DownTriangleMatrix mat)//两个下三角矩阵的相加
{
if(this.rows!=mat.rows)
throw new IllegalArgumentException("两个矩阵的阶数不同,不能相加");
for(int i=0;i<this.element.length;i++)
this.element[i] += mat.element[i];
}
public String toString()
{
String str="下三角矩阵"+this.getClass().getName()+"("+this.rows+"阶):\n";
for(int i=0;i<this.rows;i++){
for(int j=0;j<this.rows;j++)
str += String.format("%4d", this.get(i, j));
str +="\n";
}
return str;
}

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics