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 FBaseIntMatrix.cpp
20 * @brief This is the implementation for IntMatrix class.
23 #include <FBaseIntMatrix.h>
24 #include <FBaseSysLog.h>
25 #include <unique_ptr.h>
27 namespace Tizen { namespace Base
30 IntMatrix::IntMatrix(void)
38 IntMatrix::IntMatrix(const IntMatrix& 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 IntMatrix::IntMatrix(int rowCount, int columnCount)
56 , __column(columnCount)
58 AllocateCapacity(__row, __column);
62 IntMatrix::IntMatrix(int rowCount, int columnCount, const int* 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 * __row + j];
82 for (int i = 0; i < __column; i++)
84 for (int j = 0; j < __row; j++)
86 __pMatrix[j][i] = pArray[i * __column + j];
97 IntMatrix::IntMatrix(int rowCount, int columnCount, const int* 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 IntMatrix::~IntMatrix(void)
121 for ( int i = 0 ; i < __row ; i++ )
123 delete[] __pMatrix[i];
130 IntMatrix::operator ==(const IntMatrix& 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 IntMatrix::operator !=(const IntMatrix& rhs) const
159 return !(*this == rhs);
163 IntMatrix::operator =(const IntMatrix& 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 IntMatrix::Equals(const Tizen::Base::Object& obj) const
188 const IntMatrix* pOther = dynamic_cast <const IntMatrix*>(&obj);
195 return (*this == *pOther);
199 IntMatrix::GetHashCode(void) const
202 for (int i = 0; i < __row; i++)
204 for (int j = 0; j < __column; j++)
206 hash = hash + Tizen::Base::Integer(__pMatrix[i][j]).GetHashCode();
214 IntMatrix::Add(const IntMatrix& 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 IntMatrix::AddToEachElement(int 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 IntMatrix::AllocateCapacity(int rowCount, int columnCount)
245 std::unique_ptr<int* []> pMatrix(new (std::nothrow) int* [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) int[columnCount];
252 if (pMatrix[i] == null)
254 for (int j = 0; j < i; j++)
262 __pMatrix = pMatrix.release();
268 IntMatrix::CalculateDeterminant(int** pMatrix, int order) const // For perfomance, we have to change the logic of recursive to LU decomposition.
270 SysTryReturn(NID_BASE, pMatrix != null, 0, E_INVALID_ARG, "[%s] Invalid argument is used. pMatrix is null.",
271 GetErrorMessage(E_INVALID_ARG));
275 return pMatrix[0][0];
279 std::unique_ptr<int* []> pMinor(new (std::nothrow) int* [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) int[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 IntMatrix::GetColumnCount(void) const
327 IntMatrix::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<int []> pColumn(new (std::nothrow) int [__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 IntMatrix::GetDeterminant(void) const
348 SysTryReturn(NID_BASE, __row == __column, 0, E_INVALID_OPERATION,
349 "[%s] The current instance is not a square matrix.", GetErrorMessage(E_INVALID_OPERATION));
351 return CalculateDeterminant(__pMatrix, __row);
355 IntMatrix::GetElement(int rowIndex, int columnIndex) const
357 SysTryReturn(NID_BASE, (rowIndex <= __row) && (columnIndex <= __column), 0, 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 IntMatrix::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<IntMatrix> pInverseMatrix(new (std::nothrow) IntMatrix(__row, __column));
370 SysTryReturn(NID_BASE, pInverseMatrix != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
371 GetErrorMessage(E_OUT_OF_MEMORY));
373 int determinant = CalculateDeterminant(__pMatrix, __row);
375 if (determinant == 0)
380 std::unique_ptr<int* []> pMinor(new (std::nothrow) int* [__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) int[__row - 1];
387 if (pMinor[i] == null)
389 for (int j = 0; j < i; j++)
397 for (int i = 0; i < __row; i++)
399 for (int j = 0; j < __row; j++)
401 GetMinor(__pMatrix, pMinor.get(), i, j, __row);
402 pInverseMatrix->__pMatrix[j][i] = CalculateDeterminant(pMinor.get(), __row - 1) / determinant;
403 if ((i + j + 2) % 2 == 1)
405 pInverseMatrix->__pMatrix[j][i] = -pInverseMatrix->__pMatrix[j][i];
410 for ( int i = 0 ; i < __row - 1 ; i++)
415 return pInverseMatrix.release();
419 IntMatrix::GetMinor(int** pSrc, int** pDest, int rowIndex, int columnIndex, int order) const
421 SysTryReturn(NID_BASE, pSrc != null, , E_INVALID_ARG, "[%s] Invalid argument is used. pSrc is null.",
422 GetErrorMessage(E_INVALID_ARG));
423 SysTryReturn(NID_BASE, pDest != null, , E_INVALID_ARG, "[%s] Invalid argument is used. pDest is null.",
424 GetErrorMessage(E_INVALID_ARG));
429 for (int i = 0; i < order; i++)
435 for (int j = 0; j < order; j++)
437 if (j != columnIndex)
439 pDest[rowCount][columnCount] = pSrc[i][j];
449 IntMatrix::GetRowCount(void) const
455 IntMatrix::GetRowN(int rowIndex) const
457 SysTryReturn(NID_BASE, rowIndex <= __row, null, E_INVALID_ARG,
458 "[%s] Invalid argument is used. The rowIndex is larger than the row count of the current instance.",
459 GetErrorMessage(E_INVALID_ARG));
461 std::unique_ptr<int []> pRow(new (std::nothrow) int [__column]);
462 SysTryReturn(NID_BASE, pRow != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
463 GetErrorMessage(E_OUT_OF_MEMORY));
465 for (int i = 0; i < __column; i++)
467 pRow[i] = __pMatrix[rowIndex][i];
470 return pRow.release();
474 IntMatrix::GetTrace(int& value) const
476 SysTryReturnResult(NID_BASE, __row == __column, E_INVALID_OPERATION, "The current instance is not a square matrix.");
480 for (int i = 0; i < __row; i++)
482 value += __pMatrix[i][i];
489 IntMatrix::GetTransposeN(void) const
491 SysTryReturn(NID_BASE, __row == __column, null, E_INVALID_OPERATION,
492 "[%s] The current instance is not a square matrix.", GetErrorMessage(E_INVALID_OPERATION));
494 std::unique_ptr<IntMatrix> pTransposeMatrix(new (std::nothrow) IntMatrix(*this));
495 SysTryReturn(NID_BASE, pTransposeMatrix != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
496 GetErrorMessage(E_OUT_OF_MEMORY));
500 for (int i = 0; i < __row; i++)
502 for (int j = 0; j < columnIndex; j++)
504 int temp = pTransposeMatrix->__pMatrix[i][j];
505 pTransposeMatrix->__pMatrix[i][j] = pTransposeMatrix->__pMatrix[j][i];
506 pTransposeMatrix->__pMatrix[j][i] = temp;
512 return pTransposeMatrix.release();
516 IntMatrix::IsIdentity(void) const
518 if (__row != __column)
523 for (int i = 0; i < __row; i++)
525 for (int j = 0; j < __column; j++)
529 if (__pMatrix[i][j] != 1)
536 if (__pMatrix[i][j] != 0)
547 IntMatrix::IsInvertible(void) const
549 int ret = GetDeterminant();
562 IntMatrix::Multiply(const IntMatrix& matrix)
564 SysTryReturnResult(NID_BASE, __column == matrix.__row, E_INVALID_ARG,
565 "The column count of the current instance is not same with the row count of the specified instance.");
567 std::unique_ptr<int* []> pResult(new (std::nothrow) int* [__row]);
568 SysTryReturnResult(NID_BASE, pResult != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
570 for ( int i = 0 ; i < __row ; i++ )
572 pResult[i] = new int[matrix.__column];
573 if (pResult[i] == null)
575 for (int j = 0; j < i; j++)
579 return E_OUT_OF_MEMORY;
583 for (int i = 0; i < __row; i++)
585 for (int j = 0; j < matrix.__column; j++)
589 for (int k = 0; k < __column; k++)
591 pResult[i][j] += __pMatrix[i][k] * matrix.__pMatrix[k][j];
596 for ( int i = 0 ; i < __row ; i++ )
598 delete[] __pMatrix[i];
603 __pMatrix = pResult.release();
609 IntMatrix::Multiply(int 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 IntMatrix::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 IntMatrix::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++)
656 IntMatrix::Invert(void)
658 SysTryReturnResult(NID_BASE, __row == __column, E_INVALID_OPERATION, "The current instance is not a square matrix.");
660 int determinant = CalculateDeterminant(__pMatrix, __row);
661 SysTryReturnResult(NID_BASE, determinant != 0, E_INVALID_OPERATION, "The current instance is not invertible.");
663 std::unique_ptr<int* []> pInverse(new (std::nothrow) int* [__row]);
664 SysTryReturn(NID_BASE, pInverse != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
665 GetErrorMessage(E_OUT_OF_MEMORY));
667 for (int i = 0; i < __row; i++)
669 pInverse[i] = new (std::nothrow) int[__row];
670 if (pInverse[i] == null)
672 for (int j = 0; j < i; j++)
674 delete[] pInverse[j];
676 return E_OUT_OF_MEMORY;
680 std::unique_ptr<int* []> pMinor(new (std::nothrow) int* [__row - 1]);
683 for ( int i = 0 ; i < __row ; i++ )
685 delete[] pInverse[i];
687 return E_OUT_OF_MEMORY;
690 for (int i = 0; i < __row - 1; i++)
692 pMinor[i] = new int[__row - 1];
693 if (pMinor[i] == null)
695 for ( int i = 0 ; i < __row ; i++ )
697 delete[] pInverse[i];
700 for (int j = 0; j < i; j++)
705 return E_OUT_OF_MEMORY;
709 for (int i = 0; i < __row; i++)
711 for (int j = 0; j < __row; j++)
713 GetMinor(__pMatrix, pMinor.get(), i, j, __row);
714 pInverse[j][i] = CalculateDeterminant(pMinor.get(), __row - 1) / determinant;
715 if ((i +j +2) % 2 == 1)
717 pInverse[j][i] = -pInverse[j][i];
722 for ( int i = 0 ; i < __row ; i++ )
724 delete[] __pMatrix[i];
728 __pMatrix = pInverse.release();
730 for ( int i = 0 ; i < __row - 1; i++ )
739 IntMatrix::Transpose(void)
741 SysTryReturnResult(NID_BASE, __row == __column, E_INVALID_OPERATION, "The current instance is not a square matrix.");
745 for (int i = 0; i < __row; i++)
747 for (int j = 0; j < columnIndex; j++)
749 int temp = __pMatrix[i][j];
750 __pMatrix[i][j] = __pMatrix[j][i];
751 __pMatrix[j][i] = temp;
761 IntMatrix::SetColumn(int columnIndex, const int* pArray)
763 SysTryReturnResult(NID_BASE, pArray != null, E_INVALID_ARG, "pArray is null.");
764 SysTryReturnResult(NID_BASE, columnIndex <= __column, E_INVALID_ARG,
765 "columnIndex is larger than the column count of the current instance.");
767 for (int i = 0; i < __row; i++)
769 __pMatrix[i][columnIndex] = pArray[i];
776 IntMatrix::SetRow(int rowIndex, const int* pArray)
778 SysTryReturnResult(NID_BASE, pArray != null, E_INVALID_ARG, "pArray is null.");
779 SysTryReturnResult(NID_BASE, rowIndex <= __row, E_INVALID_ARG,
780 "rowIndex is larger than the row count of the current instance.");
782 for (int i = 0; i < __column; i++)
784 __pMatrix[rowIndex][i] = pArray[i];
791 IntMatrix::SetElement(int rowIndex, int columnIndex, int value)
793 SysTryReturnResult(NID_BASE, columnIndex <= __column, E_INVALID_ARG,
794 "columnIndex is larger than the column count of the current instance.");
795 SysTryReturnResult(NID_BASE, rowIndex <= __row, E_INVALID_ARG,
796 "rowIndex is larger than the row count of the current instance.");
798 __pMatrix[rowIndex][columnIndex] = value;
804 IntMatrix::SetValue(const int* pArray, bool rowMajor)
806 SysTryReturnResult(NID_BASE, pArray != null, E_INVALID_ARG, "pArray is null.");
808 if (rowMajor == true)
810 for (int i = 0; i < __row; i++)
812 for (int j = 0; j < __column; j++)
814 __pMatrix[i][j] = pArray[i * __row + j];
820 for (int i = 0; i < __column; i++)
822 for (int j = 0; j < __row; j++)
824 __pMatrix[j][i] = pArray[i * __column + j];
833 IntMatrix::SetAsNull(void)
835 for (int i = 0; i < __row; i++)
837 for (int j = 0; j < __column; j++)
845 IntMatrix::Subtract(const IntMatrix& matrix)
847 SysTryReturnResult(NID_BASE, (__row == matrix.__row) && (__column == matrix.__column), E_INVALID_ARG,
848 "Either row or column count of the current instance is not same with that of the specified instance.");
850 for (int i = 0; i < __row; i++)
852 for (int j = 0; j < __column; j++)
854 __pMatrix[i][j] = __pMatrix[i][j] - matrix.__pMatrix[i][j];
862 IntMatrix::SubtractToEachElement(int value)
864 for (int i = 0; i < __row; i++)
866 for (int j = 0; j < __column; j++)
868 __pMatrix[i][j] = __pMatrix[i][j] - value;