Skip to content

@mutable.Vector

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


@mutable.Vector[T]

moonbit
pub struct Vector[T](Array[T])

Design & Performance Notes

  • Backend-Specific Optimization: Just like Matrix operations, Vector utilities are optimized for each target backend (Wasm/JS and Native) to ensure maximum speed while keeping the API consistent.

  • Random Access: For random access and modification, directly use the indexing syntax v[i] and v[i] = x. This provides the most efficient individual element interaction.

  • Bulk Operations: Use optimized methods like .map_inplace() or .dot() for aggregate computations instead of manual indexing loops where possible.

  • Description Represents a mutable vector, which is a wrapper around Array[T]. Elements can be accessed and modified via indexing.

Semantic Notes

  • @mutable.Vector is mutation-oriented: indexing writes and *_inplace APIs modify the existing value.

  • The non-inplace algebraic helpers still return fresh vectors so that code can choose between execution-oriented mutation and value-producing transforms explicitly.

  • Functions and Methods


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

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

    • fn[T] Vector::from_array(arr : Array[T]) -> Vector[T]
      • Description Converts an existing array to a 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 Returns the element at the specified index. Supports v[i] syntax.

    • fn[T] Vector::op_set(self : Vector[T], i : Int, x : T) -> Unit
      • Description Sets the value of the element at the specified index. Supports v[i] = x syntax.

    • fn[T] copy(self : Vector[T]) -> Vector[T]
      • Description Creates a deep copy of the vector.

    • fn[T, U] map(self : Vector[T], f : (T) -> U) -> Vector[U]
      • Description Applies a mapping function and returns a new vector.

    • fn[T] map_inplace(self : Vector[T], f : (T) -> T) -> Unit
      • Description Applies a transformation function inplace, modifying the original vector.

    • fn[T : Mul] left_scale(self : Vector[T], scalar : T) -> Vector[T]
      • Description Scales the vector by a scalar from the left (returns a new vector): scalar * x.

    • fn[T : Mul] right_scale(self : Vector[T], scalar : T) -> Vector[T]
      • Description Scales the vector by a scalar from the right (returns a new vector): x * scalar.

    • fn[T : Mul] left_scale_inplace(self : Vector[T], scalar : T) -> Unit
      • Description Scales the vector inplace by a scalar from the left.

    • fn[T : Mul] right_scale_inplace(self : Vector[T], scalar : T) -> Unit
      • Description Scales the vector inplace by a scalar from the right.

    • fn[T : Add + Mul] dot(self : Vector[T], other : Vector[T]) -> T
      • Description Computes the dot product of two vectors.

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

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