[devel_3.0_main] Cherry-pick Beautification of source-code. 80383
[platform/framework/native/appfw.git] / src / base / FBaseDoubleMatrix.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        FBaseDoubleMatrix.cpp
19  * @brief       This is the implementation for DoubleMatrix class.
20  */
21 #include <FBaseDoubleMatrix.h>
22 #include <FBaseSysLog.h>
23 #include <unique_ptr.h>
24
25 namespace Tizen { namespace Base
26 {
27
28 DoubleMatrix::DoubleMatrix(void)
29         : __pImpl(null)
30         , __pMatrix(null)
31         , __row(0)
32         , __column(0)
33 {
34 }
35
36 DoubleMatrix::DoubleMatrix(const DoubleMatrix& 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 DoubleMatrix::DoubleMatrix(int rowCount, int columnCount)
52         : __pImpl(null)
53         , __row(rowCount)
54         , __column(columnCount)
55 {
56         AllocateCapacity(__row, __column);
57         SetAsNull();
58 }
59
60 DoubleMatrix::DoubleMatrix(int rowCount, int columnCount, const double* 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 DoubleMatrix::DoubleMatrix(int rowCount, int columnCount, const double* 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 DoubleMatrix::~DoubleMatrix(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 DoubleMatrix::operator ==(const DoubleMatrix& 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 DoubleMatrix::operator !=(const DoubleMatrix& rhs) const
156 {
157         return !(*this == rhs);
158 }
159
160 DoubleMatrix&
161 DoubleMatrix::operator =(const DoubleMatrix& 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 DoubleMatrix::Equals(const Tizen::Base::Object& obj) const
185 {
186         const DoubleMatrix* pOther = dynamic_cast< const DoubleMatrix* >(&obj);
187
188         if (pOther == null)
189         {
190                 return false;
191         }
192
193         return (*this == *pOther);
194 }
195
196 int
197 DoubleMatrix::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::Double::GetHashCode(__pMatrix[i][j]);
205                 }
206         }
207
208         return hash;
209 }
210
211 result
212 DoubleMatrix::Add(const DoubleMatrix& 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 DoubleMatrix::AddToEachElement(double 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 DoubleMatrix::AllocateCapacity(int rowCount, int columnCount)
242 {
243         std::unique_ptr< double*[] > pMatrix(new (std::nothrow) double*[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) double[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 double
266 DoubleMatrix::CalculateDeterminant(double** 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.0f, 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         double determinant = 0.0f;
277         std::unique_ptr< double*[] > pMinor(new (std::nothrow) double*[order - 1]);
278         SysTryReturn(NID_BASE, pMinor != null, determinant, 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) double[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 DoubleMatrix::GetColumnCount(void) const
320 {
321         return __column;
322 }
323
324 double*
325 DoubleMatrix::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< double[] > pColumn(new (std::nothrow) double[__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 double
344 DoubleMatrix::GetDeterminant(void) const
345 {
346         SysTryReturn(NID_BASE, __row == __column, 0.0f, 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 double
353 DoubleMatrix::GetElement(int rowIndex, int columnIndex) const
354 {
355         SysTryReturn(NID_BASE, (rowIndex <= __row) && (columnIndex <= __column), 0.0f, 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 DoubleMatrix*
362 DoubleMatrix::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< DoubleMatrix > pInverseMatrix(new (std::nothrow) DoubleMatrix(__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         double determinant = CalculateDeterminant(__pMatrix, __row);
372
373         if (Tizen::Base::Double::Compare(determinant, 0.0f) == 0)
374         {
375                 return null;
376         }
377
378         std::unique_ptr< double*[] > pMinor(new (std::nothrow) double*[__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) double[__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 DoubleMatrix::GetMinor(double** pSrc, double** 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 DoubleMatrix::GetRowCount(void) const
448 {
449         return __row;
450 }
451
452 double*
453 DoubleMatrix::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< double[] > pRow(new (std::nothrow) double[__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 DoubleMatrix::GetTrace(double& value) const
473 {
474         SysTryReturnResult(NID_BASE, __row == __column, E_INVALID_OPERATION, "The current instance is not a square matrix.");
475
476         value = 0.0f;
477
478         for (int i = 0; i < __row; i++)
479         {
480                 value += __pMatrix[i][i];
481         }
482
483         return E_SUCCESS;
484 }
485
486 DoubleMatrix*
487 DoubleMatrix::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< DoubleMatrix > pTransposeMatrix(new (std::nothrow) DoubleMatrix(*this));
493         SysTryReturn(NID_BASE, pTransposeMatrix != null, null, E_OUT_OF_MEMORY,
494                 "[%s] Memory allocation failed.", 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                         double 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 DoubleMatrix::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 (Tizen::Base::Double::Compare(__pMatrix[i][j], 1.0f) != 0)
528                                 {
529                                         return false;
530                                 }
531                         }
532                         else
533                         {
534                                 if (Tizen::Base::Double::Compare(__pMatrix[i][j], 0.0f) != 0)
535                                 {
536                                         return false;
537                                 }
538                         }
539
540
541                 }
542         }
543         return true;
544 }
545
546 bool
547 DoubleMatrix::IsInvertible(void) const
548 {
549         int ret = Tizen::Base::Double::Compare(GetDeterminant(), 0.0f);
550
551         if (ret == 0)
552         {
553                 return false;
554         }
555         else
556         {
557                 return true;
558         }
559 }
560
561 result
562 DoubleMatrix::Multiply(const DoubleMatrix& matrix)
563 {
564         SysTryReturnResult(NID_BASE, __column == matrix.__row, E_INVALID_ARG,
565                 "The column count of the current instance is not same with the row count of the specified instance.");
566
567         std::unique_ptr< double*[] > pResult(new (std::nothrow) double*[__row]);
568         SysTryReturnResult(NID_BASE, pResult != null, E_OUT_OF_MEMORY, "Allocating memory is failed.");
569
570         for (int i = 0; i < __row; i++)
571         {
572                 pResult[i] = new (std::nothrow) double[matrix.__column];
573                 if (pResult[i] == null)
574                 {
575                         for (int j = 0; j < i; j++)
576                         {
577                                 delete[] pResult[j];
578                         }
579                         return E_OUT_OF_MEMORY;
580                 }
581         }
582
583         for (int i = 0; i < __row; i++)
584         {
585                 for (int j = 0; j < matrix.__column; j++)
586                 {
587                         pResult[i][j] = 0.0f;
588
589                         for (int k = 0; k < __column; k++)
590                         {
591                                 pResult[i][j] += __pMatrix[i][k] * matrix.__pMatrix[k][j];
592                         }
593                 }
594         }
595
596         for (int i = 0; i < __row; i++)
597         {
598                 delete[] __pMatrix[i];
599         }
600
601         delete[] __pMatrix;
602
603         __pMatrix = pResult.release();
604
605         return E_SUCCESS;
606 }
607
608 void
609 DoubleMatrix::Multiply(double value)
610 {
611         for (int i = 0; i < __row; i++)
612         {
613                 for (int j = 0; j < __column; j++)
614                 {
615                         __pMatrix[i][j] = __pMatrix[i][j] * value;
616                 }
617         }
618 }
619
620 void
621 DoubleMatrix::Negate(void)
622 {
623         for (int i = 0; i < __row; i++)
624         {
625                 for (int j = 0; j < __column; j++)
626                 {
627                         __pMatrix[i][j] = -__pMatrix[i][j];
628                 }
629         }
630 }
631
632 result
633 DoubleMatrix::SetAsIdentity(void)
634 {
635         SysTryReturnResult(NID_BASE, __row == __column, E_INVALID_OPERATION, "The current instance is not a square matrix.");
636
637         for (int i = 0; i < __row; i++)
638         {
639                 for (int j = 0; j < __column; j++)
640                 {
641                         if (i == j)
642                         {
643                                 __pMatrix[i][j] = 1.0f;
644                         }
645                         else
646                         {
647                                 __pMatrix[i][j] = 0.0f;
648                         }
649                 }
650         }
651
652         return E_SUCCESS;
653 }
654
655 result
656 DoubleMatrix::Invert(void)
657 {
658         SysTryReturnResult(NID_BASE, __row == __column, E_INVALID_OPERATION, "The current instance is not a square matrix.");
659
660         double determinant = CalculateDeterminant(__pMatrix, __row);
661         SysTryReturnResult(NID_BASE, Tizen::Base::Double::Compare(determinant, 0.0f) != 0, E_INVALID_OPERATION,
662                 "The current instance is not invertible.");
663
664         std::unique_ptr< double*[] > pInverse(new (std::nothrow) double*[__row]);
665         SysTryReturnResult(NID_BASE, pInverse != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
666                 GetErrorMessage(E_OUT_OF_MEMORY));
667
668         for (int i = 0; i < __row; i++)
669         {
670                 pInverse[i] = new (std::nothrow) double[__row];
671                 if (pInverse[i] == null)
672                 {
673                         for (int j = 0; j < i; j++)
674                         {
675                                 delete[] pInverse[j];
676                         }
677                         return E_OUT_OF_MEMORY;
678                 }
679         }
680
681         std::unique_ptr< double*[] > pMinor(new (std::nothrow) double*[__row - 1]);
682         if (pMinor == null)
683         {
684                 for (int i = 0; i < __row; i++)
685                 {
686                         delete[] pInverse[i];
687                 }
688                 return E_OUT_OF_MEMORY;
689         }
690
691         for (int i = 0; i < __row - 1; i++)
692         {
693                 pMinor[i] = new double[__row - 1];
694                 if (pMinor[i] == null)
695                 {
696                         for (int k = 0; k < __row; k++)
697                         {
698                                 delete[] pInverse[k];
699                         }
700
701                         for (int j = 0; j < i; j++)
702                         {
703                                 delete[] pMinor[j];
704                         }
705                         return E_OUT_OF_MEMORY;
706                 }
707         }
708
709         for (int i = 0; i < __row; i++)
710         {
711                 for (int j = 0; j < __row; j++)
712                 {
713                         GetMinor(__pMatrix, pMinor.get(), i, j, __row);
714                         pInverse[j][i] = CalculateDeterminant(pMinor.get(), __row - 1) / determinant;
715                         if ((i + j + 2) % 2 == 1)
716                         {
717                                 pInverse[j][i] = -pInverse[j][i];
718                         }
719                 }
720         }
721
722         for (int i = 0; i < __row; i++)
723         {
724                 delete[] __pMatrix[i];
725         }
726         delete[] __pMatrix;
727
728         __pMatrix = pInverse.release();
729
730         for (int i = 0; i < __row - 1; i++)
731         {
732                 delete[] pMinor[i];
733         }
734
735         return E_SUCCESS;
736 }
737
738 result
739 DoubleMatrix::Transpose(void)
740 {
741         SysTryReturnResult(NID_BASE, __row == __column, E_INVALID_OPERATION, "The current instance is not a square matrix.");
742
743         int columnIndex = 1;
744
745         for (int i = 0; i < __row; i++)
746         {
747                 for (int j = 0; j < columnIndex; j++)
748                 {
749                         double temp = __pMatrix[i][j];
750                         __pMatrix[i][j] = __pMatrix[j][i];
751                         __pMatrix[j][i] = temp;
752                 }
753
754                 columnIndex++;
755         }
756
757         return E_SUCCESS;
758 }
759
760 result
761 DoubleMatrix::SetColumn(int columnIndex, const double* pArray)
762 {
763         SysTryReturnResult(NID_BASE, pArray != null, E_INVALID_ARG, "pArray is null");
764         SysTryReturnResult(NID_BASE, columnIndex <= __column, E_INVALID_ARG,
765                 "columnIndex is larger than the column count of the current instance.");
766
767         for (int i = 0; i < __row; i++)
768         {
769                 __pMatrix[i][columnIndex] = pArray[i];
770         }
771
772         return E_SUCCESS;
773 }
774
775 result
776 DoubleMatrix::SetRow(int rowIndex, const double* pArray)
777 {
778         SysTryReturnResult(NID_BASE, pArray != null, E_INVALID_ARG, "pArray is null");
779         SysTryReturnResult(NID_BASE, rowIndex <= __row, E_INVALID_ARG,
780                 "rowIndex is larger than the row count of the current instance.");
781
782         for (int i = 0; i < __column; i++)
783         {
784                 __pMatrix[rowIndex][i] = pArray[i];
785         }
786
787         return E_SUCCESS;
788 }
789
790 result
791 DoubleMatrix::SetElement(int rowIndex, int columnIndex, double value)
792 {
793         SysTryReturnResult(NID_BASE, columnIndex <= __column, E_INVALID_ARG,
794                 "columnIndex is larger than the column count of the current instance.");
795         SysTryReturnResult(NID_BASE, rowIndex <= __row, E_INVALID_ARG,
796                 "rowIndex is larger than the row count of the current instance.");
797
798         __pMatrix[rowIndex][columnIndex] = value;
799
800         return E_SUCCESS;
801 }
802
803 result
804 DoubleMatrix::SetValue(const double* pArray, bool rowMajor)
805 {
806         SysTryReturnResult(NID_BASE, pArray != null, E_INVALID_ARG, "pArray is null.");
807
808         if (rowMajor == true)
809         {
810                 for (int i = 0; i < __row; i++)
811                 {
812                         for (int j = 0; j < __column; j++)
813                         {
814                                 __pMatrix[i][j] = pArray[i * __column + j];
815                         }
816                 }
817         }
818         else
819         {
820                 for (int i = 0; i < __column; i++)
821                 {
822                         for (int j = 0; j < __row; j++)
823                         {
824                                 __pMatrix[j][i] = pArray[i * __row + j];
825                         }
826                 }
827         }
828
829         return E_SUCCESS;
830 }
831
832 void
833 DoubleMatrix::SetAsNull(void)
834 {
835         for (int i = 0; i < __row; i++)
836         {
837                 for (int j = 0; j < __column; j++)
838                 {
839                         __pMatrix[i][j] = 0.0f;
840                 }
841         }
842 }
843
844 result
845 DoubleMatrix::Subtract(const DoubleMatrix& matrix)
846 {
847         SysTryReturnResult(NID_BASE, (__row == matrix.__row) && (__column == matrix.__column), E_INVALID_ARG,
848                 "Either row or column count of the current instance is not same with that of the specified instance.");
849
850         for (int i = 0; i < __row; i++)
851         {
852                 for (int j = 0; j < __column; j++)
853                 {
854                         __pMatrix[i][j] = __pMatrix[i][j] - matrix.__pMatrix[i][j];
855                 }
856         }
857
858         return E_SUCCESS;
859 }
860
861 void
862 DoubleMatrix::SubtractToEachElement(double value)
863 {
864         for (int i = 0; i < __row; i++)
865         {
866                 for (int j = 0; j < __column; j++)
867                 {
868                         __pMatrix[i][j] = __pMatrix[i][j] - value;
869                 }
870         }
871 }
872
873 }} // Tizen::Base