I'm using the header file below. I'm a little confused about how to store any matrices I create...

If I do:

``````int main()
{
Matrix A(2, 3);
Matrix B(3);

return 0;
}

Matrix::Matrix(int mdim_, int ndim_)
{
data_.resize (mdim_*ndim_);

for (int i = 0; i < mdim_*ndim_; i++)
{
data_[i] = 0;
}
}

Matrix::Matrix(int ndim_)
{
data_.resize (ndim_*ndim_);

for (int i = 0; i < ndim_*ndim_; i++)
{
data_[i] = 0;
}
}``````

Then does this create 2 separate matrices stored in data_? Or will Matrix B just overide the data in Matrix A? And if this is the case how would I get around this? (the header needs to remain unchanged).

``````#include<string>
#include<vector>

class Matrix {
public:

Matrix(int mdim, int ndim);
// Create a zero matrix with mdim rows and ndim columns

Matrix(int ndim);
// Create an zero ndim x ndim matrix

// Add Matrix b to this matrix

void Subtract(Matrix b);
// Subtract b from this matrix

private:
int mdim_; // Number of rows of matrix
int ndim_; // Number of columns of matrix
std::vector<double> data_; // Vector storing the matrix data
};``````

Edited by sexyzebra19: n/a

3
Contributors
6
Replies
7
Views
7 Years
Discussion Span
Last Post by Fbody
``````int main()
{
Matrix A(2, 3);
Matrix B(3);

return 0;
}``````

>> Then does this create 2 separate matrices stored in data_?
Yes it does. You have two distinct objects of type Matrix, they both withhold their own vector, so there is no interference between the two objects.

If you'd have a static vector there, then each instance of a Matrix would operate on the very same vector, so

``````class Matrix {
public:
<snip>

private:
static std::vector<double> data_; // Vector storing the matrix data
};``````

Then you'd be better of passing in the Matrix objects by reference and even by const reference, just to avoid temporary Matrix from being created upon method calls, so rather

``````// if 'b' is not supposed to change -> make it const
// and pass it in by reference, to be more efficient
void Subtract(const Matrix & b);``````

Edited by mitrmkar: a

I'm having a little more trouble...I know this is a stupid question but how do I refer to each individual matrix since both are stored in data_?

For example for the Add function,

data_ = data_ + data_;

how do I refer to each individual matrix since both are stored in data_?

As per the above class declaration ...

``````// Add Matrix b to this matrix
{
data_[ <index here> ] += b.data_[ <same index here> ];
}``````

That's how you access the other object's (b) `data_` member. So the next step would be to put that inside a loop to process all the data_.

Edited by mitrmkar: colour

As per the above class declaration ...

``````// Add Matrix b to this matrix
{
data_[ <index here> ] += b.data_[ <same index here> ];
}``````

That's how you access the other object's (b) `data_` member. So the next step would be to put that inside a loop to process all the data_.

I'm sorry, but I don't see this working, mitrmkar.

Based on what I see, there are 2 separate matrices, each with a private "data_" member, which means it's only accessible internally to b. There would have to be some sort of getElement method that allows you to request a specific element from data_.

i.e.:

``````Matrix::getElement(int elementId) {
return data_[elementId];
}``````

In addition, if data_ were static, the matrices would be stored in the same memory. Wouldn't the net result be 2 identical matrices that modify each other? Also that would cause Add to generate the result `2*data_[i]` for every "i" every time Matrix::Add() was called...

Edited by Fbody: n/a

I'm sorry, but I don't see this working, mitrmkar.

Hmm, it really should work, that's a method were both of the objects are of same class, hence it does not matter whether or not there is access to a private member variable.

Here is a little test program ...

``````class access_test
{
private:

public:

int plus(const access_test & other)
{
// increment this instance's 'answer' by other's
// and return the current value ...
}
};

#include <iostream>
int main()
{
access_test fourty(40);
access_test two(2);

std::cout << fourty.plus(two) << std::endl;

std::cin.get();
}``````

that should output 42 (which is the correct answer)

[EDIT]
Oh, I didn't at first notice what you were saying ...
>> In addition, if data_ were static, the matrices would be stored in the same memory

Yes, that would be quite unsuitable, I sort of tried to point that out there in post #2.

Edited by mitrmkar: edit

Interesting, looks like I have some experimenting to do.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.