github.com/gonum/lapack@v0.0.0-20181123203213-e4cdc5a0bff9/testlapack/dlaln2.go (about)

     1  // Copyright ©2016 The gonum Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package testlapack
     6  
     7  import (
     8  	"fmt"
     9  	"math"
    10  	"math/cmplx"
    11  	"math/rand"
    12  	"testing"
    13  )
    14  
    15  type Dlaln2er interface {
    16  	Dlaln2(trans bool, na, nw int, smin, ca float64, a []float64, lda int, d1, d2 float64, b []float64, ldb int, wr, wi float64, x []float64, ldx int) (scale, xnorm float64, ok bool)
    17  }
    18  
    19  func Dlaln2Test(t *testing.T, impl Dlaln2er) {
    20  	rnd := rand.New(rand.NewSource(1))
    21  	for _, trans := range []bool{true, false} {
    22  		for _, na := range []int{1, 2} {
    23  			for _, nw := range []int{1, 2} {
    24  				for _, extra := range []int{0, 1, 2, 13} {
    25  					for cas := 0; cas < 1000; cas++ {
    26  						testDlaln2(t, impl, trans, na, nw, extra, rnd)
    27  					}
    28  				}
    29  			}
    30  		}
    31  	}
    32  }
    33  
    34  func testDlaln2(t *testing.T, impl Dlaln2er, trans bool, na, nw, extra int, rnd *rand.Rand) {
    35  	const tol = 1e-12
    36  	const dlamchE = 1.0 / (1 << 53)
    37  	const dlamchP = 2 * dlamchE
    38  
    39  	ca := rnd.NormFloat64()
    40  	d1 := rnd.NormFloat64()
    41  	d2 := rnd.NormFloat64()
    42  
    43  	var w complex128
    44  	if nw == 1 {
    45  		w = complex(rand.NormFloat64(), 0)
    46  	} else {
    47  		w = complex(rand.NormFloat64(), rand.NormFloat64())
    48  	}
    49  	smin := dlamchP * (math.Abs(real(w)) + math.Abs(imag(w)))
    50  
    51  	a := randomGeneral(na, na, na+extra, rnd)
    52  	b := randomGeneral(na, nw, nw+extra, rnd)
    53  	x := randomGeneral(na, nw, nw+extra, rnd)
    54  
    55  	scale, xnormGot, ok := impl.Dlaln2(trans, na, nw, smin, ca, a.Data, a.Stride, d1, d2, b.Data, b.Stride, real(w), imag(w), x.Data, x.Stride)
    56  
    57  	prefix := fmt.Sprintf("Case trans=%v, na=%v, nw=%v, extra=%v", trans, na, nw, extra)
    58  
    59  	if !generalOutsideAllNaN(a) {
    60  		t.Errorf("%v: out-of-range write to A\n%v", prefix, a.Data)
    61  	}
    62  	if !generalOutsideAllNaN(b) {
    63  		t.Errorf("%v: out-of-range write to B\n%v", prefix, b.Data)
    64  	}
    65  	if !generalOutsideAllNaN(x) {
    66  		t.Errorf("%v: out-of-range write to X\n%v", prefix, x.Data)
    67  	}
    68  
    69  	if scale <= 0 || 1 < scale {
    70  		t.Errorf("%v: invalid value of scale=%v", prefix, scale)
    71  	}
    72  
    73  	var xnormWant float64
    74  	for i := 0; i < na; i++ {
    75  		var rowsum float64
    76  		for j := 0; j < nw; j++ {
    77  			rowsum += math.Abs(x.Data[i*x.Stride+j])
    78  		}
    79  		if rowsum > xnormWant {
    80  			xnormWant = rowsum
    81  		}
    82  	}
    83  	if xnormWant != xnormGot {
    84  		t.Errorf("Case %v: unexpected xnorm with scale=%v. Want %v, got %v", prefix, scale, xnormWant, xnormGot)
    85  	}
    86  
    87  	if !ok {
    88  		// If ok is false, the matrix has been perturbed but we don't
    89  		// know how. Return without comparing both sides of the
    90  		// equation.
    91  		return
    92  	}
    93  
    94  	m := make([]complex128, na*na)
    95  	if trans {
    96  		for i := 0; i < na; i++ {
    97  			for j := 0; j < na; j++ {
    98  				m[i*na+j] = complex(ca*a.Data[j*a.Stride+i], 0)
    99  			}
   100  		}
   101  	} else {
   102  		for i := 0; i < na; i++ {
   103  			for j := 0; j < na; j++ {
   104  				m[i*na+j] = complex(ca*a.Data[i*a.Stride+j], 0)
   105  			}
   106  		}
   107  	}
   108  	m[0] -= w * complex(d1, 0)
   109  	if na == 2 {
   110  		m[3] -= w * complex(d2, 0)
   111  	}
   112  
   113  	cx := make([]complex128, na)
   114  	cb := make([]complex128, na)
   115  	switch nw {
   116  	case 1:
   117  		for i := 0; i < na; i++ {
   118  			cx[i] = complex(x.Data[i*x.Stride], 0)
   119  			cb[i] = complex(scale*b.Data[i*x.Stride], 0)
   120  		}
   121  	case 2:
   122  		for i := 0; i < na; i++ {
   123  			cx[i] = complex(x.Data[i*x.Stride], x.Data[i*x.Stride+1])
   124  			cb[i] = complex(scale*b.Data[i*b.Stride], scale*b.Data[i*b.Stride+1])
   125  		}
   126  	}
   127  
   128  	mx := make([]complex128, na)
   129  	for i := 0; i < na; i++ {
   130  		for j := 0; j < na; j++ {
   131  			mx[i] += m[i*na+j] * cx[j]
   132  		}
   133  	}
   134  	for i := 0; i < na; i++ {
   135  		if cmplx.Abs(mx[i]-cb[i]) > tol {
   136  			t.Errorf("Case %v: unexpected value of left-hand side at row %v with scale=%v. Want %v, got %v", prefix, i, scale, cb[i], mx[i])
   137  		}
   138  	}
   139  }