gorgonia.org/gorgonia@v0.9.17/example_basic_test.go (about) 1 package gorgonia_test 2 3 import ( 4 "fmt" 5 "log" 6 7 . "gorgonia.org/gorgonia" 8 ) 9 10 // Basic example of representing mathematical equations as graphs. 11 // 12 // In this example, we want to represent the following equation 13 // z = x + y 14 func Example_basic() { 15 g := NewGraph() 16 17 var x, y, z *Node 18 var err error 19 20 // define the expression 21 x = NewScalar(g, Float64, WithName("x")) 22 y = NewScalar(g, Float64, WithName("y")) 23 if z, err = Add(x, y); err != nil { 24 log.Fatal(err) 25 } 26 27 // create a VM to run the program on 28 machine := NewTapeMachine(g) 29 defer machine.Close() 30 31 // set initial values then run 32 Let(x, 2.0) 33 Let(y, 2.5) 34 if err = machine.RunAll(); err != nil { 35 log.Fatal(err) 36 } 37 38 fmt.Printf("%v", z.Value()) 39 // Output: 4.5 40 }