gonum.org/v1/gonum@v0.14.0/spatial/r3/vector.go (about)

     1  // Copyright ©2019 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 r3
     6  
     7  import "math"
     8  
     9  // Vec is a 3D vector.
    10  type Vec struct {
    11  	X, Y, Z float64
    12  }
    13  
    14  // Add returns the vector sum of p and q.
    15  func Add(p, q Vec) Vec {
    16  	return Vec{
    17  		X: p.X + q.X,
    18  		Y: p.Y + q.Y,
    19  		Z: p.Z + q.Z,
    20  	}
    21  }
    22  
    23  // Sub returns the vector sum of p and -q.
    24  func Sub(p, q Vec) Vec {
    25  	return Vec{
    26  		X: p.X - q.X,
    27  		Y: p.Y - q.Y,
    28  		Z: p.Z - q.Z,
    29  	}
    30  }
    31  
    32  // Scale returns the vector p scaled by f.
    33  func Scale(f float64, p Vec) Vec {
    34  	return Vec{
    35  		X: f * p.X,
    36  		Y: f * p.Y,
    37  		Z: f * p.Z,
    38  	}
    39  }
    40  
    41  // Dot returns the dot product p·q.
    42  func Dot(p, q Vec) float64 {
    43  	return p.X*q.X + p.Y*q.Y + p.Z*q.Z
    44  }
    45  
    46  // Cross returns the cross product p×q.
    47  func Cross(p, q Vec) Vec {
    48  	return Vec{
    49  		p.Y*q.Z - p.Z*q.Y,
    50  		p.Z*q.X - p.X*q.Z,
    51  		p.X*q.Y - p.Y*q.X,
    52  	}
    53  }
    54  
    55  // Rotate returns a new vector, rotated by alpha around the provided axis.
    56  func Rotate(p Vec, alpha float64, axis Vec) Vec {
    57  	return NewRotation(alpha, axis).Rotate(p)
    58  }
    59  
    60  // Norm returns the Euclidean norm of p
    61  //
    62  //	|p| = sqrt(p_x^2 + p_y^2 + p_z^2).
    63  func Norm(p Vec) float64 {
    64  	return math.Hypot(p.X, math.Hypot(p.Y, p.Z))
    65  }
    66  
    67  // Norm2 returns the Euclidean squared norm of p
    68  //
    69  //	|p|^2 = p_x^2 + p_y^2 + p_z^2.
    70  func Norm2(p Vec) float64 {
    71  	return p.X*p.X + p.Y*p.Y + p.Z*p.Z
    72  }
    73  
    74  // Unit returns the unit vector colinear to p.
    75  // Unit returns {NaN,NaN,NaN} for the zero vector.
    76  func Unit(p Vec) Vec {
    77  	if p.X == 0 && p.Y == 0 && p.Z == 0 {
    78  		return Vec{X: math.NaN(), Y: math.NaN(), Z: math.NaN()}
    79  	}
    80  	return Scale(1/Norm(p), p)
    81  }
    82  
    83  // Cos returns the cosine of the opening angle between p and q.
    84  func Cos(p, q Vec) float64 {
    85  	return Dot(p, q) / (Norm(p) * Norm(q))
    86  }
    87  
    88  // Divergence returns the divergence of the vector field at the point p,
    89  // approximated using finite differences with the given step sizes.
    90  func Divergence(p, step Vec, field func(Vec) Vec) float64 {
    91  	sx := Vec{X: step.X}
    92  	divx := (field(Add(p, sx)).X - field(Sub(p, sx)).X) / step.X
    93  	sy := Vec{Y: step.Y}
    94  	divy := (field(Add(p, sy)).Y - field(Sub(p, sy)).Y) / step.Y
    95  	sz := Vec{Z: step.Z}
    96  	divz := (field(Add(p, sz)).Z - field(Sub(p, sz)).Z) / step.Z
    97  	return 0.5 * (divx + divy + divz)
    98  }
    99  
   100  // Gradient returns the gradient of the scalar field at the point p,
   101  // approximated using finite differences with the given step sizes.
   102  func Gradient(p, step Vec, field func(Vec) float64) Vec {
   103  	dx := Vec{X: step.X}
   104  	dy := Vec{Y: step.Y}
   105  	dz := Vec{Z: step.Z}
   106  	return Vec{
   107  		X: (field(Add(p, dx)) - field(Sub(p, dx))) / (2 * step.X),
   108  		Y: (field(Add(p, dy)) - field(Sub(p, dy))) / (2 * step.Y),
   109  		Z: (field(Add(p, dz)) - field(Sub(p, dz))) / (2 * step.Z),
   110  	}
   111  }
   112  
   113  // minElem return a vector with the minimum components of two vectors.
   114  func minElem(a, b Vec) Vec {
   115  	return Vec{
   116  		X: math.Min(a.X, b.X),
   117  		Y: math.Min(a.Y, b.Y),
   118  		Z: math.Min(a.Z, b.Z),
   119  	}
   120  }
   121  
   122  // maxElem return a vector with the maximum components of two vectors.
   123  func maxElem(a, b Vec) Vec {
   124  	return Vec{
   125  		X: math.Max(a.X, b.X),
   126  		Y: math.Max(a.Y, b.Y),
   127  		Z: math.Max(a.Z, b.Z),
   128  	}
   129  }
   130  
   131  // absElem returns the vector with components set to their absolute value.
   132  func absElem(a Vec) Vec {
   133  	return Vec{
   134  		X: math.Abs(a.X),
   135  		Y: math.Abs(a.Y),
   136  		Z: math.Abs(a.Z),
   137  	}
   138  }
   139  
   140  // mulElem returns the Hadamard product between vectors a and b.
   141  //
   142  //	v = {a.X*b.X, a.Y*b.Y, a.Z*b.Z}
   143  func mulElem(a, b Vec) Vec {
   144  	return Vec{
   145  		X: a.X * b.X,
   146  		Y: a.Y * b.Y,
   147  		Z: a.Z * b.Z,
   148  	}
   149  }
   150  
   151  // divElem returns the Hadamard product between vector a
   152  // and the inverse components of vector b.
   153  //
   154  //	v = {a.X/b.X, a.Y/b.Y, a.Z/b.Z}
   155  func divElem(a, b Vec) Vec {
   156  	return Vec{
   157  		X: a.X / b.X,
   158  		Y: a.Y / b.Y,
   159  		Z: a.Z / b.Z,
   160  	}
   161  }