2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file FBaseFloatMatrix.cpp
20 * @brief This is the implementation for FloatMatrix class.
23 #include <FBaseFloatMatrix.h>
24 #include <FBaseSysLog.h>
25 #include <unique_ptr.h>
27 namespace Tizen { namespace Base
30 FloatMatrix::FloatMatrix(void)
38 FloatMatrix::FloatMatrix(const FloatMatrix& rhs)
41 , __column(rhs.__column)
43 AllocateCapacity(__row, __column);
44 for (int i = 0; i < __row; i++)
46 for (int j = 0; j < __column; j++)
48 __pMatrix[i][j] = rhs.__pMatrix[i][j];
53 FloatMatrix::FloatMatrix(int rowCount, int columnCount)
56 , __column(columnCount)
58 AllocateCapacity(__row, __column);
62 FloatMatrix::FloatMatrix(int rowCount, int columnCount, const float* pArray, bool rowMajor)
65 , __column(columnCount)
67 AllocateCapacity(__row, __column);
72 for (int i = 0; i < __row; i++)
74 for (int j = 0; j < __column; j++)
76 __pMatrix[i][j] = pArray[i * __column + j];
82 for (int i = 0; i < __column; i++)
84 for (int j = 0; j < __row; j++)
86 __pMatrix[j][i] = pArray[i * __row + j];
97 FloatMatrix::FloatMatrix(int rowCount, int columnCount, const float* pArray[])
100 , __column(columnCount)
102 AllocateCapacity(__row, __column);
105 for (int i = 0; i < __row; i++)
107 for (int j = 0; j < __column; j++)
109 __pMatrix[i][j] = pArray[i][j];
119 FloatMatrix::~FloatMatrix(void)
121 for ( int i = 0 ; i < __row ; i++ )
123 delete[] __pMatrix[i];
130 FloatMatrix::operator ==(const FloatMatrix& rhs) const
137 if ((__row != rhs.__row) || (__column != rhs.__column))
142 for (int i = 0; i < __row; i++)
144 for (int j = 0; j < __column; j++)
146 if (__pMatrix[i][j] != rhs.__pMatrix[i][j])
157 FloatMatrix::operator !=(const FloatMatrix& rhs) const
159 return !(*this == rhs);
163 FloatMatrix::operator =(const FloatMatrix& rhs)
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));
174 for (int i = 0; i < __row; i++)
176 for (int j = 0; j < __column; j++)
178 __pMatrix[i][j] = rhs.__pMatrix[i][j];
186 FloatMatrix::Equals(const Tizen::Base::Object& obj) const
188 const FloatMatrix* pOther = dynamic_cast <const FloatMatrix*>(&obj);
195 return (*this == *pOther);
199 FloatMatrix::GetHashCode(void) const
202 for (int i = 0; i < __row; i++)
204 for (int j = 0; j < __column; j++)
206 hash = hash + Tizen::Base::Float::GetHashCode(__pMatrix[i][j]);
214 FloatMatrix::Add(const FloatMatrix& matrix)
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.");
219 for (int i = 0; i < __row; i++)
221 for (int j = 0; j < __column; j++)
223 __pMatrix[i][j] = __pMatrix[i][j] + matrix.__pMatrix[i][j];
231 FloatMatrix::AddToEachElement(float value)
233 for (int i = 0; i < __row; i++)
235 for (int j = 0; j < __column; j++)
237 __pMatrix[i][j] = __pMatrix[i][j] + value;
243 FloatMatrix::AllocateCapacity(int rowCount, int columnCount)
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));
249 for ( int i = 0 ; i < rowCount ; i++ )
251 pMatrix[i] = new (std::nothrow) float[columnCount];
252 if (pMatrix[i] == null)
254 for (int j = 0; j < i; j++)
262 __pMatrix = pMatrix.release();
268 FloatMatrix::CalculateDeterminant(float** pMatrix, int order) const // For perfomance, we have to change the logic of recursive to LU decomposition.
270 SysTryReturn(NID_BASE, pMatrix != null, 0.0f, E_INVALID_ARG, "[%s] Invalid argument is used. pMatrix is null.",
271 GetErrorMessage(E_INVALID_ARG));
275 return pMatrix[0][0];
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++)
284 pMinor[i] = new (std::nothrow) float[order - 1];
285 if (pMinor[i] == null)
287 for (int j = 0; j < i; j++)
295 bool signFlag = true;
296 for (int i = 0; i < order; i++)
298 GetMinor(pMatrix, pMinor.get(), 0, i, order);
300 if (signFlag == true)
302 determinant += pMatrix[0][i] * CalculateDeterminant(pMinor.get(), order - 1);
307 determinant += -pMatrix[0][i] * CalculateDeterminant(pMinor.get(), order - 1);
312 for (int i = 0; i < order - 1; i++)
321 FloatMatrix::GetColumnCount(void) const
327 FloatMatrix::GetColumnN(int columnIndex) const
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));
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));
337 for (int i = 0; i < __row; i++)
339 pColumn[i] = __pMatrix[i][columnIndex];
342 return pColumn.release();
346 FloatMatrix::GetDeterminant(void) const
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));
351 return CalculateDeterminant(__pMatrix, __row);
355 FloatMatrix::GetElement(int rowIndex, int columnIndex) const
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));
360 return __pMatrix[rowIndex][columnIndex];
364 FloatMatrix::GetInverseN(void) const
366 SysTryReturn(NID_BASE, __row == __column, null, E_INVALID_OPERATION,
367 "[%s] The current instance is not a square matrix.", GetErrorMessage(E_INVALID_OPERATION));
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));
373 float determinant = CalculateDeterminant(__pMatrix, __row);
375 if (Tizen::Base::Float::Compare(determinant,0.0f) == 0)
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));
384 for (int i = 0; i < __row -1; i++)
386 pMinor[i] = new (std::nothrow) float[__row - 1];
387 if (pMinor[i] == null)
389 for (int j = 0; j < i; j++)
398 for (int i = 0; i < __row; i++)
400 for (int j = 0; j < __row; j++)
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)
406 pInverseMatrix->__pMatrix[j][i] = -pInverseMatrix->__pMatrix[j][i];
411 for ( int i = 0 ; i < __row -1 ; i++ )
416 return pInverseMatrix.release();
420 FloatMatrix::GetMinor(float** pSrc, float** pDest, int rowIndex, int columnIndex, int order) const
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));
428 for (int i = 0; i < order; i++)
434 for (int j = 0; j < order; j++)
436 if (j != columnIndex)
438 pDest[rowCount][columnCount] = pSrc[i][j];
448 FloatMatrix::GetRowCount(void) const
454 FloatMatrix::GetRowN(int rowIndex) const
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));
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));
464 for (int i = 0; i < __column; i++)
466 pRow[i] = __pMatrix[rowIndex][i];
469 return pRow.release();
473 FloatMatrix::GetTrace(float& value) const
475 SysTryReturnResult(NID_BASE, __row == __column, E_INVALID_OPERATION, "The current instance is not a square matrix.");
479 for (int i = 0; i < __row; i++)
481 value += __pMatrix[i][i];
488 FloatMatrix::GetTransposeN(void) const
490 SysTryReturn(NID_BASE, __row == __column, null, E_INVALID_OPERATION,
491 "[%s] The current instance is not a square matrix.", GetErrorMessage(E_INVALID_OPERATION));
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));
499 for (int i = 0; i < __row; i++)
501 for (int j = 0; j < columnIndex; j++)
503 float temp = pTransposeMatrix->__pMatrix[i][j];
504 pTransposeMatrix->__pMatrix[i][j] = pTransposeMatrix->__pMatrix[j][i];
505 pTransposeMatrix->__pMatrix[j][i] = temp;
511 return pTransposeMatrix.release();
515 FloatMatrix::IsIdentity(void) const
517 if (__row != __column)
522 for (int i = 0; i < __row; i++)
524 for (int j = 0; j < __column; j++)
528 if (Tizen::Base::Float::Compare(__pMatrix[i][j],1.0f) != 0)
535 if (Tizen::Base::Float::Compare(__pMatrix[i][j],0.0f) != 0)
548 FloatMatrix::IsInvertible(void) const
550 int ret = Tizen::Base::Float::Compare(GetDeterminant(),0.0f);
563 FloatMatrix::Multiply(const FloatMatrix& matrix)
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.");
568 std::unique_ptr<float* []> pResult(new (std::nothrow) float* [__row]);
569 SysTryReturnResult(NID_BASE, pResult != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
571 for ( int i = 0 ; i < __row ; i++ )
573 pResult[i] = new (std::nothrow) float[matrix.__column];
574 if (pResult[i] == null)
576 for (int j = 0; j < i; j++)
580 return E_OUT_OF_MEMORY;
584 for (int i = 0; i < __row; i++)
586 for (int j = 0; j < matrix.__column; j++)
588 pResult[i][j] = 0.0f;
590 for (int k = 0; k < __column; k++)
592 pResult[i][j] += __pMatrix[i][k] * matrix.__pMatrix[k][j];
597 for ( int i = 0 ; i < __row ; i++ )
599 delete[] __pMatrix[i];
603 __pMatrix = pResult.release();
609 FloatMatrix::Multiply(float value)
611 for (int i = 0; i < __row; i++)
613 for (int j = 0; j < __column; j++)
615 __pMatrix[i][j] = __pMatrix[i][j] * value;
621 FloatMatrix::Negate(void)
623 for (int i = 0; i < __row; i++)
625 for (int j = 0; j < __column; j++)
627 __pMatrix[i][j] = -__pMatrix[i][j];
633 FloatMatrix::SetAsIdentity(void)
635 SysTryReturnResult(NID_BASE, __row == __column, E_INVALID_OPERATION, "The current instance is not a square matrix.");
637 for (int i = 0; i < __row; i ++)
639 for (int j = 0; j < __column; j++)
643 __pMatrix[i][j] = 1.0f;
647 __pMatrix[i][j] = 0.0f;
656 FloatMatrix::Invert(void)
658 SysTryReturnResult(NID_BASE, __row == __column, E_INVALID_OPERATION, "The current instance is not a square matrix.");
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.");
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));
668 for (int i = 0; i < __row; i++)
670 pInverse[i] = new (std::nothrow) float[__row];
671 if (pInverse[i] == null)
673 for (int j = 0; j < i; j++)
675 delete[] pInverse[j];
677 return E_OUT_OF_MEMORY;
681 std::unique_ptr<float* []> pMinor(new (std::nothrow) float* [__row - 1]);
684 for ( int i = 0 ; i < __row ; i++ )
686 delete[] pInverse[i];
688 return E_OUT_OF_MEMORY;
691 for (int i = 0; i < __row - 1; i++)
693 pMinor[i] = new (std::nothrow) float[__row - 1];
694 if (pMinor[i] == null)
696 for (int i = 0 ; i < __row ; i++)
698 delete[] pInverse[i];
701 for (int j = 0; j < i; j++)
706 return E_OUT_OF_MEMORY;
710 for (int i = 0; i < __row; i++)
712 for (int j = 0; j < __row; j++)
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)
718 pInverse[j][i] = -pInverse[j][i];
723 for ( int i = 0 ; i < __row ; i++ )
725 delete[] __pMatrix[i];
729 __pMatrix = pInverse.release();
731 for ( int i = 0 ; i < __row -1; i++ )
740 FloatMatrix::Transpose(void)
742 SysTryReturnResult(NID_BASE, __row == __column, E_INVALID_OPERATION, "The current instance is not a square matrix.");
746 for (int i = 0; i < __row; i++)
748 for (int j = 0; j < columnIndex; j++)
750 float temp = __pMatrix[i][j];
751 __pMatrix[i][j] = __pMatrix[j][i];
752 __pMatrix[j][i] = temp;
762 FloatMatrix::SetColumn(int columnIndex, const float* pArray)
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.");
768 for (int i = 0; i < __row; i++)
770 __pMatrix[i][columnIndex] = pArray[i];
777 FloatMatrix::SetRow(int rowIndex, const float* pArray)
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.");
783 for (int i = 0; i < __column; i++)
785 __pMatrix[rowIndex][i] = pArray[i];
792 FloatMatrix::SetElement(int rowIndex, int columnIndex, float value)
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.");
799 __pMatrix[rowIndex][columnIndex] = value;
805 FloatMatrix::SetValue(const float* pArray, bool rowMajor)
807 SysTryReturnResult(NID_BASE, pArray != null, E_INVALID_ARG, "pArray is null.");
809 if (rowMajor == true)
811 for (int i = 0; i < __row; i++)
813 for (int j = 0; j < __column; j++)
815 __pMatrix[i][j] = pArray[i * __column + j];
821 for (int i = 0; i < __column; i++)
823 for (int j = 0; j < __row; j++)
825 __pMatrix[j][i] = pArray[i * __row + j];
834 FloatMatrix::SetAsNull(void)
836 for (int i = 0; i < __row; i++)
838 for (int j = 0; j < __column; j++)
840 __pMatrix[i][j] = 0.0f;
846 FloatMatrix::Subtract(const FloatMatrix& matrix)
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.");
851 for (int i = 0; i < __row; i++)
853 for (int j = 0; j < __column; j++)
855 __pMatrix[i][j] = __pMatrix[i][j] - matrix.__pMatrix[i][j];
863 FloatMatrix::SubtractToEachElement(float value)
865 for (int i = 0; i < __row; i++)
867 for (int j = 0; j < __column; j++)
869 __pMatrix[i][j] = __pMatrix[i][j] - value;