All Downloads are FREE. Search and download functionalities are using the official Maven repository.

breeze.linalg.functions.accumulate.scala Maven / Gradle / Ivy

There is a newer version: 1.0
Show newest version
package breeze.linalg

import breeze.generic.UFunc
import breeze.macros.expand

/**
* Returns a cumulative sum of the vector (ie cumsum).
* @author ktakagaki
*/
object accumulate extends UFunc {

  @expand
  @expand.valify
  implicit def dvAccumulate[@expand.args(Int, Long, Float, Double) T]: Impl[DenseVector[T], DenseVector[T]] =
    new Impl[DenseVector[T], DenseVector[T]] {
      def apply(dv: DenseVector[T]): DenseVector[T] = {
        DenseVector( cumImpl(List[T]( 0.asInstanceOf[T] ), dv.toArray.toList).reverse.drop(1).toArray )
      }
      def cumImpl(listNew: List[T], listOld: List[T]): List[T] = {
        if(listOld == Nil) listNew
        else cumImpl( (listOld.head + listNew.head) :: listNew, listOld.tail)
      }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy