Sort by

recency

|

10 Discussions

|

  • + 0 comments
    A = [[1,2,3],[2,3,4],[1,1,1]]
    B = [[4,5,6],[7,8,9],[4,5,7]]
    
    ROWS_A, COLS_A = len(A), len(A[0])
    ROWS_B, COLS_B = len(B), len(B[0])
    assert COLS_A == ROWS_B
    COMMON_D = ROWS_B
    
    # zeros for Rows-A x Cols-B
    M_TIMES = []
    for r in range(ROWS_A):
        M_TIMES.append( [0] * COLS_B )
    
    # Loop over Rows-A, then Cols-B, then the common dimension
    for rA in range(ROWS_A):
        for cB in range(COLS_B):
            for t in range(COMMON_D):
                M_TIMES[rA][cB] += A[rA][t]*B[t][cB]
            print(M_TIMES[rA][cB])
    
  • + 0 comments

    include

    include

    include

    using namespace std; int minCostMatrixMultiplication(vector& dimensions) { int n = dimensions.size() - 1; vector> dp(n, vector(n, 0)); for (int len = 1; len < n; len++) { for (int i = 0; i < n - len; i++) { int j = i + len; dp[i][j] = INT_MAX; for (int k = i; k < j; k++) { int cost = dp[i][k] + dp[k + 1][j] + dimensions[i] * dimensions[k + 1] * dimensions[j + 1]; dp[i][j] = min(dp[i][j], cost); } } } return dp[0][n - 1]; } int main() { int n; cin >> n; vector dimensions(n + 1); for (int i = 0; i <= n; i++) { cin >> dimensions[i]; } int result = minCostMatrixMultiplication(dimensions); cout<

    return 0;
    

    }

  • + 0 comments

    Boring as hell ...

    Python3

    mmul = lambda m1, m2: [[sum(aij*bjk for aij, bjk in zip(ai, bk))
                            for bk in zip(*m2)] for ai in m1]
    
    a = [[1,2,3],[2,3,4],[1,1,1]]
    b = [[4,5,6],[7,8,9],[4,5,7]]
    
    for row in mmul(a, b):
        for i in row: print(i)
    
  • + 0 comments
    a = [1, 2,3], [2, 3,4],[1,1,1]
    b = [4, 5,6], [7, 8,9],[4,5,7]
    res = 0 
    for i in range(3):
        for j in range(3):
                for k in range(3):
                        res += a[i][k]*b[k][j]           
                print(res)
                res = 0
    
  • + 0 comments
    if __name__ == "__main__":
        M = [[1, 2, 3], [2, 3, 4], [1, 1, 1]]
        N = [[4, 5, 6], [7, 8, 9], [4, 5, 7]]  
        P = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
        
        for i in range(len(M)):
            for j in range(len(N[0])):
                for k in range(len(N)):
                    P[i][j] += M[i][k] * N[k][j]
        for j in P:
            print(*j, sep='\n')
    

    or

    if __name__ == "__main__":
        M = [[1, 2, 3], [2, 3, 4], [1, 1, 1]]
        N = [[4, 5, 6], [7, 8, 9], [4, 5, 7]]
        
        lst = []
        for row in M:
            for i in range(len(row)-2):
                lst += [row[i]*N[0][0] + row[i+1]*N[1][0] + row[i+2]*N[2][0]]
                lst += [row[i]*N[0][1] + row[i+1]*N[1][1] + row[i+2]*N[2][1]]
                lst += [row[i]*N[0][2] + row[i+1]*N[1][2] + row[i+2]*N[2][2]]
        print(*lst, sep='\n')