Search code examples
gogonum

How to multiply complex matrices Golang gonum/mat


I have:

    dataA := []complex128{
        1 + 2i, 3 + 4i,
        5 + 6i, 7 + 8i,
    }
    A := mat.NewCDense(2, 2, dataA)
    dataB := []complex128{
        9 + 10i, 11 + 12i,
        13 + 14i, 15 + 16i,
    }
    B := mat.NewCDense(2, 2, dataB)

    var C mat.CDense

    C.Mul(A, B)

it doesn't work

type mat.CDense has no field or method Mul)

how to multiply complex matrices Golang gonum/mat


Solution

  • I was able to multiply complex matrices as follows:

    package main
    
    import (
         "fmt"
         "gonum.org/v1/gonum/blas"
         "gonum.org/v1/gonum/blas/cblas64"
    )
    
    func main() {
         a := cblas64.General{
             Rows:   2,
             Cols:   2,
             Stride: 2,
             Data:   []complex64{1 + 2i, 3 + 4i, 5 + 6i, 7 + 8i},
         }
    
         b := cblas64.General{
             Rows:   2,
             Cols:   1,
             Stride: 1,
             Data:   []complex64{9 + 10i, 11 + 12i},
         }
    
         c := cblas64.General{
             Rows:   2,
             Cols:   1,
             Stride: 1,
             Data:   make([]complex64, 2),
         }
    
    
         cblas64.Gemm(blas.NoTrans, blas.NoTrans, 1, a, b, 0, c)
    
         for i := 0; i < c.Rows; i++ {
             fmt.Printf("%v\n", c.Data[i])
         }
    

    }