gorgonia.org/tensor@v0.9.24/example_apply_test.go (about)

     1  package tensor_test
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"gorgonia.org/tensor"
     7  )
     8  
     9  func ExampleDense_Apply() {
    10  	a := tensor.New(tensor.WithShape(2, 2), tensor.WithBacking([]float64{1, 2, 3, 4}))
    11  	cube := func(a float64) float64 { return a * a * a }
    12  
    13  	b, err := a.Apply(cube)
    14  	if err != nil {
    15  		fmt.Printf("b is an error %v", err)
    16  	}
    17  	fmt.Printf("a and b are the same object - %t\n", a.Eq(b))
    18  	fmt.Printf("a is unmutated\n%v\n", a)
    19  
    20  	c, err := a.Apply(cube, tensor.WithReuse(a))
    21  	if err != nil {
    22  		fmt.Printf("c is an error %v\n", err)
    23  	}
    24  	fmt.Printf("a and c are the same object - %t\n", a.Eq(c))
    25  
    26  	fmt.Printf("a is now mutated\n%v\n", a)
    27  	// Output:
    28  	// a and b are the same object - false
    29  	// a is unmutated
    30  	// ⎡1  2⎤
    31  	// ⎣3  4⎦
    32  	//
    33  	// a and c are the same object - true
    34  	// a is now mutated
    35  	// ⎡ 1   8⎤
    36  	// ⎣27  64⎦
    37  }