美文网首页
稀疏矩阵的压缩和转置(Java实现)

稀疏矩阵的压缩和转置(Java实现)

作者: thebigsilly | 来源:发表于2018-04-21 12:40 被阅读0次
  1. 简介

稀疏矩阵是矩阵中的非零元素个数远远少于零元素个数。
一般会通过稀疏因子来进行确定。假设在m×n的矩阵中,有t个元素不为0.令∂=t÷(m×n).若∂<=0.05时称为稀疏矩阵。
稀疏矩阵通过三元组进行存储
三元组结构

  private class Node {
        //列数
        int x;
        //行数
        int y;
        //元素
        int elem;

        public Node(int x, int y, int elem) {
            this.x = x;
            this.y = y;
            this.elem = elem;
        }
    }
  1. Java代码实现

/**
 * 稀疏矩阵
 */
public class SparseMatrix {
    //记录列数
    private int col;
    //记录行数
    private int row;
    //三元组存储矩阵
    private List<Node> compression;
    //三元组
    private class Node {
        //列数
        int x;
        //行数
        int y;
        //元素
        int elem;

        public Node(int x, int y, int elem) {
            this.x = x;
            this.y = y;
            this.elem = elem;
        }
    }

    /**
     * 三元组压缩稀疏矩阵。
     * 从左往右,从下往上的顺序压缩。
     * @param target 待压缩的稀疏矩阵
     * @return 当前对象
     */
    SparseMatrix compression(int[][] target) {
        if (target == null) {
            throw new UnsupportedOperationException("不支持Null");
        }
        col = target[0].length;
        row = target.length;
        compression = new ArrayList<>();
        for (int y = 0; y < row; y++) {
            for (int x = 0; x < col; x++) {
                if (target[y][x] != 0) {
                    compression.add(new Node(x, y, target[y][x]));
                }
            }
        }
        return this;
    }

    /**
     * 三元组打印矩阵
     * @return 当前对象
     */
    SparseMatrix printMatrix() {
        if (compression == null) {
            throw new UnsupportedOperationException("未初始化");
        }
        Node node = null;
        int index = 0;
        if (index < compression.size()) {
            node = compression.get(index++);
        }
        for (int y = 0; y < row; y++) {
            for (int x = 0; x < col; x++) {
                if (node != null && node.x == x && node.y == y) {
                    System.out.print(node.elem + "\t");
                    node = index < compression.size() ? compression.get(index++) : null;
                } else {
                    System.out.print("0\t");
                }
            }
            System.out.println();
        }
        System.out.println();
        return this;
    }

    /**
     * 线性打印三元组
     * @return 当前对象
     */
    SparseMatrix printLinear() {
        if (compression == null) {
            throw new UnsupportedOperationException("未初始化");
        }
        for (Node node : compression) {
            System.out.print(node.elem + "\t");
        }
        System.out.println();
        return this;
    }

    /**
     * 暴力转置矩阵
     * @return
     */
    SparseMatrix forceRotateMatrix() {
        if (compression == null) {
            throw new UnsupportedOperationException("未初始化");
        }
        List<Node> rotate = new ArrayList<>(compression.size());
        int temp = col;
        col = row;
        row = temp;
        int y = 0;
        //搜索列
        while (y < row) {
            for (Node node : compression) {
                //X和Y交换
                if (node.x == y) {
                    rotate.add(new Node(node.y, node.x, node.elem));
                }
            }
            y++;
        }
        compression = rotate;
        return this;
    }

