使用自定义Golang库计算算术平均值

package main

import (
    "fmt"

    maths "github.com/ematvey/go-fn/fn"
)

var avg float64

func main() {

    A := []float64{2, 3, 5, 7, 11, 13}

    avg := maths.ArithMean(A)
    fmt.Println(avg)
}

I am not able to call the ArithMean function. It is giving the error:

cannot use A (type []float64) as type *fn.Vector in argument to fn.ArithMean

package fn

type Vector

type Vector struct {
    A   []float64 // data
    L   int       // length
}

For example,

package main

import (
    "fmt"

    maths "github.com/ematvey/go-fn/fn"
)

func main() {
    a := []float64{2, 3, 5, 7, 11, 13}
    v := &maths.Vector{A: a, L: len(a)}
    avg := maths.ArithMean(v)
    fmt.Println(avg)
}

Output:

6.833333333333333

Other statistical packages may be easier and more intuitive to use.

For example,

func Mean

func Mean(x, weights []float64) float64

Mean computes the weighted mean of the data set.

sum_i {w_i * x_i} / sum_i {w_i}

If weights is nil then all of the weights are 1. If weights is not nil, then len(x) must equal len(weights).

package main

import (
    "fmt"

    "gonum.org/v1/gonum/stat"
)

func main() {
    a := []float64{2, 3, 5, 7, 11, 13}
    mean := stat.Mean(a, nil)
    fmt.Println(mean)
}

Output:

6.833333333333333