上一篇
在科学计算、机器学习和图论等领域,我们经常会遇到稀疏矩阵(Sparse Matrix)——即大部分元素为零的矩阵。如果使用传统的二维数组来存储这类矩阵,不仅浪费大量内存,还会降低程序运行效率。本教程将带你从零开始,用Java语言实现稀疏矩阵的高效存储与基本操作。
稀疏矩阵是指非零元素数量远小于总元素数量的矩阵。例如,一个 1000×1000 的矩阵(共100万个元素),如果只有5000个非零元素,那么它就是稀疏的。
假设我们用 int[1000][1000] 存储上述矩阵,即使99%是0,也要占用约4MB内存(每个int占4字节)。而如果我们只存储非零元素及其位置,内存消耗可大幅降低。这就是稀疏矩阵存储的核心思想。
在Java中,最常用的稀疏矩阵表示法有:
下面我们用Java实现一个简单的稀疏矩阵类,采用三元组表示法。
import java.util.*;public class SparseMatrix { private int rows; private int cols; private List<Integer> rowIndices; private List<Integer> colIndices; private List<Integer> values; public SparseMatrix(int rows, int cols) { this.rows = rows; this.cols = cols; this.rowIndices = new ArrayList<>(); this.colIndices = new ArrayList<>(); this.values = new ArrayList<>(); } // 设置矩阵中某个位置的值 public void set(int row, int col, int value) { if (value == 0) { // 如果设为0,则移除该位置(保持稀疏性) removeElement(row, col); } else { // 检查是否已存在该位置 int index = findIndex(row, col); if (index != -1) { values.set(index, value); } else { rowIndices.add(row); colIndices.add(col); values.add(value); } } } // 获取矩阵中某个位置的值 public int get(int row, int col) { int index = findIndex(row, col); return index == -1 ? 0 : values.get(index); } // 查找(row, col)对应的索引 private int findIndex(int row, int col) { for (int i = 0; i < rowIndices.size(); i++) { if (rowIndices.get(i) == row && colIndices.get(i) == col) { return i; } } return -1; } // 移除非零元素(设为0时调用) private void removeElement(int row, int col) { int index = findIndex(row, col); if (index != -1) { rowIndices.remove(index); colIndices.remove(index); values.remove(index); } } // 打印稀疏矩阵(调试用) public void printSparse() { System.out.println("稀疏矩阵 (" + rows + "x" + cols + "):"); for (int i = 0; i < rowIndices.size(); i++) { System.out.printf("(%d, %d) = %d\n", rowIndices.get(i), colIndices.get(i), values.get(i)); } }} public class Main { public static void main(String[] args) { SparseMatrix matrix = new SparseMatrix(5, 5); // 设置几个非零元素 matrix.set(0, 1, 10); matrix.set(2, 3, 20); matrix.set(4, 4, 30); // 打印稀疏表示 matrix.printSparse(); // 获取值 System.out.println("matrix[2][3] = " + matrix.get(2, 3)); // 输出 20 System.out.println("matrix[1][1] = " + matrix.get(1, 1)); // 输出 0 }} 对于大规模计算,压缩稀疏行格式(CSR)更为高效。它使用三个数组:
values[]:按行顺序存储所有非零值colIndices[]:对应每个值的列号rowPtr[]:长度为 rows + 1,rowPtr[i] 表示第 i 行第一个非零元素在 values 中的索引CSR格式支持快速行访问和矩阵向量乘法,是许多高性能库(如Eigen、SciPy)的基础。
通过本教程,你已经掌握了:
下一步,你可以尝试实现矩阵加法、转置或与向量相乘等操作,进一步提升对稀疏结构的理解!
本文由主机测评网于2025-12-27发表在主机测评网_免费VPS_免费云服务器_免费独立服务器,如有疑问,请联系我们。
本文链接:https://www.vpshk.cn/20251213253.html