File: examples_test.go

package info (click to toggle)
golang-gonum-v1-gonum 0.15.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,792 kB
  • sloc: asm: 6,252; fortran: 5,271; sh: 377; ruby: 211; makefile: 98
file content (112 lines) | stat: -rw-r--r-- 2,041 bytes parent folder | download
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
105
106
107
108
109
110
111
112
// 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_test

import (
	"fmt"

	"gonum.org/v1/gonum/floats"
)

// 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}
	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]
	// 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))

	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]
	// s2 = [5 6 7 8]
	// s3 = [1 1 1 1]
}

func ExampleAdd_unequallengths() {
	// If the lengths of the slices are unknown,
	// use EqualLengths to check
	s1 := []float64{1, 2, 3}
	s2 := []float64{5, 6, 7, 8}

	eq := floats.EqualLengths(s1, s2)
	if eq {
		floats.Add(s1, s2)
	} else {
		fmt.Println("Unequal lengths")
	}

	// Output:
	// Unequal lengths
}

func ExampleAddConst() {
	s := []float64{1, -2, 3, -4}
	c := 5.0

	floats.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))

	floats.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))

	floats.CumSum(dst, s)

	fmt.Println("dst =", dst)
	fmt.Println("s =", s)

	// Output:
	// dst = [1 -1 2 -2]
	// s = [1 -2 3 -4]
}