Skip to content

Instantly share code, notes, and snippets.

@fancellu
Created December 10, 2015 23:09
Show Gist options
  • Save fancellu/43d0a71cbafc8ac885a7 to your computer and use it in GitHub Desktop.
Save fancellu/43d0a71cbafc8ac885a7 to your computer and use it in GitHub Desktop.
Breeze UFunc example
import com.felstar.breeze.numerics.inc
import com.felstar.breeze.numerics.inc._
object breezeFunc {
inc(55.0) //> res0: Double = 56.0
inc(45) //> res1: Int = 46
inc('a') //> res2: Char = b
val v=linspace(0, 10, 3) //> v : breeze.linalg.DenseVector[Double] = DenseVector(0.0, 5.0, 10.0)
inc(v) //> res3: breeze.linalg.DenseVector[Double] = DenseVector(1.0, 6.0, 11.0)
inc(Array(1.0,2.0,3.0)) //> res4: Array[Double] = Array(2.0, 3.0, 4.0)
inc(List(1.0,2.0,3.0)) //> res5: List[Double] = List(2.0, 3.0, 4.0)
inc(List(1,2,3)) //> res6: List[Int] = List(2, 3, 4)
inc("hello".toCharArray()).mkString //> res7: String = ifmmp
inc("hello") //> res8: String = ifmmp
inc(List("hello","goodbye")) //> res9: List[String] = List(ifmmp, hppeczf)
}
package com.felstar.breeze.numerics
import breeze.generic.UFunc
import breeze.generic.MappingUFunc
object inc extends UFunc with MappingUFunc{
implicit object implDouble extends Impl[Double, Double] {
def apply(a: Double) = a+1
}
implicit object implInt extends Impl[Int, Int] {
def apply(a: Int) = a+1
}
implicit object implChar extends Impl[Char, Char] {
def apply(a: Char) = a.toInt+1 toChar
}
implicit object implString extends Impl[String, String] {
def apply(a: String) = a.map(implChar.apply)
}
implicit def implList[T] =new breeze.linalg.support.CanMapValues[List[T],T,T,List[T]]{
def map(from: List[T],fn: T=> T) = from.map(fn)
def mapActive(from: List[T],fn: T => T) = from.map(fn)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment