/******************************************************************************** * ReactPhysics3D physics library, http://code.google.com/p/reactphysics3d/ * * Copyright (c) 2010 Daniel Chappuis * ********************************************************************************* * * * Permission is hereby granted, free of charge, to any person obtaining a copy * * of this software and associated documentation files (the "Software"), to deal * * in the Software without restriction, including without limitation the rights * * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * * copies of the Software, and to permit persons to whom the Software is * * furnished to do so, subject to the following conditions: * * * * The above copyright notice and this permission notice shall be included in * * all copies or substantial portions of the Software. * * * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * * THE SOFTWARE. * ********************************************************************************/ // Libraries #include #include "Matrix.h" // Namespaces using namespace reactphysics3d; // Constructor without argument Matrix::Matrix() :nbRow(0), nbColumn(0) { array = 0; } // Constructor of the class Matrix Matrix::Matrix(int nbRow, int nbColumn) throw(std::invalid_argument) :nbRow(nbRow),nbColumn(nbColumn) { // Check the arguments if (nbRow>0 && nbColumn>0) { // Create the two dimensional dynamic array array = new double*[nbRow]; assert(array != 0); // Array pointer musn't be null for(int i=0; iarray; } void Matrix::changeSize(uint newNbRows, uint newNbColumns) { if (array) { // Destruction of the dynamic array for(int i=0; iarray; } // Create the two dimensional dynamic array array = new double*[newNbRows]; assert(array != 0); // Array pointer musn't be null for(int i=0; i nbRow || j+sizeColumns > nbColumn) { // Throw an exception throw std::invalid_argument("Error : The arguments are out of matrix bounds"); } // Compute the sub-matrix Matrix resultMatrix(sizeRows, sizeColumns); for (unsigned int k=0; k 0) { // Create a new matrix Matrix identityMatrix(dimension,dimension); // Fill in the identity matrix for(int i=0; i= subMatrix.nbColumn); assert(nbColumn-colIndex >= subMatrix.nbColumn); // Fill in the sub-matrix for (unsigned int i=0; igetValue(i,j) + matrix2.getValue(i,j)); } } // Return the sum matrix return sumMatrix; } else { // We throw an MathematicsException throw MathematicsException("MathematicsException : Addition of the matrices isn't possible beacause the size of the matrices aren't the same"); } } // Definition of the operator - for the substraction of two matrices with references Matrix Matrix::operator-(const Matrix& matrix2) const throw(MathematicsException) { if (nbRow == matrix2.nbRow && nbColumn == matrix2.nbColumn) { // Create a new matrix Matrix sumMatrix(nbRow, nbColumn); // Substract the two matrices for(int i=0; inbColumn; ++j) { sumMatrix.setValue(i, j, this->getValue(i,j) - matrix2.getValue(i,j)); } } // Return the sum matrix return sumMatrix; } else { // We throw a MathematicsException throw MathematicsException("MathematicsException : Substraction of the matrices isn't possible beacause the size of the matrices aren't the same"); } } // Overloaded operator * for the multiplication of the matrix with a number Matrix Matrix::operator*(double nb) const { // Creation of the result matrix Matrix result(nbRow, nbColumn); // Multiplication of the matrix with the number for(int i=0; isetValue(i,j, matrix2.getValue(i,j)); } } } // Return a reference to the matrix return *this; } else { // Throw a MathematicsException throw MathematicsException("MathematicsException : Assignment impossible because the size of the matrices aren't the same !"); } } // Overloaded operator for equality condition bool Matrix::operator==(const Matrix& matrix2) const throw(MathematicsException) { // Check if the matrices dimensions are compatible if (nbRow == matrix2.nbRow && nbColumn == matrix2.nbColumn) { for (int i=0; i