2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file FBaseFloatMatrix.cpp
19 * @brief This is the implementation for FloatMatrix class.
21 #include <FBaseFloatMatrix.h>
22 #include <FBaseSysLog.h>
23 #include <unique_ptr.h>
25 namespace Tizen { namespace Base
28 FloatMatrix::FloatMatrix(void)
36 FloatMatrix::FloatMatrix(const FloatMatrix& rhs)
39 , __column(rhs.__column)
41 AllocateCapacity(__row, __column);
42 for (int i = 0; i < __row; i++)
44 for (int j = 0; j < __column; j++)
46 __pMatrix[i][j] = rhs.__pMatrix[i][j];
51 FloatMatrix::FloatMatrix(int rowCount, int columnCount)
54 , __column(columnCount)
56 AllocateCapacity(__row, __column);
60 FloatMatrix::FloatMatrix(int rowCount, int columnCount, const float* pArray, bool rowMajor)
63 , __column(columnCount)
65 AllocateCapacity(__row, __column);
70 for (int i = 0; i < __row; i++)
72 for (int j = 0; j < __column; j++)
74 __pMatrix[i][j] = pArray[i * __column + j];
80 for (int i = 0; i < __column; i++)
82 for (int j = 0; j < __row; j++)
84 __pMatrix[j][i] = pArray[i * __row + j];
95 FloatMatrix::FloatMatrix(int rowCount, int columnCount, const float* pArray[])
98 , __column(columnCount)
100 AllocateCapacity(__row, __column);
103 for (int i = 0; i < __row; i++)
105 for (int j = 0; j < __column; j++)
107 __pMatrix[i][j] = pArray[i][j];
117 FloatMatrix::~FloatMatrix(void)
119 for (int i = 0; i < __row; i++)
121 delete[] __pMatrix[i];
128 FloatMatrix::operator ==(const FloatMatrix& rhs) const
135 if ((__row != rhs.__row) || (__column != rhs.__column))
140 for (int i = 0; i < __row; i++)
142 for (int j = 0; j < __column; j++)
144 if (__pMatrix[i][j] != rhs.__pMatrix[i][j])
155 FloatMatrix::operator !=(const FloatMatrix& rhs) const
157 return !(*this == rhs);
161 FloatMatrix::operator =(const FloatMatrix& rhs)
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));
172 for (int i = 0; i < __row; i++)
174 for (int j = 0; j < __column; j++)
176 __pMatrix[i][j] = rhs.__pMatrix[i][j];
184 FloatMatrix::Equals(const Tizen::Base::Object& obj) const
186 const FloatMatrix* pOther = dynamic_cast< const FloatMatrix* >(&obj);
193 return (*this == *pOther);
197 FloatMatrix::GetHashCode(void) const
200 for (int i = 0; i < __row; i++)
202 for (int j = 0; j < __column; j++)
204 hash = hash + Tizen::Base::Float::GetHashCode(__pMatrix[i][j]);
212 FloatMatrix::Add(const FloatMatrix& matrix)
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.");
217 for (int i = 0; i < __row; i++)
219 for (int j = 0; j < __column; j++)
221 __pMatrix[i][j] = __pMatrix[i][j] + matrix.__pMatrix[i][j];
229 FloatMatrix::AddToEachElement(float value)
231 for (int i = 0; i < __row; i++)
233 for (int j = 0; j < __column; j++)
235 __pMatrix[i][j] = __pMatrix[i][j] + value;
241 FloatMatrix::AllocateCapacity(int rowCount, int columnCount)
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));
247 for (int i = 0; i < rowCount; i++)
249 pMatrix[i] = new (std::nothrow) float[columnCount];
250 if (pMatrix[i] == null)
252 for (int j = 0; j < i; j++)
260 __pMatrix = pMatrix.release();
266 FloatMatrix::CalculateDeterminant(float** pMatrix, int order) const // For perfomance, we have to change the logic of recursive to LU decomposition.
268 SysTryReturn(NID_BASE, pMatrix != null, 0.0f, E_INVALID_ARG, "[%s] Invalid argument is used. pMatrix is null.",
269 GetErrorMessage(E_INVALID_ARG));
273 return pMatrix[0][0];
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++)
282 pMinor[i] = new (std::nothrow) float[order - 1];
283 if (pMinor[i] == null)
285 for (int j = 0; j < i; j++)
293 bool signFlag = true;
294 for (int i = 0; i < order; i++)
296 GetMinor(pMatrix, pMinor.get(), 0, i, order);
298 if (signFlag == true)
300 determinant += pMatrix[0][i] * CalculateDeterminant(pMinor.get(), order - 1);
305 determinant += -pMatrix[0][i] * CalculateDeterminant(pMinor.get(), order - 1);
310 for (int i = 0; i < order - 1; i++)
319 FloatMatrix::GetColumnCount(void) const
325 FloatMatrix::GetColumnN(int columnIndex) const
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));
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));
335 for (int i = 0; i < __row; i++)
337 pColumn[i] = __pMatrix[i][columnIndex];
340 return pColumn.release();
344 FloatMatrix::GetDeterminant(void) const
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));
349 return CalculateDeterminant(__pMatrix, __row);
353 FloatMatrix::GetElement(int rowIndex, int columnIndex) const
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));
358 return __pMatrix[rowIndex][columnIndex];
362 FloatMatrix::GetInverseN(void) const
364 SysTryReturn(NID_BASE, __row == __column, null, E_INVALID_OPERATION,
365 "[%s] The current instance is not a square matrix.", GetErrorMessage(E_INVALID_OPERATION));
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));
371 float determinant = CalculateDeterminant(__pMatrix, __row);
373 if (Tizen::Base::Float::Compare(determinant, 0.0f) == 0)
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));
382 for (int i = 0; i < __row - 1; i++)
384 pMinor[i] = new (std::nothrow) float[__row - 1];
385 if (pMinor[i] == null)
387 for (int j = 0; j < i; j++)
396 for (int i = 0; i < __row; i++)
398 for (int j = 0; j < __row; j++)
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)
404 pInverseMatrix->__pMatrix[j][i] = -pInverseMatrix->__pMatrix[j][i];
409 for (int i = 0; i < __row - 1; i++)
414 return pInverseMatrix.release();
418 FloatMatrix::GetMinor(float** pSrc, float** pDest, int rowIndex, int columnIndex, int order) const
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));
426 for (int i = 0; i < order; i++)
432 for (int j = 0; j < order; j++)
434 if (j != columnIndex)
436 pDest[rowCount][columnCount] = pSrc[i][j];
446 FloatMatrix::GetRowCount(void) const
452 FloatMatrix::GetRowN(int rowIndex) const
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));
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));
462 for (int i = 0; i < __column; i++)
464 pRow[i] = __pMatrix[rowIndex][i];
467 return pRow.release();
471 FloatMatrix::GetTrace(float& value) const
473 SysTryReturnResult(NID_BASE, __row == __column, E_INVALID_OPERATION, "The current instance is not a square matrix.");
477 for (int i = 0; i < __row; i++)
479 value += __pMatrix[i][i];
486 FloatMatrix::GetTransposeN(void) const
488 SysTryReturn(NID_BASE, __row == __column, null, E_INVALID_OPERATION,
489 "[%s] The current instance is not a square matrix.", GetErrorMessage(E_INVALID_OPERATION));
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));
497 for (int i = 0; i < __row; i++)
499 for (int j = 0; j < columnIndex; j++)
501 float temp = pTransposeMatrix->__pMatrix[i][j];
502 pTransposeMatrix->__pMatrix[i][j] = pTransposeMatrix->__pMatrix[j][i];
503 pTransposeMatrix->__pMatrix[j][i] = temp;
509 return pTransposeMatrix.release();
513 FloatMatrix::IsIdentity(void) const
515 if (__row != __column)
520 for (int i = 0; i < __row; i++)
522 for (int j = 0; j < __column; j++)
526 if (Tizen::Base::Float::Compare(__pMatrix[i][j], 1.0f) != 0)
533 if (Tizen::Base::Float::Compare(__pMatrix[i][j], 0.0f) != 0)
546 FloatMatrix::IsInvertible(void) const
548 int ret = Tizen::Base::Float::Compare(GetDeterminant(), 0.0f);
561 FloatMatrix::Multiply(const FloatMatrix& matrix)
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.");
566 std::unique_ptr< float*[] > pResult(new (std::nothrow) float*[__row]);
567 SysTryReturnResult(NID_BASE, pResult != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
569 for (int i = 0; i < __row; i++)
571 pResult[i] = new (std::nothrow) float[matrix.__column];
572 if (pResult[i] == null)
574 for (int j = 0; j < i; j++)
578 return E_OUT_OF_MEMORY;
582 for (int i = 0; i < __row; i++)
584 for (int j = 0; j < matrix.__column; j++)
586 pResult[i][j] = 0.0f;
588 for (int k = 0; k < __column; k++)
590 pResult[i][j] += __pMatrix[i][k] * matrix.__pMatrix[k][j];
595 for (int i = 0; i < __row; i++)
597 delete[] __pMatrix[i];
601 __pMatrix = pResult.release();
607 FloatMatrix::Multiply(float value)
609 for (int i = 0; i < __row; i++)
611 for (int j = 0; j < __column; j++)
613 __pMatrix[i][j] = __pMatrix[i][j] * value;
619 FloatMatrix::Negate(void)
621 for (int i = 0; i < __row; i++)
623 for (int j = 0; j < __column; j++)
625 __pMatrix[i][j] = -__pMatrix[i][j];
631 FloatMatrix::SetAsIdentity(void)
633 SysTryReturnResult(NID_BASE, __row == __column, E_INVALID_OPERATION, "The current instance is not a square matrix.");
635 for (int i = 0; i < __row; i++)
637 for (int j = 0; j < __column; j++)
641 __pMatrix[i][j] = 1.0f;
645 __pMatrix[i][j] = 0.0f;
654 FloatMatrix::Invert(void)
656 SysTryReturnResult(NID_BASE, __row == __column, E_INVALID_OPERATION, "The current instance is not a square matrix.");
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.");
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));
666 for (int i = 0; i < __row; i++)
668 pInverse[i] = new (std::nothrow) float[__row];
669 if (pInverse[i] == null)
671 for (int j = 0; j < i; j++)
673 delete[] pInverse[j];
675 return E_OUT_OF_MEMORY;
679 std::unique_ptr< float*[] > pMinor(new (std::nothrow) float*[__row - 1]);
682 for (int i = 0; i < __row; i++)
684 delete[] pInverse[i];
686 return E_OUT_OF_MEMORY;
689 for (int i = 0; i < __row - 1; i++)
691 pMinor[i] = new (std::nothrow) float[__row - 1];
692 if (pMinor[i] == null)
694 for (int k = 0; k < __row; k++)
696 delete[] pInverse[k];
699 for (int j = 0; j < i; j++)
704 return E_OUT_OF_MEMORY;
708 for (int i = 0; i < __row; i++)
710 for (int j = 0; j < __row; j++)
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)
716 pInverse[j][i] = -pInverse[j][i];
721 for (int i = 0; i < __row; i++)
723 delete[] __pMatrix[i];
727 __pMatrix = pInverse.release();
729 for (int i = 0; i < __row - 1; i++)
738 FloatMatrix::Transpose(void)
740 SysTryReturnResult(NID_BASE, __row == __column, E_INVALID_OPERATION, "The current instance is not a square matrix.");
744 for (int i = 0; i < __row; i++)
746 for (int j = 0; j < columnIndex; j++)
748 float temp = __pMatrix[i][j];
749 __pMatrix[i][j] = __pMatrix[j][i];
750 __pMatrix[j][i] = temp;
760 FloatMatrix::SetColumn(int columnIndex, const float* pArray)
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.");
766 for (int i = 0; i < __row; i++)
768 __pMatrix[i][columnIndex] = pArray[i];
775 FloatMatrix::SetRow(int rowIndex, const float* pArray)
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.");
781 for (int i = 0; i < __column; i++)
783 __pMatrix[rowIndex][i] = pArray[i];
790 FloatMatrix::SetElement(int rowIndex, int columnIndex, float value)
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.");
797 __pMatrix[rowIndex][columnIndex] = value;
803 FloatMatrix::SetValue(const float* pArray, bool rowMajor)
805 SysTryReturnResult(NID_BASE, pArray != null, E_INVALID_ARG, "pArray is null.");
807 if (rowMajor == true)
809 for (int i = 0; i < __row; i++)
811 for (int j = 0; j < __column; j++)
813 __pMatrix[i][j] = pArray[i * __column + j];
819 for (int i = 0; i < __column; i++)
821 for (int j = 0; j < __row; j++)
823 __pMatrix[j][i] = pArray[i * __row + j];
832 FloatMatrix::SetAsNull(void)
834 for (int i = 0; i < __row; i++)
836 for (int j = 0; j < __column; j++)
838 __pMatrix[i][j] = 0.0f;
844 FloatMatrix::Subtract(const FloatMatrix& matrix)
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.");
849 for (int i = 0; i < __row; i++)
851 for (int j = 0; j < __column; j++)
853 __pMatrix[i][j] = __pMatrix[i][j] - matrix.__pMatrix[i][j];
861 FloatMatrix::SubtractToEachElement(float value)
863 for (int i = 0; i < __row; i++)
865 for (int j = 0; j < __column; j++)
867 __pMatrix[i][j] = __pMatrix[i][j] - value;