mirror of
https://github.com/gonum/gonum.git
synced 2025-10-12 18:40:09 +08:00
Changed ApplyFunc to Apply, fixed Cumsum example
This commit is contained in:
@@ -110,7 +110,7 @@ func ExampleCumsum() {
|
|||||||
s := []float64{1, -2, 3, -4}
|
s := []float64{1, -2, 3, -4}
|
||||||
dst := make([]float64, len(s))
|
dst := make([]float64, len(s))
|
||||||
|
|
||||||
Cumprod(dst, s)
|
Cumsum(dst, s)
|
||||||
|
|
||||||
fmt.Println("dst = ", dst)
|
fmt.Println("dst = ", dst)
|
||||||
fmt.Println("s = ", s)
|
fmt.Println("s = ", s)
|
||||||
|
@@ -2,8 +2,6 @@ package sliceops
|
|||||||
|
|
||||||
import "math"
|
import "math"
|
||||||
|
|
||||||
// Test comment
|
|
||||||
|
|
||||||
// InsufficientElements is an error type used by FindFirst
|
// InsufficientElements is an error type used by FindFirst
|
||||||
type InsufficientElements struct{}
|
type InsufficientElements struct{}
|
||||||
|
|
||||||
@@ -38,7 +36,7 @@ func AddConst(s []float64, c float64) {
|
|||||||
|
|
||||||
// ApplyFunc applies a function (math.Exp, math.Sin, etc.) to every element
|
// ApplyFunc applies a function (math.Exp, math.Sin, etc.) to every element
|
||||||
// of the slice
|
// of the slice
|
||||||
func ApplyFunc(s []float64, f func(float64) float64) {
|
func Apply(s []float64, f func(float64) float64) {
|
||||||
for i, val := range s {
|
for i, val := range s {
|
||||||
s[i] = f(val)
|
s[i] = f(val)
|
||||||
}
|
}
|
||||||
@@ -164,7 +162,7 @@ func Linspace(dst []float64, l, u float64) {
|
|||||||
// zeros if l or u is zero.
|
// zeros if l or u is zero.
|
||||||
func Logspace(dst []float64, l, u float64) {
|
func Logspace(dst []float64, l, u float64) {
|
||||||
Linspace(dst, math.Log(l), math.Log(u))
|
Linspace(dst, math.Log(l), math.Log(u))
|
||||||
ApplyFunc(dst, math.Exp)
|
Apply(dst, math.Exp)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Logsumexp returns the log of the sum of the exponentials of the values in s
|
// Logsumexp returns the log of the sum of the exponentials of the values in s
|
||||||
|
@@ -41,14 +41,14 @@ func TestAddconst(t *testing.T) {
|
|||||||
AreSlicesEqual(t, truth, s, "Wrong addition of constant")
|
AreSlicesEqual(t, truth, s, "Wrong addition of constant")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestApplyFunc(t *testing.T) {
|
func TestApply(t *testing.T) {
|
||||||
s := []float64{3, 4, 1, 7, 5}
|
s := []float64{3, 4, 1, 7, 5}
|
||||||
f := math.Sin
|
f := math.Sin
|
||||||
truth := make([]float64, len(s))
|
truth := make([]float64, len(s))
|
||||||
for i, val := range s {
|
for i, val := range s {
|
||||||
truth[i] = math.Sin(val)
|
truth[i] = math.Sin(val)
|
||||||
}
|
}
|
||||||
ApplyFunc(s, f)
|
Apply(s, f)
|
||||||
AreSlicesEqual(t, truth, s, "Wrong application of function")
|
AreSlicesEqual(t, truth, s, "Wrong application of function")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user