gpt4 book ai didi

go - 逐元素矩阵运算 Google Go?

转载 作者:数据小太阳 更新时间:2023-10-29 03:17:54 25 4
gpt4 key购买 nike

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

最佳答案

很容易调用例如通过 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)
}

关于go - 逐元素矩阵运算 Google Go?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15621889/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com