github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/num/quat/conj_test.go (about)

     1  // Copyright ©2018 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 quat
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/jingcheng-WU/gonum/floats/scalar"
    11  )
    12  
    13  var invTests = []struct {
    14  	q       Number
    15  	wantNaN bool
    16  }{
    17  	{q: Number{Real: 1, Imag: 1, Jmag: 1, Kmag: 1}},
    18  	{q: Number{Real: 3, Imag: -1, Jmag: 5, Kmag: -40}},
    19  	{q: Number{Real: 1e6, Imag: -1e5, Jmag: 4, Kmag: -10}},
    20  	{q: Number{Real: 0, Imag: 1, Jmag: 1, Kmag: 1}},
    21  	{q: Number{Real: 1, Imag: 0, Jmag: 1, Kmag: 1}},
    22  	{q: Number{Real: 1, Imag: 1, Jmag: 0, Kmag: 1}},
    23  	{q: Number{Real: 1, Imag: 1, Jmag: 1, Kmag: 0}},
    24  	{q: Number{}, wantNaN: true},
    25  }
    26  
    27  func TestInv(t *testing.T) {
    28  	t.Parallel()
    29  	const tol = 1e-14
    30  	for _, test := range invTests {
    31  		got := Mul(test.q, Inv(test.q))
    32  		if test.wantNaN {
    33  			if !IsNaN(got) {
    34  				t.Errorf("unexpected result for Mul(%v, Inv(%[1]v)): got:%v want:%v", test.q, got, NaN())
    35  			}
    36  			continue
    37  		}
    38  		if !(scalar.EqualWithinAbsOrRel(got.Real, 1, tol, tol) && scalar.EqualWithinAbsOrRel(Abs(got), 1, tol, tol)) {
    39  			t.Errorf("unexpected result for Mul(%v, Inv(%[1]v)): got:%v want:%v", test.q, got, Number{Real: 1})
    40  		}
    41  	}
    42  }