score:2
The problem is that the type parameter T
can be anything, but you have to make sure that your type T
supports at least addition as an algebraic operation. If T
is a semiring, then you can add two elements of type T
. You can enforce T
to be a semiring by specifying a context bound:
def vectorSum[T: Semiring](vectors: Seq[Vector[T]]): Vector[T] = {
vectors.reduce(_ + _)
}
That way you enforce that for every instantiation of T
you also have a Semiring[T]
in your scope which defines the addition operation. Breeze already defines this structure for all primitive types which support addition.
If you want to support more algebraic operations such as division, then you should constrain your type variable to have a Field
context bound.
def vectorDiv[T: Field](vectors: Seq[Vector[T]]): Vector[T] = {
vectors.reduce(_ / _)
}
If you want to support general purpose element-wise binary operations on vectors:
def vectorBinaryOp[T](
vectors: Seq[Vector[T]], op: (T, T) => T)(
implicit canZipMapValues: CanZipMapValues[Vector[T], T, T, Vector[T]])
: Vector[T] = {
vectors.reduce{
(left, right) => implicitly[CanZipMapValues[Vector[T], T, T, Vector[T]]].map(left, right, op)
}
}
Then you can define arbitrary binary operations on vectors:
val vectors = Seq(DenseVector(1.0,2.0,3.0,4.0), DenseVector(2.0,3.0,4.0,5.0))
val result = VectorSum.vectorBinaryOp(vectors, (a: Double, b: Double) => (a / b))
Source: stackoverflow.com
Related Query
- Generic Breeze Vector method
- Generic method that take as input Breeze Vector or Breeze Matrix
- Scala generic method - No ClassTag available for T
- scala generic method overriding
- Returning original collection type in generic method
- Why wrapping a generic method call with Option defers ClassCastException?
- How to make method return the same generic as the input?
- Scala: Method overloading over generic types
- Scala: Declaring method with generic type parameter
- Scala: Is it possible to indicate a generic class which implements a certain method
- How to call a generic method with an anonymous type involving generics?
- How do I create an instance of a trait in a generic method in scala?
- Generic method convertible to structural type in scala
- Calling an overloaded java generic method from scala
- Scala: Overwriting a Generic Java Method that returns null
- Generic method with `cannot resolve symbol` errors in Scala
- Generic method to return first of two values
- Scala, can't implement generic java method
- "Missing parameter type" in overloaded generic method taking a function argument
- Calling Java Generic Typed Method from Scala gives a Type mismatch error : Scala
- avoiding cast to Nothing in generic method
- scala: how to create a generic type which is subtype of all the number classes in scala so that it can include compare method
- Generic getter method for tuples in Scala which preserves dynamic type?
- Writing a generic 'fill' method
- Mocking generic scala method in mockito
- using Java generic method from Scala
- Move the implementation of a generic method to an abstract super class
- Generic unapply method for different types of List
- Scala method where type of second parameter equals part of generic type from first parameter
- Implementing a generic Vector in Scala
More Query from same tag
- How to drop malformed rows while reading csv with schema Spark?
- How to check if value of one column is in the array of another column in Apache Spark?
- When typechecking code from within a macro, is it possible to detect a typecheck failure caused by a macro expansion within that code?
- Manually transforming tree recursion into tail recursion using a stack
- How to filter a List with another List based on some conditions?
- Akka. Is there a way to share code between two receives?
- Summary Statistics for string types in spark
- How to express this with jOOQ "Select alias.*, otherAlias.Column From .."
- Akka teskit.spawn to return ActorSystem
- How to use contents = to add to a Scala Panel?
- scala: why does underscore (_) initialization work for fields but not method variables?
- Scala merge between 2 lists base on condition
- How to avoid using null?
- How to pass parameters to an avro deserializer in apache beam (KafkaIO)?
- SBT how to create custom command
- How to change String to Int in Json Parser in scala
- Spark Error : java.lang.RuntimeException: [5.52] failure: ``union'' expected but `('
- spark: how to include dependencies with build/sbt compile
- Get a type identified by a string in the Scala macros
- Scala's generic toOption not working as expected
- Cohesive way to validate a class in Scala using Scalaz 7
- Reconstructing a pattern match so that I can call methods on it?
- On splitting Scala list, clarification needed
- Scala type inference when mixing type member and type parameter
- Spark how to group by arbitrary number of keys?
- How to iterate all mill targets in build.sc, how to get target.dest from outside the target?
- Iso macro in Scala
- Scala - Why does my for loop not return?
- How can I split a list of tuples scala
- Parboiled2 grammar for parsing escaped CSV line