元素矩阵运算 Google Go?

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


肥皂起泡泡
浏览 205回答 2
2回答

德玛西亚99

通过 cgo 调用例如 cblas 很容易:package main// #include <cblas.h>// #cgo LDFLAGS: -L/usr/lib64/atlas -lcblasimport "C"import "fmt"type matrix struct {&nbsp; &nbsp; rows&nbsp; int&nbsp; &nbsp; cols&nbsp; int&nbsp; &nbsp; elems []float32}func (a matrix) cblasmul(b matrix) (c matrix) {&nbsp; &nbsp; c = matrix{a.rows, b.cols, make([]float32, a.rows*b.cols)}&nbsp; &nbsp; C.cblas_sgemm(&nbsp; &nbsp; &nbsp; &nbsp; C.CblasRowMajor, C.CblasNoTrans, C.CblasNoTrans,&nbsp; &nbsp; &nbsp; &nbsp; C.int(a.rows), C.int(b.cols), C.int(a.cols),&nbsp; &nbsp; &nbsp; &nbsp; 1.0,&nbsp; &nbsp; &nbsp; &nbsp; (*C.float)(&a.elems[0]), C.int(a.cols),&nbsp; &nbsp; &nbsp; &nbsp; (*C.float)(&b.elems[0]), C.int(b.cols),&nbsp; &nbsp; &nbsp; &nbsp; 0.0,&nbsp; &nbsp; &nbsp; &nbsp; (*C.float)(&c.elems[0]), C.int(c.cols))&nbsp; &nbsp; return c}func main() {&nbsp; &nbsp; a := matrix{100, 100, make([]float32, 100*100)}&nbsp; &nbsp; b := matrix{100, 100, make([]float32, 100*100)}&nbsp; &nbsp; // ...&nbsp; &nbsp; c := a.cblasmul(b)&nbsp; &nbsp; fmt.Println(c)}

慕森王

GSL 有各种 cgo 绑定,甚至还有一些对纯 Go 端口的尝试。似乎还没有多少人认可(就明星而言)并且已经有几个月没有活动了,但您可能想看看代码:http://godoc.org/?q=gsl
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go