OSDN Git Service

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