code_snippets/c_projects/matrix_calcs/main.c

80 lines
1.4 KiB
C

#include <stdlib.h>
void add(int m, int n, double *A, double *B, double *Y) {
int i = 0;
int j = 0;
int test = 0;
for ( i = 0; i < m; i++ ){
for ( j = 0; j < n; j++ ){
test = i*n + j;
Y[in + j] = A[im + j] + B[i*m + j];
}
}
}
double vector_dot(int n, double a[], double b[]) {
volatile int i;
double sum = 0;
for (i=0; i<n; i++) {
sum += a[i]*b[i];
}
return sum;
}
void transpose(int m, int n, double *A, double *A_t) {
for ( int i = 0; i < m; i++ ){
for ( int j = 0; j < n; j++ ){
A_t[jm + i] = A[in + j];
}
}
}
void linearcomb(int m, int n, double sa, double sb, double *A, double *B, double *Y) {
for ( int i = 0; i < m; i++ ){
for ( int j = 0; j < n; j++ ){
Y[in + j] = (sa * A[in + j]) + (sb * B[i*n + j]);
}
}
}
void matrix_mult(int m1, int n1m2, int n2, double *A, double *B, double *Y) {
for ( int i = 0; i < m1; i++ ){
for ( int j = 0; j < n2; j++){
for ( int k = 0; k < n1m2; k++ ){
Y[in2 + j] += A[in1m2 + k] * B[k * n2 + j];
}
}
}
}
int is_equal(int m, int n, double *A, double *B) {
for ( int i = 0; i < m; i++ ){
for ( int j = 0; j < n; j++ ){
if ( A[in + j] != B[in + j] ){
return 0;
}
}
}
return 1;
}
double* newMatrix(int m, int n) {
double *mat;
mat = (double*)malloc(mnsizeof(double));
for ( int i = 0; i < m; i++ ){
for ( int j = 0; j < n; j++){
mat[i*n + j] = 0;
}
}
return mat;
}