# Untitled

unknown
plain_text
2 years ago
5.9 kB
6
Indexable
Never
```Step 1/9
Let's begin,
All the prototypes are already given by you, you can simply put them in matrixLib.h file.

The implementaion has to be put in matrixLib.c file, let's start implementing,

(i) returnVector function
float* returnVector(int size){
return new float[size];
}

(ii) returnMatrix function
float **returnMatrix(int row,int col){
// create row number of pointers for rows
float** a = new float*[row];

// for each row create a array of col size
for (int i = 0; i < row; i++) {
a[i] = new int[col];
}
return a;
}

Explanationfor step 1
new keyword reserves space on heap and return a pointer to the memory location.
Step 2/9
(iii) freeMatrix

void freeMatrix(float **mat, int row){
for(int i = 0; i < row; i++)
free(mat[i]);       // free releases the memory pointer
free(mat);
}

(iv) mean
float mean(float *vec, int size){
float sum=0;
for(int i=0; i<vec.size(); i++){
sum+=vec[i];
}
return sum/size;
}

Step 3/9
(v) variance
float variance(float *vec1, float *vec2, int size){
float sum=0;
// taking sum of all the elements in both arrays
for (i = 0; i < n; i++){
sum+=vec1[i];
sum+=vec2[i];
}
float mean=sum/(2*size);    // calculating mean

// calculating variance
float sum1=0;
for(int i=0; i<n; i++){
sum1 = sum1 + pow((vec1[i] - mean), 2);
sum1 = sum1 + pow((vec2[i] - mean), 2);
}

float variance = sum1 / (2*n);

return variance;
}
You can reference formulas for variance from wikipedia.
Step 4/9
(vi) calculation covariance, the only difference between the formulas of variance and co-variance is the numerator term, you can see that in the code.

float variance(float *vec1, float *vec2, int size){
// calculating mean for the two arrays separately
float mean1=mean(vec1, vec1.size());
float mean2=mean(vec2, vec2.size());

// calculating variance
float sum1=0;
for(int i=0; i<n; i++){
sum1 = sum1 + (pow((vec1[i] - mean1), 2)*pow((vec2[i] - mean2), 2));
}

float variance = sum1 / n;

return variance;
}
Again, you can reference the formulas from wikipedia.
Step 5/9
(vii) matrix multiplication.

Algorithm for multiplication:
Initialise the value of the element (i, j) of the new matrix to 0.
Set an inner loop inside the above loop from k=0 to k=p. Here p refers to number of columns in first matrix or rows in second matrix.
Using the add and assign operator (+=) store the value of a[i][k] * b[k][j] in the third matrix, c[i][j].
Print the third matrix.
Stop.
float **matrixMultiplication(float **mat1, float **mat2, int row1, int col1, int row2, int col2){
if(col1!=row2) return NULL; // checks for multiplication possibility

float** c=returnMatrix(row1, col2);   // dimension of new matrix will be row1*col2
for (i = 0; i < row1; i++) {
for (j = 0; j < col2; j++) {
c[i][j] = 0;
for (k = 0; k < col1; k++) {
c[i][j] += mat1[i][k] * mat2[k][j];
}
}
}

return c;
}

Step 6/9
(viii) matrixTranspose

Transpose is just swapping the values of mat[i][j] and mat[j][i]

The code for matrix transpose is as:

float **matrixTranspose(float **mat, int row, int col){
float** c=returnMatrix(row1, col2); // created the transpose matrix
for (int i = 0; i < r; ++i){
for (int j = 0; j < c; ++j) {
c[j][i] = a[i][j];
}
}

return c;
}

Step 7/9
(ix) row means function

taking means of the all the vertical lines by first creating those lines using temp array and then using mean function defined earlier.

loat *rowMeans(float **mat, int row, int col){
float* c=returnVector(col);
for(int i=0; i<col; i++){
float* temp=returnVector(row);
for(int j=0; j<row; j++){
temp[j]=mat[j][i];
}
c[i]=mean(temp,temp.size());
}
return c;
}
(x) column mean function
float *columnMeans(float **mat, int row, int col){
float* c=returnVector(row);
for(int i=0; i<row; i++){
c[i]=mean(mat[i], mat[i].size());
}
return c;
}

Step 8/9
(xi) variance covariance matrix by column

float **covarianceMatrix(float **mat, int row, int col){
float** c=returnMatrix(row, col);
for(int i=0; i<row; i++){
for(int j=0; j<col; j++){
c[i][j]=variance(mat[i],mat[j],mat[i].size());
}
}
return c;
}

(xii) determinant function

float determinant(float **mat, int row){
int i,j,k;
float factor=1, det=0;
float **newm;
if(mat==NULL) return -1;
if(row==1) return **m; //when matrix is a number, determinant is the number
for(i=0; i<row; i++)
{
if(NULL == (newm = malloc((row-1) * sizeof(float*)))) return -1;
for(j=0; j<row-1; j++) if (NULL == (newm[j] = malloc((row-1) * sizeof(float)))) return -1;
for(j=1; j<row; j++)
{
//skip first row as we dont need it for the adjunt matrixes
for (k=0; k<row; k++)
{
if(k==i) continue; //skip same column
newm=[j-1][k<i?k:(k-1)]=mat[j][k]; //asign the number to new matrix
}
}
det+= factor*mat[0][i]*determinant(newm, row-1); //recursivity, determinant of the adjunt matrix
factor*=-1;
for(j=0;j<row-1;j++) free(newm[j]);
free(newm);
}
return det;
}

Step 9/9
(xiii) printvector function

void printVector(float *vec, int N){
for(int i=0; i<N; i++){
cout << vec[i] << " ";
}
cout << "\n";
}

(xiv) printMatrix function

void printMatrix(float **mat, int row, int col){
for(int i=0; i<row; i++){
for(int j=0; j<col; j++){
cout << mat[row][col] << " ";
}
cout << "\n";
}
}

It was a long ride!!