0

我想知道是否有一个包可以在 Go 中提供有效的元素矩阵运算?类似于 GSL 的东西?

4

2 回答 2

1

通过 cgo 调用例如 cblas 很容易:

package main

// #include <cblas.h>
// #cgo LDFLAGS: -L/usr/lib64/atlas -lcblas
import "C"

import "fmt"

type matrix struct {
    rows  int
    cols  int
    elems []float32
}

func (a matrix) cblasmul(b matrix) (c matrix) {
    c = matrix{a.rows, b.cols, make([]float32, a.rows*b.cols)}
    C.cblas_sgemm(
        C.CblasRowMajor, C.CblasNoTrans, C.CblasNoTrans,
        C.int(a.rows), C.int(b.cols), C.int(a.cols),
        1.0,
        (*C.float)(&a.elems[0]), C.int(a.cols),
        (*C.float)(&b.elems[0]), C.int(b.cols),
        0.0,
        (*C.float)(&c.elems[0]), C.int(c.cols))

    return c
}

func main() {
    a := matrix{100, 100, make([]float32, 100*100)}
    b := matrix{100, 100, make([]float32, 100*100)}
    // ...
    c := a.cblasmul(b)
    fmt.Println(c)
}
于 2013-03-27T20:26:47.580 回答
0

GSL 有各种 cgo 绑定,甚至还有一些纯 Go 端口的尝试。似乎还没有一个得到太多认可(就明星而言)并且已经闲置了几个月,但您可能想看看代码:

http://godoc.org/?q=gsl

于 2013-03-25T19:44:20.210 回答