稀疏矩阵的存储及乘法实现

未完全测试,整体思路如下:

import java.util.*;

public class Main{
    public static void main(String[] args){
        Matrix a = new Matrix(2,2, new ArrayList<Triple>(), new ArrayList<Integer>());
        a.val_list.add(new Triple(0, 0, 1));
        a.val_list.add(new Triple(1,1,1));
        a.rpos.add(0);
        a.rpos.add(1);

        Matrix b = new Matrix(2,2, new ArrayList<Triple>(), new ArrayList<Integer>());
        b.val_list.add(new Triple(0, 0, 1));
        b.val_list.add(new Triple(1,0,1));
        b.val_list.add(new Triple(1,1,1));
        b.rpos.add(0);
        b.rpos.add(1);

        Matrix c = new Matrix(0,0, new ArrayList<Triple>(), new ArrayList<Integer>());

        a.multiply(b, c);

        System.out.println(c.val_list.size());
        for(int i = 0; i < c.val_list.size(); i++){
            System.out.print(c.val_list.get(i).x);
            System.out.print(c.val_list.get(i).y);
            System.out.print(c.val_list.get(i).val);
            System.out.print("\t");
        }
    }
}

// 使用三元组来记录非0值位置
class Triple{
    int x, y;
    int val;
    Triple(int _x, int _y, int _val){
        x = _x;
        y = _y;
        val = _val;
    }
}

// 使用val_list存储所有数据元素,rpos为矩阵中每一行非0元素在val_list中的起始位置,row,col分别为矩阵的行和列大小,。
class Matrix{
    int row, col;
    ArrayList<Triple> val_list;
    ArrayList<Integer> rpos;

    Matrix(int _row, int _col, ArrayList<Triple> _val_list, ArrayList<Integer> _rpos){
        row = _row;
        col = _col;
        val_list = _val_list;
        rpos = _rpos;
    }

    int multiply(Matrix b, Matrix c){
        if(this.col != b.row){
            return 1;
        }

        c.row = this.row;
        c.col = b.col;

//        添加一个虚拟的行
        this.rpos.add(this.val_list.size());
        b.rpos.add(b.val_list.size());

//        暂存计算值
        int[] temp_count = new int[c.col];

//        稀疏矩阵c的行
        for(int tr = 0; tr < c.row; tr++){
//            稀疏矩阵c的列
            for(int tc = 0; tc < c.col; tc++){
//                使用暂时变量把this的行与所有b的列计算结果存下来
                Arrays.fill(temp_count, 0);

//                遍历this稀疏矩阵行中的元素
                int thisStartRow = this.rpos.get(tr);
                int thisEndRow = this.rpos.get(tr+1);
                for(int p = thisStartRow; p < thisEndRow; p++){

//                    this的一行与b的所有的列计算结果暂存
//                    我们可以通过对this中一个元素的y值找到它要与b中参与计算的行
                    int bStartRow = b.rpos.get(this.val_list.get(p).y);
                    int bEndEow = b.rpos.get(this.val_list.get(p).y+1);
                    for(int q = bStartRow; q < bEndEow; q++){
                        int col = b.val_list.get(q).y;
                        temp_count[col] += this.val_list.get(p).val * b.val_list.get(q).val;
                    }
                }
            }
//                记录初始位置
            c.rpos.add(c.rpos.size());
//                往稀疏矩阵c里面存储数据
            for(int i = 0; i < c.col; i++){
                if(temp_count[i] != 0){
                    c.val_list.add(new Triple(tr, i, temp_count[i]));
                }
            }
        }
        return 0;
    }
}
发布了557 篇原创文章 · 获赞 500 · 访问量 153万+

猜你喜欢

转载自blog.csdn.net/qq_16234613/article/details/100189730
今日推荐