Dot product (Java)

From LiteratePrograms
Jump to: navigation, search
Other implementations: Haskell | Java | Python

Vector dot product


\overrightarrow{u} + \overrightarrow{v}
=
\begin{bmatrix} 
u_1 & u_2 & ... & u_n
\end{bmatrix}
+
\begin{bmatrix} 
v_1 & v_2 & ... & v_n
\end{bmatrix}
=
u_1 * v_1 + u_2 * v_2 + ... + u_n * v_n
=
\sum_{i=1}^n u_i * v_i

[edit] Iteration

<<dot_product_iteration.java>>=
public Double dot(Double[] vector1, Double[] vector2) {
    assert vector1.length == vector2.length;
    Integer length = vector1.length;
    Double result = 0.0;
    for (int i = 0; i < length; i++)
        result += vector1[i] * vector2[i];
    return result;
}
Download code
hijacker
hijacker
hijacker
hijacker