    /**
     * 快速转置三元组
     * @return
     */
    SparseMatrix quickRotateMatrix() {
        if (compression == null) {
            throw new UnsupportedOperationException("未初始化");
        }
        //记录旋转后每行的个数,也就是当前每列的个数
        int[] location = new int[col];
        for (Node node : compression) {
            location[node.x]++;
        }
        //计算列第一个非0元素的开始位置
        int[] begin = new int[col];
        for (int i = 1; i < location.length; i++) {
            begin[i] = begin[i - 1] + location[i];
        }
        //推算索引位置
        int[] back = location.clone();
        //ArrayList不支持插入任意位置
        Node[] rotate = new Node[compression.size()];
        int offset;
        for (Node node : compression) {
            offset = location[node.x] - back[node.x]--;
//            System.out.println("偏移量:" + offset);
//            System.out.println("位置:" + (begin[node.x] + offset));
            rotate[begin[node.x] + offset] = new Node(node.y, node.x, node.elem);
        }
        int temp = col;
        col = row;
        row = temp;
        compression = Arrays.asList(rotate);
        return this;
    }
}
  1. 测试

 int[][] target = {
                {0, 12, 9, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0},
                {-3, 0, 0, 0, 0, 14, 0},
                {0, 0, 24, 0, 0, 0, 0},
                {0, 18, 0, 0, 0, 0, 0},
                {15, 0, 0, -7, 0, 0, 0}
        };

        SparseMatrix sparseMatrix = new SparseMatrix();
        sparseMatrix.compression(target).printMatrix().forceRotateMatrix().printMatrix().quickRotateMatrix().printMatrix();

打印结果

0   12  9   0   0   0   0   
0   0   0   0   0   0   0   
-3  0   0   0   0   14  0   
0   0   24  0   0   0   0   
0   18  0   0   0   0   0   
15  0   0   -7  0   0   0   

0   0   -3  0   0   15  
12  0   0   0   18  0   
9   0   0   24  0   0   
0   0   0   0   0   -7  
0   0   0   0   0   0   
0   0   14  0   0   0   
0   0   0   0   0   0   

0   12  9   0   0   0   0   
0   0   0   0   0   0   0   
-3  0   0   0   0   14  0   
0   0   0   0   0   0   0   
0   18  0   0   0   0   0   
0   0   0   0   0   0   0   

相关文章

  • 稀疏矩阵的压缩和转置(Java实现)

    简介 稀疏矩阵是矩阵中的非零元素个数远远少于零元素个数。一般会通过稀疏因子来进行确定。假设在m×n的矩阵中,有t个...

  • 三元组压缩存储稀疏矩阵的转置

    数据结构的一道上机题 主要实现快速转置算法 参考博客: 稀疏矩阵的压缩存储及其转置算法 参考博客把思路讲的很清晰了...

  • 2019-05-21(矩阵的顺序储存----一维数组)

    采用数组的形式保存数据 就需要考虑空间利用率的问题:稀疏矩阵的存储矩阵的转置的两种算法:一种是先转置后按照行主序进...

  • 2019-02-22 Day 48 待提高

    1.转置矩阵 给定一个矩阵 A, 返回 A 的转置矩阵。 矩阵的转置是指将矩阵的主对角线翻转,交换矩阵的行索引与列...

  • 机器学习里的数学基础——矩阵论

    1. 基本概念 1.1 向量及其转置 一个维列向量及其转置可记作: 1.2 矩阵及其转置 一个的矩阵及其的转置矩阵...

  • 深度学习之线性代数

    标量、向量、矩阵和张量 转置(transpose)是矩阵的重要操作之一。 向量可以看做只有一列的矩阵。向量的转置可...

  • 稀疏矩阵用于python的keras和theano

    稀疏矩阵 稀疏矩阵(sparse matrix)是由于矩阵中存在大量0,从而可以采用特别的存储技巧来压缩内存。由于...

  • 2019-03-13

    矩阵的转置则称 为A的转置,记为 设为矩阵,则为 矩阵 为对称矩阵,则 为反对称矩阵,则 为n阶方阵,,为对称矩阵...

  • Octave教程(三)

    矩阵运算 其中,“.”表示元素位运算。 如何求转置矩阵 一些有用的函数 其中,flipud()实现矩阵的上下翻转。

  • 正交矩阵

    转置矩阵为其逆矩阵。

网友评论

      本文标题:稀疏矩阵的压缩和转置(Java实现)

      本文链接:https://www.haomeiwen.com/subject/kwcxlftx.html