就地更改二维切片

Change 2D slice in-place

我正在 Go 中实现矩阵-矩阵乘法算法,但我无法推断如何就地更改输出矩阵。我试过将输入更改为指针,但 2D 切片不能是指针?

package main

import (
    "fmt"
    "strconv"
    "math/rand"
    "os"
    "time"
)

func main() {
    L := len(os.Args)
    m, n, p, q, err := mapVars(L, os.Args)
    if err != 0 {
        fmt.Fprintf(os.Stderr, "error: Incorrect command line arguments.\n")
        os.Exit(1)
    }

    fmt.Println("The product array has dimensions.")
    fmt.Printf("\tC is %dx%d\n", m, q)

    fmt.Println("\nPopulating matrix A.")
    A, _ := createMat(m, n)
    fmt.Println("Matrix A.")
    printMat(m, A)

    fmt.Println("\nPopulating matrix B.")
    B, _ := createMat(p, q)
    fmt.Println("Matrix B.")
    printMat(p, B)

    fmt.Println("\nPerforming row-wise matrix-matrix multiplication AB.")
    startRow := time.Now()
    C := rowMultMat(m, n, q, A, B)
    dtRow := time.Since(startRow)
    fmt.Printf("Time elapsed: %v\n", dtRow)
    fmt.Println("Matrix C.")
    printMat(q, C)

}

func mapVars(l int, args []string) (m int, n int, p int, q int, err int) {
    if l == 2 {
        m, _ := strconv.Atoi(args[1])
        n, _ := strconv.Atoi(args[1])
        p, _ := strconv.Atoi(args[1])
        q, _ := strconv.Atoi(args[1])
        fmt.Printf("Creating two arrays, A, B, with square dimensions.\n")
        fmt.Printf("\tA is %dx%d\n\tB is %dx%d\n", m, n, p, q)
        return m, n, p, q, 0
    } else if (l == 5 || n != p) {
        m, _ := strconv.Atoi(args[1])
        n, _ := strconv.Atoi(args[2])
        p, _ := strconv.Atoi(args[3])
        q, _ := strconv.Atoi(args[4])
        fmt.Println("Creating two arrays, A, B, with dimensions.")
        fmt.Printf("\tA is %dx%d\n\tB is %dx%d\n", m, n, p, q)
        return m, n, p, q, 0
    } else {
        fmt.Println("Incorrect command line arguments.\n")
        return 0, 0, 0, 0, 1
    }   
}

func initMat(m int, n int) (M [][]float64, rows []float64) {
    M = make([][]float64, m)
    rows = make([]float64, n*m)
    for i := 0; i < m; i++ {
        M[i] = rows[i*n : (i+1)*n]
    }
    return M, rows
}

func createMat(m int, n int) (M [][]float64, rows []float64) {
    M = make([][]float64, m)
    rows = make([]float64, n*m)
    for i := 0; i < m; i++ {
        for j := 0; j < n; j++ {
            rows[i*n + j] = float64(rand.Int63()%10)
        }
        M[i] = rows[i*n : (i+1)*n]
    }
    return M, rows
}

func printMat(row int, M [][]float64) {
    for i := 0; i < row; i++ {
        fmt.Printf("%v\n", M[i])
    }
}

func rowMultMat(m int, n int, q int, A [][]float64, B [][]float64) (C [][]float64) {
    C, _ = initMat(m, q)
    var total float64 = 0.0
    for i := 0; i < m; i++ {
        for j := 0; j < q; j++ {
            for k := 0; k < n; k++ {
                total += A[i][k] * (B[k][j])
            }
            C[i][j] = total
            total = 0
        }
    }
    return C
}

目前我正在初始化 rowMultMat 内的矩阵,因为我无法将 C 作为指向二维切片的指针传递。例如,run main.go 2 3 3 2 会将 2x3 乘以 3x2 得到 2x2。

切片已经是参考值了。如果将切片传递给函数,该函数可以修改其内容 (*),并且一旦 returns.

调用者就可以看到修改

或者,返回一个新切片也很有效 - 因为切片只是引用,不会占用太多内存。


(*) 这里的 contents 是指切片指向的底层数组的内容。某些属性,如切片的长度 不能 以这种方式更改;例如,如果您的函数需要使切片更长,则您必须传递一个指向切片的指针。