danivdwerf
danivdwerf

Reputation: 274

Matrix multiplication (with different dimensions)

For Math class in school I need to create an application that does something (just anything) with matrices. I decided to create a matrix calculator. I have a Matrix class which contains a 2D array, an row integer and a column integer. I created the following function to multiply two matrices:

public: Matrix* multiply(Matrix* other)
{
  Matrix* temp = new Matrix(other->r, other->c);
  for(int i = 0; i < this->r; i++)
  {
    for(int j = 0; j < this->c; j++)
    {
      for(int k = 0; k < other->c; k++)
        temp->mat[i][j] += this->mat[i][k] * other->mat[k][j];
    }
  }
  return temp;
}

This works perfectly, but only if I multiply matrices with the same dimensions (e.g. Mat4x4*Mat4x4 or Mat2x4*Mat2x4). I understand I can't just multiply an Mat4x4 with an Mat9X2 or anything, but I do know the second matrix's columns should be equal to the first matrix's rows (so a Mat2x2 should be able to multiply with a Mat2x1) and that the answer will have the dimensions of the second matrix. How could (or should) I make the function so it will multiply the matrices with the same and with different dimensions?

Thanks in advance

Upvotes: 0

Views: 2125

Answers (3)

Escualo
Escualo

Reputation: 42072

The following code contains a Matrix class implementation meant to show a few features of C++ (like unique pointers, random numbers, and stream formatting). I often use it when I want to explain a little bit about the language. Maybe it can help you.

#include <cassert>
#include <iostream>
#include <iomanip>
#include <memory>
#include <random>

// Pedagogical implementation of matrix type.
class Matrix {

 public:

  // Create a rows-by-cols matrix filled with random numbers in (-1, 1).
  static Matrix Random(std::size_t rows, std::size_t cols) {

    Matrix m(rows, cols);

    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_real_distribution<double> dis(-1, 1);

    for (std::size_t row = 0; row < rows; ++row) {
      for (std::size_t col = 0; col < cols; ++col) {
        m(row, col) = dis(gen);
      }
    }

    return m;
  }

  // Build an uninitialized rows-by-cols matrix.
  Matrix(std::size_t rows, std::size_t cols)
      : m_data { std::make_unique<double[]>(rows * cols) },
        m_rows { rows },
        m_cols { cols }
  {
    assert(m_rows > 0);
    assert(m_cols > 0);
  }

  // Return number of rows
  std::size_t rows() const { return m_rows; }

  // Return number of columns
  std::size_t cols() const { return m_cols; }

  // Value at (row, col)
  double operator()(std::size_t row, std::size_t col) const {
    assert(row < rows());
    assert(col < cols());
    return m_data[row * cols() + col];
  }

  // Reference to value at (row, col)
  double& operator()(std::size_t row, std::size_t col) {
    assert(row < rows());
    assert(col < cols());
    return m_data[row * cols() + col];
  }

  // Matrix multiply
  Matrix operator*(const Matrix& other) const {

    assert(cols() == other.rows());

    Matrix out(rows(), other.cols());

    for (std::size_t i = 0; i < rows(); ++i) {
      for (std::size_t j = 0; j < other.cols(); ++j) {
        double sum { 0 };
        for (std::size_t k = 0; k < cols(); ++k) {
          sum += (*this)(i, k) * other(k, j);
        }
        out(i, j) = sum;
      }
    }
    return out;
  }

 private:

  std::unique_ptr<double[]> m_data; // will cleanup after itself
  const std::size_t m_rows;
  const std::size_t m_cols;
};

// Pretty-print a matrix
std::ostream& operator<<(std::ostream& os, const Matrix& m) {
  os << std::scientific << std::setprecision(16);
  for (std::size_t row = 0; row < m.rows(); ++row) {
    for (std::size_t col = 0; col < m.cols(); ++col) {
      os << std::setw(23) << m(row, col) << " ";
    }
    os << "\n";
  }
  return os;
}

int main() {

  Matrix A = Matrix::Random(3, 4);
  Matrix B = Matrix::Random(4, 2);

  std::cout << "A\n" << A
            << "B\n" << B
            << "A * B\n" << (A * B);
}

Possible output:

$ clang++ matmul.cpp -std=c++17 -Ofast -march=native -Wall -Wextra
$ ./a.out
A
 1.0367049464391398e-01  7.4917987082978588e-03 -2.7966084757805687e-01 -7.2325095373639048e-01 
 2.2478938813996119e-01  8.4194832286446353e-01  5.3602376615184033e-01  7.1132727553003439e-01 
 1.9608747339865196e-01 -6.4829263198209253e-01 -2.7477471919710350e-01  1.2721104074473044e-01 
B
-8.5938605801284385e-01 -6.2981285198013204e-01 
-6.0333085647033191e-01 -6.8234173530317577e-01 
-1.2614486249714407e-01 -3.3875904433100934e-01 
-6.9618174970366520e-01  6.6785401241316045e-01 
A * B
 4.4517888255515814e-01 -4.5869338680118737e-01 
-1.2639839804611623e+00 -4.2259184895688506e-01 
 1.6871952235091500e-01  4.9689953389829533e-01 

Upvotes: 1

danivdwerf
danivdwerf

Reputation: 274

It turnes out the order of the rows and columns got me heckin' bamboozled. The formula was correct. Sorry for unnecessary post.

Upvotes: 0

Jake Freeman
Jake Freeman

Reputation: 1698

A solution for your program would be to make the temp dimensions not the others dimension but this->r, other->c in order to make the dimensions valid with the outputs from the matrix multiplication.

Hope this helps.

Upvotes: 2

Related Questions