簡體   English   中英

如何基於 Java 中的稀疏列矩陣參數創建二維數組?

[英]How do I create 2d arrays based on sparse column matrix parameters in Java?

我是 Java 的新手。 我想基於壓縮的稀疏列矩陣參數創建一個二維 java 數組。 例如,我可以通過在 python 中使用以下代碼創建一個二維數組:

from scipy.sparse import csc_matrix

indices = [0, 2, 2, 0, 1, 2]
indptr = [0,2,3,6]
data = [1, 2, 3, 4, 5, 6]
shape = [3,3]
sp_mat = csc_matrix((data, indices, indptr), shape=shape).todense()
print(sp_mat)
[[1 0 4]
 [0 0 5]
 [2 3 6]]

但我不知道如何在 Java 中實現它。

import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        int[] indices = new int[]{0, 2, 2, 0, 1, 2};
        int[] indptr = new int[]{0, 2, 3, 6};
        int[] data = new int[]{1, 2, 3, 4, 5, 6};
        int[] shape = new int[]{3, 3};
        // some code to create 2D arrays
    }
}

我將擁有上述代碼的indicesindptrdatashape indptr數組。 我希望能夠創建以下二維數組:

int[][] sp_mat = new int[][]{{1, 0, 4}, {0, 0, 5}, {2, 3, 6}};

我懷疑是否有任何標准方法可以做到這一點。 如果您想使用專用類,那么可能一些具有稀疏矩陣的數學庫將提供此類構造函數。 如果您只想要二維數組,那么您可以自己輕松實現此類代碼。 如果您需要這樣的簽名,那么這里是一個簡單的方法:

private static int[][] to2dArray(int[] row, int[] col, int[] data, int[] shape) {
    //check here row.length == col.length == data.length and shape.length == 2
    int[][] mat = new int[shape[0]][shape[1]];
    var length = row.length;
    for (int i = 0; i < length; i++) {
        mat[row[i]][col[i]] = data[i];
    }
    return mat;
}

后期編輯后,這里是 CSC(壓縮稀疏列)版本:

private static int[][] to2dArray(
        int[] indices, int[] indicesPtr, int[] data, int[] shape) {
    int[][] mat = new int[shape[0]][shape[1]];
    for (int column = 0; column < shape[1]; column++) {
        var from = indicesPtr[column];
        var to = indicesPtr[column + 1];
        for (int index = from; index < to; index++) {
            mat[indices[index]][column] = data[index];
        }
    }
    return mat;
}

我終於找到了我需要的 Java la4j庫。 首先添加依賴:

<dependency>
    <groupId>org.la4j</groupId>
    <artifactId>la4j</artifactId>
    <version>0.6.0</version>
</dependency>

然后我可以創建CCSMatrix並將矩陣轉換為二維數組。

import java.io.IOException;
import org.la4j.matrix.sparse.CCSMatrix;

public class Main {
    public static void main(String[] args) throws IOException {
        int[] indices = new int[]{0, 2, 2, 0, 1, 2};
        int[] indptr = new int[]{0, 2, 3, 6};
        double[] data = new int[]{1, 2, 3, 4, 5, 6};
        int[] shape = new int[]{3, 3};
        // some code to create 2D arrays
        CCSMatrix a = new CCSMatrix(
                shape[0], shape[1], data.length, data, indices, indptr);
        double[][] mat = a.toDenseMatrix().toArray();
    }
}

您可以使用流中的流自己創建toDenseMatrix方法:

public static int[][] toDenseMatrix(
        int[] indices, int[] indptr, int[] data, int[] shape) {
    int[][] m = new int[shape[0]][shape[1]];
    IntStream.range(0, shape[1])
            .forEach(column -> IntStream.range(indptr[column], indptr[column + 1])
                    .forEach(index -> m[indices[index]][column] = data[index]));
    return m;
}
public static void main(String[] args) {
    int[] indices = {0, 2, 2, 0, 1, 2};
    int[] indptr = {0, 2, 3, 6};
    int[] data = {1, 2, 3, 4, 5, 6};
    int[] shape = {3, 3};

    int[][] m = toDenseMatrix(indices, indptr, data, shape);

    // output
    Arrays.stream(m).map(Arrays::toString).forEach(System.out::println);
}

輸出:

[1, 0, 4]
[0, 0, 5]
[2, 3, 6]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM