Link to home
Start Free TrialLog in
Avatar of Chitambara Murali Krishna Billakurthi
Chitambara Murali Krishna Billakurthi

asked on

Complex number matrix

Hey Guys, I have a doubt in c++ I am trying to develop a complex number matrix (a+bi) which takes 320k values in each row. Initially, we dump zeros into a matrix like this (0+0i).
Then during the execution of the program, we dump the data we have obtained.
Avatar of d-glitch
d-glitch
Flag of United States of America image

And the question is . . . .
Genericity is your friend (AKA template).

Write a Complex class.
Write a generic Matrix class.
Instanciate a Matric<Complex> object.

A Matrix class can start as follow:
#ifndef _MATRIX_T
#define _MATRIX_T
 
#include <vector>
#include <cassert>
 
template<class T>
class Matrix{
public:
  Matrix(std::size_t width, std::size_t height, T const& t = T{}) :
    w{width},
    h{height},
    datas{w*h, t}
  {}
  Matrix(Matrix const& m)                = default;
  Matrix& operator=(Matrix const& m)     = default;
  Matrix(Matrix&& m) noexcept            = default;
  Matrix& operator=(Matrix&& m) noexcept = default;
 
  T const& operator()(std::size_t x, std::size_t y) const{
    assert(x < w && y < h && "Out of bound");
    return datas[y*w+x];
  }
 
  T& operator()(std::size_t x, std::size_t y){
    assert(x < w && y < h && "Out of bound");
    return datas[y*w+x];
  }
 
  std::size_t width() const{ return w; }
  std::size_t height() const{ return h; }
 
private:
  std::size_t w;
  std::size_t h;
  std::vector<T> datas;
};
 
#endif

Open in new window

a complex number is 16 bytes. 320 k per row therefore is 20 k numbers per row. for a nxn matrix it is 400 million numbers and 6.4 GB storage. even for a big server it would give problems to store this into contiguous memory or even do calculations wth a multiple of these matrices.

you could try to use a complex number class (or struct) that only needs 8 byte storage. for example by using float type for a and b. or if the number of decimal places is fixed by using integer type (it giv3s 9 to 10 significant figures both for and b) . even then, you will need some mechanisms to store the whole matrix to hard disc and load only a part of rows and columns for the calculations.

Sara
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.