数据结构-矩阵-对角矩阵(Java语言)

详细的代码可见github:

https://github.com/AbitGo/myClassWork/tree/master/workspace_ds

对角矩阵(diagonal matrix)是一个主对角线之外的元素皆为0的矩阵,常写为diag(a1,a2,...,an) 。对角矩阵可以认为是矩阵中最简单的一种,值得一提的是:对角线上的元素可以为 0 或其他值,对角线上元素相等的对角矩阵称为数量矩阵;对角线上元素全为1的对角矩阵称为单位矩阵。对角矩阵的运算包括和、差运算、数乘运算、同阶对角阵的乘积运算,且结果仍为对角阵。

具体实现类

package com.company.ch4.Matrix;

import com.company.ch4.TripleNode;

//特殊矩阵的压缩存储-对角矩阵的操作
public class DiagonalMatrix {
    /***
     * maxSize最大行数
     * tripleNodes矩阵
     * row行数
     */
    final private int maxSize = 100;
    private TripleNode[] tripleNodes;
    private int row;

    public DiagonalMatrix(int row) {
        //数组的个数为3n-2
        //最后一个存储0
        int num = row * 3 - 2;
        tripleNodes = new TripleNode[num];
        for (int i = 0; i < num; i++)
            tripleNodes[i] = new TripleNode();
        this.row = row;
    }

    //自动填充
    public void autoGenerate() {
        int k = 0;
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < row; j++) {
                if (i == 0) {
                    //第一行两个
                    if (j == 0) {
                        this.tripleNodes[k++].setValue(i);
                    } else if (j == 1) {
                        this.tripleNodes[k++].setValue(i + 1);
                    }
                } else if (i == row - 1) {
                    //最后一行两个
                    if (j == row - 2) {
                        this.tripleNodes[k++].setValue(i);
                    } else if (j == row - 1) {
                        this.tripleNodes[k++].setValue(i + 1);
                    }
                } else {
                    if (i == j - 1 || i == j + 1 || j == i)
                        this.tripleNodes[k++].setValue(j);
                }
            }
        }
//        //debug过程
//        for(int x=0;x<row*3-2;x++){
//            System.out.println("x:"+this.tripleNodes[x].getValue());
//        }
    }

    public void disPlay() {
        int k = 0;
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < row; j++) {
                if (i == 0) {
                    //第一行
                    if (j <=1) {//j==1 || j==0
                        System.out.print(this.tripleNodes[j].getValue() + "\t");
                    } else {
                        System.out.print(0 + "\t");
                    }
                } else if (i == row - 1) {
                    //第二行
                    if (j == row - 2 ) {
                        //倒数第二个
                        System.out.print(this.tripleNodes[3* this.row-4].getValue() + "\t");
                    } else if(j == row - 1){
                        //倒数第一个
                        System.out.print(this.tripleNodes[3*this.row-3].getValue() + "\t");
                    }
                    else {
                        System.out.print(0 + "\t");
                    }
                } else {
                    if (i == j) {
                        k = 3 * i;
                        System.out.print(this.tripleNodes[k].getValue() + "\t");
                    } else if (i == j - 1) {
                        k = 3 * i + 1;
                        System.out.print(this.tripleNodes[k].getValue() + "\t");
                    } else if (i == j + 1) {
                        k = 3 * i - 1;
                        System.out.print(this.tripleNodes[k].getValue() + "\t");
                    }else{
                        System.out.print(0 + "\t");
                    }

                }
            }
            System.out.println();
        }
    }
}

测试类:

package com.company.ch4.Matrix;

public class DiagonalMatrixTest {
    public static void main(String[] args) {
        DiagonalMatrix diagonalMatrix = new DiagonalMatrix(8);
        diagonalMatrix.autoGenerate();
        diagonalMatrix.disPlay();
    }
}

测试结果:

0	1	0	0	0	0	0	0	
0	1	2	0	0	0	0	0	
0	1	2	3	0	0	0	0	
0	0	2	3	4	0	0	0	
0	0	0	3	4	5	0	0	
0	0	0	0	4	5	6	0	
0	0	0	0	0	5	6	7	
0	0	0	0	0	0	7	8
发布了84 篇原创文章 · 获赞 39 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/Abit_Go/article/details/104149426