Skip to content
Snippets Groups Projects
Commit b220072c authored by Nicolas Fley's avatar Nicolas Fley
Browse files

grad_conj struct vector and matrix plus methode associated implemented

parent 3efb2e6c
Branches master
No related tags found
No related merge requests found
Showing
with 365 additions and 1 deletion
#include <stdlib.h>
#include <stdio.h>
#include "mathObject.h"
#include "customError.h"
#include "algebre.h"
Vector * prod_mat_vec(Matrix * matrix, Vector * vector){
if(matrix->width!=vector->size)
exit(UNCOMPATIBLE_ALGEBRIC_MULTIPLICATION);
Vector * vector_res = allocateVector(matrix->height);
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];
}
}
return vector_res;
}
void testAlgebric(){
Vector * vector = allocateVector(8);
Vector * vector2 = allocateVector(8);
Matrix * matrix = allocateMatrix(5,8);
Matrix * matrix2 = allocateMatrix(8,8);
initProbVector(vector,10);
initProbMatrix(matrix,10);
initProbVector(vector2,10);
initProbMatrix(matrix2,10);
Vector * vector_res_prod_mat_vec = prod_mat_vec(matrix,vector);
printVector(vector_res_prod_mat_vec);
freeVector(vector_res_prod_mat_vec);
freeVector(vector);
freeVector(vector2);
freeMatrix(matrix);
freeMatrix(matrix2);
}
#ifndef ALGEBRE_H_INCLUDED
#define ALGEBRE_H_INCLUDED
Vector * prod_mat_vec(Matrix * matrix, Vector * vector);
#endif // ALGEBRE_H_INCLUDED
No preview for this file type
#ifndef CUSTOMERROR_H_INCLUDED
#define CUSTOMERROR_H_INCLUDED
#define ERROR_NO_MEMORY_LEFT 1000
#define UNCOMPATIBLE_ALGEBRIC_MULTIPLICATION 1001
#define NON_SQUARE_MATRIX 1002
#endif // CUSTOMERROR_H_INCLUDED
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="grad_conj" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/grad_conj" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-g" />
</Compiler>
</Target>
<Target title="Release">
<Option output="bin/Release/grad_conj" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O2" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
</Compiler>
<Unit filename="customError.h" />
<Unit filename="main.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="mathObject.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="mathObject.h" />
<Extensions>
<code_completion />
<envvars />
<debugger />
</Extensions>
</Project>
</CodeBlocks_project_file>
# depslib dependency file v1.0
1489421217 source:b:\mes documents\progra\calcint\grad_conj\mathobject.c
<stdlib.h>
<stdio.h>
"mathObject.h"
"customError.h"
1489420631 b:\mes documents\progra\calcint\grad_conj\mathobject.h
1489421052 b:\mes documents\progra\calcint\grad_conj\customerror.h
1489420337 source:b:\mes documents\progra\calcint\grad_conj\main.c
<stdio.h>
<stdlib.h>
<time.h>
<mpi.h>
<time.h>
"customError.h"
"mathObject.h"
"algebre.h"
"iterative_methods.h"
1489419800 source:b:\mes documents\progra\calcint\grad_conj\algebre.c
<stdlib.h>
<stdio.h>
"mathObject.h"
"customError.h"
"algebre.h"
1489419451 b:\mes documents\progra\calcint\grad_conj\algebre.h
1489420232 source:b:\mes documents\progra\calcint\grad_conj\iterative_methods.c
<stdlib.h>
<stdio.h>
"mathObject.h"
"customError.h"
"algebre.h"
1489420308 b:\mes documents\progra\calcint\grad_conj\iterative_methods.h
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_layout_file>
<FileVersion major="1" minor="0" />
<ActiveTarget name="Debug" />
<File name="main.c" open="1" top="1" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="338" topLine="6" />
</Cursor>
</File>
<File name="mathObject.h" open="1" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="167" topLine="0" />
</Cursor>
</File>
<File name="mathObject.c" open="1" top="0" tabpos="4" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="643" topLine="15" />
</Cursor>
</File>
</CodeBlocks_layout_file>
#include <stdlib.h>
#include <stdio.h>
#include "mathObject.h"
#include "customError.h"
#include "algebre.h"
#ifndef ITERATIVE_METHODS_H_INCLUDED
#define ITERATIVE_METHODS_H_INCLUDED
#endif // ITERATIVE_METHODS_H_INCLUDED
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <mpi.h>
#include <time.h>
#include "customError.h"
#include "mathObject.h"
#include "algebre.h"
#include "iterative_methods.h"
int main (int argc, char *argv[]) {
int rank, size;
MPI_Status status;
int test=0;
srand(time(NULL));
testMatrix();
testVector();
testAlgebric();
}
#include <stdlib.h>
#include <stdio.h>
#include "mathObject.h"
#include "customError.h"
/**
MATRIX LOGIC
**/
/* LIFE CYCLE OF MATRIX STRUCT */
Matrix * allocateMatrix(int height, int width){
Matrix * matrix = malloc(sizeof(Matrix));
matrix->height = height;
matrix->width = width;
matrix->c = (float **)malloc(height * sizeof(float));
if(matrix->c == NULL)
exit(ERROR_NO_MEMORY_LEFT);
for(int i=0;i<height;i++){
matrix->c[i]=(float *)malloc(sizeof(float)*width);
}
if(matrix->c[height-1]== NULL)
exit(ERROR_NO_MEMORY_LEFT);
return matrix;
}
void initProbMatrixSymetric(Matrix * matrix, int max_value){
float random_number;
if(matrix->height!=matrix->width)
exit(NON_SQUARE_MATRIX);
for(int i=0; i < matrix->height; i++){
for(int j=i; j < matrix->width; j++){
random_number=((float)rand())/32768.0*(float)max_value;
matrix->c[i][j]=random_number;
if(i!=j)
matrix->c[j][i]=random_number;
}
}
}
void initProbMatrix(Matrix * matrix, int max_value){
for(int i=0; i < matrix->height; i++){
for(int j=0; j < matrix->width; j++){
matrix->c[i][j]=((float)rand())/32768.0*(float)max_value;
}
}
}
void freeMatrix(Matrix * matrix){
for(int i=0; i < matrix->height; i++){
free(matrix->c[i]);
}
free(matrix->c);
free(matrix);
}
/* PRINT MATRIX */
void printMatrix(Matrix * matrix){
for(int i=0; i<matrix->height; i++){
for(int j=0; j<matrix->width; j++){
printf("%5.1f, ",matrix->c[i][j]);
}
printf("\n");
}
}
/* TEST BASIC FUNCTIONALITY */
void testMatrix(){
printf("Test of Matrix features :\n");
printf("Allocation : ");
Matrix * matrix = allocateMatrix(5,8);
printf("OK\nInitialisation : ");
initProbMatrix(matrix,10);
printf("OK\n");
printMatrix(matrix);
printf("Printing : OK\nFree : ");
freeMatrix(matrix);
printf("Ok\nThis is a random (from 0 to 10) 5x8 generated matrix, it works well !\n");
matrix = allocateMatrix(5,5);
initProbMatrixSymetric(matrix,10);
printMatrix(matrix);
freeMatrix(matrix);
printf("This is a random (from 0 to 10) 5x5 generated symetric matrix, it works well !\n");
}
/**
VECTOR LOGIC
**/
/* LIFE CYCLE OF VECTOR STRUCT */
Vector * allocateVector(int size){
Vector * vector = malloc(sizeof(Vector));
vector->size = size;
vector->c = (float *)malloc(size * sizeof(float));
if(vector->c == NULL)
exit(ERROR_NO_MEMORY_LEFT);
return vector;
}
void initProbVector(Vector * vector, int max_value){
for(int i=0; i < vector->size; i++){
vector->c[i]=((float)rand())/32768.0*(float)max_value;
}
}
void freeVector(Vector * vector){
free(vector->c);
free(vector);
}
/* PRINT VECTOR */
void printVector(Vector * vector){
for(int i=0; i<vector->size; i++){
printf("%5.1f \n",vector->c[i]);
}
}
/* TEST BASIC FUNCTIONALITY */
void testVector(){
printf("Test of vector features :\n");
printf("Allocation : ");
Vector * vector = allocateVector(8);
printf("OK\nInitialisation : ");
initProbVector(vector,10);
printf("OK\n");
printVector(vector);
printf("Printing : OK\nFree : ");
freeVector(vector);
printf("Ok\nThis is a random (from 0 to 10) 8 sized generated vector, it works well !\n");
}
#ifndef MATHOBJECT_H_INCLUDED
#define MATHOBJECT_H_INCLUDED
typedef struct vector{
int size;
float * c;
}Vector;
typedef struct matrix{
int width;
int height;
float ** c;
}Matrix;
Matrix * allocateMatrix(int, int);
void initProbMatrix(Matrix *, int);
void initProbMatrixSymetric(Matrix *, int);
void freeMatrix(Matrix *);
void printMatrix(Matrix *);
void testMatrix();
Vector * allocateVector(int);
void initProbVector(Vector *, int);
void freeVector(Vector *);
void printVector(Vector *);
void testVector();
#endif // MATHOBJECT_H_INCLUDED
File added
File added
File added
File added
File deleted
......@@ -4,7 +4,7 @@
<ActiveTarget name="Debug" />
<File name="main.c" open="1" top="1" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="184" topLine="115" />
<Cursor1 position="226" topLine="54" />
</Cursor>
</File>
</CodeBlocks_layout_file>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment