Dot product (Prolog)

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

<<dot.pl>>=
dot([], [], 0).
dot([H1|T1], [H2|T2], X) :-
    dot(T1, T2, X0),
    X is H1 * H2 + X0.
Download code
hijacker
hijacker
hijacker
hijacker