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 FBaseIntMatrix.cpp
19 * @brief This is the implementation for IntMatrix class.
22 #include <FBaseIntMatrix.h>
23 #include <FBaseSysLog.h>
24 #include <unique_ptr.h>
26 namespace Tizen { namespace Base
29 IntMatrix::IntMatrix(void)
37 IntMatrix::IntMatrix(const IntMatrix& rhs)
40 , __column(rhs.__column)
42 AllocateCapacity(__row, __column);
43 for (int i = 0; i < __row; i++)
45 for (int j = 0; j < __column; j++)
47 __pMatrix[i][j] = rhs.__pMatrix[i][j];
52 IntMatrix::IntMatrix(int rowCount, int columnCount)
55 , __column(columnCount)
57 AllocateCapacity(__row, __column);
61 IntMatrix::IntMatrix(int rowCount, int columnCount, const int* pArray, bool rowMajor)
64 , __column(columnCount)
66 AllocateCapacity(__row, __column);
71 for (int i = 0; i < __row; i++)
73 for (int j = 0; j < __column; j++)
75 __pMatrix[i][j] = pArray[i * __column + j];
81 for (int i = 0; i < __column; i++)
83 for (int j = 0; j < __row; j++)
85 __pMatrix[j][i] = pArray[i * __row + j];
96 IntMatrix::IntMatrix(int rowCount, int columnCount, const int* pArray[])
99 , __column(columnCount)
101 AllocateCapacity(__row, __column);
104 for (int i = 0; i < __row; i++)
106 for (int j = 0; j < __column; j++)
108 __pMatrix[i][j] = pArray[i][j];
118 IntMatrix::~IntMatrix(void)
120 for ( int i = 0 ; i < __row ; i++ )
122 delete[] __pMatrix[i];
129 IntMatrix::operator ==(const IntMatrix& rhs) const
136 if ((__row != rhs.__row) || (__column != rhs.__column))
141 for (int i = 0; i < __row; i++)
143 for (int j = 0; j < __column; j++)
145 if (__pMatrix[i][j] != rhs.__pMatrix[i][j])
156 IntMatrix::operator !=(const IntMatrix& rhs) const
158 return !(*this == rhs);
162 IntMatrix::operator =(const IntMatrix& rhs)
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));
173 for (int i = 0; i < __row; i++)
175 for (int j = 0; j < __column; j++)
177 __pMatrix[i][j] = rhs.__pMatrix[i][j];
185 IntMatrix::Equals(const Tizen::Base::Object& obj) const
187 const IntMatrix* pOther = dynamic_cast <const IntMatrix*>(&obj);
194 return (*this == *pOther);
198 IntMatrix::GetHashCode(void) const
201 for (int i = 0; i < __row; i++)
203 for (int j = 0; j < __column; j++)
205 hash = hash + Tizen::Base::Integer(__pMatrix[i][j]).GetHashCode();
213 IntMatrix::Add(const IntMatrix& matrix)
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.");
218 for (int i = 0; i < __row; i++)
220 for (int j = 0; j < __column; j++)
222 __pMatrix[i][j] = __pMatrix[i][j] + matrix.__pMatrix[i][j];
230 IntMatrix::AddToEachElement(int value)
232 for (int i = 0; i < __row; i++)
234 for (int j = 0; j < __column; j++)
236 __pMatrix[i][j] = __pMatrix[i][j] + value;
242 IntMatrix::AllocateCapacity(int rowCount, int columnCount)
244 std::unique_ptr<int* []> pMatrix(new (std::nothrow) int* [rowCount]);
245 SysTryReturn(NID_BASE, pMatrix != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
246 GetErrorMessage(E_OUT_OF_MEMORY));
248 for ( int i = 0 ; i < rowCount ; i++ )
250 pMatrix[i] = new (std::nothrow) int[columnCount];
251 if (pMatrix[i] == null)
253 for (int j = 0; j < i; j++)
261 __pMatrix = pMatrix.release();
267 IntMatrix::CalculateDeterminant(int** pMatrix, int order) const // For perfomance, we have to change the logic of recursive to LU decomposition.
269 SysTryReturn(NID_BASE, pMatrix != null, 0, E_INVALID_ARG, "[%s] Invalid argument is used. pMatrix is null.",
270 GetErrorMessage(E_INVALID_ARG));
274 return pMatrix[0][0];
278 std::unique_ptr<int* []> pMinor(new (std::nothrow) int* [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++)
283 pMinor[i] = new (std::nothrow) int[order - 1];
284 if (pMinor[i] == null)
286 for (int j = 0; j < i; j++)
294 bool signFlag = true;
295 for (int i = 0; i < order; i++)
297 GetMinor(pMatrix, pMinor.get(), 0, i, order);
299 if (signFlag == true)
301 determinant += pMatrix[0][i] * CalculateDeterminant(pMinor.get(), order -1);
306 determinant += -pMatrix[0][i] * CalculateDeterminant(pMinor.get(), order -1);
311 for (int i = 0; i < order -1; i++)
320 IntMatrix::GetColumnCount(void) const
326 IntMatrix::GetColumnN(int columnIndex) const
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));
332 std::unique_ptr<int []> pColumn(new (std::nothrow) int [__row]);
333 SysTryReturn(NID_BASE, pColumn != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
334 GetErrorMessage(E_OUT_OF_MEMORY));
336 for (int i = 0; i < __row; i++)
338 pColumn[i] = __pMatrix[i][columnIndex];
341 return pColumn.release();
345 IntMatrix::GetDeterminant(void) const
347 SysTryReturn(NID_BASE, __row == __column, 0, E_INVALID_OPERATION,
348 "[%s] The current instance is not a square matrix.", GetErrorMessage(E_INVALID_OPERATION));
350 return CalculateDeterminant(__pMatrix, __row);
354 IntMatrix::GetElement(int rowIndex, int columnIndex) const
356 SysTryReturn(NID_BASE, (rowIndex <= __row) && (columnIndex <= __column), 0, E_INVALID_ARG,
357 "[%s] Invalid argument is used. The current instance is not a square matrix.", GetErrorMessage(E_INVALID_ARG));
359 return __pMatrix[rowIndex][columnIndex];
363 IntMatrix::GetInverseN(void) const
365 SysTryReturn(NID_BASE, __row == __column, null, E_INVALID_OPERATION,
366 "[%s] The current instance is not a square matrix.", GetErrorMessage(E_INVALID_OPERATION));
368 std::unique_ptr<IntMatrix> pInverseMatrix(new (std::nothrow) IntMatrix(__row, __column));
369 SysTryReturn(NID_BASE, pInverseMatrix != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
370 GetErrorMessage(E_OUT_OF_MEMORY));
372 int determinant = CalculateDeterminant(__pMatrix, __row);
374 if (determinant == 0)
379 std::unique_ptr<int* []> pMinor(new (std::nothrow) int* [__row - 1]);
380 SysTryReturn(NID_BASE, pMinor != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
381 GetErrorMessage(E_OUT_OF_MEMORY));
383 for (int i = 0; i < __row - 1; i++)
385 pMinor[i] = new (std::nothrow) int[__row - 1];
386 if (pMinor[i] == null)
388 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 IntMatrix::GetMinor(int** pSrc, int** 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.",
421 GetErrorMessage(E_INVALID_ARG));
422 SysTryReturn(NID_BASE, pDest != null, , E_INVALID_ARG, "[%s] Invalid argument is used. pDest is null.",
423 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 IntMatrix::GetRowCount(void) const
454 IntMatrix::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<int []> pRow(new (std::nothrow) int [__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 IntMatrix::GetTrace(int& 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 IntMatrix::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<IntMatrix> pTransposeMatrix(new (std::nothrow) IntMatrix(*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 int 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 IntMatrix::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 (__pMatrix[i][j] != 1)
535 if (__pMatrix[i][j] != 0)
546 IntMatrix::IsInvertible(void) const
548 int ret = GetDeterminant();
561 IntMatrix::Multiply(const IntMatrix& 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<int* []> pResult(new (std::nothrow) int* [__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 int[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++)
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];
602 __pMatrix = pResult.release();
608 IntMatrix::Multiply(int value)
610 for (int i = 0; i < __row; i++)
612 for (int j = 0; j < __column; j++)
614 __pMatrix[i][j] = __pMatrix[i][j] * value;
620 IntMatrix::Negate(void)
622 for (int i = 0; i < __row; i++)
624 for (int j = 0; j < __column; j++)
626 __pMatrix[i][j] = -__pMatrix[i][j];
632 IntMatrix::SetAsIdentity(void)
634 SysTryReturnResult(NID_BASE, __row == __column, E_INVALID_OPERATION, "The current instance is not a square matrix.");
636 for (int i = 0; i < __row; i++)
638 for (int j = 0; j < __column; j++)
655 IntMatrix::Invert(void)
657 SysTryReturnResult(NID_BASE, __row == __column, E_INVALID_OPERATION, "The current instance is not a square matrix.");
659 int determinant = CalculateDeterminant(__pMatrix, __row);
660 SysTryReturnResult(NID_BASE, determinant != 0, E_INVALID_OPERATION, "The current instance is not invertible.");
662 std::unique_ptr<int* []> pInverse(new (std::nothrow) int* [__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) int[__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<int* []> pMinor(new (std::nothrow) int* [__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 int[__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 IntMatrix::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 int temp = __pMatrix[i][j];
749 __pMatrix[i][j] = __pMatrix[j][i];
750 __pMatrix[j][i] = temp;
760 IntMatrix::SetColumn(int columnIndex, const int* 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 IntMatrix::SetRow(int rowIndex, const int* 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 IntMatrix::SetElement(int rowIndex, int columnIndex, int 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 IntMatrix::SetValue(const int* 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 IntMatrix::SetAsNull(void)
834 for (int i = 0; i < __row; i++)
836 for (int j = 0; j < __column; j++)
844 IntMatrix::Subtract(const IntMatrix& 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 IntMatrix::SubtractToEachElement(int 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;