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.
21 #include <FBaseIntMatrix.h>
22 #include <FBaseSysLog.h>
23 #include <unique_ptr.h>
25 namespace Tizen { namespace Base
28 IntMatrix::IntMatrix(void)
36 IntMatrix::IntMatrix(const IntMatrix& 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 IntMatrix::IntMatrix(int rowCount, int columnCount)
54 , __column(columnCount)
56 AllocateCapacity(__row, __column);
60 IntMatrix::IntMatrix(int rowCount, int columnCount, const int* 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 IntMatrix::IntMatrix(int rowCount, int columnCount, const int* 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 IntMatrix::~IntMatrix(void)
119 for (int i = 0; i < __row; i++)
121 delete[] __pMatrix[i];
128 IntMatrix::operator ==(const IntMatrix& 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 IntMatrix::operator !=(const IntMatrix& rhs) const
157 return !(*this == rhs);
161 IntMatrix::operator =(const IntMatrix& 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 IntMatrix::Equals(const Tizen::Base::Object& obj) const
186 const IntMatrix* pOther = dynamic_cast< const IntMatrix* >(&obj);
193 return (*this == *pOther);
197 IntMatrix::GetHashCode(void) const
200 for (int i = 0; i < __row; i++)
202 for (int j = 0; j < __column; j++)
204 hash = hash + Tizen::Base::Integer(__pMatrix[i][j]).GetHashCode();
212 IntMatrix::Add(const IntMatrix& 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 IntMatrix::AddToEachElement(int 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 IntMatrix::AllocateCapacity(int rowCount, int columnCount)
243 std::unique_ptr< int*[] > pMatrix(new (std::nothrow) int*[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) int[columnCount];
250 if (pMatrix[i] == null)
252 for (int j = 0; j < i; j++)
260 __pMatrix = pMatrix.release();
266 IntMatrix::CalculateDeterminant(int** pMatrix, int order) const // For perfomance, we have to change the logic of recursive to LU decomposition.
268 SysTryReturn(NID_BASE, pMatrix != null, 0, E_INVALID_ARG, "[%s] Invalid argument is used. pMatrix is null.",
269 GetErrorMessage(E_INVALID_ARG));
273 return pMatrix[0][0];
277 std::unique_ptr< int*[] > pMinor(new (std::nothrow) int*[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) int[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 IntMatrix::GetColumnCount(void) const
325 IntMatrix::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< int[] > pColumn(new (std::nothrow) int[__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 IntMatrix::GetDeterminant(void) const
346 SysTryReturn(NID_BASE, __row == __column, 0, E_INVALID_OPERATION,
347 "[%s] The current instance is not a square matrix.", GetErrorMessage(E_INVALID_OPERATION));
349 return CalculateDeterminant(__pMatrix, __row);
353 IntMatrix::GetElement(int rowIndex, int columnIndex) const
355 SysTryReturn(NID_BASE, (rowIndex <= __row) && (columnIndex <= __column), 0, 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 IntMatrix::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< IntMatrix > pInverseMatrix(new (std::nothrow) IntMatrix(__row, __column));
368 SysTryReturn(NID_BASE, pInverseMatrix != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
369 GetErrorMessage(E_OUT_OF_MEMORY));
371 int determinant = CalculateDeterminant(__pMatrix, __row);
373 if (determinant == 0)
378 std::unique_ptr< int*[] > pMinor(new (std::nothrow) int*[__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) int[__row - 1];
385 if (pMinor[i] == null)
387 for (int j = 0; j < i; j++)
395 for (int i = 0; i < __row; i++)
397 for (int j = 0; j < __row; j++)
399 GetMinor(__pMatrix, pMinor.get(), i, j, __row);
400 pInverseMatrix->__pMatrix[j][i] = CalculateDeterminant(pMinor.get(), __row - 1) / determinant;
401 if ((i + j + 2) % 2 == 1)
403 pInverseMatrix->__pMatrix[j][i] = -pInverseMatrix->__pMatrix[j][i];
408 for (int i = 0; i < __row - 1; i++)
413 return pInverseMatrix.release();
417 IntMatrix::GetMinor(int** pSrc, int** pDest, int rowIndex, int columnIndex, int order) const
419 SysTryReturn(NID_BASE, pSrc != null, , E_INVALID_ARG, "[%s] Invalid argument is used. pSrc is null.",
420 GetErrorMessage(E_INVALID_ARG));
421 SysTryReturn(NID_BASE, pDest != null, , E_INVALID_ARG, "[%s] Invalid argument is used. pDest is null.",
422 GetErrorMessage(E_INVALID_ARG));
427 for (int i = 0; i < order; i++)
433 for (int j = 0; j < order; j++)
435 if (j != columnIndex)
437 pDest[rowCount][columnCount] = pSrc[i][j];
447 IntMatrix::GetRowCount(void) const
453 IntMatrix::GetRowN(int rowIndex) const
455 SysTryReturn(NID_BASE, rowIndex <= __row, null, E_INVALID_ARG,
456 "[%s] Invalid argument is used. The rowIndex is larger than the row count of the current instance.",
457 GetErrorMessage(E_INVALID_ARG));
459 std::unique_ptr< int[] > pRow(new (std::nothrow) int[__column]);
460 SysTryReturn(NID_BASE, pRow != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
461 GetErrorMessage(E_OUT_OF_MEMORY));
463 for (int i = 0; i < __column; i++)
465 pRow[i] = __pMatrix[rowIndex][i];
468 return pRow.release();
472 IntMatrix::GetTrace(int& value) const
474 SysTryReturnResult(NID_BASE, __row == __column, E_INVALID_OPERATION, "The current instance is not a square matrix.");
478 for (int i = 0; i < __row; i++)
480 value += __pMatrix[i][i];
487 IntMatrix::GetTransposeN(void) const
489 SysTryReturn(NID_BASE, __row == __column, null, E_INVALID_OPERATION,
490 "[%s] The current instance is not a square matrix.", GetErrorMessage(E_INVALID_OPERATION));
492 std::unique_ptr< IntMatrix > pTransposeMatrix(new (std::nothrow) IntMatrix(*this));
493 SysTryReturn(NID_BASE, pTransposeMatrix != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
494 GetErrorMessage(E_OUT_OF_MEMORY));
498 for (int i = 0; i < __row; i++)
500 for (int j = 0; j < columnIndex; j++)
502 int temp = pTransposeMatrix->__pMatrix[i][j];
503 pTransposeMatrix->__pMatrix[i][j] = pTransposeMatrix->__pMatrix[j][i];
504 pTransposeMatrix->__pMatrix[j][i] = temp;
510 return pTransposeMatrix.release();
514 IntMatrix::IsIdentity(void) const
516 if (__row != __column)
521 for (int i = 0; i < __row; i++)
523 for (int j = 0; j < __column; j++)
527 if (__pMatrix[i][j] != 1)
534 if (__pMatrix[i][j] != 0)
545 IntMatrix::IsInvertible(void) const
547 int ret = GetDeterminant();
560 IntMatrix::Multiply(const IntMatrix& matrix)
562 SysTryReturnResult(NID_BASE, __column == matrix.__row, E_INVALID_ARG,
563 "The column count of the current instance is not same with the row count of the specified instance.");
565 std::unique_ptr< int*[] > pResult(new (std::nothrow) int*[__row]);
566 SysTryReturnResult(NID_BASE, pResult != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
568 for (int i = 0; i < __row; i++)
570 pResult[i] = new int[matrix.__column];
571 if (pResult[i] == null)
573 for (int j = 0; j < i; j++)
577 return E_OUT_OF_MEMORY;
581 for (int i = 0; i < __row; i++)
583 for (int j = 0; j < matrix.__column; j++)
587 for (int k = 0; k < __column; k++)
589 pResult[i][j] += __pMatrix[i][k] * matrix.__pMatrix[k][j];
594 for (int i = 0; i < __row; i++)
596 delete[] __pMatrix[i];
601 __pMatrix = pResult.release();
607 IntMatrix::Multiply(int 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 IntMatrix::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 IntMatrix::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++)
654 IntMatrix::Invert(void)
656 SysTryReturnResult(NID_BASE, __row == __column, E_INVALID_OPERATION, "The current instance is not a square matrix.");
658 int determinant = CalculateDeterminant(__pMatrix, __row);
659 SysTryReturnResult(NID_BASE, determinant != 0, E_INVALID_OPERATION, "The current instance is not invertible.");
661 std::unique_ptr< int*[] > pInverse(new (std::nothrow) int*[__row]);
662 SysTryReturn(NID_BASE, pInverse != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
663 GetErrorMessage(E_OUT_OF_MEMORY));
665 for (int i = 0; i < __row; i++)
667 pInverse[i] = new (std::nothrow) int[__row];
668 if (pInverse[i] == null)
670 for (int j = 0; j < i; j++)
672 delete[] pInverse[j];
674 return E_OUT_OF_MEMORY;
678 std::unique_ptr< int*[] > pMinor(new (std::nothrow) int*[__row - 1]);
681 for (int i = 0; i < __row; i++)
683 delete[] pInverse[i];
685 return E_OUT_OF_MEMORY;
688 for (int i = 0; i < __row - 1; i++)
690 pMinor[i] = new int[__row - 1];
691 if (pMinor[i] == null)
693 for (int k = 0; k < __row; k++)
695 delete[] pInverse[k];
698 for (int j = 0; j < i; j++)
703 return E_OUT_OF_MEMORY;
707 for (int i = 0; i < __row; i++)
709 for (int j = 0; j < __row; j++)
711 GetMinor(__pMatrix, pMinor.get(), i, j, __row);
712 pInverse[j][i] = CalculateDeterminant(pMinor.get(), __row - 1) / determinant;
713 if ((i + j + 2) % 2 == 1)
715 pInverse[j][i] = -pInverse[j][i];
720 for (int i = 0; i < __row; i++)
722 delete[] __pMatrix[i];
726 __pMatrix = pInverse.release();
728 for (int i = 0; i < __row - 1; i++)
737 IntMatrix::Transpose(void)
739 SysTryReturnResult(NID_BASE, __row == __column, E_INVALID_OPERATION, "The current instance is not a square matrix.");
743 for (int i = 0; i < __row; i++)
745 for (int j = 0; j < columnIndex; j++)
747 int temp = __pMatrix[i][j];
748 __pMatrix[i][j] = __pMatrix[j][i];
749 __pMatrix[j][i] = temp;
759 IntMatrix::SetColumn(int columnIndex, const int* pArray)
761 SysTryReturnResult(NID_BASE, pArray != null, E_INVALID_ARG, "pArray is null.");
762 SysTryReturnResult(NID_BASE, columnIndex <= __column, E_INVALID_ARG,
763 "columnIndex is larger than the column count of the current instance.");
765 for (int i = 0; i < __row; i++)
767 __pMatrix[i][columnIndex] = pArray[i];
774 IntMatrix::SetRow(int rowIndex, const int* pArray)
776 SysTryReturnResult(NID_BASE, pArray != null, E_INVALID_ARG, "pArray is null.");
777 SysTryReturnResult(NID_BASE, rowIndex <= __row, E_INVALID_ARG,
778 "rowIndex is larger than the row count of the current instance.");
780 for (int i = 0; i < __column; i++)
782 __pMatrix[rowIndex][i] = pArray[i];
789 IntMatrix::SetElement(int rowIndex, int columnIndex, int value)
791 SysTryReturnResult(NID_BASE, columnIndex <= __column, E_INVALID_ARG,
792 "columnIndex is larger than the column count of the current instance.");
793 SysTryReturnResult(NID_BASE, rowIndex <= __row, E_INVALID_ARG,
794 "rowIndex is larger than the row count of the current instance.");
796 __pMatrix[rowIndex][columnIndex] = value;
802 IntMatrix::SetValue(const int* pArray, bool rowMajor)
804 SysTryReturnResult(NID_BASE, pArray != null, E_INVALID_ARG, "pArray is null.");
806 if (rowMajor == true)
808 for (int i = 0; i < __row; i++)
810 for (int j = 0; j < __column; j++)
812 __pMatrix[i][j] = pArray[i * __column + j];
818 for (int i = 0; i < __column; i++)
820 for (int j = 0; j < __row; j++)
822 __pMatrix[j][i] = pArray[i * __row + j];
831 IntMatrix::SetAsNull(void)
833 for (int i = 0; i < __row; i++)
835 for (int j = 0; j < __column; j++)
843 IntMatrix::Subtract(const IntMatrix& matrix)
845 SysTryReturnResult(NID_BASE, (__row == matrix.__row) && (__column == matrix.__column), E_INVALID_ARG,
846 "Either row or column count of the current instance is not same with that of the specified instance.");
848 for (int i = 0; i < __row; i++)
850 for (int j = 0; j < __column; j++)
852 __pMatrix[i][j] = __pMatrix[i][j] - matrix.__pMatrix[i][j];
860 IntMatrix::SubtractToEachElement(int value)
862 for (int i = 0; i < __row; i++)
864 for (int j = 0; j < __column; j++)
866 __pMatrix[i][j] = __pMatrix[i][j] - value;