gonum.org/v1/gonum@v0.14.0/num/dualcmplx/dual_simple_example_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 dualcmplx_test
     6  
     7  import (
     8  	"fmt"
     9  	"math"
    10  
    11  	"gonum.org/v1/gonum/num/dualcmplx"
    12  )
    13  
    14  // Example point, displacement and rotation from Euclidean Space Dual Complex Number page:
    15  // http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/other/dualComplex/index.htm
    16  
    17  func Example_displace() {
    18  	// Displace a point [3, 4] by [4, 3].
    19  
    20  	// Point to be transformed in the dual imaginary vector.
    21  	p := dualcmplx.Number{Real: 1, Dual: 3 + 4i}
    22  
    23  	// Displacement vector, half [4, 3], in the dual imaginary vector.
    24  	d := dualcmplx.Number{Real: 1, Dual: 2 + 1.5i}
    25  
    26  	fmt.Printf("%.0f\n", dualcmplx.Mul(dualcmplx.Mul(d, p), dualcmplx.Conj(d)).Dual)
    27  
    28  	// Output:
    29  	//
    30  	// (7+7i)
    31  }
    32  
    33  func Example_rotate() {
    34  	// Rotate a point [3, 4] by 90° around the origin.
    35  
    36  	// Point to be transformed in the dual imaginary vector.
    37  	p := dualcmplx.Number{Real: 1, Dual: 3 + 4i}
    38  
    39  	// Half the rotation in the real complex number.
    40  	r := dualcmplx.Number{Real: complex(math.Cos(math.Pi/4), math.Sin(math.Pi/4))}
    41  
    42  	fmt.Printf("%.0f\n", dualcmplx.Mul(dualcmplx.Mul(r, p), dualcmplx.Conj(r)).Dual)
    43  
    44  	// Output:
    45  	//
    46  	// (-4+3i)
    47  }
    48  
    49  func Example_displaceAndRotate() {
    50  	// Displace a point [3, 4] by [4, 3] and then rotate
    51  	// by 90° around the origin.
    52  
    53  	// Point to be transformed in the dual imaginary vector.
    54  	p := dualcmplx.Number{Real: 1, Dual: 3 + 4i}
    55  
    56  	// Displacement vector, half [4, 3], in the dual imaginary vector.
    57  	d := dualcmplx.Number{Real: 1, Dual: 2 + 1.5i}
    58  
    59  	// Rotation in the real complex number.
    60  	r := dualcmplx.Number{Real: complex(math.Cos(math.Pi/4), math.Sin(math.Pi/4))}
    61  
    62  	// Combine the rotation and displacement so
    63  	// the displacement is performed first.
    64  	q := dualcmplx.Mul(r, d)
    65  
    66  	fmt.Printf("%.0f\n", dualcmplx.Mul(dualcmplx.Mul(q, p), dualcmplx.Conj(q)).Dual)
    67  
    68  	// Output:
    69  	//
    70  	// (-7+7i)
    71  }
    72  
    73  func Example_rotateAndDisplace() {
    74  	// Rotate a point [3, 4] by 90° around the origin and then
    75  	// displace by [4, 3].
    76  
    77  	// Point to be transformed in the dual imaginary vector.
    78  	p := dualcmplx.Number{Real: 1, Dual: 3 + 4i}
    79  
    80  	// Displacement vector, half [4, 3], in the dual imaginary vector.
    81  	d := dualcmplx.Number{Real: 1, Dual: 2 + 1.5i}
    82  
    83  	// Rotation in the real complex number.
    84  	r := dualcmplx.Number{Real: complex(math.Cos(math.Pi/4), math.Sin(math.Pi/4))}
    85  
    86  	// Combine the rotation and displacement so
    87  	// the displacement is performed first.
    88  	q := dualcmplx.Mul(d, r)
    89  
    90  	fmt.Printf("%.0f\n", dualcmplx.Mul(dualcmplx.Mul(q, p), dualcmplx.Conj(q)).Dual)
    91  
    92  	// Output:
    93  	//
    94  	// (0+6i)
    95  }