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