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