We help IT Professionals succeed at work.

Complex number matrix

Chitambara Murali Krishna Billakurthi
Chitambara Murali Krishna Billakurthi used Ask the Experts™
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.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
And the question is . . . .
Fabrice LambertConsulting
Distinguished Expert 2017

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{
  Matrix(std::size_t width, std::size_t height, T const& t = T{}) :
    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; }
  std::size_t w;
  std::size_t h;
  std::vector<T> datas;

Open in new window

Top Expert 2016

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.