Adding vectors (Java)

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

Vector addition


\overrightarrow{u} + \overrightarrow{v}
=
\begin{bmatrix} 
u_1 & u_2 & ... & u_n
\end{bmatrix}
+
\begin{bmatrix} 
v_1 & v_2 & ... & v_n
\end{bmatrix}
=
\begin{bmatrix} 
u_1 + v_1 & u_2 + v_2 & ... & u_n + v_n
\end{bmatrix}

[edit] Iteration

<<vadd_iteration.java>>=
public Double[] vadd(Double[] vector1, Double[] vector2) {
    assert vector1.length == vector2.length;
    Integer length = vector1.length;
    Double[] newVector = new Double[length];
    for (int i = 0; i < length; i++)
        newVector[i] = vector1[i] + vector2[i];
    return newVector;
}
Download code
hijacker
hijacker
hijacker
hijacker