Efficient Vector multiplication in Java

50 views
Skip to first unread message

UNM

unread,
Jun 5, 2017, 1:07:36 PM6/5/17
to Scala Breeze
Hi Team,

My requirement is to multiply two vectors element wise. Here's the code I've written but I am not able to make it work because I don't know what to do with "UFunc.UImpl2" and I am also not sure whether I am using the right function or not. Can any one help please.

Thanks in advance,
UNM

//Original Vector and Weight Vector init
org.apache.spark.ml.linalg.Vector rowVec=Vectors.dense(2.0, 5.0, 10.0);
org.apache.spark.ml.linalg.Vector wtVec=Vectors.dense(1.0, 2.0, 0.5);

//Converting to Breeze
breeze.linalg.Vector brzRowVector= rowVec.asBreeze();
breeze.linalg.Vector brzWeightVector= wtVec.asBreeze();

//Multiply the Original Vector with Weight Vector
org.apache.spark.ml.linalg.Vector updRowVector = (org.apache.spark.ml.linalg.Vector)
brzRowVector.$colon$times(brzWeightVector, new UFunc.UImpl2() {
@Override
public Object apply(Object v, Object o) {
return null;
}
});

//updRowVectorhould return {2.0, 10.0, 5.0}

David Hall

unread,
Jun 5, 2017, 1:12:09 PM6/5/17
to scala-...@googlegroups.com
Breeze really isn't designed to be used from Java. You can do it, but it's not going to be any better than just writing a loop yourself most of the time.

--
You received this message because you are subscribed to the Google Groups "Scala Breeze" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-breeze+unsubscribe@googlegroups.com.
To post to this group, send email to scala-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/scala-breeze/76c8cb8f-53c1-4eb2-a311-35e6b7f434a6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

UNM

unread,
Jun 6, 2017, 3:12:12 AM6/6/17
to Scala Breeze
Thanks David for the reply. But I am wondering how do we do these vector operations  like subtraction, addition etc efficiently on Spark.


On Monday, June 5, 2017 at 10:42:09 PM UTC+5:30, David Hall wrote:
Breeze really isn't designed to be used from Java. You can do it, but it's not going to be any better than just writing a loop yourself most of the time.
On Mon, Jun 5, 2017 at 8:27 AM, UNM <umesh...@gmail.com> wrote:
Hi Team,

My requirement is to multiply two vectors element wise. Here's the code I've written but I am not able to make it work because I don't know what to do with "UFunc.UImpl2" and I am also not sure whether I am using the right function or not. Can any one help please.

Thanks in advance,
UNM

//Original Vector and Weight Vector init
org.apache.spark.ml.linalg.Vector rowVec=Vectors.dense(2.0, 5.0, 10.0);
org.apache.spark.ml.linalg.Vector wtVec=Vectors.dense(1.0, 2.0, 0.5);

//Converting to Breeze
breeze.linalg.Vector brzRowVector= rowVec.asBreeze();
breeze.linalg.Vector brzWeightVector= wtVec.asBreeze();

//Multiply the Original Vector with Weight Vector
org.apache.spark.ml.linalg.Vector updRowVector = (org.apache.spark.ml.linalg.Vector)
brzRowVector.$colon$times(brzWeightVector, new UFunc.UImpl2() {
@Override
public Object apply(Object v, Object o) {
return null;
}
});

//updRowVectorhould return {2.0, 10.0, 5.0}

--
You received this message because you are subscribed to the Google Groups "Scala Breeze" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-breeze...@googlegroups.com.

David Hall

unread,
Jun 6, 2017, 3:49:26 AM6/6/17
to scala-...@googlegroups.com
I really can't help you unless you're using spark from Scala. You might look into mtj which is a Java library that provides similar functionality.

To unsubscribe from this group and stop receiving emails from it, send an email to scala-breeze+unsubscribe@googlegroups.com.

To post to this group, send email to scala-...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages