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 FBaseDoubleMatrix.cpp
19 * @brief This is the implementation for DoubleMatrix class.
22 #include <FBaseDoubleMatrix.h>
23 #include <FBaseSysLog.h>
24 #include <unique_ptr.h>
26 namespace Tizen { namespace Base
29 DoubleMatrix::DoubleMatrix(void)
37 DoubleMatrix::DoubleMatrix(const DoubleMatrix& 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 DoubleMatrix::DoubleMatrix(int rowCount, int columnCount)
55 , __column(columnCount)
57 AllocateCapacity(__row, __column);
61 DoubleMatrix::DoubleMatrix(int rowCount, int columnCount, const double* 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 DoubleMatrix::DoubleMatrix(int rowCount, int columnCount, const double* 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 DoubleMatrix::~DoubleMatrix(void)
120 for ( int i = 0 ; i < __row ; i++ )
122 delete[] __pMatrix[i];
129 DoubleMatrix::operator ==(const DoubleMatrix& 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 DoubleMatrix::operator !=(const DoubleMatrix& rhs) const
158 return !(*this == rhs);
162 DoubleMatrix::operator =(const DoubleMatrix& 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 DoubleMatrix::Equals(const Tizen::Base::Object& obj) const
187 const DoubleMatrix* pOther = dynamic_cast <const DoubleMatrix*>(&obj);
194 return (*this == *pOther);
198 DoubleMatrix::GetHashCode(void) const
201 for (int i = 0; i < __row; i++)
203 for (int j = 0; j < __column; j++)
205 hash = hash + Tizen::Base::Double::GetHashCode(__pMatrix[i][j]);
213 DoubleMatrix::Add(const DoubleMatrix& 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 DoubleMatrix::AddToEachElement(double 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 DoubleMatrix::AllocateCapacity(int rowCount, int columnCount)
244 std::unique_ptr<double* []> pMatrix(new (std::nothrow) double* [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) double[columnCount];
251 if (pMatrix[i] == null)
253 for (int j = 0; j < i; j++)
261 __pMatrix = pMatrix.release();
267 DoubleMatrix::CalculateDeterminant(double** pMatrix, int order) const // For perfomance, we have to change the logic of recursive to LU decomposition.
269 SysTryReturn(NID_BASE, pMatrix != null, 0.0f, E_INVALID_ARG, "[%s] Invalid argument is used. pMatrix is null.",
270 GetErrorMessage(E_INVALID_ARG));
274 return pMatrix[0][0];
277 double determinant = 0.0f;
278 std::unique_ptr<double* []> pMinor(new (std::nothrow) double* [order - 1]);
279 SysTryReturn(NID_BASE, pMinor != null, determinant, 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) double[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 DoubleMatrix::GetColumnCount(void) const
326 DoubleMatrix::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<double []> pColumn(new (std::nothrow) double [__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 DoubleMatrix::GetDeterminant(void) const
347 SysTryReturn(NID_BASE, __row == __column, 0.0f, E_INVALID_OPERATION,
348 "[%s] The current instance is not a square matrix.", GetErrorMessage(E_INVALID_OPERATION));
350 return CalculateDeterminant(__pMatrix, __row);
354 DoubleMatrix::GetElement(int rowIndex, int columnIndex) const
356 SysTryReturn(NID_BASE, (rowIndex <= __row) && (columnIndex <= __column), 0.0f, 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 DoubleMatrix::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<DoubleMatrix> pInverseMatrix(new (std::nothrow) DoubleMatrix(__row, __column));
369 SysTryReturn(NID_BASE, pInverseMatrix != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
370 GetErrorMessage(E_OUT_OF_MEMORY));
372 double determinant = CalculateDeterminant(__pMatrix, __row);
374 if (Tizen::Base::Double::Compare(determinant,0.0f) == 0)
379 std::unique_ptr<double* []> pMinor(new (std::nothrow) double* [__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) double[__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 DoubleMatrix::GetMinor(double** pSrc, double** 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 DoubleMatrix::GetRowCount(void) const
454 DoubleMatrix::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<double []> pRow(new (std::nothrow) double [__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 DoubleMatrix::GetTrace(double& 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 DoubleMatrix::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<DoubleMatrix> pTransposeMatrix(new (std::nothrow) DoubleMatrix(*this));
494 SysTryReturn(NID_BASE, pTransposeMatrix != null, null, E_OUT_OF_MEMORY,
495 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
499 for (int i = 0; i < __row; i++)
501 for (int j = 0; j < columnIndex; j++)
503 double 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 DoubleMatrix::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::Double::Compare(__pMatrix[i][j],1.0f) != 0)
535 if (Tizen::Base::Double::Compare(__pMatrix[i][j],0.0f) != 0)
548 DoubleMatrix::IsInvertible(void) const
550 int ret = Tizen::Base::Double::Compare(GetDeterminant(),0.0f);
563 DoubleMatrix::Multiply(const DoubleMatrix& 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<double* []> pResult(new (std::nothrow) double* [__row]);
569 SysTryReturnResult(NID_BASE, pResult != null, E_OUT_OF_MEMORY, "Allocating memory is failed.");
571 for ( int i = 0 ; i < __row ; i++ )
573 pResult[i] = new (std::nothrow) double[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];
604 __pMatrix = pResult.release();
610 DoubleMatrix::Multiply(double value)
612 for (int i = 0; i < __row; i++)
614 for (int j = 0; j < __column; j++)
616 __pMatrix[i][j] = __pMatrix[i][j] * value;
622 DoubleMatrix::Negate(void)
624 for (int i = 0; i < __row; i++)
626 for (int j = 0; j < __column; j++)
628 __pMatrix[i][j] = -__pMatrix[i][j];
634 DoubleMatrix::SetAsIdentity(void)
636 SysTryReturnResult(NID_BASE, __row == __column, E_INVALID_OPERATION, "The current instance is not a square matrix.");
638 for (int i = 0; i < __row; i++)
640 for (int j = 0; j < __column; j++)
644 __pMatrix[i][j] = 1.0f;
648 __pMatrix[i][j] = 0.0f;
657 DoubleMatrix::Invert(void)
659 SysTryReturnResult(NID_BASE, __row == __column, E_INVALID_OPERATION, "The current instance is not a square matrix.");
661 double determinant = CalculateDeterminant(__pMatrix, __row);
662 SysTryReturnResult(NID_BASE, Tizen::Base::Double::Compare(determinant, 0.0f) != 0, E_INVALID_OPERATION,
663 "The current instance is not invertible.");
665 std::unique_ptr<double* []> pInverse(new (std::nothrow) double* [__row]);
666 SysTryReturnResult(NID_BASE, pInverse != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
667 GetErrorMessage(E_OUT_OF_MEMORY));
669 for (int i = 0; i < __row; i++)
671 pInverse[i] = new (std::nothrow) double[__row];
672 if (pInverse[i] == null)
674 for (int j = 0; j < i; j++)
676 delete[] pInverse[j];
678 return E_OUT_OF_MEMORY;
682 std::unique_ptr<double* []> pMinor(new (std::nothrow) double* [__row - 1]);
685 for ( int i = 0 ; i < __row ; i++ )
687 delete[] pInverse[i];
689 return E_OUT_OF_MEMORY;
692 for (int i = 0; i < __row -1; i++)
694 pMinor[i] = new double[__row - 1];
695 if (pMinor[i] == null)
697 for( int k = 0 ; k < __row ; k++ )
699 delete[] pInverse[k];
702 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 DoubleMatrix::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 double temp = __pMatrix[i][j];
751 __pMatrix[i][j] = __pMatrix[j][i];
752 __pMatrix[j][i] = temp;
762 DoubleMatrix::SetColumn(int columnIndex, const double* 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 DoubleMatrix::SetRow(int rowIndex, const double* 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 DoubleMatrix::SetElement(int rowIndex, int columnIndex, double 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 DoubleMatrix::SetValue(const double* 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 DoubleMatrix::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 DoubleMatrix::Subtract(const DoubleMatrix& 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 DoubleMatrix::SubtractToEachElement(double 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;