05cb4de0cc747fe6d0c29ffc5b3d8de21015e109
[platform/framework/native/appfw.git] / src / base / FBaseFloatMatrix.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        FBaseFloatMatrix.cpp
19  * @brief       This is the implementation for FloatMatrix class.
20  */
21
22 #include <FBaseFloatMatrix.h>
23 #include <FBaseSysLog.h>
24 #include <unique_ptr.h>
25
26 namespace Tizen { namespace Base
27 {
28
29 FloatMatrix::FloatMatrix(void)
30         : __pImpl(null)
31         , __pMatrix(null)
32         , __row(0)
33         , __column(0)
34 {
35 }
36
37 FloatMatrix::FloatMatrix(const FloatMatrix& 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 FloatMatrix::FloatMatrix(int rowCount, int columnCount)
53         : __pImpl(null)
54         , __row(rowCount)
55         , __column(columnCount)
56 {
57         AllocateCapacity(__row, __column);
58         SetAsNull();
59 }
60
61 FloatMatrix::FloatMatrix(int rowCount, int columnCount, const float* 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 FloatMatrix::FloatMatrix(int rowCount, int columnCount, const float* 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 FloatMatrix::~FloatMatrix(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 FloatMatrix::operator ==(const FloatMatrix& 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 FloatMatrix::operator !=(const FloatMatrix& rhs) const
157 {
158         return !(*this == rhs);
159 }
160
161 FloatMatrix&
162 FloatMatrix::operator =(const FloatMatrix& 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 FloatMatrix::Equals(const Tizen::Base::Object& obj) const
186 {
187         const FloatMatrix* pOther = dynamic_cast <const FloatMatrix*>(&obj);
188
189         if (pOther == null)
190         {
191                 return false;
192         }
193
194         return (*this == *pOther);
195 }
196
197 int
198 FloatMatrix::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::Float::GetHashCode(__pMatrix[i][j]);
206                 }
207         }
208
209         return hash;
210 }
211
212 result
213 FloatMatrix::Add(const FloatMatrix& 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 FloatMatrix::AddToEachElement(float 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 FloatMatrix::AllocateCapacity(int rowCount, int columnCount)
243 {
244         std::unique_ptr<float* []> pMatrix(new (std::nothrow) float* [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) float[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 float
267 FloatMatrix::CalculateDeterminant(float** 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         float determinant = 0.0f;
278         std::unique_ptr<float* []> pMinor(new (std::nothrow) float* [order - 1]);
279         SysTryReturn(NID_BASE, pMinor != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
280                 GetErrorMessage(E_OUT_OF_MEMORY));
281         for (int i = 0; i < order - 1; i++)
282         {
283                 pMinor[i] = new (std::nothrow) float[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 FloatMatrix::GetColumnCount(void) const
321 {
322         return __column;
323 }
324
325 float*
326 FloatMatrix::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<float []> pColumn(new (std::nothrow) float [__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 float
345 FloatMatrix::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 float
354 FloatMatrix::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 FloatMatrix*
363 FloatMatrix::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<FloatMatrix> pInverseMatrix(new (std::nothrow) FloatMatrix(__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         float determinant = CalculateDeterminant(__pMatrix, __row);
373
374         if (Tizen::Base::Float::Compare(determinant,0.0f) == 0)
375         {
376                 return null;
377         }
378
379         std::unique_ptr<float* []> pMinor(new (std::nothrow) float* [__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) float[__row - 1];
386                 if (pMinor[i] == null)
387                 {
388                         for (int j = 0; j < i;  j++)
389                         {
390                                 delete[] pMinor[j];
391                         }               
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 FloatMatrix::GetMinor(float** pSrc, float** 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.", GetErrorMessage(E_INVALID_ARG));
422         SysTryReturn(NID_BASE, pDest != null, , E_INVALID_ARG, "[%s] Invalid argument is used. pDest is null.", 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 FloatMatrix::GetRowCount(void) const
448 {
449         return __row;
450 }
451
452 float*
453 FloatMatrix::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<float []> pRow(new (std::nothrow) float [__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 FloatMatrix::GetTrace(float& 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 FloatMatrix*
487 FloatMatrix::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<FloatMatrix> pTransposeMatrix(new (std::nothrow) FloatMatrix(*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                         float 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 FloatMatrix::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::Float::Compare(__pMatrix[i][j],1.0f) != 0)
528                                 {
529                                         return false;
530                                 }
531                         }
532                         else
533                         {
534                                 if (Tizen::Base::Float::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 FloatMatrix::IsInvertible(void) const
548 {
549         int ret = Tizen::Base::Float::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 FloatMatrix::Multiply(const FloatMatrix& 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<float* []> pResult(new (std::nothrow) float* [__row]);
568         SysTryReturnResult(NID_BASE, pResult != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
569
570         for ( int i = 0 ; i < __row ; i++ )
571         {
572                 pResult[i] = new (std::nothrow) float[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         delete[] __pMatrix;
601
602         __pMatrix = pResult.release();
603
604         return E_SUCCESS;
605 }
606
607 void
608 FloatMatrix::Multiply(float value)
609 {
610         for (int i = 0; i < __row; i++)
611         {
612                 for (int j = 0; j < __column; j++)
613                 {
614                         __pMatrix[i][j] = __pMatrix[i][j] * value;
615                 }
616         }
617 }
618
619 void
620 FloatMatrix::Negate(void)
621 {
622         for (int i = 0; i < __row; i++)
623         {
624                 for (int j = 0; j < __column; j++)
625                 {
626                         __pMatrix[i][j] = -__pMatrix[i][j];
627                 }
628         }
629 }
630
631 result
632 FloatMatrix::SetAsIdentity(void)
633 {
634         SysTryReturnResult(NID_BASE, __row == __column, E_INVALID_OPERATION, "The current instance is not a square matrix.");
635
636         for (int i = 0; i < __row; i ++)
637         {
638                 for (int j = 0; j < __column; j++)
639                 {
640                         if (i == j)
641                         {
642                                 __pMatrix[i][j] = 1.0f;
643                         }
644                         else
645                         {
646                                 __pMatrix[i][j] = 0.0f;
647                         }                               
648                 }
649         }
650
651         return E_SUCCESS;
652 }
653
654 result
655 FloatMatrix::Invert(void)
656 {
657         SysTryReturnResult(NID_BASE, __row == __column, E_INVALID_OPERATION, "The current instance is not a square matrix.");
658
659         float determinant = CalculateDeterminant(__pMatrix, __row);
660         SysTryReturnResult(NID_BASE, Tizen::Base::Float::Compare(determinant, 0.0f) != 0, E_INVALID_OPERATION,
661                 "The current instance is not invertible.");
662
663         std::unique_ptr<float* []> pInverse(new (std::nothrow) float* [__row]);
664         SysTryReturn(NID_BASE, pInverse != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
665                 GetErrorMessage(E_OUT_OF_MEMORY));
666
667         for (int i = 0; i < __row; i++)
668         {
669                 pInverse[i] = new (std::nothrow) float[__row];
670                 if (pInverse[i] == null)
671                 {
672                         for (int j = 0; j < i;  j++)
673                         {
674                                 delete[] pInverse[j];
675                         }                       
676                         return E_OUT_OF_MEMORY;
677                 }
678         }
679
680         std::unique_ptr<float* []> pMinor(new (std::nothrow) float* [__row - 1]);
681         if (pMinor == null)
682         {
683                 for ( int i = 0 ; i < __row ; i++ )
684                 {
685                         delete[] pInverse[i];
686                 }
687                 return E_OUT_OF_MEMORY;
688         }
689
690         for (int i = 0; i < __row - 1; i++)
691         {
692                 pMinor[i] = new (std::nothrow) float[__row - 1];
693                 if (pMinor[i] == null)
694                 {
695                         for (int k = 0 ; k < __row ; k++)
696                         {
697                                 delete[] pInverse[k];
698                         }
699
700                         for (int j = 0; j < i;  j++)
701                         {
702                                 delete[] pMinor[j];
703                         }
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 FloatMatrix::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                         float 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 FloatMatrix::SetColumn(int columnIndex, const float* 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 FloatMatrix::SetRow(int rowIndex, const float* 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 FloatMatrix::SetElement(int rowIndex, int columnIndex, float 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 FloatMatrix::SetValue(const float* 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 FloatMatrix::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 FloatMatrix::Subtract(const FloatMatrix& 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 FloatMatrix::SubtractToEachElement(float 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