예제 #1
0
  /** 
   * 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)));
  }
예제 #2
0
  /** 
   * 15. 
   * @brief Extract the diagonal elements of a matrix.
   * @param[in] A The (square) matrix whose diagonal is to be extracted.
   * @param[out] B A diagonal matrix containing entries from A.
   */
  static void diag(const matrix_type& A,
                   matrix_type& B) {
    /* create a zeros matrix of the right dimension and assign to B */
    const int n = A.Width();
    B = zeros(n, n);

    /* Set the diagonals of B */
    for (int i=0; i<n; ++i) B.Set(i, i, A.Get(i,i));
  }
예제 #3
0
  /**
   * 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)));
  }
예제 #4
0
  /** 
   * 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;
  }