逐元素矩阵运算 Google Go?

huangapple go评论79阅读模式
英文:

Element-wise matrix operations Google Go?

问题

我想知道是否有一个在Go语言中提供高效逐元素矩阵操作的包?类似于GSL的东西?

英文:

I'm wondering if theres a package that provides efficient element-wise matrix operations in Go? Something similar to GSL?

答案1

得分: 1

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, 100100)}
b := matrix{100, 100, make([]float32, 100
100)}
// ...
c := a.cblasmul(b)
fmt.Println(c)
}

英文:

It is easy to call e.g. cblas via cgo:

package main

// #include &lt;cblas.h&gt;
// #cgo LDFLAGS: -L/usr/lib64/atlas -lcblas
import &quot;C&quot;

import &quot;fmt&quot;

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)(&amp;a.elems[0]), C.int(a.cols),
        (*C.float)(&amp;b.elems[0]), C.int(b.cols),
        0.0,
        (*C.float)(&amp;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)
}

答案2

得分: 0

有各种各样的cgo绑定到GSL,甚至还有一些纯Go端口的尝试。目前似乎没有太多的认可(就星星而言),而且已经有几个月没有活动了,但你可能想看一下这些代码:

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

英文:

There are various cgo-bindings to GSL and even some attempts at pure Go ports out there. None seem to have much recognition yet (as far as stars are concerned) and have been inactive for a few months, but you may want to take a look at the code:

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

huangapple
  • 本文由 发表于 2013年3月26日 02:16:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/15621889.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定