or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

blas.mddistributions.mdindex.mdmatrices.mdutils.mdvectors.md
tile.json

vectors.mddocs/

Vector Operations

Core vector data structures and operations for numerical computing in machine learning applications. Supports both dense and sparse representations with unified API and automatic optimization.

Capabilities

Vector Creation

Factory methods for creating different types of vectors.

object Vectors {
  /**
   * Creates a dense vector from values
   * @param values Array of double values
   * @return Dense vector instance
   */
  def dense(values: Array[Double]): Vector
  
  /**
   * Creates a dense vector from varargs
   * @param firstValue First value
   * @param otherValues Additional values
   * @return Dense vector instance
   */
  def dense(firstValue: Double, otherValues: Double*): Vector
  
  /**
   * Creates a sparse vector from indices and values
   * @param size Vector size
   * @param indices Array of indices (must be strictly increasing)
   * @param values Array of values (same length as indices)
   * @return Sparse vector instance
   */
  def sparse(size: Int, indices: Array[Int], values: Array[Double]): Vector
  
  /**
   * Creates a sparse vector from (index, value) pairs
   * @param size Vector size
   * @param elements Sequence of (index, value) pairs
   * @return Sparse vector instance
   */
  def sparse(size: Int, elements: Seq[(Int, Double)]): Vector
  
  /**
   * Creates a sparse vector from (index, value) pairs (Java-friendly)
   * @param size Vector size
   * @param elements Java iterable of (Integer, Double) pairs
   * @return Sparse vector instance
   */
  def sparse(size: Int, elements: java.lang.Iterable[(java.lang.Integer, java.lang.Double)]): Vector
  
  /**
   * Creates a vector of all zeros
   * @param size Vector size
   * @return Dense zero vector
   */
  def zeros(size: Int): Vector
}

Usage Examples:

import org.apache.spark.ml.linalg._

// Dense vectors
val dense1 = Vectors.dense(Array(1.0, 2.0, 3.0))
val dense2 = Vectors.dense(1.0, 2.0, 3.0)

// Sparse vectors
val sparse1 = Vectors.sparse(5, Array(0, 2, 4), Array(1.0, 3.0, 5.0))
val sparse2 = Vectors.sparse(5, Seq((0, 1.0), (2, 3.0), (4, 5.0)))

// Zero vector
val zeros = Vectors.zeros(10)

Vector Operations

Core operations available on all vector types.

sealed trait Vector extends Serializable {
  /**
   * Size of the vector
   * @return Number of elements
   */
  def size: Int
  
  /**
   * Convert to dense array
   * @return Array containing all vector values
   */
  def toArray: Array[Double]
  
  /**
   * Get value at index
   * @param i Index (0-based)
   * @return Value at index i
   */
  def apply(i: Int): Double
  
  /**
   * Make deep copy
   * @return New vector instance with copied values
   */
  def copy: Vector
  
  /**
   * Dot product with another vector
   * @param v Other vector (must have same size)
   * @return Dot product result
   */
  def dot(v: Vector): Double
  
  /**
   * Number of non-zero elements
   * @return Count of non-zero values
   */
  def numNonzeros: Int
  
  /**
   * Number of active (stored) elements
   * @return Count of stored values
   */
  def numActives: Int
  
  /**
   * Convert to sparse representation
   * @return Sparse vector with explicit zeros removed
   */
  def toSparse: SparseVector
  
  /**
   * Convert to dense representation
   * @return Dense vector with all elements
   */
  def toDense: DenseVector
  
  /**
   * Get vector in most compact format
   * @return Vector in dense or sparse format using less storage
   */
  def compressed: Vector
  
  /**
   * Find index of maximum element
   * @return Index of first maximum element, -1 if empty
   */
  def argmax: Int
  
  /**
   * Apply function to all active elements
   * @param f Function taking (index, value) parameters
   */
  def foreachActive(f: (Int, Double) => Unit): Unit
  
  /**
   * Returns iterator over all elements (including zeros)
   * @return Iterator of (index, value) pairs for all positions
   */
  private[spark] def iterator: Iterator[(Int, Double)]
  
  /**
   * Returns iterator over all active (stored) elements
   * @return Iterator of (index, value) pairs for stored positions
   */
  private[spark] def activeIterator: Iterator[(Int, Double)]
  
