all: put examples into a xxx_test package clause
Fixes gonum/gonum#1395.
MODIFY
cmplxs/examples_test.go
cmplxs/examples_test.go
// Use of this code is governed by a BSD-style
// license that can be found in the LICENSE file
-package cmplxs
+package cmplxs_test
-import "fmt"
+import (
+ "fmt"
+
+ "gonum.org/v1/gonum/cmplxs"
+)
// Set of examples for all the functions
@@ -14,12 +18,13 @@ func ExampleAdd_simple() {
s1 := []complex128{1 + 1i, 2 + 2i, 3 + 3i, 4 + 4i}
s2 := []complex128{5 + 7i, 6 + 7i, 7 + 7i, 8 + 8i}
s3 := []complex128{1 + 2i, 1 + 2i, 1 + 2i, 1 + 2i}
- Add(s1, s2)
- Add(s1, s3)
+ cmplxs.Add(s1, s2)
+ cmplxs.Add(s1, s3)
fmt.Println("s1 =", s1)
fmt.Println("s2 =", s2)
fmt.Println("s3 =", s3)
+
// Output:
// s1 = [(7+10i) (9+11i) (11+12i) (13+14i)]
// s2 = [(5+7i) (6+7i) (7+7i) (8+8i)]
@@ -34,13 +39,14 @@ func ExampleAdd_newslice() {
s3 := []complex128{1 + 2i, 1 + 2i, 1 + 2i, 1 + 2i}
dst := make([]complex128, len(s1))
- AddTo(dst, s1, s2)
- Add(dst, s3)
+ cmplxs.AddTo(dst, s1, s2)
+ cmplxs.Add(dst, s3)
fmt.Println("dst =", dst)
fmt.Println("s1 =", s1)
fmt.Println("s2 =", s2)
fmt.Println("s3 =", s3)
+
// Output:
// dst = [(7+10i) (9+11i) (11+12i) (13+14i)]
// s1 = [(1+1i) (2+2i) (3+3i) (4+4i)]
@@ -54,12 +60,13 @@ func ExampleAdd_unequallengths() {
s1 := []complex128{1 + 1i, 2 + 2i, 3 + 3i}
s2 := []complex128{5 + 5i, 6 + 6i, 7 + 7i, 8 + 8i}
- eq := EqualLengths(s1, s2)
+ eq := cmplxs.EqualLengths(s1, s2)
if eq {
- Add(s1, s2)
+ cmplxs.Add(s1, s2)
} else {
fmt.Println("Unequal lengths")
}
+
// Output:
// Unequal lengths
}
@@ -68,9 +75,10 @@ func ExampleAddConst() {
s := []complex128{1 - 1i, -2 - 1i, 3 - 1i, -4 - 1i}
c := 5 + 1i
- AddConst(c, s)
+ cmplxs.AddConst(c, s)
fmt.Println("s =", s)
+
// Output:
// s = [(6+0i) (3+0i) (8+0i) (1+0i)]
}
@@ -79,10 +87,11 @@ func ExampleCumProd() {
s := []complex128{1 + 1i, -2 - 2i, 3 + 3i, -4 - 4i}
dst := make([]complex128, len(s))
- CumProd(dst, s)
+ cmplxs.CumProd(dst, s)
fmt.Println("dst =", dst)
fmt.Println("s =", s)
+
// Output:
// dst = [(1+1i) (0-4i) (12-12i) (-96+0i)]
// s = [(1+1i) (-2-2i) (3+3i) (-4-4i)]
@@ -92,10 +101,11 @@ func ExampleCumSum() {
s := []complex128{1 + 1i, -2 - 2i, 3 + 3i, -4 - 4i}
dst := make([]complex128, len(s))
- CumSum(dst, s)
+ cmplxs.CumSum(dst, s)
fmt.Println("dst =", dst)
fmt.Println("s =", s)
+
// Output:
// dst = [(1+1i) (-1-1i) (2+2i) (-2-2i)]
// s = [(1+1i) (-2-2i) (3+3i) (-4-4i)]

MODIFY
floats/examples_test.go
floats/examples_test.go
// Use of this code is governed by a BSD-style
// license that can be found in the LICENSE file.
-package floats
+package floats_test
-import "fmt"
+import (
+ "fmt"
+
+ "gonum.org/v1/gonum/floats"
+)
// Set of examples for all the functions
@@ -14,12 +18,13 @@ func ExampleAdd_simple() {
s1 := []float64{1, 2, 3, 4}
s2 := []float64{5, 6, 7, 8}
s3 := []float64{1, 1, 1, 1}
- Add(s1, s2)
- Add(s1, s3)
+ floats.Add(s1, s2)
+ floats.Add(s1, s3)
fmt.Println("s1 =", s1)
fmt.Println("s2 =", s2)
fmt.Println("s3 =", s3)
+
// Output:
// s1 = [7 9 11 13]
// s2 = [5 6 7 8]
@@ -34,13 +39,14 @@ func ExampleAdd_newslice() {
s3 := []float64{1, 1, 1, 1}
dst := make([]float64, len(s1))
- AddTo(dst, s1, s2)
- Add(dst, s3)
+ floats.AddTo(dst, s1, s2)
+ floats.Add(dst, s3)
fmt.Println("dst =", dst)
fmt.Println("s1 =", s1)
fmt.Println("s2 =", s2)
fmt.Println("s3 =", s3)
+
// Output:
// dst = [7 9 11 13]
// s1 = [1 2 3 4]
@@ -54,12 +60,13 @@ func ExampleAdd_unequallengths() {
s1 := []float64{1, 2, 3}
s2 := []float64{5, 6, 7, 8}
- eq := EqualLengths(s1, s2)
+ eq := floats.EqualLengths(s1, s2)
if eq {
- Add(s1, s2)
+ floats.Add(s1, s2)
} else {
fmt.Println("Unequal lengths")
}
+
// Output:
// Unequal lengths
}
@@ -68,9 +75,10 @@ func ExampleAddConst() {
s := []float64{1, -2, 3, -4}
c := 5.0
- AddConst(c, s)
+ floats.AddConst(c, s)
fmt.Println("s =", s)
+
// Output:
// s = [6 3 8 1]
}
@@ -79,10 +87,11 @@ func ExampleCumProd() {
s := []float64{1, -2, 3, -4}
dst := make([]float64, len(s))
- CumProd(dst, s)
+ floats.CumProd(dst, s)
fmt.Println("dst =", dst)
fmt.Println("s =", s)
+
// Output:
// dst = [1 -2 -6 24]
// s = [1 -2 3 -4]
@@ -92,10 +101,11 @@ func ExampleCumSum() {
s := []float64{1, -2, 3, -4}
dst := make([]float64, len(s))
- CumSum(dst, s)
+ floats.CumSum(dst, s)
fmt.Println("dst =", dst)
fmt.Println("s =", s)
+
// Output:
// dst = [1 -1 2 -2]
// s = [1 -2 3 -4]

MODIFY
graph/community/bisect_test.go
graph/community/bisect_test.go
package community
import (
- "fmt"
- "log"
- "sort"
"testing"
- "golang.org/x/exp/rand"
-
"gonum.org/v1/gonum/graph"
- "gonum.org/v1/gonum/graph/internal/ordered"
"gonum.org/v1/gonum/graph/simple"
)
-func ExampleProfile_simple() {
- // Profile calls Modularize which implements the Louvain modularization algorithm.
- // Since this is a randomized algorithm we use a defined random source to ensure
- // consistency between test runs. In practice, results will not differ greatly
- // between runs with different PRNG seeds.
- src := rand.NewSource(1)
-
- // Create dumbell graph:
- //
- // 0 4
- // |\ /|
- // | 2 - 3 |
- // |/ \|
- // 1 5
- //
- g := simple.NewUndirectedGraph()
- for u, e := range smallDumbell {
- for v := range e {
- g.SetEdge(simple.Edge{F: simple.Node(u), T: simple.Node(v)})
- }
- }
-
- // Get the profile of internal node weight for resolutions
- // between 0.1 and 10 using logarithmic bisection.
- p, err := Profile(ModularScore(g, Weight, 10, src), true, 1e-3, 0.1, 10)
- if err != nil {
- log.Fatal(err)
- }
-
- // Print out each step with communities ordered.
- for _, d := range p {
- comm := d.Communities()
- for _, c := range comm {
- sort.Sort(ordered.ByID(c))
- }
- sort.Sort(ordered.BySliceIDs(comm))
- fmt.Printf("Low:%.2v High:%.2v Score:%v Communities:%v Q=%.3v\n",
- d.Low, d.High, d.Score, comm, Q(g, comm, d.Low))
- }
-
- // Output:
- // Low:0.1 High:0.29 Score:14 Communities:[[0 1 2 3 4 5]] Q=0.9
- // Low:0.29 High:2.3 Score:12 Communities:[[0 1 2] [3 4 5]] Q=0.714
- // Low:2.3 High:3.5 Score:4 Communities:[[0 1] [2] [3] [4 5]] Q=-0.31
- // Low:3.5 High:10 Score:0 Communities:[[0] [1] [2] [3] [4] [5]] Q=-0.607
-}
-
-var friends, enemies *simple.WeightedUndirectedGraph
-
-func init() {
- friends = simple.NewWeightedUndirectedGraph(0, 0)
- for u, e := range middleEast.friends {
- // Ensure unconnected nodes are included.
- if friends.Node(int64(u)) == nil {
- friends.AddNode(simple.Node(u))
- }
- for v := range e {
- friends.SetWeightedEdge(simple.WeightedEdge{F: simple.Node(u), T: simple.Node(v), W: 1})
- }
- }
- enemies = simple.NewWeightedUndirectedGraph(0, 0)
- for u, e := range middleEast.enemies {
- // Ensure unconnected nodes are included.
- if enemies.Node(int64(u)) == nil {
- enemies.AddNode(simple.Node(u))
- }
- for v := range e {
- enemies.SetWeightedEdge(simple.WeightedEdge{F: simple.Node(u), T: simple.Node(v), W: -1})
- }
- }
-}
-
-func ExampleProfile_multiplex() {
- // Profile calls ModularizeMultiplex which implements the Louvain modularization
- // algorithm. Since this is a randomized algorithm we use a defined random source
- // to ensure consistency between test runs. In practice, results will not differ
- // greatly between runs with different PRNG seeds.
- src := rand.NewSource(1)
-
- // The undirected graphs, friends and enemies, are the political relationships
- // in the Middle East as described in the Slate article:
- // http://www.slate.com/blogs/the_world_/2014/07/17/the_middle_east_friendship_chart.html
- g, err := NewUndirectedLayers(friends, enemies)
- if err != nil {
- log.Fatal(err)
- }
- weights := []float64{1, -1}
-
- // Get the profile of internal node weight for resolutions
- // between 0.1 and 10 using logarithmic bisection.
- p, err := Profile(ModularMultiplexScore(g, weights, true, WeightMultiplex, 10, src), true, 1e-3, 0.1, 10)
- if err != nil {
- log.Fatal(err)
- }
-
- // Print out each step with communities ordered.
- for _, d := range p {
- comm := d.Communities()
- for _, c := range comm {
- sort.Sort(ordered.ByID(c))
- }
- sort.Sort(ordered.BySliceIDs(comm))
- fmt.Printf("Low:%.2v High:%.2v Score:%v Communities:%v Q=%.3v\n",
- d.Low, d.High, d.Score, comm, QMultiplex(g, comm, weights, []float64{d.Low}))
- }
-
- // Output:
- // Low:0.1 High:0.72 Score:26 Communities:[[0] [1 7 9 12] [2 8 11] [3 4 5 10] [6]] Q=[24.7 1.97]
- // Low:0.72 High:1.1 Score:24 Communities:[[0 6] [1 7 9 12] [2 8 11] [3 4 5 10]] Q=[16.9 14.1]
- // Low:1.1 High:1.2 Score:18 Communities:[[0 2 6 11] [1 7 9 12] [3 4 5 8 10]] Q=[9.16 25.1]
- // Low:1.2 High:1.6 Score:10 Communities:[[0 3 4 5 6 10] [1 7 9 12] [2 8 11]] Q=[10.5 26.7]
- // Low:1.6 High:1.6 Score:8 Communities:[[0 1 6 7 9 12] [2 8 11] [3 4 5 10]] Q=[5.56 39.8]
- // Low:1.6 High:1.8 Score:2 Communities:[[0 2 3 4 5 6 10] [1 7 8 9 11 12]] Q=[-1.82 48.6]
- // Low:1.8 High:2.3 Score:-6 Communities:[[0 2 3 4 5 6 8 10 11] [1 7 9 12]] Q=[-5 57.5]
- // Low:2.3 High:2.4 Score:-10 Communities:[[0 1 2 6 7 8 9 11 12] [3 4 5 10]] Q=[-11.2 79]
- // Low:2.4 High:4.3 Score:-52 Communities:[[0 1 2 3 4 5 6 7 8 9 10 11 12]] Q=[-46.1 117]
- // Low:4.3 High:10 Score:-54 Communities:[[0 1 2 3 4 6 7 8 9 10 11 12] [5]] Q=[-82 254]
-}
-
func TestProfileUndirected(t *testing.T) {
for _, test := range communityUndirectedQTests {
g := simple.NewUndirectedGraph()

MODIFY
stat/sampleuv/example_burnin_test.go
stat/sampleuv/example_burnin_test.go
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-package sampleuv
+package sampleuv_test
-import "gonum.org/v1/gonum/stat/distuv"
+import (
+ "gonum.org/v1/gonum/stat/distuv"
+ "gonum.org/v1/gonum/stat/sampleuv"
+)
type ProposalDist struct {
Sigma float64
@@ -32,6 +35,6 @@ func ExampleMetropolisHastings_burnin() {
proposal := ProposalDist{Sigma: 0.2}
samples := make([]float64, n)
- mh := MetropolisHastings{Initial: initial, Target: target, Proposal: proposal, BurnIn: burnin}
+ mh := sampleuv.MetropolisHastings{Initial: initial, Target: target, Proposal: proposal, BurnIn: burnin}
mh.Sample(samples)
}

MODIFY
stat/sampleuv/example_rate_test.go
stat/sampleuv/example_rate_test.go
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-package sampleuv
+package sampleuv_test
-import "gonum.org/v1/gonum/stat/distuv"
+import (
+ "gonum.org/v1/gonum/stat/distuv"
+ "gonum.org/v1/gonum/stat/sampleuv"
+)
func ExampleMetropolisHastings_samplingRate() {
// See Burnin example for a description of these quantities.
@@ -22,7 +25,7 @@ func ExampleMetropolisHastings_samplingRate() {
// samples are not stored in `samples`.
rate := 50
- mh := MetropolisHastings{
+ mh := sampleuv.MetropolisHastings{
Initial: initial,
Target: target,
Proposal: proposal,

all: put examples into a xxx_test package clause

Fixes gonum/gonum#1395.