github.com/gopherd/gonum@v0.0.4/floats/examples_test.go (about) 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_test 6 7 import ( 8 "fmt" 9 10 "github.com/gopherd/gonum/floats" 11 ) 12 13 // Set of examples for all the functions 14 15 func ExampleAdd_simple() { 16 // Adding three slices together. Note that 17 // the result is stored in the first slice 18 s1 := []float64{1, 2, 3, 4} 19 s2 := []float64{5, 6, 7, 8} 20 s3 := []float64{1, 1, 1, 1} 21 floats.Add(s1, s2) 22 floats.Add(s1, s3) 23 24 fmt.Println("s1 =", s1) 25 fmt.Println("s2 =", s2) 26 fmt.Println("s3 =", s3) 27 28 // Output: 29 // s1 = [7 9 11 13] 30 // s2 = [5 6 7 8] 31 // s3 = [1 1 1 1] 32 } 33 34 func ExampleAdd_newslice() { 35 // If one wants to store the result in a 36 // new container, just make a new slice 37 s1 := []float64{1, 2, 3, 4} 38 s2 := []float64{5, 6, 7, 8} 39 s3 := []float64{1, 1, 1, 1} 40 dst := make([]float64, len(s1)) 41 42 floats.AddTo(dst, s1, s2) 43 floats.Add(dst, s3) 44 45 fmt.Println("dst =", dst) 46 fmt.Println("s1 =", s1) 47 fmt.Println("s2 =", s2) 48 fmt.Println("s3 =", s3) 49 50 // Output: 51 // dst = [7 9 11 13] 52 // s1 = [1 2 3 4] 53 // s2 = [5 6 7 8] 54 // s3 = [1 1 1 1] 55 } 56 57 func ExampleAdd_unequallengths() { 58 // If the lengths of the slices are unknown, 59 // use EqualLengths to check 60 s1 := []float64{1, 2, 3} 61 s2 := []float64{5, 6, 7, 8} 62 63 eq := floats.EqualLengths(s1, s2) 64 if eq { 65 floats.Add(s1, s2) 66 } else { 67 fmt.Println("Unequal lengths") 68 } 69 70 // Output: 71 // Unequal lengths 72 } 73 74 func ExampleAddConst() { 75 s := []float64{1, -2, 3, -4} 76 c := 5.0 77 78 floats.AddConst(c, s) 79 80 fmt.Println("s =", s) 81 82 // Output: 83 // s = [6 3 8 1] 84 } 85 86 func ExampleCumProd() { 87 s := []float64{1, -2, 3, -4} 88 dst := make([]float64, len(s)) 89 90 floats.CumProd(dst, s) 91 92 fmt.Println("dst =", dst) 93 fmt.Println("s =", s) 94 95 // Output: 96 // dst = [1 -2 -6 24] 97 // s = [1 -2 3 -4] 98 } 99 100 func ExampleCumSum() { 101 s := []float64{1, -2, 3, -4} 102 dst := make([]float64, len(s)) 103 104 floats.CumSum(dst, s) 105 106 fmt.Println("dst =", dst) 107 fmt.Println("s =", s) 108 109 // Output: 110 // dst = [1 -1 2 -2] 111 // s = [1 -2 3 -4] 112 }