File: example_apply_test.go

package info (click to toggle)
golang-github-gorgonia-tensor 0.9.24-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,696 kB
  • sloc: sh: 18; asm: 18; makefile: 8
file content (37 lines) | stat: -rw-r--r-- 821 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
package tensor_test

import (
	"fmt"

	"gorgonia.org/tensor"
)

func ExampleDense_Apply() {
	a := tensor.New(tensor.WithShape(2, 2), tensor.WithBacking([]float64{1, 2, 3, 4}))
	cube := func(a float64) float64 { return a * a * a }

	b, err := a.Apply(cube)
	if err != nil {
		fmt.Printf("b is an error %v", err)
	}
	fmt.Printf("a and b are the same object - %t\n", a.Eq(b))
	fmt.Printf("a is unmutated\n%v\n", a)

	c, err := a.Apply(cube, tensor.WithReuse(a))
	if err != nil {
		fmt.Printf("c is an error %v\n", err)
	}
	fmt.Printf("a and c are the same object - %t\n", a.Eq(c))

	fmt.Printf("a is now mutated\n%v\n", a)
	// Output:
	// a and b are the same object - false
	// a is unmutated
	// ⎡1  2⎤
	// ⎣3  4⎦
	//
	// a and c are the same object - true
	// a is now mutated
	// ⎡ 1   8⎤
	// ⎣27  64⎦
}