矩阵:指纵横排列的二维数据表格
创新互联成立十余年来,这条路我们正越走越好,积累了技术与客户资源,形成了良好的口碑。为客户提供成都网站建设、成都网站设计、网站策划、网页设计、国际域名空间、网络营销、VI设计、网站改版、漏洞修补等服务。网站是否美观、功能强大、用户体验好、性价比高、打开快等等,这些对于网站建设都非常重要,创新互联通过对建站技术性的掌握、对创意设计的研究为客户提供一站式互联网解决方案,携手广大客户,共同发展进步。
下三角矩阵:主对角线上方均为常数0的矩阵
矩阵的加减乘运算:
这些都是线性代数的概念 和java没啥关系 你是要在java中实现矩阵的加减乘吧?
import java.util.Scanner;
/**
* 输出公式
* 1 2 3 .....
* (3*n - i - 2 + j) ....n+i
* (3*n - i - 2 + j) ....n+i
*
*/
public class test {
static int N=30;
static int a[][] = new int[N][N];
/**
* @param args
*/
public static void main(String[] args) {
int i = 0 ; // 行循环
int j = 0; // 列循环
int n = 5; // 接收参数
int s = 0; // 临时变量
Scanner sin = new Scanner(System.in);
n = sin.nextInt();
while(n = 30) {
for (i = 0; i n; i++) {
// 输出第一行
if (i == 0) {
for (j = 0; j n; j++){
s = j + 1;
prin(s + "");
System.out.print(" ");
}
System.out.println();
continue;
}
// 第二行开始的输出
for ( j = 0; j n - i - 1 ; j++) {
s = 3 * n - i - 2 + j;
prin(s + "");
System.out.print(" ");
}
s = n + i;
prin(s + "");
System.out.print("\n");
}
n = sin.nextInt();
}
}
// 控制输出格式,补空格
static void prin(String s) {
for (int i = s.length(); i 6; i++){
s += " ";
}
System.out.print(s);
}
}
class Matrix
{
private int value[][]; //存储矩阵元素的二维数组
public Matrix(int m, int n) //构造m行n列的空矩阵
{
this.value=new int[m][n];
}
public Matrix(int n) //构造n行n列的空矩阵
{
this(n,n);
}
public Matrix()
{
this(10,10);
}
public Matrix(int mat[][]) //构造矩阵,由数组mat提供矩阵元素
{
this(mat.length,mat[0].length);
for (int i=0; imat.length; i++)
for (int j=0; jmat[i].length; j++)
this.value[i][j] = mat[i][j];
}
public int get(int i, int j) //获得矩阵第i行第j列的元素,O(1)
{
return value[i][j];
}
public void set(int i, int j, int k) //设置矩阵第i行第j列的元素,O(1)
{
value[i][j]=k;
}
public void add(Matrix b) //this和b两个矩阵相加,改变当前矩阵
{
for (int i=0; ithis.value.length; i++)
for (int j=0; jthis.value[i].length; j++)
this.value[i][j] += b.value[i][j];
}
public String toString() //行主序遍历,访问矩阵全部元素
{
String str="";
for (int i=0; ivalue.length; i++)
{
for (int j=0; jvalue[i].length; j++)
str += " "+value[i][j];
str += "\n";
}
return str;
}
public Matrix transpose() //矩阵的转置
{
Matrix trans = new Matrix(value[0].length, value.length);
for (int i=0; ithis.value.length; i++)
for (int j=0; jthis.value[i].length; j++)
trans.value[j][i]=this.value[i][j];
return trans;
}
//判断一个矩阵是否为上三角矩阵
public boolean isUpperTriangularMatrix() {
int i, j = 0;
int c = this.value[1][0];
for(i=1; ithis.value.length; i++)
for(j=0; ji; j++)
if(this.value[i][j] != c)
break;
if(i=this.value.length)
return true;
return false;
}
//判断一个矩阵是否为下三角矩阵
public boolean isLowerTriangularMatrix() {
int i, j = 0;
int c = this.value[0][1];
for(i=0; ithis.value.length-1; i++)
for(j=i+1; jthis.value[0].length; j++)
if(this.value[i][j] != c)
break;
if(i=this.value.length-1)
return true;
return false;
}
//判断一个矩阵是否为对称矩阵
public boolean isSymmetricMatrix () {
int i, j = 0;
for(i=1; ithis.value.length; i++)
for(j=0; ji; j++)
if(this.value[i][j] != this.value[j][i])
break;
if(i=this.value.length)
return true;
return false;
}
//比较两个矩阵是否相等
public boolean equals(Matrix b) {
int i, j = 0;
if(this.value.length != b.value.length || this.value[0].length != b.value[0].length)
return false;
for(i=0; ithis.value.length; i++)
for(j=0; jthis.value[0].length; j++)
if(this.value[i][j] != b.value[j][i])
break;
if(i=this.value.length)
return true;
return false;
}
//计算两个矩阵的乘积
public Matrix multiply(Matrix b){
int i, j, k;
int sum;
Matrix mtr;
if(this.value[0].length != b.value.length) {
return null;
}
mtr = new Matrix(this.value.length, b.value[0].length);
for(i=0; ithis.value.length; i++)
{
for(k=0; kb.value[0].length; k++){
for(sum=0,j=0; jthis.value[0].length; j++){
sum += this.value[i][j] * b.value[j][k];
mtr.value[i][k] = sum;
}
}
}
return mtr;
}
}
public class Test
{
public static void main(String args[])
{
int m1[][]={{1,2,3},{4,5,6}};
Matrix a=new Matrix(m1);
int m2[][]={{1,0,0},{0,1,0}};
Matrix b=new Matrix(m2);
System.out.print("Matrix a:\n"+a.toString());
System.out.print("Matrix b:\n"+b.toString());
a.add(b);
System.out.print("Matrix a:\n"+a.toString());
System.out.println("a的转置矩阵:\n"+a.transpose().toString());
int m3[][] = {{1,2,1},{0,3,1},{0,0,2}};
int m4[][] = {{1,0,0},{2,1,0},{3,2,1}};
int m5[][] = {{1,0,2},{0,1,0},{2,0,2}};
Matrix mtr1 = new Matrix(m3);
Matrix mtr2 = new Matrix(m4);
Matrix mtr3 = new Matrix(m5);
if(mtr1.isUpperTriangularMatrix())
System.out.println("上三角矩阵:\n" + mtr1.toString());
if(mtr2.isLowerTriangularMatrix())
System.out.println("下三角矩阵:\n" + mtr2.toString());
if(mtr3.isSymmetricMatrix())
System.out.println("对称矩阵:\n" + mtr3.toString());
System.out.println(mtr1.toString() + "\n乘以\n" + mtr2.toString() + "\n=\n");
Matrix tempM = mtr1.multiply(mtr2);
System.out.println(tempM.toString());
}
}