This repository has been archived by the owner on Dec 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathexamples_test.go
104 lines (85 loc) · 1.88 KB
/
examples_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2013 The Gonum Authors. All rights reserved.
// Use of this code is governed by a BSD-style
// license that can be found in the LICENSE file
package floats
import (
"fmt"
)
// Set of examples for all the functions
func ExampleAdd_simple() {
// Adding three slices together. Note that
// the result is stored in the first slice
s1 := []float64{1, 2, 3, 4}
s2 := []float64{5, 6, 7, 8}
s3 := []float64{1, 1, 1, 1}
Add(s1, s2)
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]
// s3 = [1 1 1 1]
}
func ExampleAdd_newslice() {
// If one wants to store the result in a
// new container, just make a new slice
s1 := []float64{1, 2, 3, 4}
s2 := []float64{5, 6, 7, 8}
s3 := []float64{1, 1, 1, 1}
dst := make([]float64, len(s1))
AddTo(dst, s1, s2)
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]
// s2 = [5 6 7 8]
// s3 = [1 1 1 1]
}
func ExampleAdd_unequallengths() {
// If the lengths of the slices are unknown,
// use Eqlen to check
s1 := []float64{1, 2, 3}
s2 := []float64{5, 6, 7, 8}
eq := EqualLengths(s1, s2)
if eq {
Add(s1, s2)
} else {
fmt.Println("Unequal lengths")
}
// Output:
// Unequal lengths
}
func ExampleAddConst() {
s := []float64{1, -2, 3, -4}
c := 5.0
AddConst(c, s)
fmt.Println("s =", s)
// Output:
// s = [6 3 8 1]
}
func ExampleCumProd() {
s := []float64{1, -2, 3, -4}
dst := make([]float64, len(s))
CumProd(dst, s)
fmt.Println("dst =", dst)
fmt.Println("s =", s)
// Output:
// dst = [1 -2 -6 24]
// s = [1 -2 3 -4]
}
func ExampleCumSum() {
s := []float64{1, -2, 3, -4}
dst := make([]float64, len(s))
CumSum(dst, s)
fmt.Println("dst =", dst)
fmt.Println("s =", s)
// Output:
// dst = [1 -1 2 -2]
// s = [1 -2 3 -4]
}