Select Git revision
BDPA_Assign2_WJIN.md
algebre.c 4.84 KiB
#include <stdlib.h>
#include <stdio.h>
#include "mathObject.h"
#include "customError.h"
#include "algebre.h"
/* MATRIX x VECTOR */
void prod_mat_vec(Vector * vector_res, Matrix * matrix, Vector * vector){
if(vector->t!=0)
exit(TRANSPOSED_VECTOR_USED);
if(matrix->width!=vector->size)
exit(UNCOMPATIBLE_ALGEBRIC_MULTIPLICATION);
if(vector_res->size!=matrix->height)
exit(CONTAINER_RES_DO_NOT_FIT);
for(int i=0; i!=matrix->height; i++){
vector_res->c[i]=0;
for(int j=0; j!=matrix->width; j++){
vector_res->c[i]+=matrix->c[i][j]*vector->c[j];
}
}
}
// parallelisable
void prod_mat_vec_p(Vector * vector_res, Matrix * matrix, Vector * vector, int beg, int nbLigne){
if(vector->t!=0)
exit(TRANSPOSED_VECTOR_USED);
if(matrix->width!=vector->size)
exit(UNCOMPATIBLE_ALGEBRIC_MULTIPLICATION);
if(nbLigne>vector_res->size)
exit(CONTAINER_RES_DO_NOT_FIT);
for(int i=0; i!=nbLigne; i++){
vector_res->c[i]=0;
for(int j=0; j!=matrix->width; j++){
vector_res->c[i]+=matrix->c[beg+i][j]*vector->c[j];
}
}
}
/* VECTOR_t x MATRIX */
void prod_vec_mat(Vector * vector_res, Vector * vector, Matrix * matrix){
if(vector->t!=1)
exit(NON_TRANSPOSED_VECTOR_USED);
if(matrix->height!=vector->size)
exit(UNCOMPATIBLE_ALGEBRIC_MULTIPLICATION);
if(vector_res->size!=matrix->width)
exit(CONTAINER_RES_DO_NOT_FIT);
vector_res->t=1;
for(int i=0; i!=matrix->width; i++){
vector_res->c[i]=0;
for(int j=0; j!=matrix->height; j++){
vector_res->c[i]+=matrix->c[j][i]*vector->c[j];
}
}
}
/* VECTOR + alpha x VECTOR */
void sum_vec_alpha_vec(Vector * vector_res, Vector * vector1, float alpha, Vector * vector2){
if(vector_res->size!=vector1->size || vector1->size != vector2->size)
exit(UNCOMPATIBLE_ALGEBRIC_ADDITION);
for(int i=0;i!=vector1->size;i++)
vector_res->c[i]=vector1->c[i]+alpha*vector2->c[i];
}
// parallelisable
void sum_vec_alpha_vec_p(Vector * vector_res, Vector * vector1, float alpha, Vector * vector2, int beg, int nbLigne){
if(vector_res->size<nbLigne)
exit(CONTAINER_RES_DO_NOT_FIT);
for(int i=0;i!=nbLigne;i++){
vector_res->c[i]=vector1->c[beg+i]+alpha*vector2->c[beg+i];
}
}
/* VECTOR_t x MATRIX x VECTOR */
float prod_vec_t_mat_vec(Vector * vector, Matrix * matrix){
vector->t=1;
Vector * subVec = allocateVector(matrix->width);
prod_vec_mat(subVec,vector,matrix);
vector->t=0;
float res = prod_vec_t_vec(subVec,vector);
freeVector(subVec);
return res;
}
/* NORM */
float norm_vec_2(Vector * vector){
float res=0;
for(int i=0;i!=vector->size;i++)
res+=vector->c[i]*vector->c[i];
return res;
}
/* VECTOR_t . VECTOR */
float prod_vec_t_vec(Vector * vector_t, Vector * vector){
if(vector->size!=vector_t->size)
exit(UNCOMPATIBLE_ALGEBRIC_MULTIPLICATION);
float res=0;
for(int i=0;i!=vector->size;i++)
res+=vector->c[i]*vector_t->c[i];
return res;
}
// parallelisable
float prod_vec_t_vec_p(Vector * vector_t, Vector * vector, int beg, int nbLigne){
if(vector->size<beg+nbLigne)
exit(UNCOMPATIBLE_ALGEBRIC_MULTIPLICATION);
float res=0;
for(int i=beg;i!=beg+nbLigne;i++)
res+=vector->c[i]*vector_t->c[i];
return res;
}
/* VECTOR x Scal without creating new object */
void prod_vec_scal(Vector * vector_res, Vector * vector, float scal){
if(vector_res->size!=vector->size)
exit(CONTAINER_RES_DO_NOT_FIT);
for(int i=0; i!=vector->size; i++){
vector_res->c[i]=vector->c[i]*scal;
}
}
void testAlgebric(){
Vector * vector = allocateVector(8);
Vector * vector2 = allocateVector(8);
Vector * vector_res_5 = allocateVector(5);
Vector * vector_res_8 = allocateVector(8);
Matrix * matrix = allocateMatrix(5,8);
Matrix * matrix2 = allocateMatrix(8,8);
initProbVector(vector,10);
initProbMatrix(matrix,10);
initProbVector(vector2,10);
initProbMatrix(matrix2,10);
prod_mat_vec(vector_res_5,matrix,vector);
printf("prod_mat_vec : \n");
printVector(vector_res_5);
vector->t=1;
prod_vec_mat(vector_res_8,vector,matrix2);
printf("prod_vec_mat : \n");
printVector(vector_res_8);
printf("prod_vec_t_vec : %f\n",prod_vec_t_vec(vector,vector2));
printf("prod_vec_t_mat_vec : %f\n",prod_vec_t_mat_vec(vector,matrix2));
printf("norm_vec_2 : %f\n",norm_vec_2(vector));
printf("sum_vec_alpha_vec : \n");
sum_vec_alpha_vec(vector,vector,-1,vector);
printVector(vector);
printf("prod_vec_scal_u : \n");
prod_vec_scal(vector_res_8,vector_res_8,10.0);
printVector(vector_res_8);
freeVector(vector);
freeVector(vector2);
freeVector(vector_res_5);
freeVector(vector_res_8);
freeMatrix(matrix);
freeMatrix(matrix2);
}