/** * 17. * @brief Multiply one scalar with matrix. * @param[in] A The matrix. * @param[in] a The scalar. * @param[out] B B is overwritten with (A*B). */ static void multiply(const matrix_type& A, const value_type& a, matrix_type& B) { /** Result matrices should always be of the right size */ B.Resize(A.Height(), A.Width()); /** Get the matrix dimensions. */ const int m = A.Height(); const int n = A.Width(); /** Simple scalar-matrix product. */ for (int i=0; i<m; ++i) for (int j=0; j<n; ++j) B.Set(i, j, (a*A.Get(i, j))); }
/** * 16. * @brief Compute the element-wise product of two matrices. * @param[in] A the first matrix. * @param[in] B the second matrix. * @param[out] C the result, which contains A.*B. */ static void elementwiseProduct(const matrix_type& A, const matrix_type& B, matrix_type& C) { /** Result matrices should always be of the right size */ C.Resize (A.Height(), A.Width()); /* Get the matrix dimensions */ const int m = A.Height(); const int n = A.Width(); /* Simple element-wise product */ for (int i=0; i<m; ++i) for (int j=0; j<n; ++j) C.Set(i, j, (A.Get(i,j)*B.Get(i,j))); }
/** * 13. * @brief Copy the contents of a matrix. * @param[out] A A is overwritten with B. * @param[in] B The matrix to be copied. */ static void copy (matrix_type &A, const matrix_type &B) { /** Result matrices should always be of the right size */ A.Resize (B.Height(), B.Width()); /** This one is pretty simple, but the order is different */ elem::Copy (B, A); }
/** * 6. * @brief Compute the matrix transpose. * @param[in] A The matrix to be transposed. * @param[out] B B is overwritten with A^{T} */ static void transpose (const matrix_type& A, matrix_type& B) { /** Result matrices should always have sufficient space */ B.Resize(A.Width(), A.Height()); /** Compute transpose */ elem::Transpose (A, B); }
/** * 5. * @brief Multiply one matrix with another. * @param[in] A The first matrix * @param[in] B The second matrix * @param[out] C C is overwritten with (A*B) */ static void multiply (const matrix_type& A, const matrix_type& B, matrix_type& C) { /** Result matrices should always have sufficient space */ C.Resize(A.Height(), B.Width()); /** We have to do a Gemm */ elem::Gemm (elem::NORMAL, elem::NORMAL, 1.0, A, B, 0.0, C); }
/** * 8. * @brief Compute the inverse of a matrix. * @param[in] A The (non-singular and square) matrix to be inverted. * @param[out] B B is overwritten with A's inverse. */ static void inv(const matrix_type& A, matrix_type& B) { /** Assume that the matrix has an inverse */ const int n = A.Height(); /** First, copy over an identity as the right hand side */ B = eye(n); /** Solve the linear system using LU */ elem::lu::SolveAfter(elem::NORMAL, A, B); }
/** * 7. * @brief Compute element-wise negation of a matrix. * @param[in] A The matrix to be negated. * @param[out] B B is overwritten with -1.0*A */ static void negation (const matrix_type& A, matrix_type& B) { /** Result matrices should always have sufficient space */ B.Resize(A.Height(), A.Width()); /** Copy over the matrix */ elem::Copy (A, B); /** Multiply by -1.0 */ elem::Scal(-1.0, B); }
/** * 4. * @brief Subtract one matrix from another. * @param[in] A The first matrix * @param[in] B The second matrix * @param[out] C C is overwritten with (A-B) */ static void minus (const matrix_type& A, const matrix_type& B, matrix_type& C) { /** Result matrices should always have sufficient space */ C.Resize(A.Height(), A.Width()); /** first copy the matrix over */ elem::Copy (A, C); /** now, subtract the other matrix in */ elem::Axpy (-1.0, B, C); }
/** * Apply rowwise the sketching transform that is described by the * the transform with output sketch_of_A. */ void apply (const matrix_type& A, output_matrix_type& sketch_of_A, rowwise_tag dimension) const { const value_type *a = A.LockedBuffer(); El::Int lda = A.LDim(); value_type *sa = sketch_of_A.Buffer(); El::Int ldsa = sketch_of_A.LDim(); for (El::Int j = 0; j < data_type::_S; j++) for (El::Int i = 0; i < A.Height(); i++) sa[j * ldsa + i] = a[data_type::_samples[j] * lda + i]; }
void apply_impl_vdist(const matrix_type& A, output_matrix_type& sketch_of_A, skylark::sketch::rowwise_tag tag) const { matrix_type sketch_of_A_STAR_RD(A.Height(), data_type::_S); dense_transform_t<matrix_type, matrix_type, ValuesAccessor> transform(*this); transform.apply(A, sketch_of_A_STAR_RD, tag); sketch_of_A = sketch_of_A_STAR_RD; }
/** * 1. * @brief A function to return the number of rows in a matrix * @param[in] A The matrix for which we need the number of rows. * @return number of rows in A */ static int getNumRows (const matrix_type& A) { return A.Height(); }
/** * 9. * @brief Compute the trace of a matrix. * @param[in] A The (square) matrix whose trace is to be computed. * @return Trace of A */ static value_type trace(const matrix_type& A) { value_type traceVal = 0.0; for (int i=0; i<A.Height(); ++i) traceVal += A.Get(i,i); return traceVal; }