sync with tizen_2.0
[platform/framework/native/appfw.git] / src / base / FBaseFloatMatrix.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        FBaseFloatMatrix.cpp
20  * @brief       This is the implementation for FloatMatrix class.
21  */
22
23 #include <FBaseFloatMatrix.h>
24 #include <FBaseSysLog.h>
25 #include <unique_ptr.h>
26
27 namespace Tizen { namespace Base
28 {
29
30 FloatMatrix::FloatMatrix(void)
31         : __pImpl(null)
32         , __pMatrix(null)
33         , __row(0)
34         , __column(0)
35 {
36 }
37
38 FloatMatrix::FloatMatrix(const FloatMatrix& 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 FloatMatrix::FloatMatrix(int rowCount, int columnCount)
54         : __pImpl(null)
55         , __row(rowCount)
56         , __column(columnCount)
57 {
58         AllocateCapacity(__row, __column);
59         SetAsNull();
60 }
61
62 FloatMatrix::FloatMatrix(int rowCount, int columnCount, const float* 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 FloatMatrix::FloatMatrix(int rowCount, int columnCount, const float* 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 FloatMatrix::~FloatMatrix(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 FloatMatrix::operator ==(const FloatMatrix& 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 FloatMatrix::operator !=(const FloatMatrix& rhs) const
158 {
159         return !(*this == rhs);
160 }
161
162 FloatMatrix&
163 FloatMatrix::operator =(const FloatMatrix& 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 FloatMatrix::Equals(const Tizen::Base::Object& obj) const
187 {
188         const FloatMatrix* pOther = dynamic_cast <const FloatMatrix*>(&obj);
189
190         if (pOther == null)
191         {
192                 return false;
193         }
194
195         return (*this == *pOther);
196 }
197
198 int
199 FloatMatrix::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::Float::GetHashCode(__pMatrix[i][j]);
207                 }
208         }
209
210         return hash;
211 }
212
213 result
214 FloatMatrix::Add(const FloatMatrix& 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 FloatMatrix::AddToEachElement(float 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 FloatMatrix::AllocateCapacity(int rowCount, int columnCount)
244 {
245         std::unique_ptr<float* []> pMatrix(new (std::nothrow) float* [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) float[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 float
268 FloatMatrix::CalculateDeterminant(float** 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         float determinant = 0.0f;
279         std::unique_ptr<float* []> pMinor(new (std::nothrow) float* [order - 1]);
280         SysTryReturn(NID_BASE, pMinor != null, false, 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) float[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 FloatMatrix::GetColumnCount(void) const
322 {
323         return __column;
324 }
325
326 float*
327 FloatMatrix::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<float []> pColumn(new (std::nothrow) float [__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 float
346 FloatMatrix::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 float
355 FloatMatrix::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 FloatMatrix*
364 FloatMatrix::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<FloatMatrix> pInverseMatrix(new (std::nothrow) FloatMatrix(__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         float determinant = CalculateDeterminant(__pMatrix, __row);
374
375         if (Tizen::Base::Float::Compare(determinant,0.0f) == 0)
376         {
377                 return null;
378         }
379
380         std::unique_ptr<float* []> pMinor(new (std::nothrow) float* [__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) float[__row - 1];
387                 if (pMinor[i] == null)
388                 {
389                         for (int j = 0; j < i;  j++)
390                         {
391                                 delete[] pMinor[j];
392                         }               
393
394                         return null;
395                 }
396         }
397
398         for (int i = 0; i < __row; i++)
399         {
400                 for (int j = 0; j < __row; j++)
401                 {
402                         GetMinor(__pMatrix, pMinor.get(), i, j, __row);
403                         pInverseMatrix->__pMatrix[j][i] = CalculateDeterminant(pMinor.get(), __row - 1) / determinant;
404                         if ((i + j + 2) % 2 == 1)
405                         {
406                                 pInverseMatrix->__pMatrix[j][i] = -pInverseMatrix->__pMatrix[j][i];
407                         }
408                 }       
409         }
410
411         for ( int i = 0 ; i < __row -1 ; i++ )
412         {
413                 delete[] pMinor[i];
414         }
415
416         return pInverseMatrix.release();
417 }
418
419 void
420 FloatMatrix::GetMinor(float** pSrc, float** pDest, int rowIndex, int columnIndex, int order) const
421 {
422         SysTryReturn(NID_BASE, pSrc != null, , E_INVALID_ARG, "[%s] Invalid argument is used. pSrc is null.", GetErrorMessage(E_INVALID_ARG));
423         SysTryReturn(NID_BASE, pDest != null, , E_INVALID_ARG, "[%s] Invalid argument is used. pDest is null.", 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 FloatMatrix::GetRowCount(void) const
449 {
450         return __row;
451 }
452
453 float*
454 FloatMatrix::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<float []> pRow(new (std::nothrow) float [__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 FloatMatrix::GetTrace(float& 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 FloatMatrix*
488 FloatMatrix::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<FloatMatrix> pTransposeMatrix(new (std::nothrow) FloatMatrix(*this));
494         SysTryReturn(NID_BASE, pTransposeMatrix != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
495                 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                         float 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 FloatMatrix::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::Float::Compare(__pMatrix[i][j],1.0f) != 0)
529                                 {
530                                         return false;
531                                 }
532                         }
533                         else
534                         {
535                                 if (Tizen::Base::Float::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 FloatMatrix::IsInvertible(void) const
549 {
550         int ret = Tizen::Base::Float::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 FloatMatrix::Multiply(const FloatMatrix& 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<float* []> pResult(new (std::nothrow) float* [__row]);
569         SysTryReturnResult(NID_BASE, pResult != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
570
571         for ( int i = 0 ; i < __row ; i++ )
572         {
573                 pResult[i] = new (std::nothrow) float[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         delete[] __pMatrix;
602
603         __pMatrix = pResult.release();
604
605         return E_SUCCESS;
606 }
607
608 void
609 FloatMatrix::Multiply(float 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 FloatMatrix::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 FloatMatrix::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 FloatMatrix::Invert(void)
657 {
658         SysTryReturnResult(NID_BASE, __row == __column, E_INVALID_OPERATION, "The current instance is not a square matrix.");
659
660         float determinant = CalculateDeterminant(__pMatrix, __row);
661         SysTryReturnResult(NID_BASE, Tizen::Base::Float::Compare(determinant, 0.0f) != 0, E_INVALID_OPERATION,
662                 "The current instance is not invertible.");
663
664         std::unique_ptr<float* []> pInverse(new (std::nothrow) float* [__row]);
665         SysTryReturn(NID_BASE, pInverse != null, false, 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) float[__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<float* []> pMinor(new (std::nothrow) float* [__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 (std::nothrow) float[__row - 1];
694                 if (pMinor[i] == null)
695                 {
696                         for (int i = 0 ; i < __row ; i++)
697                         {
698                                 delete[] pInverse[i];
699                         }
700
701                         for (int j = 0; j < i;  j++)
702                         {
703                                 delete[] pMinor[j];
704                         }
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 FloatMatrix::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                         float 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 FloatMatrix::SetColumn(int columnIndex, const float* 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 FloatMatrix::SetRow(int rowIndex, const float* 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 FloatMatrix::SetElement(int rowIndex, int columnIndex, float 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 FloatMatrix::SetValue(const float* 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 * __row + 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 * __column + j];
826                         }
827                 }
828         }
829
830         return E_SUCCESS;
831 }
832
833 void
834 FloatMatrix::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 FloatMatrix::Subtract(const FloatMatrix& 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 FloatMatrix::SubtractToEachElement(float 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