+/EE312

software design

git clone https://git.julia.blue/+/EE312

Joseph RyanProject 20211622

master
1.8 KiB59 linesraw
1// matrix-multiplication Project2.cpp
2// EE 312 Project 2 submission by
3// Joseph Ryan
4// jbr2558
5// Slip days used: 0
6
7#include <stdio.h>
8#include <stdint.h>
9#include <stdlib.h>
10#include "MatrixMultiply.h"
11
12void multiplyMatrices(double a[], const uint32_t a_rows, const uint32_t a_cols,
13                      double b[], const uint32_t b_cols, double c[]) {
14    for(uint32_t i = 0; i < a_rows; ++i) {
15        for(uint32_t j = 0; j < b_cols; ++j) {
16            double sum = 0;
17            for(uint32_t k = 0; k < a_cols; ++k) {
18                sum += a[i * a_cols + k] * b[b_cols * k + j];
19            }
20            c[i * b_cols + j] = sum;
21        }
22    }
23}
24
25double** createMatrix(const uint32_t rows, const uint32_t cols) {
26    double** m = (double**)malloc(cols * sizeof(double*));
27    for(uint32_t i = 0; i < cols; ++i) {
28        m[i] = (double*)malloc(rows * sizeof(double));
29    }
30    return m;
31}
32
33double** multiplyMatricesPtr(double** a, const uint32_t a_rows,
34                             const uint32_t a_cols, double** b,
35                             const uint32_t b_cols) {
36    double** result = createMatrix(a_rows, b_cols);
37    for(uint32_t i = 0; i < a_rows; ++i) {
38        for(uint32_t j = 0; j < b_cols; ++j) {
39            double sum = 0;
40            for(uint32_t k = 0; k < a_cols; ++k) {
41                sum += a[i][k] * b[k][j];
42            }
43            result[i][j] = sum;
44        }
45    }
46    return result;
47}
48
49double** transposeMatrix(double** a,
50                         const uint32_t a_rows,
51                         const uint32_t a_cols) {
52    double** result = createMatrix(a_rows, a_cols);
53    for(uint32_t i = 0; i < a_cols; ++i) {
54        for(uint32_t j = 0; j < a_rows; ++j) {
55            result[i][j] = a[j][i];
56        }
57    }
58    return result;
59}