|
2 | 2 | #include<omp.h> |
3 | 3 | #include<stdexcept> |
4 | 4 |
|
| 5 | + |
| 6 | +//extra imports for utility |
| 7 | +#include<random> |
| 8 | + |
5 | 9 | using namespace std; |
6 | 10 |
|
7 | 11 | template<typename DATA> |
8 | 12 | class matrix { |
9 | | - /* |
10 | | - val = flattened 2d array of type DATA in row major form |
11 | | - row = number of rows of the matrix |
12 | | - col = number of columns of the matrix |
13 | | -
|
14 | | - Note: memory is dynamically allocated for the `val` |
15 | | - member variable. It's lifetime is until |
16 | | - the object instance of `matrix` defining it is alive. |
| 13 | + /* |
| 14 | + matrix abstract DS reflects properties and behaviour of a matrix |
| 15 | + |
| 16 | + DATA MEMEBERS:- |
| 17 | + val = flattened 2d array of type DATA in row major form |
| 18 | + row = number of rows of the matrix |
| 19 | + col = number of columns of the matrix |
| 20 | +
|
| 21 | + Note: |
| 22 | + 1. memory is dynamically allocated for the `val` |
| 23 | + member variable. It's lifetime is until |
| 24 | + the object instance of `matrix` defining it is alive. |
| 25 | + 2. This class object makes use of openMP paralleisation to |
| 26 | + take advantage of parallel computing in some operations |
| 27 | + such as matrix multiplication. |
17 | 28 | */ |
18 | 29 | DATA *val; |
19 | 30 | int row, col; |
@@ -240,7 +251,7 @@ matrix<DATA> matrix<DATA>::operator-(matrix const& obj) { |
240 | 251 | template<typename DATA> |
241 | 252 | void matrix<DATA>::insertAll() { |
242 | 253 | int i,j; |
243 | | - cout<<"\nNote: you have to insert "<<this->row*this->col<<" values. Values will be filled row-major wise.\n"; |
| 254 | + cout<<"\nNote: you have to insert "<<this->row*this->col<<" values. Values will be filled row-major wise in a "<<this->row<<'x'<<this->col<<" matrix.\n"; |
244 | 255 | for(i=0; i<this->row; i++) |
245 | 256 | for(j=0; j<this->col; j++) |
246 | 257 | cin>>*(val + (this->col)*i + j); |
@@ -282,38 +293,74 @@ void init2dArray(int *array, int size_0, int size_1) { |
282 | 293 | cin>>*(array + i*size_1 + j); |
283 | 294 | } |
284 | 295 |
|
| 296 | +void init2dRandArray(int *array, int size_0, int size_1) { |
| 297 | + /* |
| 298 | + UTIL FUNCTION |
| 299 | + Flattened 2d array in row major form will be initialised using a |
| 300 | + uniform integer distribution. |
| 301 | + */ |
| 302 | + |
| 303 | + cout<<"\nInitializing our random 2d integer array"; |
| 304 | + std::default_random_engine generator; |
| 305 | + std::uniform_int_distribution<int> distribution(0, 9); |
| 306 | + |
| 307 | + for(int i=0; i<size_0; i++) |
| 308 | + for(int j=0; j<size_1; j++) |
| 309 | + *(array + i*size_1 + j) = distribution(generator); |
| 310 | +} |
| 311 | + |
285 | 312 | int main() { |
286 | 313 |
|
287 | | - int row=2, |
288 | | - col=3, |
289 | | - row2=3, |
290 | | - col2=2; |
| 314 | + // int row=2, |
| 315 | + // col=3, |
| 316 | + // row2=3, |
| 317 | + // col2=2; |
291 | 318 |
|
292 | | - int *array1 = new int[row*col]; |
293 | | - int *array2 = new int[row2*col2]; |
294 | | - init2dArray(array1, row, col); // 2x3 array |
295 | | - init2dArray(array2, row2, col2); // 3x2 array |
| 319 | + // int *array1 = new int[row*col]; |
| 320 | + // int *array2 = new int[row2*col2]; |
| 321 | + // init2dArray(array1, row, col); // 2x3 array |
| 322 | + // init2dArray(array2, row2, col2); // 3x2 array |
296 | 323 |
|
297 | | - matrix<int> m1(array1, row, col); //2x3 matrix |
298 | | - matrix<int> m2(array2, row2, col2); //3x2 matrix |
| 324 | + // matrix<int> m1(array1, row, col); //2x3 matrix |
| 325 | + // matrix<int> m2(array2, row2, col2); //3x2 matrix |
299 | 326 |
|
300 | | - matrix<int> m3 = m1 & m2; |
| 327 | + // matrix<int> m3 = m1 & m2; |
| 328 | + |
| 329 | + // m1.display(); |
| 330 | + // m2.display(); |
| 331 | + |
| 332 | + // cout<<"\nMatrix Multiplication Result:-\n"; |
| 333 | + // m3.display(); |
| 334 | + |
| 335 | + // matrix<int> m4 = m3*2; |
| 336 | + // m4.display(); |
| 337 | + |
| 338 | + // delete array1; |
| 339 | + // delete array2; |
| 340 | + // array1 = NULL; |
| 341 | + // array2 = NULL; |
| 342 | + |
301 | 343 |
|
302 | | - m1.display(); |
303 | | - m2.display(); |
| 344 | + int r = 10, c = 10; |
| 345 | + int *arr1 = new int[r*c], *arr2= new int[r*c]; |
| 346 | + init2dRandArray(arr1, r, c); |
| 347 | + init2dRandArray(arr2, r, c); |
304 | 348 |
|
305 | | - cout<<"\nMatrix Multiplication Result:-\n"; |
306 | | - m3.display(); |
| 349 | + matrix<int> m10(arr1, r, c); |
| 350 | + matrix<int> m11(arr2, r, c); |
| 351 | + matrix<int> m12 = m10 & m11; |
307 | 352 |
|
308 | | - matrix<int> m4 = m3*2; |
309 | | - m4.display(); |
| 353 | + m10.display(); |
| 354 | + m11.display(); |
| 355 | + cout<<"\nMatrix Mul Result:-\n"; |
| 356 | + m12.display(); |
310 | 357 |
|
311 | | - delete array1; |
312 | | - delete array2; |
313 | | - array1 = NULL; |
314 | | - array2 = NULL; |
| 358 | + delete arr1; |
| 359 | + delete arr2; |
| 360 | + arr1 = NULL; |
| 361 | + arr2 = NULL; |
315 | 362 |
|
316 | | - return 1; |
| 363 | + return 0; |
317 | 364 | } |
318 | 365 |
|
319 | 366 |
|
|
0 commit comments