You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Was just poking through the code and noticed that the performance of the MatrixMath.dotProduct method isn't as good as it could be for two reasons: it creates copies of arrays unnecessarily using the Matrix.toPackedArray method, and the Matrix.toPackedArray method actually returns arrays of type Double rather than double.
On my machine (Intel i7, Ubuntu 11.04, Sun Java 1.6) the following alternate method runs between 10 and 14 times faster (the double/Double and array copying contribute approximately equally to the performance difference):
publicstaticdoubledotProduct2(finalMatrixa, finalMatrixb) {
if (!a.isVector() || !b.isVector()) {
thrownewMatrixError("To take the dot product, both matrices must be vectors.");
}
finaldouble[][] aArray = a.getData();
finaldouble[][] bArray = b.getData();
finalintaLength = aArray.length == 1 ? aArray[0].length : aArray.length;
finalintbLength = bArray.length == 1 ? bArray[0].length : bArray.length;
if (aLength != bLength) {
thrownewMatrixError("To take the dot product, both matrices must be of the same length.");
}
doubleresult = 0;
if (aArray.length == 1 && bArray.length == 1) {
for (inti = 0; i < aLength; i++) {
result += aArray[0][i] * bArray[0][i];
}
}
elseif (aArray.length == 1 && bArray[0].length == 1) {
for (inti = 0; i < aLength; i++) {
result += aArray[0][i] * bArray[i][0];
}
}
elseif (aArray[0].length == 1 && bArray.length == 1) {
for (inti = 0; i < aLength; i++) {
result += aArray[i][0] * bArray[0][i];
}
}
elseif (aArray[0].length == 1 && bArray[0].length == 1) {
for (inti = 0; i < aLength; i++) {
result += aArray[i][0] * bArray[i][0];
}
}
returnresult;
}
The text was updated successfully, but these errors were encountered:
Perhaps the bigger issue is Matrix.toPackedArray returning a Double[] rather than double[]. It's only called 4 times from within core, and no times in the workbench (didn't check other packages).
Thanks for the code! I will take a look. As to the toPackedArray method, I will look at where it is called from. It used Double, a long time ago, because it was used by the genetic algorithms that placed them into a List. That code has since "evolved" int something better. So I am not sure toPackedArray has any reason to still use Double. I will take a look.
Hi there,
Was just poking through the code and noticed that the performance of the MatrixMath.dotProduct method isn't as good as it could be for two reasons: it creates copies of arrays unnecessarily using the Matrix.toPackedArray method, and the Matrix.toPackedArray method actually returns arrays of type Double rather than double.
On my machine (Intel i7, Ubuntu 11.04, Sun Java 1.6) the following alternate method runs between 10 and 14 times faster (the double/Double and array copying contribute approximately equally to the performance difference):
The text was updated successfully, but these errors were encountered: