Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 7503

C/C++ • Matrix class for C++

$
0
0
I do not actually need it myself but as an interesting exercise I have started to develop a matrix class. At the moment it supports multiplication with vectors or another matrix. It should work with int, double, complex etc. but so far has had minimal testing.
Is there any interest in such a class? If so what other overloads would be useful? Comments welcome.
This is the header file

Code:

//rw_matrix#include <vector>#include <stdexcept>namespace rw{/*    matrix class    elements accessed as m[row][column]    overloads for m*m, m*v and v*m where m is matrix and v is vector    constructor takes rows,columns*/template <typename T>class matrix{public:    matrix(std::size_t r,std::size_t c):        m(std::vector<std::vector<T>>(r,std::vector<T>(c,0))),        rows(r),cols(c){}    matrix() = delete;    std::vector<T>& operator[](std::size_t x){return m[x];}    std::size_t get_rows(){return rows;}    std::size_t get_cols(){return cols;}    // matrix*vector    std::vector<T> operator*(const std::vector<T>& r)    {        if(r.size() != cols)            throw std::invalid_argument("Sizes do not match");        std::vector<T> ret(rows);                for(std::size_t i = 0; i < rows; i++)            for(std::size_t j = 0; j < cols; j++)                ret[i]  += m[i][j] * r[j];        return ret;    }    // matrix*matrix    matrix<T> operator*(matrix<T>& r)    {        if((cols != r.rows) || (rows != r.cols))            throw std::invalid_argument("Sizes do not match");        matrix ret(rows,cols);        for(std::size_t i = 0; i < cols; i++)            for(std::size_t j = 0; j < rows; j++)                ret[j][i] += m[j][i] * r[i][j];        return ret;    }private:    std::vector<std::vector<T>> m;    std::size_t rows;    std::size_t cols;};// vector*matrixtemplate <typename T>std::vector<T> operator*(const std::vector<T>& l, matrix<T>& r){    if(l.size() != r.get_rows())        throw std::invalid_argument("Sizes do not match");    std::vector<T> ret(r.get_cols());    for(std::size_t i = 0; i < r.get_cols(); i++)        for(std::size_t j = 0; j < l.size(); j++)            ret[i] += r[j][i] * l[j];        return ret;}} //namespace rw
and this is my simple test program

Code:

//test.cpp#include <iostream>#include <complex>#include "rw_matrix"//using t = std::complex<double>;using t = int;int main(){    std::cout << "Matrix test\n";    rw::matrix<t> m1(2,3);    m1[0] = {0,1,2};    m1[1] = {3,4,5};    for (int i = 0; i < 2 ; i++)    {        for(int j = 0; j < 3; j++)            std::cout << m1[i][j] << ",";        std::cout << "\n";    }    std::cout << "test m*v\n";    std::vector<t> x1 = {1,2,3};    auto y1 = m1*x1;    for(auto i : y1)        std::cout << i << ",";    std::cout << "\n";    std::cout << "test v*m \n";    std::vector<t> x2 = {1,2};    auto y2 = x2*m1;    for(auto i : y2)        std::cout << i << ",";    std::cout << "\n";    std::cout << "test m*m\n";    rw::matrix<t> m2(3,2);    m2[0] = {1,2};    m2[1] = {3,4};    m2[2] = {4,5};    rw::matrix<t> x = m1*m2;    for (std::size_t i = 0; i < x.get_rows() ; i++)    {        for(std::size_t j = 0; j < x.get_cols(); j++)            std::cout << x[i][j] << ",";        std::cout << "\n";    }    return 0;}

Statistics: Posted by RogerW — Thu Jan 09, 2025 5:37 pm — Replies 0 — Views 29



Viewing all articles
Browse latest Browse all 7503

Trending Articles