Scalar multiplication (Java)

From LiteratePrograms
Jump to: navigation, search
Other implementations: Haskell | Java | 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}


Contents

[edit] Scalar multiplication over vectors

[edit] Iteration

<<scale_vector_iteration.java>>=
public Double[] vscale(Double x, Double[] vector) {
    for (Double element : vector)
        element *= x;
    return vector;
}

[edit] Scalar multiplication over matrices

[edit] Iteration

<<scale_matrix_recursion.java>>=
public Double[][] mscale(Double x, Double[][] matrix) {
    for (Double[] vector : matrix)
        for (Double element : vector)
            element *= x;
    return matrix;
}
<<scale_matrix_recursion_with_vscale.java>>=
public Double[][] mscale(Double x, Double[][] matrix) {
    for (Double[] vector : matrix)
        vscale(x, vector);
    return matrix;
}
Download code
hijacker
hijacker
hijacker
hijacker