github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/cmplxs/cscalar/parse_example_test.go (about)

     1  // Copyright ©2017 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 cscalar_test
     6  
     7  import (
     8  	"bufio"
     9  	"fmt"
    10  	"log"
    11  	"strings"
    12  
    13  	"github.com/jingcheng-WU/gonum/cmplxs"
    14  	"github.com/jingcheng-WU/gonum/cmplxs/cscalar"
    15  	"github.com/jingcheng-WU/gonum/floats"
    16  )
    17  
    18  func ExampleParseWithNA() {
    19  	// Calculate the mean of a list of numbers
    20  	// ignoring missing values.
    21  	const data = `6+2i
    22  missing
    23  4-4i
    24  `
    25  
    26  	var (
    27  		vals    []complex128
    28  		weights []float64
    29  	)
    30  	sc := bufio.NewScanner(strings.NewReader(data))
    31  	for sc.Scan() {
    32  		v, w, err := cscalar.ParseWithNA(sc.Text(), "missing")
    33  		if err != nil {
    34  			log.Fatal(err)
    35  		}
    36  		vals = append(vals, v)
    37  		weights = append(weights, w)
    38  	}
    39  	err := sc.Err()
    40  	if err != nil {
    41  		log.Fatal(err)
    42  	}
    43  	fmt.Println(cmplxs.Sum(vals) / complex(floats.Sum(weights), 0))
    44  
    45  	// Output:
    46  	// (5-1i)
    47  }