Added Hadamard product and dot product
This commit is contained in:
parent
079823dcab
commit
2d044eaa36
19
matrix.c
19
matrix.c
@ -116,6 +116,20 @@ matrix *multiplyMatrices(matrix *m1, matrix *m2){
|
||||
return r;
|
||||
}
|
||||
|
||||
matrix *HadamardProduct(matrix *m1, matrix *m2){
|
||||
if(!sameDimensions(m1, m2)){
|
||||
fprintf(stderr, "Wrong dimensions (%dx%d != %dx%d)\n", m1->rows, m1->cols, m2->rows, m2->cols);
|
||||
return NULL;
|
||||
}
|
||||
matrix *r = newMatrix(m1->rows, m1->cols);
|
||||
for(int i = 0; i < r->rows; ++i){
|
||||
for(int j = 0; j < r->cols; ++j){
|
||||
r->data[i][j] = m1->data[i][j] * m2->data[i][j];
|
||||
}
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
static inline bool isSquare(matrix *m){
|
||||
return m->rows == m->cols;
|
||||
}
|
||||
@ -179,6 +193,10 @@ matrix *transpose(matrix *m){
|
||||
return r;
|
||||
}
|
||||
|
||||
matrix *dotProduct(matrix *m1, matrix *m2){
|
||||
return multiplyMatrices(m1, transpose(m2));
|
||||
}
|
||||
|
||||
matrix *adjugate(matrix *m){
|
||||
return transpose(cofactor(m));
|
||||
}
|
||||
@ -208,6 +226,7 @@ matrix *raiseMatrix(matrix *m, int n){
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
/*int main(){
|
||||
|
||||
int input = 784, hidden = 300, output = 10;
|
||||
|
4
matrix.h
4
matrix.h
@ -37,6 +37,8 @@ matrix *subtractMatrices(matrix *m1, matrix *m2);
|
||||
|
||||
matrix *multiplyMatrices(matrix *m1, matrix *m2);
|
||||
|
||||
matrix HadamardProduct(matrix *m1, matrix *m2);
|
||||
|
||||
matrix *subMatrix(matrix *m, int row, int col);
|
||||
|
||||
long double determinant(matrix *m);
|
||||
@ -45,6 +47,8 @@ matrix *cofactor(matrix *m);
|
||||
|
||||
matrix *transpose(matrix *m);
|
||||
|
||||
matrix *dotProduct(matrix *m1, matrix *m2);
|
||||
|
||||
matrix *adjugate(matrix *m);
|
||||
|
||||
matrix *inverse(matrix *m);
|
||||
|
Loading…
x
Reference in New Issue
Block a user