  /**
   * Returns iterator over all non-zero elements
   * @return Iterator of (index, value) pairs for non-zero positions
   */
  private[spark] def nonZeroIterator: Iterator[(Int, Double)]
}

Usage Examples:

import org.apache.spark.ml.linalg._

val vec1 = Vectors.dense(1.0, 2.0, 3.0)
val vec2 = Vectors.dense(4.0, 5.0, 6.0)

// Basic operations
println(vec1.size) // 3
println(vec1(1))   // 2.0
val copy = vec1.copy

// Dot product
val dot = vec1.dot(vec2) // 32.0

// Statistics
println(vec1.numNonzeros) // 3
println(vec1.argmax)      // 2

// Format conversion
val sparse = vec1.toSparse
val dense = sparse.toDense
val compressed = sparse.compressed

// Iteration
vec1.foreachActive { (i, v) =>
  println(s"Index $i: value $v")
}

// Iterator access (internal API)
// vec1.activeIterator.foreach { case (i, v) => println(s"Active: $i -> $v") }
// vec1.nonZeroIterator.foreach { case (i, v) => println(s"NonZero: $i -> $v") }

Dense Vectors

Dense vector implementation storing all elements in an array.

class DenseVector(val values: Array[Double]) extends Vector {
  /**
   * The underlying value array
   * @return Array of all vector values
   */
  def values: Array[Double]
}

object DenseVector {
  /**
   * Extract values from dense vector (for pattern matching)
   * @param dv Dense vector instance
   * @return Some(values array) or None
   */
  def unapply(dv: DenseVector): Option[Array[Double]]
}

Sparse Vectors

Sparse vector implementation storing only non-zero elements.

class SparseVector(
  override val size: Int,
  val indices: Array[Int],
  val values: Array[Double]
) extends Vector {
  /**
   * Array of indices of non-zero elements (strictly increasing)
   * @return Index array
   */
  def indices: Array[Int]
  
  /**
   * Array of non-zero values (same length as indices)
   * @return Value array
   */
  def values: Array[Double]
  
  /**
   * Create a slice of this vector based on given indices
   * @param selectedIndices Array of indices to extract
   * @param sorted Whether input indices are already sorted (default: false)
   * @return New SparseVector with values at specified indices
   */
  private[spark] def slice(selectedIndices: Array[Int], sorted: Boolean = false): SparseVector
}

object SparseVector {
  /**
   * Extract components from sparse vector (for pattern matching)
   * @param sv Sparse vector instance
   * @return Some((size, indices, values)) or None
   */
  def unapply(sv: SparseVector): Option[(Int, Array[Int], Array[Double])]
}

Vector Utilities

Additional utility functions for vector operations.

object Vectors {
  /**
   * Compute p-norm of vector
   * @param vector Input vector
   * @param p Norm parameter (must be >= 1.0)
   * @return L^p norm of vector
   */
  def norm(vector: Vector, p: Double): Double
  
  /**
   * Compute squared Euclidean distance between vectors
   * @param v1 First vector
   * @param v2 Second vector (must have same size as v1)
   * @return Squared distance between vectors
   */
  def sqdist(v1: Vector, v2: Vector): Double
}

Usage Examples:

import org.apache.spark.ml.linalg._
import scala.math._

val vec = Vectors.dense(3.0, 4.0, 0.0)

// Norms
val l1Norm = Vectors.norm(vec, 1.0)   // 7.0 (Manhattan norm)
val l2Norm = Vectors.norm(vec, 2.0)   // 5.0 (Euclidean norm)
val infNorm = Vectors.norm(vec, Double.PositiveInfinity) // 4.0 (max norm)

// Distance
val vec2 = Vectors.dense(0.0, 0.0, 0.0)
val distance = Vectors.sqdist(vec, vec2) // 25.0

Type Hierarchy

Vector (sealed trait)
├── DenseVector (class)
└── SparseVector (class)

Performance Notes

  • Dense vectors: Optimal for small to medium vectors or when most elements are non-zero
  • Sparse vectors: Optimal for large vectors with many zero elements
  • Automatic optimization: The compressed method automatically chooses the most efficient representation
  • Native acceleration: Dense operations may use optimized native BLAS when available