Merge "remove compiler warning" into devel_3.0_main
[platform/framework/native/appfw.git] / src / base / FBaseIntMatrix.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 /**
18  * @file        FBaseIntMatrix.cpp
19  * @brief       This is the implementation for IntMatrix class.
20  */
21
22 #include <FBaseIntMatrix.h>
23 #include <FBaseSysLog.h>
24 #include <unique_ptr.h>
25
26 namespace Tizen { namespace Base
27 {
28
29 IntMatrix::IntMatrix(void)
30         : __pImpl(null)
31         , __pMatrix(null)
32         , __row(0)
33         , __column(0)
34 {
35 }
36
37 IntMatrix::IntMatrix(const IntMatrix& rhs)
38         : __pImpl(null)
39         , __row(rhs.__row)
40         , __column(rhs.__column)
41 {
42         AllocateCapacity(__row, __column);
43         for (int i = 0; i < __row; i++)
44         {
45                 for (int j = 0; j < __column; j++)
46                 {
47                         __pMatrix[i][j] = rhs.__pMatrix[i][j];
48                 }
49         }
50 }
51
52 IntMatrix::IntMatrix(int rowCount, int columnCount)
53         : __pImpl(null)
54         , __row(rowCount)
55         , __column(columnCount)
56 {
57         AllocateCapacity(__row, __column);
58         SetAsNull();
59 }
60
61 IntMatrix::IntMatrix(int rowCount, int columnCount, const int* pArray, bool rowMajor)
62         : __pImpl(null)
63         , __row(rowCount)
64         , __column(columnCount)
65 {
66         AllocateCapacity(__row, __column);
67         if (pArray != null)
68         {
69                 if (rowMajor == true)
70                 {
71                         for (int i = 0; i < __row; i++)
72                         {
73                                 for (int j = 0; j < __column; j++)
74                                 {
75                                         __pMatrix[i][j] = pArray[i * __column + j];
76                                 }
77                         }
78                 }
79                 else
80                 {
81                         for (int i = 0; i < __column; i++)
82                         {
83                                 for (int j = 0; j < __row; j++)
84                                 {
85                                         __pMatrix[j][i] = pArray[i * __row + j];
86                                 }
87                         }
88                 }
89         }
90         else
91         {
92                 SetAsNull();
93         }
94 }
95
96 IntMatrix::IntMatrix(int rowCount, int columnCount, const int* pArray[])
97         : __pImpl(null)
98         , __row(rowCount)
99         , __column(columnCount)
100 {
101         AllocateCapacity(__row, __column);
102         if (pArray != null)
103         {
104                 for (int i = 0; i < __row; i++)
105                 {
106                         for (int j = 0; j < __column; j++)
107                         {
108                                 __pMatrix[i][j] = pArray[i][j];
109                         }
110                 }
111         }
112         else
113         {
114                 SetAsIdentity();
115         }
116 }
117
118 IntMatrix::~IntMatrix(void)
119 {
120         for ( int i = 0 ; i < __row ; i++ )
121         {
122                 delete[] __pMatrix[i];
123         }
124
125         delete[] __pMatrix;
126 }
127
128 bool
129 IntMatrix::operator ==(const IntMatrix& rhs) const
130 {
131         if (this == &rhs)
132         {
133                 return true;
134         }
135
136         if ((__row != rhs.__row) || (__column != rhs.__column))
137         {
138                 return false;
139         }
140
141         for (int i = 0; i < __row; i++)
142         {
143                 for (int j = 0; j < __column; j++)
144                 {
145                         if (__pMatrix[i][j] != rhs.__pMatrix[i][j])
146                         {
147                                 return false;
148                         }
149                 }
150         }
151
152         return true;
153 }
154
155 bool
156 IntMatrix::operator !=(const IntMatrix& rhs) const
157 {
158         return !(*this == rhs);
159 }
160
161 IntMatrix&
162 IntMatrix::operator =(const IntMatrix& rhs)
163 {
164         if (this == &rhs)
165         {
166                 return *this;
167         }
168
169         SysTryReturn(NID_BASE, (__row == rhs.__row) && (__column == rhs.__column), *this, E_INVALID_ARG,
170                 "[%s] Invalid argument is used. Either row or column count of the current instance is not same with that of the specified instance.",
171                 GetErrorMessage(E_INVALID_ARG));
172
173         for (int i = 0; i < __row; i++)
174         {
175                 for (int j = 0; j < __column; j++)
176                 {
177                         __pMatrix[i][j] = rhs.__pMatrix[i][j];
178                 }
179         }
180
181         return *this;
182 }
183
184 bool
185 IntMatrix::Equals(const Tizen::Base::Object& obj) const
186 {
187         const IntMatrix* pOther = dynamic_cast <const IntMatrix*>(&obj);
188
189         if (pOther == null)
190         {
191                 return false;
192         }
193
194         return (*this == *pOther);
195 }
196
197 int
198 IntMatrix::GetHashCode(void) const
199 {
200         int hash = 0;
201         for (int i = 0; i < __row; i++)
202         {
203                 for (int j = 0; j < __column; j++)
204                 {
205                         hash = hash + Tizen::Base::Integer(__pMatrix[i][j]).GetHashCode();
206                 }
207         }
208
209         return hash;
210 }
211
212 result
213 IntMatrix::Add(const IntMatrix& matrix)
214 {
215         SysTryReturnResult(NID_BASE, (__row == matrix.__row) && (__column == matrix.__column), E_INVALID_ARG,
216                 "Either row or column count of the current instance is not same with that of the specified instance.");
217
218         for (int i = 0; i < __row; i++)
219         {
220                 for (int j = 0; j < __column; j++)
221                 {
222                         __pMatrix[i][j] = __pMatrix[i][j] + matrix.__pMatrix[i][j];
223                 }
224         }
225
226         return E_SUCCESS;
227 }
228
229 void
230 IntMatrix::AddToEachElement(int value)
231 {
232         for (int i = 0; i < __row; i++)
233         {
234                 for (int j = 0; j < __column; j++)
235                 {
236                         __pMatrix[i][j] = __pMatrix[i][j] + value;
237                 }
238         }
239 }
240
241 bool
242 IntMatrix::AllocateCapacity(int rowCount, int columnCount)
243 {
244         std::unique_ptr<int* []> pMatrix(new (std::nothrow) int* [rowCount]);
245         SysTryReturn(NID_BASE, pMatrix != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
246                 GetErrorMessage(E_OUT_OF_MEMORY));
247
248         for ( int i = 0 ; i < rowCount ; i++ )
249         {
250                 pMatrix[i] = new (std::nothrow) int[columnCount];
251                 if (pMatrix[i] == null)
252                 {
253                         for (int j = 0; j < i;  j++)
254                         {
255                                 delete[] pMatrix[j];
256                         }
257                         return  false;
258                 }
259         }
260
261         __pMatrix = pMatrix.release();
262
263         return true;
264 }
265
266 int
267 IntMatrix::CalculateDeterminant(int** pMatrix, int order) const  // For perfomance, we have to change the logic of recursive to LU decomposition.
268 {
269         SysTryReturn(NID_BASE, pMatrix != null, 0, E_INVALID_ARG, "[%s] Invalid argument is used. pMatrix is null.",
270                 GetErrorMessage(E_INVALID_ARG));
271
272         if (order == 1)
273         {
274                 return pMatrix[0][0];
275         }
276
277         int determinant = 0;
278         std::unique_ptr<int* []> pMinor(new (std::nothrow) int* [order - 1]);
279         SysTryReturn(NID_BASE, pMinor != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
280                 GetErrorMessage(E_OUT_OF_MEMORY));
281         for (int i = 0; i < order -1; i++)
282         {
283                 pMinor[i] = new (std::nothrow) int[order - 1];
284                 if (pMinor[i] == null)
285                 {
286                         for (int j = 0; j < i;  j++)
287                         {
288                                 delete[] pMinor[j];
289                         }
290                         return  determinant;
291                 }
292         }
293
294         bool signFlag = true;
295         for (int i = 0; i < order; i++)
296         {
297                 GetMinor(pMatrix, pMinor.get(), 0, i, order);
298
299                 if (signFlag == true)
300                 {
301                         determinant += pMatrix[0][i] * CalculateDeterminant(pMinor.get(), order -1);
302                         signFlag = false;
303                 }
304                 else
305                 {
306                         determinant += -pMatrix[0][i] * CalculateDeterminant(pMinor.get(), order -1);
307                         signFlag = true;
308                 }
309         }
310
311         for (int i = 0; i < order -1; i++)
312         {
313                 delete[] pMinor[i];
314         }
315
316         return determinant;
317 }
318
319 int
320 IntMatrix::GetColumnCount(void) const
321 {
322         return __column;
323 }
324
325 int*
326 IntMatrix::GetColumnN(int columnIndex) const
327 {
328         SysTryReturn(NID_BASE, columnIndex <= __column, null, E_INVALID_ARG,
329                 "[%s] Invalid argument is used. The columnIndex is larger than the column count of the current instance.",
330                 GetErrorMessage(E_INVALID_ARG));
331
332         std::unique_ptr<int []> pColumn(new (std::nothrow) int [__row]);
333         SysTryReturn(NID_BASE, pColumn != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
334                 GetErrorMessage(E_OUT_OF_MEMORY));
335
336         for (int i = 0; i < __row; i++)
337         {
338                 pColumn[i] = __pMatrix[i][columnIndex];
339         }
340
341         return pColumn.release();
342 }
343
344 int
345 IntMatrix::GetDeterminant(void) const
346 {
347         SysTryReturn(NID_BASE, __row == __column, 0, E_INVALID_OPERATION,
348                 "[%s] The current instance is not a square matrix.", GetErrorMessage(E_INVALID_OPERATION));
349
350         return CalculateDeterminant(__pMatrix, __row);
351 }
352
353 int
354 IntMatrix::GetElement(int rowIndex, int columnIndex) const
355 {
356         SysTryReturn(NID_BASE, (rowIndex <= __row) && (columnIndex <= __column), 0, E_INVALID_ARG,
357                 "[%s] Invalid argument is used. The current instance is not a square matrix.", GetErrorMessage(E_INVALID_ARG));
358
359         return __pMatrix[rowIndex][columnIndex];
360 }
361
362 IntMatrix*
363 IntMatrix::GetInverseN(void) const
364 {
365         SysTryReturn(NID_BASE, __row == __column, null, E_INVALID_OPERATION,
366                 "[%s] The current instance is not a square matrix.", GetErrorMessage(E_INVALID_OPERATION));
367
368         std::unique_ptr<IntMatrix> pInverseMatrix(new (std::nothrow) IntMatrix(__row, __column));
369         SysTryReturn(NID_BASE, pInverseMatrix != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
370                 GetErrorMessage(E_OUT_OF_MEMORY));
371
372         int determinant = CalculateDeterminant(__pMatrix, __row);
373
374         if (determinant == 0)
375         {
376                 return null;
377         }
378
379         std::unique_ptr<int* []> pMinor(new (std::nothrow) int* [__row - 1]);
380         SysTryReturn(NID_BASE, pMinor != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
381                 GetErrorMessage(E_OUT_OF_MEMORY));
382
383         for (int i = 0; i < __row - 1; i++)
384         {
385                 pMinor[i] = new (std::nothrow) int[__row - 1];
386                 if (pMinor[i] == null)
387                 {
388                         for (int j = 0; j < i;  j++)
389                         {
390                                 delete[] pMinor[j];
391                         }
392                         return null;
393                 }
394         }
395
396         for (int i = 0; i < __row; i++)
397         {
398                 for (int j = 0; j < __row; j++)
399                 {
400                         GetMinor(__pMatrix, pMinor.get(), i, j, __row);
401                         pInverseMatrix->__pMatrix[j][i] = CalculateDeterminant(pMinor.get(), __row - 1) / determinant;
402                         if ((i + j + 2) % 2 == 1)
403                         {
404                                 pInverseMatrix->__pMatrix[j][i] = -pInverseMatrix->__pMatrix[j][i];
405                         }
406                 }
407         }
408
409         for ( int i = 0 ; i < __row - 1 ; i++)
410         {
411                 delete[] pMinor[i];
412         }
413
414         return pInverseMatrix.release();
415 }
416
417 void
418 IntMatrix::GetMinor(int** pSrc, int** pDest, int rowIndex, int columnIndex, int order) const
419 {
420         SysTryReturn(NID_BASE, pSrc != null, , E_INVALID_ARG, "[%s] Invalid argument is used. pSrc is null.",
421                 GetErrorMessage(E_INVALID_ARG));
422         SysTryReturn(NID_BASE, pDest != null, , E_INVALID_ARG, "[%s] Invalid argument is used. pDest is null.",
423                 GetErrorMessage(E_INVALID_ARG));
424
425         int rowCount = 0;
426         int columnCount = 0;
427
428         for (int i = 0; i < order; i++)
429         {
430                 if (i != rowIndex)
431                 {
432                         columnCount = 0;
433
434                         for (int j = 0; j < order; j++)
435                         {
436                                 if (j != columnIndex)
437                                 {
438                                         pDest[rowCount][columnCount] = pSrc[i][j];
439                                         columnCount++;
440                                 }
441                         }
442                         rowCount++;
443                 }
444         }
445 }
446
447 int
448 IntMatrix::GetRowCount(void) const
449 {
450         return __row;
451 }
452
453 int*
454 IntMatrix::GetRowN(int rowIndex) const
455 {
456         SysTryReturn(NID_BASE, rowIndex <= __row, null, E_INVALID_ARG,
457                 "[%s] Invalid argument is used. The rowIndex is larger than the row count of the current instance.",
458                 GetErrorMessage(E_INVALID_ARG));
459
460         std::unique_ptr<int []> pRow(new (std::nothrow) int [__column]);
461         SysTryReturn(NID_BASE, pRow != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
462                 GetErrorMessage(E_OUT_OF_MEMORY));
463
464         for (int i = 0; i < __column; i++)
465         {
466                 pRow[i] = __pMatrix[rowIndex][i];
467         }
468
469         return pRow.release();
470 }
471
472 result
473 IntMatrix::GetTrace(int& value) const
474 {
475         SysTryReturnResult(NID_BASE, __row == __column, E_INVALID_OPERATION, "The current instance is not a square matrix.");
476
477         value = 0;
478
479         for (int i = 0; i < __row; i++)
480         {
481                 value += __pMatrix[i][i];
482         }
483
484         return E_SUCCESS;
485 }
486
487 IntMatrix*
488 IntMatrix::GetTransposeN(void) const
489 {
490         SysTryReturn(NID_BASE, __row == __column, null, E_INVALID_OPERATION,
491                 "[%s] The current instance is not a square matrix.", GetErrorMessage(E_INVALID_OPERATION));
492
493         std::unique_ptr<IntMatrix> pTransposeMatrix(new (std::nothrow) IntMatrix(*this));
494         SysTryReturn(NID_BASE, pTransposeMatrix != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
495                 GetErrorMessage(E_OUT_OF_MEMORY));
496
497         int columnIndex = 1;
498
499         for (int i = 0; i < __row; i++)
500         {
501                 for (int j = 0; j < columnIndex; j++)
502                 {
503                         int temp = pTransposeMatrix->__pMatrix[i][j];
504                         pTransposeMatrix->__pMatrix[i][j] = pTransposeMatrix->__pMatrix[j][i];
505                         pTransposeMatrix->__pMatrix[j][i] = temp;
506                 }
507
508                 columnIndex++;
509         }
510
511         return pTransposeMatrix.release();
512 }
513
514 bool
515 IntMatrix::IsIdentity(void) const
516 {
517         if (__row != __column)
518         {
519                 return false;
520         }
521
522         for (int i = 0; i < __row; i++)
523         {
524                 for (int j = 0; j < __column; j++)
525                 {
526                         if (i == j)
527                         {
528                                 if (__pMatrix[i][j] != 1)
529                                 {
530                                         return false;
531                                 }
532                         }
533                         else
534                         {
535                                 if (__pMatrix[i][j] != 0)
536                                 {
537                                         return false;
538                                 }
539                         }
540                 }
541         }
542         return true;
543 }
544
545 bool
546 IntMatrix::IsInvertible(void) const
547 {
548         int ret = GetDeterminant();
549
550         if (ret == 0)
551         {
552                 return false;
553         }
554         else
555         {
556                 return true;
557         }
558 }
559
560 result
561 IntMatrix::Multiply(const IntMatrix& matrix)
562 {
563         SysTryReturnResult(NID_BASE, __column == matrix.__row, E_INVALID_ARG,
564                 "The column count of the current instance is not same with the row count of the specified instance.");
565
566         std::unique_ptr<int* []> pResult(new (std::nothrow) int* [__row]);
567         SysTryReturnResult(NID_BASE, pResult != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
568
569         for ( int i = 0 ; i < __row ; i++ )
570         {
571                 pResult[i] = new int[matrix.__column];
572                 if (pResult[i] == null)
573                 {
574                         for (int j = 0; j < i;  j++)
575                         {
576                                 delete[] pResult[j];
577                         }
578                         return E_OUT_OF_MEMORY;
579                 }
580         }
581
582         for (int i = 0; i < __row; i++)
583         {
584                 for (int j = 0; j < matrix.__column; j++)
585                 {
586                         pResult[i][j] = 0;
587
588                         for (int k = 0; k < __column; k++)
589                         {
590                                 pResult[i][j] += __pMatrix[i][k] * matrix.__pMatrix[k][j];
591                         }
592                 }
593         }
594
595         for ( int i = 0 ; i < __row ; i++ )
596         {
597                 delete[] __pMatrix[i];
598         }
599
600         delete[] __pMatrix;
601
602         __pMatrix = pResult.release();
603
604         return E_SUCCESS;
605 }
606
607 void
608 IntMatrix::Multiply(int value)
609 {
610         for (int i = 0; i < __row; i++)
611         {
612                 for (int j = 0; j < __column; j++)
613                 {
614                         __pMatrix[i][j] = __pMatrix[i][j] * value;
615                 }
616         }
617 }
618
619 void
620 IntMatrix::Negate(void)
621 {
622         for (int i = 0; i < __row; i++)
623         {
624                 for (int j = 0; j < __column; j++)
625                 {
626                         __pMatrix[i][j] = -__pMatrix[i][j];
627                 }
628         }
629 }
630
631 result
632 IntMatrix::SetAsIdentity(void)
633 {
634         SysTryReturnResult(NID_BASE, __row == __column, E_INVALID_OPERATION, "The current instance is not a square matrix.");
635
636         for (int i = 0; i < __row; i++)
637         {
638                 for (int j = 0; j < __column; j++)
639                 {
640                         if (i == j)
641                         {
642                                 __pMatrix[i][j] = 1;
643                         }
644                         else
645                         {
646                                 __pMatrix[i][j] = 0;
647                         }
648                 }
649         }
650
651         return E_SUCCESS;
652 }
653
654 result
655 IntMatrix::Invert(void)
656 {
657         SysTryReturnResult(NID_BASE, __row == __column, E_INVALID_OPERATION, "The current instance is not a square matrix.");
658
659         int determinant = CalculateDeterminant(__pMatrix, __row);
660         SysTryReturnResult(NID_BASE, determinant != 0, E_INVALID_OPERATION, "The current instance is not invertible.");
661
662         std::unique_ptr<int* []> pInverse(new (std::nothrow) int* [__row]);
663         SysTryReturn(NID_BASE, pInverse != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
664                 GetErrorMessage(E_OUT_OF_MEMORY));
665
666         for (int i = 0; i < __row; i++)
667         {
668                 pInverse[i] = new (std::nothrow) int[__row];
669                 if (pInverse[i] == null)
670                 {
671                         for (int j = 0; j < i;  j++)
672                         {
673                                 delete[] pInverse[j];
674                         }                       
675                         return E_OUT_OF_MEMORY;
676                 }
677         }
678
679         std::unique_ptr<int* []> pMinor(new (std::nothrow) int* [__row - 1]);
680         if (pMinor == null)
681         {
682                 for ( int i = 0 ; i < __row ; i++ )
683                 {
684                         delete[] pInverse[i];
685                 }
686                 return E_OUT_OF_MEMORY;
687         }
688
689         for (int i = 0; i < __row - 1; i++)
690         {
691                 pMinor[i] = new int[__row - 1];
692                 if (pMinor[i] == null)
693                 {
694                         for ( int k = 0 ; k < __row ; k++ )
695                         {
696                                 delete[] pInverse[k];
697                         }
698
699                         for (int j = 0; j < i;  j++)
700                         {
701                                 delete[] pMinor[j];
702                         }
703
704                         return E_OUT_OF_MEMORY;
705                 }
706         }
707
708         for (int i = 0; i < __row; i++)
709         {
710                 for (int j = 0; j < __row; j++)
711                 {
712                         GetMinor(__pMatrix, pMinor.get(), i, j, __row);
713                         pInverse[j][i] = CalculateDeterminant(pMinor.get(), __row - 1) / determinant;
714                         if ((i +j +2) % 2 == 1)
715                         {
716                                 pInverse[j][i] = -pInverse[j][i];
717                         }
718                 }
719         }
720
721         for ( int i = 0 ; i < __row ; i++ )
722         {
723                 delete[] __pMatrix[i];
724         }
725         delete[] __pMatrix;
726
727         __pMatrix = pInverse.release();
728
729         for ( int i = 0 ; i < __row - 1; i++ )
730         {
731                 delete[] pMinor[i];
732         }
733
734         return E_SUCCESS;
735 }
736
737 result
738 IntMatrix::Transpose(void)
739 {
740         SysTryReturnResult(NID_BASE, __row == __column, E_INVALID_OPERATION, "The current instance is not a square matrix.");
741
742         int columnIndex = 1;
743
744         for (int i = 0; i < __row; i++)
745         {
746                 for (int j = 0; j < columnIndex; j++)
747                 {
748                         int temp = __pMatrix[i][j];
749                         __pMatrix[i][j] = __pMatrix[j][i];
750                         __pMatrix[j][i] = temp;
751                 }
752
753                 columnIndex++;
754         }
755
756         return E_SUCCESS;
757 }
758
759 result
760 IntMatrix::SetColumn(int columnIndex, const int* pArray)
761 {
762         SysTryReturnResult(NID_BASE, pArray != null, E_INVALID_ARG, "pArray is null.");
763         SysTryReturnResult(NID_BASE, columnIndex <= __column, E_INVALID_ARG,
764                 "columnIndex is larger than the column count of the current instance.");
765
766         for (int i = 0; i < __row; i++)
767         {
768                 __pMatrix[i][columnIndex] = pArray[i];
769         }
770
771         return E_SUCCESS;
772 }
773
774 result
775 IntMatrix::SetRow(int rowIndex, const int* pArray)
776 {
777         SysTryReturnResult(NID_BASE, pArray != null, E_INVALID_ARG, "pArray is null.");
778         SysTryReturnResult(NID_BASE, rowIndex <= __row, E_INVALID_ARG,
779                 "rowIndex is larger than the row count of the current instance.");
780
781         for (int i = 0; i < __column; i++)
782         {
783                 __pMatrix[rowIndex][i] = pArray[i];
784         }
785
786         return E_SUCCESS;
787 }
788
789 result
790 IntMatrix::SetElement(int rowIndex, int columnIndex, int value)
791 {
792         SysTryReturnResult(NID_BASE, columnIndex <= __column, E_INVALID_ARG,
793                 "columnIndex is larger than the column count of the current instance.");
794         SysTryReturnResult(NID_BASE, rowIndex <= __row, E_INVALID_ARG,
795                 "rowIndex is larger than the row count of the current instance.");
796
797         __pMatrix[rowIndex][columnIndex] = value;
798
799         return E_SUCCESS;
800 }
801
802 result
803 IntMatrix::SetValue(const int* pArray, bool rowMajor)
804 {
805         SysTryReturnResult(NID_BASE, pArray != null, E_INVALID_ARG, "pArray is null.");
806
807         if (rowMajor == true)
808         {
809                 for (int i = 0; i < __row; i++)
810                 {
811                         for (int j = 0; j < __column; j++)
812                         {
813                                 __pMatrix[i][j] = pArray[i * __column + j];
814                         }
815                 }
816         }
817         else
818         {
819                 for (int i = 0; i < __column; i++)
820                 {
821                         for (int j = 0; j < __row; j++)
822                         {
823                                 __pMatrix[j][i] = pArray[i * __row + j];
824                         }
825                 }
826         }
827
828         return E_SUCCESS;
829 }
830
831 void
832 IntMatrix::SetAsNull(void)
833 {
834         for (int i = 0; i < __row; i++)
835         {
836                 for (int j = 0; j < __column; j++)
837                 {
838                         __pMatrix[i][j] = 0;
839                 }
840         }
841 }
842
843 result
844 IntMatrix::Subtract(const IntMatrix& matrix)
845 {
846         SysTryReturnResult(NID_BASE, (__row == matrix.__row) && (__column == matrix.__column), E_INVALID_ARG,
847                 "Either row or column count of the current instance is not same with that of the specified instance.");
848
849         for (int i = 0; i < __row; i++)
850         {
851                 for (int j = 0; j < __column; j++)
852                 {
853                         __pMatrix[i][j] = __pMatrix[i][j] - matrix.__pMatrix[i][j];
854                 }
855         }
856
857         return E_SUCCESS;
858 }
859
860 void
861 IntMatrix::SubtractToEachElement(int value)
862 {
863         for (int i = 0; i < __row; i++)
864         {
865                 for (int j = 0; j < __column; j++)
866                 {
867                         __pMatrix[i][j] = __pMatrix[i][j] - value;
868                 }
869         }
870 }
871
872 }} // Tizen::Base