Reputation: 495
I have been using Saddle and noticed that dot product
is not defined between 2 Saddle Frames in BinOpFrame.scala.
I tried to follow examples from BinOpFrame.scala and BinOpVec.scala to write my own implementation as follows
package org.saddle.ops
import scala.{ specialized => spec }
import org.saddle._
import index._
import java.util.Date
trait BinOpFrameCustom {
final class FrFrDot[X: ST: ORD, Y: ST: ORD, @spec(Int, Long, Double) A, @spec(Int, Long, Double) B: ST, @spec(Int, Long, Double) C: ST: NUM](
val opmul: BinOp[Multiply, Frame[X, Y, A], Frame[X, Y, B], Frame[X, Y, C]],
val opadd: BinOp[Add, Series[X, C], Series[X, C], Series[X, C]])
extends BinOp[InnerProd, Frame[X, Y, A], Frame[X, Y, B], Series[X, C]] {
def apply(f1: Frame[X, Y, A], f2: Frame[X, Y, B]): Series[X, C] = {
require(f1.colIx.length == f2.colIx.length, "Frames must have the same number of columns!")
require(f1.rowIx.length == f2.rowIx.length, "Frames must have the same number of rows!")
val (l, r) = f1.align(f2, OuterJoin, OuterJoin)
val mul = opmul(l, r)
val result = mul.toColSeq.foldLeft(Series(Vec(array.empty[C](mul.rowIx.length)), mul.rowIx))((prev, element) => opadd(prev, element._2))
result
}
}
implicit def FrFrDotDDD[X, Y](implicit cm: ST[X], cmp: ORD[X], my: ST[Y], cmpY: ORD[Y], opmul: BinOp[Multiply, Frame[X, Y, Double], Frame[X, Y, Double], Frame[X, Y, Double]], opadd: BinOp[Add, Series[X, Double], Series[X, Double], Series[X, Double]]) = new FrFrDot[X, Y, Double, Double, Double](opmul, opadd)
implicit def FrFrDotDateStringDDD(implicit cm: ST[Date], cmp: ORD[Date], my: ST[String], cmpString: ORD[String], opmul: BinOp[Multiply, Frame[Date, String, Double], Frame[Date, String, Double], Frame[Date, String, Double]], opadd: BinOp[Add, Series[Date, Double], Series[Date, Double], Series[Date, Double]]) = new FrFrDot[Date, String, Double, Double, Double](opmul, opadd)
}
When I try to compute the dot product
of 2 Frame[Date, String, Double]
as
val result = frame1.dot(frame2)
I get the following compilation errors
No BinOp org.saddle.ops.InnerProd instance available to operate on values of type org.saddle.Frame[java.util.Date,String,Double] and org.saddle.Frame[java.util.Date,String,Double]
not enough arguments for method dot: (implicit op: org.saddle.ops.BinOp[org.saddle.ops.InnerProd,org.saddle.Frame[java.util.Date,String,Double],org.saddle.Frame[java.util.Date,String,Double],That])That. Unspecified value parameter op.
I can perform the dot product
of 2 Vectors using the implicit
definition given in BinOpVec.scala and have based my implementation in part on that. I understand that I have a lot going on in my implementation above and might be missing something simple. Can someone tell me what I am missing?
Thanks.
Bonus Question-
I have looked for a non-distributed full-featured Pandas-like library for the JVM for a while and Saddle
is the best I could find. Why isn't there a serious effort to develop a Scala/Java Pandas?
Upvotes: 1
Views: 219