Scalar multiplication (Prolog)

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



c * \overrightarrow{u}
=
c
*
\begin{bmatrix} 
u_1 & u_2 & ... & u_n
\end{bmatrix}
=
\begin{bmatrix} 
c * u_1 & c * u_2 & ... & c * u_n
\end{bmatrix}


c * A_{m,n} 
=
c
*
\begin{bmatrix}
  a_{1,1} & a_{1,2} & \cdots & a_{1,n} \\
  a_{2,1} & a_{2,2} & \cdots & a_{2,n} \\
  \vdots  & \vdots  & \ddots & \vdots  \\
  a_{m,1} & a_{m,2} & \cdots & a_{m,n}
\end{bmatrix}
=
\begin{bmatrix}
  c * a_{1,1} & c * a_{1,2} & \cdots & c * a_{1,n} \\
  c * a_{2,1} & c * a_{2,2} & \cdots & c * a_{2,n} \\
  \vdots  & \vdots  & \ddots & \vdots  \\
  c * a_{m,1} & c * a_{m,2} & \cdots & c * a_{m,n}
\end{bmatrix}


[edit] Scalar multiplication over vectors

<<vscale.pl>>=
vscale([], _, []).
vscale([H1|T1], X, [H2|T2]) :-
    H2 is X * H1,
    vscale(T1, X, T2).

[edit] Scalar multiplication over matrices

<<mscale.pl>>=
mscale([], _, []).
mscale([H1|T1], X, [H2|T2]) :-
    vscale(H1, X, H2),
    mscale(T1, X, T2).
Download code
hijacker
hijacker
hijacker
hijacker