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