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

breeze.linalg.support.CanZipMapValues.scala Maven / Gradle / Ivy

There is a newer version: 1.0
Show newest version
package breeze.linalg.support
/*
 Copyright 2012 David Hall

 Licensed under the Apache License, Version 2.0 (the "License")
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
*/
import breeze.math.Complex
import scala.{specialized => spec}
import scala.reflect.ClassTag

/**
 * Marker for being able to zip two collection objects (From[V]) and map the values to a new collection (To[Vout]).
 *
 * @author dlwh
 */
trait CanZipMapValues[From, @spec(Double, Int, Float, Long) V, @spec(Double, Int, Float, Long) RV, +To] {

  /** Maps all corresponding values from the two collections. */
  def map(from: From, from2: From, fn : (V,V)=>RV): To

}

object CanZipMapValues {

  def canZipMapSelf[S]: CanZipMapValues[S, S, S, S] = new CanZipMapValues[S, S, S, S] {
    /** Maps all corresponding values from the two collections. */
    def map(from: S, from2: S, fn: (S, S) => S): S = fn(from, from2)
  }

  type Op[From, V, RV, To] = CanZipMapValues[From, V, RV, To]

  //
  // Arrays
  //

  class OpArray[@spec(Double, Int, Float, Long) V, @spec(Double, Int, Float, Long) RV: ClassTag]
    extends Op[Array[V], V, RV, Array[RV]] {

    /**Maps all values from the given collection. */
    def map(from: Array[V], from2: Array[V], fn: (V, V) => RV) = {
      require(from.length == from2.length, "Array lengths don't match!")
      val arr = new Array[RV](from.length)
      for(i <- 0 until from.length) {
        arr(i) = fn(from(i), from2(i))
      }
      arr
    }

  }

  // 

  implicit def opArray[@spec V, @spec RV: ClassTag] = new OpArray[V, RV]

  implicit object OpArrayII extends OpArray[Int, Int]

  implicit object OpArraySS extends OpArray[Short, Short]

  implicit object OpArrayLL extends OpArray[Long, Long]

  implicit object OpArrayFF extends OpArray[Float, Float]

  implicit object OpArrayDD extends OpArray[Double, Double]

  implicit object OpArrayCC extends OpArray[Complex, Complex]

  implicit object OpArrayID extends OpArray[Int, Double]

  implicit object OpArraySD extends OpArray[Short, Double]

  implicit object OpArrayLD extends OpArray[Long, Double]

  implicit object OpArrayFD extends OpArray[Float, Double]

  // 

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy