Page MenuHomec4science

Functions.cpp
No OneTemporary

File Metadata

Created
Fri, May 17, 16:30

Functions.cpp

void TerminalPrint(double** A,int rows, int cols){
/*Prints to terminal values of a matrix A*/
for (int i=0;i<rows;i++){
for (int j=0;j<cols;j++){
std::cout<<A[i][j]<<" ";
}
std::cout<<std::endl;
}
std::cout<<std::endl;
}
void TerminalPrint(double* u,int size){
/*Prints to terminal values of a vector u*/
for (int i=0;i<size;i++) {
std::cout << u[i]<<std::endl;
}
}
double** AllocateMemory(int rows, int cols){
/*Allocates memory for matrix A*/
double** A;
A=new double* [rows];
for (int i=0;i<rows;i++){
A[i]=new double[cols];
}
return A;
}
double* AllocateMemory(int rows){
/*Allocates memory for vector u*/
double* u=new double [rows];
return u;
}
void FreeMemory(double** A,int rows){
/*Frees memory of matrix A*/
for (int i=0;i<rows;i++){
delete[] A[i];
}
delete[] A;
}
void FreeMemory(double* u){
/*Frees memory of vector u*/
delete[] u;
}
double** Multiply(double** A,double** B,int rowsA, int colsA, int rowsB, int colsB){
/*Multiplies matrix A[rowsA][colsA] by matrix B[rowsB][colsB]*/
assert(rowsB==colsA);
double** C=AllocateMemory(rowsA,colsB);
for (int i=0;i<rowsA;i++){
for (int j=0;j<colsB;j++){
for (int k=0;k<rowsB;k++){
C[i][j]+=A[i][k]*B[k][j];
}
}
}
return C;
}
double* Multiply(double** A,double* u,int rowsA, int colsA, int rowsU){
/*Multiplies matrix A[rowsA][colsA] by vector U[rowsU]*/
assert(rowsU==colsA);
double* v=AllocateMemory(rowsA);
for (int i=0;i<rowsA;i++){
for (int j=0;j<colsA;j++){
v[i]+=A[i][j]*u[j];
}
}
return v;
}
double* Multiply(double* u,double** A,int rowsA, int colsA, int rowsU){
/*Multiplies vector U[rowsU] by matrix A[rowsA][colsA]*/
assert(rowsU==rowsA);
double* v=AllocateMemory(colsA);
for (int i=0;i<colsA;i++){
for (int j=0;j<rowsA;j++){
v[i]+=A[j][i]*u[j];
}
}
return v;
}
double** Multiply(double** A,double scalar, int rowsA, int colsA){
/*Multiplies matrix A[rowsA][colsA] by scalar*/
double** C=AllocateMemory(rowsA,colsA);
for (int i=0;i<rowsA;i++){
for (int j=0;j<colsA;j++){
C[i][j]=A[i][j]*scalar;
}
}
return C;
}
double* Multiply(double* u,double scalar, int rowsU){
/*Multiplies vector u[rowsU] by scalar*/
double* v=AllocateMemory(rowsU);
for (int i=0;i<rowsU;i++){
v[i]=u[i]*scalar;
}
return v;
}
void ReduceMatrix(double** A,int size,int col, double** &A_hat){
/*Resizing of matrix A for determinant computation*/
for (int row = 1; row < size; ++row) {
for (int k = 0; k < col; ++k) {
A_hat[row - 1][k] = A[row][k];
}
for (int k = col + 1; k < size; ++k) {
A_hat[row - 1][k - 1] = A[row][k];
}
}
}
double Determinant(double** A,int size){
/*Computation of determinant : direct formula for 2x2, recursive otherwise*/
if (size==2){
return A[0][0]*A[1][1]-A[1][0]*A[0][1];
} else {
double det=0;
for (int j = 0; j < size; ++j) {
double** A_hat=AllocateMemory(size-1,size-1);
ReduceMatrix(A,size,j,A_hat);
det +=A[0][j]*std::pow(-1.0,j)*Determinant(A_hat,size-1);
FreeMemory(A_hat,size-1);
}
return det;
}
}

Event Timeline