Define a class Mat4 as a vector of four Vec4s?

Hi friends,

Define a class Mat4 as a vector of four Vec4s. Define operator[] to return a Vec4 for Mat4 . Define the usual matrix operations for this type. Define a function doing Gaussian elimination for a Mat4.

An example is the book of Bjarne storustrup
I could not write more.

How I can do?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204

class Vec4{
	vector<floatt> fval;
public:
	Vec4()
	{
		fval.push_back(floatt(rand() % 2 + 2.1f, rand() % 3 + 1.7f,rand() % 5 + 7.3f, rand() % 3 + 4.9f));
	}

	Vec4(floatt f)
	{
		fval.push_back(floatt(f));
	}
	Vec4(const Vec4 &f)
	{
		fval = f.fval;
	}

	Vec4 &operator=(const Vec4 &f)
	{
		fval =  f.fval;
		return *this;
	}


	void display()const
	{
		cout << "("<< fval[0].f[0] << ")" << "(" << fval[0].f[1] << ")" <<  "(" <<fval[0].f[2] << ")"<<"("<<fval[0].f[3]<< ")" << endl;

	}

	Vec4 &operator+(const Vec4 &v)
	{
		Vec4 &result = *this;
		result.fval[0].f[0] += v.fval[0].f[0];
		result.fval[0].f[1] += v.fval[0].f[1];
		result.fval[0].f[2] += v.fval[0].f[2];
		result.fval[0].f[3] += v.fval[0].f[3];
		
		return result;
	}

	Vec4 &operator-(const Vec4 &v)
	{
		Vec4 &result = *this;
		result.fval[0].f[0] -= v.fval[0].f[0];
		result.fval[0].f[1] -= v.fval[0].f[1];
		result.fval[0].f[2] -= v.fval[0].f[2];
		result.fval[0].f[3] -= v.fval[0].f[3];
		
		return result;
	}

	Vec4 &operator/(const Vec4 &v)
	{
		Vec4 &result = *this;
		result.fval[0].f[0] /= v.fval[0].f[0];
		result.fval[0].f[1] /= v.fval[0].f[2];
		result.fval[0].f[2] /= v.fval[0].f[3];
		result.fval[0].f[3] /= v.fval[0].f[4];
		
		return result;
	}

	Vec4 &operator*(const Vec4 &v)
	{
		Vec4 &result = *this;
		result.fval[0].f[0] *= v.fval[0].f[0];
		result.fval[0].f[1] *= v.fval[0].f[1];
		result.fval[0].f[2] *= v.fval[0].f[2];
		result.fval[0].f[3] *= v.fval[0].f[3];
		
		return result;
	}

	
	Vec4 &operator+=(const Vec4 &v)
	{
		this->fval[0].f[0] += v.fval[0].f[0];
		this->fval[0].f[1] += v.fval[0].f[1];
		this->fval[0].f[2] += v.fval[0].f[2];
		this->fval[0].f[3] += v.fval[0].f[3];

		return *this;

	}
	Vec4 &operator-=(const Vec4 &v)
	{
		this->fval[0].f[0] -= v.fval[0].f[0];
		this->fval[0].f[1] -= v.fval[0].f[1];
		this->fval[0].f[2] -= v.fval[0].f[2];
		this->fval[0].f[3] -= v.fval[0].f[3];

		return *this;

	}
	Vec4 &operator*=(const Vec4 &v)
	{
		this->fval[0].f[0] *= v.fval[0].f[0];
		this->fval[0].f[1] *= v.fval[0].f[1];
		this->fval[0].f[2] *= v.fval[0].f[2];
		this->fval[0].f[3] *= v.fval[0].f[3];

		return *this;

	}
	Vec4 &operator/=(const Vec4 &v)
	{
		this->fval[0].f[0] /= v.fval[0].f[0];
		this->fval[0].f[1] /= v.fval[0].f[1];
		this->fval[0].f[2] /= v.fval[0].f[2];
		this->fval[0].f[3] /= v.fval[0].f[3];

		return *this;

	}


	float &operator [] (int index)	
	{ 	

		return  this->fval[0].f[index];
	
	}

	friend ostream &operator<<(ostream &os, Vec4 &v);
	friend istream &operator>>(istream &is, Vec4 &v);



};


ostream &operator<<(ostream &os, Vec4 &v)
{
	return  os << "(" << v.fval[0].f[0] << ")"  << "(" << v.fval[0].f[1] << "(" << v.fval[0].f[2] << ")" << "(" << v.fval[0].f[3] << ")" <<   endl;

}

istream &operator>>(istream &is, Vec4 &v)
{
	return is >>  v.fval[0].f[0] >> v.fval[0].f[1] >> v.fval[0].f[2] >> v.fval[0].f[3];
}


struct Veco{
	Vec4  v[4];
	Veco(Vec4 v1, Vec4 v2, Vec4 v3, Vec4 v4)
	{

		v[0] = v1;
		v[1] = v2;
		v[2] = v3;
		v[3] = v4;
	}

	Veco(const Veco &r)
	{
		v[0] = r.v[0];
		v[1] = r.v[1];
		v[2] = r.v[2];
		v[3] = r.v[3];
	}

	Veco operator=(const Veco &r)
	{	
		v[0] = r.v[0];
		v[1] = r.v[1];
		v[2] = r.v[2];
		v[3] = r.v[3];

		return *this;
	}
};


class Mat4{
	vector<Veco> mval;
public:
	Mat4()
	{
		mval.push_back(Veco(floatt(rand() % 3 + 4.9f, rand() % 3 + 1.9f,rand() % 5 + 7.8f, rand() % 9 + 1.9f),
		floatt(rand() % 4 + 3.8f, rand() % 2 + 2.8f,rand() % 1 + 6.1f, rand() % 5 + 4.9f),
		floatt(rand() % 7 + 2.7f, rand() % 9 + 3.2f,rand() % 3 + 3.4f, rand() % 2 + 4.54f),
		floatt(rand() % 9 + 1.6f, rand() % 8 + 4.1f,rand() % 9 + 1.7f, rand() % 7 + 1.2f)));

	}

	void display()const
	{
	 
		mval[0].v[0].display();
		mval[0].v[1].display();
		mval[0].v[2].display();
		mval[0].v[3].display();

	}

	Vec4 &operator[](int index)
	{
		return this->mval[0].v[index];
	}

};





Last edited on
The declarations and implementations of most of your operators are wrong.

The correct declarations are:
1
2
3
4
    Vec4 operator+( const Vec4& rhs ) const;
    Vec4 operator-( const Vec4& rhs ) const;
    Vec4 operator/( const Vec4& rhs ) const;
    Vec4 operator*( const Vec4& rhs ) const;


However, for doing Gaussian elimination, I don't understand why you need some of them --
namely the / and *. You want to divide and multiply by scalars, not by vectors.


What you do not understand?

everything is very clear:



Define a class Mat4 as a vector of four Vec4s. Define operator[] to return a Vec4 for Mat4 . Define the usual matrix operations for this type. Define a function doing Gaussian elimination for a Mat4.


Written by Bjarne Stroustrup, the creator of C++.
Topic archived. No new replies allowed.