how to write a program of the dot product of vectors in Java ?
Vectors? Quite simple. I haven't coded in Java in a while, so my apologies if I misuse some things.
public int DotProduct(Vector<E> a, Vector<E> b) { if(a.size() != b.size()); // throw an exception of some kind int i; E sum=0; // Edit: this was previously defined as an int, as Thomas Harte pointed out for(i=0; i<a.size; i++) sum += a.get(i) * b.get(i); /* handle multiplication properly, this probably won't work in Java, I don't recall if they allow non-objects in template classes. */ return sum; }
If course this sum will not be ideally suited to all situations if you are using doubles and high precision is required. In that case, I must direct you to the paper Accurate Sum and Dot Product by T. Ogita, et. al., published in the SIAM Journal of Scientific Computing, Vol 26 , No 6, pp. 1955-1988.
Edit: alternatively, I have uploaded a class I wrote four years ago for matrices in Java, and it includes a dot product operation. (Download here.)
If course this sum will not be ideally suited to all situations if you are using doubles and high precision is required
I'm one of the least knowledgeable people in the whole world concerning Java, but assuming C++ semantics hold then your 'sum' variable is declared as int, making it not particularly useful for single precision floats either...
Yikes, you're right. I wasn't thinking. I modified it now so that it most definitely will not work at all. (I made the sum the template type E, which will throw exceptions if it compiles and it used with a class. Were I less lazy I would have fixed it so that the numeric data classes would play nice with it.)
Class DotProductVect {
public static int DotProduct (Vector<E> a, Vector<E> b) {
if (a.size() != b.size()); int i, sum=0;
for (i=0; i<a.size; i++) sum += a.get(i) * b.get(i);
return sum;
}
}
so, is this the final answer?? is there another one which is better?? thank you
Depends on what you mean by "better." The function you have repeated is the definition of the dot product for two vectors. I went ahead and assumed the vectors were stored in Vector objects.
However, if you are holding any finite data type in the vectors, such as doubles, you will experience loss of precision. If your application only requires a small amount of precision (say, 8 base-10 digits) and you aren't doing anything "iffy" which would perform many iterations of dot products to drastically degrade the final value, then you probably don't need to worry about the rounding errors which are compounded in the computation of the dot product.
If however you ARE doing things where you need all 53-bits of the fraction to be correct, then I can post an algorithm which will do just that, assuming no underflow or overflow occur.
go on, admit it Harry, you pinched your code from here
http://forum.java.sun.com/thread.jspa?threadID=5273034&tstart=0
Pretty funny. Had I known it was for a homework assignment I wouldn't have given code.
I didn't catch that in the "stripped" version of my code, he never threw an exception where my comment directed it.