Class Vector

java.lang.Object
cloud.opencode.base.math.linalg.Vector

public final class Vector extends Object
Immutable mathematical vector backed by a double array. 不可变数学向量,底层使用 double 数组存储。

All operations return new Vector instances; the original is never modified. Thread-safe by virtue of immutability.

所有运算均返回新的 Vector 实例,原始对象不会被修改。 因不可变性而天然线程安全。

Since:
JDK 25, opencode-base-math V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    add(Vector other)
    Returns the element-wise sum of this vector and another.
    double
    angle(Vector other)
    Returns the angle in radians between this vector and another.
    cross(Vector other)
    Returns the cross product of this 3D vector and another 3D vector.
    int
    Returns the dimension (number of components) of this vector.
    double
    Returns the Euclidean distance from this vector to another.
    double
    dot(Vector other)
    Returns the dot product of this vector and another.
    boolean
     
    double
    get(int index)
    Returns the component at the given index.
    int
     
    double
    Returns the Euclidean norm (magnitude) of this vector.
    Returns the negation of this vector (all components multiplied by -1).
    Returns the unit vector in the same direction as this vector.
    static Vector
    of(double... components)
     
    scale(double scalar)
    Returns this vector scaled by the given scalar.
    Returns the element-wise difference of this vector and another.
    double[]
    Returns a defensive copy of the internal components array.
     
    static Vector
    unit(int dimension, int index)
    Creates a unit vector with 1.0 at the given index and 0.0 elsewhere.
    static Vector
    zero(int dimension)
    Creates a zero vector of the given dimension.

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Method Details

    • of

      public static Vector of(double... components)
    • zero

      public static Vector zero(int dimension)
      Creates a zero vector of the given dimension. 创建指定维度的零向量。
      Parameters:
      dimension - the number of dimensions / 维度数
      Returns:
      a zero vector / 零向量
      Throws:
      MathException - if dimension is not positive / 如果维度不是正整数
    • unit

      public static Vector unit(int dimension, int index)
      Creates a unit vector with 1.0 at the given index and 0.0 elsewhere. 创建在指定索引处为 1.0、其余为 0.0 的单位向量。
      Parameters:
      dimension - the number of dimensions / 维度数
      index - the index of the 1.0 component (zero-based) / 值为 1.0 的分量索引(从 0 开始)
      Returns:
      a unit vector / 单位向量
      Throws:
      MathException - if dimension is not positive or index is out of bounds / 如果维度不是正整数或索引越界
    • dimension

      public int dimension()
      Returns the dimension (number of components) of this vector. 返回此向量的维度(分量数量)。
      Returns:
      the dimension / 维度
    • get

      public double get(int index)
      Returns the component at the given index. 返回指定索引处的分量。
      Parameters:
      index - the zero-based index / 从 0 开始的索引
      Returns:
      the component value / 分量值
      Throws:
      MathException - if the index is out of bounds / 如果索引越界
    • add

      public Vector add(Vector other)
      Returns the element-wise sum of this vector and another. 返回此向量与另一个向量的逐元素之和。
      Parameters:
      other - the other vector / 另一个向量
      Returns:
      the sum vector / 求和向量
      Throws:
      MathException - if dimensions do not match / 如果维度不匹配
    • subtract

      public Vector subtract(Vector other)
      Returns the element-wise difference of this vector and another. 返回此向量与另一个向量的逐元素之差。
      Parameters:
      other - the other vector / 另一个向量
      Returns:
      the difference vector / 差值向量
      Throws:
      MathException - if dimensions do not match / 如果维度不匹配
    • scale

      public Vector scale(double scalar)
      Returns this vector scaled by the given scalar. 返回此向量乘以给定标量的结果。
      Parameters:
      scalar - the scalar multiplier / 标量乘数
      Returns:
      the scaled vector / 缩放后的向量
    • negate

      public Vector negate()
      Returns the negation of this vector (all components multiplied by -1). 返回此向量的取反(所有分量乘以 -1)。
      Returns:
      the negated vector / 取反后的向量
    • dot

      public double dot(Vector other)
      Returns the dot product of this vector and another. 返回此向量与另一个向量的点积。
      Parameters:
      other - the other vector / 另一个向量
      Returns:
      the dot product / 点积
      Throws:
      MathException - if dimensions do not match / 如果维度不匹配
    • cross

      public Vector cross(Vector other)
      Returns the cross product of this 3D vector and another 3D vector. 返回此三维向量与另一个三维向量的叉积。
      Parameters:
      other - the other 3D vector / 另一个三维向量
      Returns:
      the cross product vector / 叉积向量
      Throws:
      MathException - if either vector is not 3-dimensional / 如果任一向量不是三维的
    • magnitude

      public double magnitude()
      Returns the Euclidean norm (magnitude) of this vector. 返回此向量的欧几里得范数(模长)。
      Returns:
      the magnitude / 模长
    • normalize

      public Vector normalize()
      Returns the unit vector in the same direction as this vector. 返回与此向量同方向的单位向量。
      Returns:
      the normalized vector / 归一化向量
      Throws:
      MathException - if this is a zero vector / 如果此向量为零向量
    • angle

      public double angle(Vector other)
      Returns the angle in radians between this vector and another. 返回此向量与另一个向量之间的弧度角。
      Parameters:
      other - the other vector / 另一个向量
      Returns:
      the angle in radians in [0, PI] / 弧度角,范围 [0, PI]
      Throws:
      MathException - if dimensions do not match or either vector is zero / 如果维度不匹配或任一向量为零向量
    • distanceTo

      public double distanceTo(Vector other)
      Returns the Euclidean distance from this vector to another. 返回此向量到另一个向量的欧几里得距离。
      Parameters:
      other - the other vector / 另一个向量
      Returns:
      the Euclidean distance / 欧几里得距离
      Throws:
      MathException - if dimensions do not match / 如果维度不匹配
    • toArray

      public double[] toArray()
      Returns a defensive copy of the internal components array. 返回内部分量数组的防御性拷贝。
      Returns:
      a copy of the components / 分量的拷贝
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object