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