Skip to content
dlwh edited this page Feb 17, 2013 · 1 revision

UFuncs (Universal Functions) are one of the core concepts in Breeze, borrowed from Numpy. UFuncs are functions which can be applied to scalars, as well as anything that implements the right type class.

For example:

import breeze.numerics._

sqrt(3.0)

log(DenseVector(1.0, 2.0, 3.0, 4.0))
// DenseVector(0.0, 0.6931471805599453, 1.0986122886681098, 1.3862943611198906)

exp(DenseMatrix( (1.0, 2.0), (3.0, 4.0)))

sin(Array(2.0, 3.0, 4.0, 42.0))

All of these work on all of the standard types: scala, DenseVectors, DenseMatrices, SparseVectors, Arrays.

UFuncs are a pretty simple interface:

trait UFunc[-V, +V2] {
  def apply(v: V):V2
  def apply[T,U](t: T)(implicit cmv: CanMapValues[T, V, V2, U]):U = {
    cmv.map(t, apply _)
  }

}

Basically, UFuncs can be applied to their main type as well as to anything that has a CanMapValues implicit, which is just:

trait CanMapValues[T, V, V2, U] {
  def map(from: T, fn: (V) => V2):U
}
Clone this wiki locally