Skip to content

@immut.Vector

This page tracks the current repository implementation and is written as the 0.3.0 API baseline.


@immut.Vector[T]

moonbit
struct Vector[T] {
  data : VecCore[T]
} derive(Eq)
  • Description Represents an immutable vector. Internally it is backed by VecCore[T], the package alias for the core persistent vector storage. The public library alias for this abstraction is VecLib[T].

Semantic Notes

  • @immut.Vector uses value semantics consistently: operations such as set, map, left_scale, and right_scale return new values.

  • This package is intended to stay aligned with the shared algebraic subset exposed by @mutable.Vector, while intentionally excluding in-place mutation APIs.

  • The optimized core immutable vector implementation is now part of the library's storage story, not just an internal detail, because it supports the flattened and more uniform execution model across packages.

  • Fields

    • data - The immutable array containing the vector elements.
  • Functions and Methods


    • fn[T] Vector::from_array(arr : Array[T]) -> Vector[T]
      • Description Creates an immutable vector from a mutable array.
      • Parameters
        • arr: Array[T] - The input mutable array.
      • ReturnsVector[T] - An immutable vector containing the elements of the array.

    • fn[T] Vector::make(n : Int, elem : T) -> Vector[T]
      • Description Creates a new vector of length n with all elements initialized to elem.
      • Parameters
        • n: Int - The length of the vector.
        • elem: T - The initial value for all elements.
      • ReturnsVector[T] - The newly created vector.

    • fn[T] Vector::makei(n : Int, f : (Int) -> T) -> Vector[T]
      • Description Creates a new vector of length n where each element is generated by applying the function f to its index.
      • Parameters
        • n: Int - The length of the vector.
        • f: (Int) -> T - The generation function.
      • ReturnsVector[T] - The generated vector.

    • fn[T] length(self : Vector[T]) -> Int
      • Description Returns the number of elements in the vector.

    • fn[T] Vector::op_get(self : Vector[T], i : Int) -> T
      • Description Retrieves the element at the specified index. Supports v[i] syntax.

    • fn[T] set(self : Vector[T], i : Int, x : T) -> Vector[T]
      • Description Returns a new vector with the element at index i replaced by x. The original vector remains unchanged.

    • fn[T, U] map(self : Vector[T], f : (T) -> U) -> Vector[U]
      • Description Creates a new vector by applying function f to each element of the vector.

    • fn[T : Add] add_constant(self : Vector[T], cst : T) -> Vector[T]
      • Description Adds a constant cst to each element of the vector.

    • fn[T, U, V] zip_with(self : Vector[T], other : Vector[U], f : (T, U) -> V) -> Vector[V]
      • Description Combines two vectors of the same length by applying binary function f to corresponding pairs of elements.

    • fn[T : Add] add(self : Vector[T], other : Vector[T]) -> Vector[T]
      • Description Element-wise addition of two vectors. Supports + operator.

    • fn[T : Mul] mul(self : Vector[T], other : Vector[T]) -> Vector[T]
      • Description Element-wise multiplication (Hadamard product). Supports * operator.

    • fn[T : Neg] neg(self : Vector[T]) -> Vector[T]
      • Description Element-wise negation. Supports -v syntax.

    • fn[T : Mul] left_scale(self : Vector[T], scalar : T) -> Vector[T]
      • Description Scales the vector by multiplying each element by a scalar from the left.

    • fn[T : Mul] right_scale(self : Vector[T], scalar : T) -> Vector[T]
      • Description Scales the vector by multiplying each element by a scalar from the right.

    • fn[T : One + Mul + Add + Neg] lerp(self : Vector[T], other : Vector[T], alpha : T) -> Vector[T]
      • Description Performs linear interpolation between two vectors: (1 - alpha) * self + alpha * other.

    • fn[T : Add + Mul] lin_comb(scalar_a : T, self : Vector[T], scalar_b : T, other : Vector[T]) -> Vector[T]
      • Description Computes the linear combination scalar_a * self + scalar_b * other.

    • fn[T] to_col_matrix(self : Vector[T]) -> Matrix[T]
      • Description Converts the vector to a column matrix (n × 1).

    • fn[T] to_row_matrix(self : Vector[T]) -> Matrix[T]
      • Description Converts the vector to a row matrix (1 × n).

    • fn[T : Zero] scaled_matrix(self : Vector[T]) -> Matrix[T]
      • Description Creates a diagonal matrix with diagonal elements from the vector.

    • fn[T : Mul] tensor_product(self : Vector[T], other : Vector[T]) -> Matrix[T]
      • Description Computes the tensor product (outer product) of two vectors.