11#include < iostream>
22#include < omp.h>
3+ #include < stdexcept>
34
45using namespace std ;
56
@@ -134,44 +135,40 @@ matrix<DATA> eye(int n) {
134135
135136template <typename DATA>
136137matrix<DATA> matrix<DATA>::operator &(matrix const &obj) {
137- try {
138- if ( this -> col != obj. row )
139- throw (- 1 );
140-
141-
142- int i, j, k;
143- matrix<DATA> m (this ->row , obj.col );
144- for (i=0 ; i<m.row ; i++)
145- for (j=0 ; j<m.col ; j++)
146- *(m.val + i*m.col + j) = (DATA)0 ; // change this later
147- // add funct for it as DEFAULT value as addition inverse of
148- // that data
149-
150-
151- // else perform multiplication in parallel using openmp
152- #pragma omp parallel for private(i,j,k) shared(this->val, obj.val, m)
153- for (i=0 ; i<this ->row ; i++)
154- for (k=0 ; k<obj.row ; k++)
155- for (j=0 ; j<obj.col ; j++)
156- *(m.val + i*m.col + j) += *(val + i*this ->col + k) * *(obj.val + k*obj.col + j);
138+ if ( this -> col != obj. row ) {
139+ throw std::invalid_argument ( " Internal dimensions do not match. " );
140+
141+ }
142+ else {
143+ int i, j, k;
144+ matrix<DATA> m (this ->row , obj.col );
145+ for (i=0 ; i<m.row ; i++)
146+ for (j=0 ; j<m.col ; j++)
147+ *(m.val + i*m.col + j) = (DATA)0 ; // change this later
148+ // add funct for it as DEFAULT value as addition inverse of
149+ // that data
150+
151+
152+ // else perform multiplication in parallel using openmp
153+ #pragma omp parallel for private(i,j,k) shared(this->val, obj.val, m)
154+ for (i=0 ; i<this ->row ; i++)
155+ for (k=0 ; k<obj.row ; k++)
156+ for (j=0 ; j<obj.col ; j++)
157+ *(m.val + i*m.col + j) += *(val + i*this ->col + k) * *(obj.val + k*obj.col + j);
157158
159+
160+ return m;
161+ }
158162
159- return m;
160- } catch (int m) {
161- cout<<" \n Error: Internal dimensions do not match." ;
162- }
163163}
164164
165165template <typename DATA>
166166DATA matrix<DATA>::operator ()(int r, int c) {
167- try {
168- if (r < this ->row && c < this ->col ) {
169- return *(val + r*this ->col + c);
167+
168+ if (r < this ->row && c < this ->col ) {
169+ return *(val + r*this ->col + c);
170170 } else {
171- throw (-1 );
172- }
173- } catch (int m) {
174- cout<<" Exeption: index values exceed the size of the matrix." ;
171+ throw std::invalid_argument (" Indices exceed the dimension size." );
175172 }
176173}
177174
@@ -195,27 +192,23 @@ matrix<DATA> matrix<DATA>::transpose() {
195192
196193template <typename DATA>
197194matrix<DATA> matrix<DATA>::operator +(matrix const & obj) {
198- try {
199- if (this ->row == obj.row && this ->col == obj.col ) {
200- matrix<DATA> m (obj.row , obj.col );
201- // addition and insertion in row major form.
202- for (int i=0 ; i<m.row ; i++)
203- for (int j=0 ; j<m.col ; j++)
204- *(m.val + i*m.col + j) = *(obj.val + i*obj.col + j) + *(val + i*this ->col + j);
195+ if (this ->row == obj.row && this ->col == obj.col ) {
196+ matrix<DATA> m (obj.row , obj.col );
197+ // addition and insertion in row major form.
198+ for (int i=0 ; i<m.row ; i++)
199+ for (int j=0 ; j<m.col ; j++)
200+ *(m.val + i*m.col + j) = *(obj.val + i*obj.col + j) + *(val + i*this ->col + j);
205201
206- return m;
207- } else {
208- throw (-1 );
209- }
210- } catch (int m) {
211- cout<<" \n corresponding dimensions do not match." ;
202+ return m;
203+ } else {
204+ throw std::invalid_argument (" dimensions do not match for addition to be valid." );
212205 }
213206}
214207
215208
216209template <typename DATA>
217210matrix<DATA> matrix<DATA>::operator -(matrix const & obj) {
218- try {
211+
219212 if (this ->row == obj.row && this ->col == obj.col ) {
220213 matrix<DATA> m (obj.row , obj.col );
221214 // subtraction and insertion in row major form.
@@ -225,11 +218,8 @@ matrix<DATA> matrix<DATA>::operator-(matrix const& obj) {
225218
226219 return m;
227220 } else {
228- throw (-1 );
229- }
230- } catch (int m) {
231- cout<<" \n corresponding dimensions do not match." ;
232- }
221+ throw std::invalid_argument (" Dimensions do not match for subtraction to be valid." );
222+ }
233223}
234224
235225
@@ -245,17 +235,11 @@ void matrix<DATA>::insertAll() {
245235
246236template <typename DATA>
247237void matrix<DATA>::insertAt(DATA value, int r, int c) {
248- try {
249238 if ( (r>-1 && r < this ->row ) && (c>-1 && c<this ->col )) {
250239 *(val + (this ->col )*r + c) = value;
251240 } else {
252- throw (-1 );
253- }
254- } catch (int m) {
255- if (-1 ) {
256- cout<<" Wrong index values input." ;
241+ throw std::invalid_argument (" The index values exceed the dimension size of the matrix." );
257242 }
258- }
259243}
260244
261245template <typename DATA>
@@ -292,10 +276,10 @@ int main() {
292276 int *array1 = new int [row*col];
293277 int *array2 = new int [row*col];
294278 init2dArray (array1, row, col); // 2x3 array
295- init2dArray (array2, col, row ); // 3x2 array
279+ init2dArray (array2, row, col ); // 3x2 array
296280
297281 matrix<int > m1 (array1, row, col); // 2x3 matrix
298- matrix<int > m2 (array2, col, row ); // 3x2 matrix
282+ matrix<int > m2 (array2, row, col ); // 3x2 matrix
299283
300284 matrix<int > m3 = m1 & m2;
301285
@@ -304,7 +288,7 @@ int main() {
304288
305289 cout<<" \n Matrix Multiplication Result:-\n " ;
306290 m3.display ();
307-
291+
308292 delete array1;
309293 delete array2;
310294 array1 = NULL ;
0 commit comments