@@ -122,28 +122,105 @@ class matrix {
122122 // accessibility operations overload
123123 DATA operator ()(int , int );
124124
125+ // raise each element to an integer power
126+ matrix<DATA> operator ^(int );
127+ // ////////////// OPERATIONS END /////////////////
128+
129+
130+ // / Generate Augmented matrix (vertical stack or horizontal stack) ///
131+ matrix<DATA> hStack (matrix const & ); // horizontal stack - hStack
132+ matrix<DATA> vStack (matrix const & ); // vertical stack - vStack
133+ matrix<DATA> stack (matrix const & obj, bool vert=false ); // generalized stack - stack
134+ // ////////////////////////////////////////////////////////////////////
135+
136+
125137};
126138
127139
128- // // identity matrix
140+ // // STACKING OPERATIONS ////
129141template <typename DATA>
130- matrix<DATA> eye (int n) {
131- matrix<DATA> m (n, n);
142+ matrix<DATA> matrix<DATA>::stack(matrix const & obj, bool vert) {
143+ if (vert)
144+ return this ->vStack (obj);
145+ else
146+ return this ->hStack (obj);
147+ }
148+
149+ template <typename DATA>
150+ matrix<DATA> matrix<DATA>::hStack(matrix const & obj) {
151+ if (this ->row != obj.row )
152+ throw std::invalid_argument (" The row dimensions do not match." );
132153
133- for (int i=0 ; i<n; i++)
134- for (int j=0 ; j<n; j++) {
135- if (i == j) {
136- m.insertAt (1 , i, j);
137- } else {
138- m.insertAt (0 , i, j);
139- }
140- }
154+ // initialize the augmented matrix
155+ matrix<DATA> m (this ->row , this ->col + obj.col );
156+
157+ for (int i=0 ; i<m.row ; i++) {
158+ for (int j=0 ; j<this ->col ; j++)
159+ *(m.val + i*m.col + j) = *(val + (this ->col )*i + j);
160+
161+ for (int j=0 ; j<obj.col ; j++)
162+ *(m.val + i*m.col + (j+this ->col )) = *(obj.val + i*obj.col + j);
163+ }
141164
142165 return m;
143166}
144167
168+ template <typename DATA>
169+ matrix<DATA> matrix<DATA>::vStack(matrix const & obj) {
170+ if (this ->col != obj.col )
171+ throw std::invalid_argument (" The column dimensions do not match." );
172+
173+ // initialize our augmented matrix
174+ matrix<DATA> m (this ->row + obj.row , this ->col );
175+
176+
177+ for (int j=0 ; j<m.col ; j++) {
178+ for (int i=0 ; i<this ->row ; i++)
179+ *(m.val + i*m.col + j) = *(val + i*m.col + j);
180+
181+ for (int i=0 ; i<obj.row ; i++)
182+ *(m.val + (i+this ->row )*m.col + j) = *(obj.val + i*obj.col + j);
183+ }
184+
185+ return m;
186+
187+ }
188+ // // STACKING OPERATIONS END ////
189+
190+
191+
145192// /// MATRIX OPERATIONS DEFINITIONS START HERE ////
146193
194+ template <typename DATA>
195+ matrix<DATA> matrix<DATA>::operator ^(int pow) {
196+ /*
197+ This is not a numerically sound operation.
198+ Use at your own risk.
199+ This is only used for positive integer powers.
200+ Will probably just use math.pow function which is
201+ numerically stable.
202+
203+ NOTE:
204+ Just want to see how this restricted logic works for me.
205+ In case of integer matrices, this is not an issue but when
206+ it would come to dealing with floats using this operation
207+ it might lead to cases where the data is lost.
208+ */
209+ matrix<int > m (this ->row , this ->col );
210+
211+ for (int i=0 ; i<(m.row *m.col ); i++) {
212+ int prod=1 ;
213+
214+ // exponent logic using loop
215+ for (int j=0 ; j<pow; j++)
216+ prod *= *(val + i);
217+
218+ *(m.val + i) = prod;
219+ }
220+
221+ return m;
222+ }
223+
147224template <typename DATA>
148225matrix<DATA> matrix<DATA>::operator *(DATA scalar) {
149226 matrix<DATA> m (this ->row , this ->col );
@@ -281,6 +358,23 @@ void matrix<DATA>::display() {
281358
282359
283360// ///// MAIN FUNCTION AND UTILS/////////
361+ // // identity matrix
362+ template <typename DATA>
363+ matrix<DATA> eye (int n) {
364+ matrix<DATA> m (n, n);
365+
366+ for (int i=0 ; i<n; i++)
367+ for (int j=0 ; j<n; j++) {
368+ if (i == j) {
369+ m.insertAt (1 , i, j);
370+ } else {
371+ m.insertAt (0 , i, j);
372+ }
373+ }
374+
375+ return m;
376+ }
377+
284378
285379void init2dArray (int *array, int size_0, int size_1) {
286380 /*
@@ -362,6 +456,51 @@ int main() {
362456 arr1 = NULL ;
363457 arr2 = NULL ;
364458
459+
460+ // augmentation
461+ cout<<" \n Generating Horinzontal stack matrix:-\n " ;
462+ matrix<int > eye5 = eye<int >(r);
463+
464+ cout<<" matrix m10 | " ;
465+ m10.display ();
466+ cout<<" Identity matrix 5x5 | " ;
467+ eye5.display ();
468+
469+ matrix<int > augmentedMatrix = m10.hStack (eye5);
470+
471+ cout<<" \n Augmented matrix | " ;
472+ augmentedMatrix.display ();
473+
474+
475+ cout<<" \n Generating Horinzontal stack matrix:-\n " ;
476+ matrix<int > eye4 = eye<int >(c);
477+
478+ cout<<" matrix m10 | " ;
479+ m10.display ();
480+
481+ cout<<" Identity matrix 4x4 | " ;
482+ eye4.display ();
483+
484+ matrix<int > augmentedMatrix2 = m10.vStack (eye4);
485+ cout<<" \n Augmented matrix | " ;
486+ augmentedMatrix2.display ();
487+
488+ // exponend
489+ cout<<" \n\n Raise each element to an integer power:-\n " ;
490+
491+ int *arrA = new int [2 *2 ];
492+ init2dArray (arrA, 2 , 2 );
493+ matrix<int > A (arrA, 2 , 2 );
494+ delete arrA;
495+ arrA = NULL ;
496+
497+ cout<<" matrix A | " ;
498+ A.display ();
499+
500+ matrix<int > A3 = A^3 ;
501+ cout<<" \n Resultant cube valued matrix | " ;
502+ A3.display ();
503+
365504 return 0 ;
366505}
367506
0 commit comments