File: dzasum.go

package info (click to toggle)
golang-gonum-v1-gonum 0.15.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,792 kB
  • sloc: asm: 6,252; fortran: 5,271; sh: 377; ruby: 211; makefile: 98
file content (58 lines) | stat: -rw-r--r-- 1,314 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright ©2017 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package testblas

import (
	"testing"

	"golang.org/x/exp/rand"

	"gonum.org/v1/gonum/floats/scalar"
)

type Dzasumer interface {
	Dzasum(n int, x []complex128, incX int) float64
}

func DzasumTest(t *testing.T, impl Dzasumer) {
	const tol = 1e-14
	rnd := rand.New(rand.NewSource(1))
	for _, n := range []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 50, 100} {
		for _, incX := range []int{-5, 1, 2, 10} {
			aincX := abs(incX)
			var x []complex128
			if n > 0 {
				x = make([]complex128, (n-1)*aincX+1)
			}
			for i := range x {
				x[i] = znan
			}
			for i := 0; i < n; i++ {
				re := float64(2*i + 1)
				if rnd.Intn(2) == 0 {
					re *= -1
				}
				im := float64(2 * (i + 1))
				if rnd.Intn(2) == 0 {
					im *= -1
				}
				x[i*aincX] = complex(re, im)
			}

			want := float64(n * (2*n + 1))
			got := impl.Dzasum(n, x, incX)

			if incX < 0 {
				if got != 0 {
					t.Errorf("Case n=%v,incX=%v: non-zero result when incX < 0. got %v", n, incX, got)
				}
				continue
			}
			if !scalar.EqualWithinAbsOrRel(got, want, tol, tol) {
				t.Errorf("Case n=%v,incX=%v: unexpected result. want %v, got %v", n, incX, want, got)
			}
		}
	}
}