github.com/gopherd/gonum@v0.0.4/cmplxs/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 cmplxs_test
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"github.com/gopherd/gonum/cmplxs"
    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 := []complex128{1 + 1i, 2 + 2i, 3 + 3i, 4 + 4i}
    19  	s2 := []complex128{5 + 7i, 6 + 7i, 7 + 7i, 8 + 8i}
    20  	s3 := []complex128{1 + 2i, 1 + 2i, 1 + 2i, 1 + 2i}
    21  	cmplxs.Add(s1, s2)
    22  	cmplxs.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+10i) (9+11i) (11+12i) (13+14i)]
    30  	// s2 = [(5+7i) (6+7i) (7+7i) (8+8i)]
    31  	// s3 = [(1+2i) (1+2i) (1+2i) (1+2i)]
    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 := []complex128{1 + 1i, 2 + 2i, 3 + 3i, 4 + 4i}
    38  	s2 := []complex128{5 + 7i, 6 + 7i, 7 + 7i, 8 + 8i}
    39  	s3 := []complex128{1 + 2i, 1 + 2i, 1 + 2i, 1 + 2i}
    40  	dst := make([]complex128, len(s1))
    41  
    42  	cmplxs.AddTo(dst, s1, s2)
    43  	cmplxs.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+10i) (9+11i) (11+12i) (13+14i)]
    52  	// s1 = [(1+1i) (2+2i) (3+3i) (4+4i)]
    53  	// s2 = [(5+7i) (6+7i) (7+7i) (8+8i)]
    54  	// s3 = [(1+2i) (1+2i) (1+2i) (1+2i)]
    55  }
    56  
    57  func ExampleAdd_unequallengths() {
    58  	// If the lengths of the slices are unknown,
    59  	// use EqualLengths to check
    60  	s1 := []complex128{1 + 1i, 2 + 2i, 3 + 3i}
    61  	s2 := []complex128{5 + 5i, 6 + 6i, 7 + 7i, 8 + 8i}
    62  
    63  	eq := cmplxs.EqualLengths(s1, s2)
    64  	if eq {
    65  		cmplxs.Add(s1, s2)
    66  	} else {
    67  		fmt.Println("Unequal lengths")
    68  	}
    69  
    70  	// Output:
    71  	// Unequal lengths
    72  }
    73  
    74  func ExampleAddConst() {
    75  	s := []complex128{1 - 1i, -2 - 1i, 3 - 1i, -4 - 1i}
    76  	c := 5 + 1i
    77  
    78  	cmplxs.AddConst(c, s)
    79  
    80  	fmt.Println("s =", s)
    81  
    82  	// Output:
    83  	// s = [(6+0i) (3+0i) (8+0i) (1+0i)]
    84  }
    85  
    86  func ExampleCumProd() {
    87  	s := []complex128{1 + 1i, -2 - 2i, 3 + 3i, -4 - 4i}
    88  	dst := make([]complex128, len(s))
    89  
    90  	cmplxs.CumProd(dst, s)
    91  
    92  	fmt.Println("dst =", dst)
    93  	fmt.Println("s =", s)
    94  
    95  	// Output:
    96  	// dst = [(1+1i) (0-4i) (12-12i) (-96+0i)]
    97  	// s = [(1+1i) (-2-2i) (3+3i) (-4-4i)]
    98  }
    99  
   100  func ExampleCumSum() {
   101  	s := []complex128{1 + 1i, -2 - 2i, 3 + 3i, -4 - 4i}
   102  	dst := make([]complex128, len(s))
   103  
   104  	cmplxs.CumSum(dst, s)
   105  
   106  	fmt.Println("dst =", dst)
   107  	fmt.Println("s =", s)
   108  
   109  	// Output:
   110  	// dst = [(1+1i) (-1-1i) (2+2i) (-2-2i)]
   111  	// s = [(1+1i) (-2-2i) (3+3i) (-4-4i)]
   112  }