Skip to content

Commit

Permalink
Récupération code fraction + inversion matrice
Browse files Browse the repository at this point in the history
  • Loading branch information
Tequhella committed Mar 19, 2024
1 parent b3003a3 commit 805b66e
Show file tree
Hide file tree
Showing 10 changed files with 214 additions and 56 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@
"streambuf": "cpp",
"typeinfo": "cpp"
},
"docwriter.style": "Doxygen"
"docwriter.style": "Doxygen",
"sarif-viewer.connectToGithubCodeScanning": "off"
}
31 changes: 0 additions & 31 deletions Calculatrice_Matrice_CPP/sources/main.cpp

This file was deleted.

Binary file added calculatriceMatrice_CPP
Binary file not shown.
117 changes: 117 additions & 0 deletions headers/fractionizer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*************************************************************/
/* Fractionizer */
/*-----------------------------------------------------------*/
/* Module : fractionizer.h */
/* Numéro de version : 0.0.23 */
/* Date : 24/08/2015 */
/* Auteurs : ajneu */
/*************************************************************/

#ifndef FRACTIONIZER_H
#define FRACTIONIZER_H



#include <cmath> // std::modf
#include <vector>


/* uncomment one of the following */
//#define OWN_LIMITS
#undef OWN_LIMITS


#ifndef OWN_LIMITS
#include <limits>
using std::numeric_limits;

#else

#include <cfloat> // FLT_EPSILON, DBL_EPSILON, LDBL_EPSILON

// just for fun
namespace own {
template <typename Tfl> // Tfl should be a floating point type (preferably double or long double)
struct numeric_limits {
static constexpr Tfl min() {
return get_min(Tfl{});
}
static constexpr Tfl epsilon() {
return get_epsilon(Tfl{});
}
private:
static constexpr float get_min(float) { return FLT_MIN; }
static constexpr double get_min(double) { return DBL_MIN; }
static constexpr long double get_min(long double) { return LDBL_MIN;}

static constexpr float get_epsilon(float) { return FLT_EPSILON; }
static constexpr double get_epsilon(double) { return DBL_EPSILON; }
static constexpr long double get_epsilon(long double) { return LDBL_EPSILON;}
};
}

using own::numeric_limits;

#endif




class Fractionizer {
public:
template <typename Tfl, typename Tfl2> // Tfl should be a floating point type (preferably double or long double)
static std::vector<Tfl> fractionize(const Tfl val, Tfl2 &num, Tfl2 &denom)
{
Tfl n;
Tfl d;
std::vector<Tfl> vec;
Tfl i;
Tfl v = val;
goto label_begin;
do {
v = 1/v;
label_begin:
//std::cout << "v == " << v << '\t';
v = std::modf(v, &i);
//std::cout << "i == " << i << '\t';
vec.push_back(i);
//std::cout << "calc == " << Fractionizer::calc_frac<Tfl>(vec, n, d) << std::endl;
}
//while (std::abs((Fractionizer::calc_frac<Tfl>(vec, n, d) - val)/val) > numeric_limits<Tfl>::min());
while (Fractionizer::calc_frac<Tfl>(vec, n, d) != val);
//assert((n/d) == val);
num = n;
denom = d;
return vec;
}

template <typename Tfl, typename Tfl2> // Tfl should be a floating point type (preferably double or long double)
static Tfl calc_frac(const std::vector<Tfl> &vec, Tfl2 &num, Tfl2 &denom)
// {2, 3, 4}
// 2 + 1/(3+1/4)
{
if (!vec.empty()) {
Tfl n = 1.0; // num
Tfl d = 0.0; // denom

auto it_end = vec.cend();
const auto it_beg = vec.cbegin();
do {
--it_end;

std::swap(n, d);
n += *it_end * d;
} while (it_end != it_beg);
num = n;
denom = d;
return n/d;
}
num = 0.0;
denom = 1.0;
return 0.0;
}

};


#endif
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@

#include <iostream>
#include <random>
#include <boost/rational.hpp>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>

/* Enumération des différents type de matrice. */
enum
{
Expand Down
27 changes: 13 additions & 14 deletions makefile
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
CC = g++
GCC = g++
CFLAGS = -g -Wall
TARGET = calculatriceMatrice_CPP
OBJS = obj/main.o obj/matrice.o

G++ = -g -Wall
all: $(OBJS)
$(GCC) $(CFLAGS) -o $(TARGET) $(OBJS)

Sources = Calculatrice_Matrice_CPP/sources
obj/main.o: sources/main.cpp headers/matrice.h
#check if obj directory exists, if not create it
@if [ ! -d "obj" ]; then mkdir obj; fi
$(GCC) $(CFLAGS) -c sources/main.cpp -o obj/main.o

obj = Calculatrice_Matrice_CPP/obj

all: $(obj)/main.o $(obj)/matrice.o
$(CC) $(G++) $(obj)/main.o $(obj)/matrice.o -o calculatrice_matrice_CPP

$(obj)/main.o:
$(CC) $(G++) -c $(Sources)/main.cpp -o $(obj)/main.o

$(obj)/matrice.o:
$(CC) $(G++) -c $(Sources)/matrice.cpp -o $(obj)/matrice.o
obj/matrice.o: sources/matrice.cpp headers/matrice.h headers/fractionizer.h
$(GCC) $(CFLAGS) -c sources/matrice.cpp -o obj/matrice.o

clean:
rm -f $(obj)/*.o calculatrice_matrice_CPP
rm -f $(OBJS) $(TARGET)
Binary file added obj/main.o
Binary file not shown.
Binary file added obj/matrice.o
Binary file not shown.
33 changes: 33 additions & 0 deletions sources/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*************************************************************/
/* Calculatrice_Matrice_CPP */
/*-----------------------------------------------------------*/
/* Module : main.cpp */
/* Numéro de version : 0.0.13 */
/* Date : 19/04/2022 */
/* Auteurs : Lilian CHARDON, Andréas CASTELLO */
/*************************************************************/


#include "../headers/matrice.h"

int main()
{
Matrice* matrice1 = new Matrice("Matrice 1", 5, 5, ALEATOIRE);
//Matrice* matrice2 = new Matrice("Matrice 2", 5, 5, ALEATOIRE);
matrice1->afficherMatrice(10);
//matrice2->afficherMatrice(10);

Matrice* matriceInversee = matrice1->inverserMatrice("Matrice 1 inversée");
matriceInversee->afficherMatrice(10);

cout << "Nombre de matrice créées : " << Matrice::nbMatriceCrees << endl;

delete matrice1;
//delete matrice2;
delete matriceInversee;

cout << "Nombre de matrice détruites : " << Matrice::nbMatriceDetruites << endl;

return 0;
}

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
/*************************************************************/

#include "../headers/matrice.h"
#include "../headers/fractionizer.h"

int Matrice::nbMatriceCrees = 0;
int Matrice::nbMatriceDetruites = 0;
Expand Down Expand Up @@ -114,6 +115,19 @@ Matrice::~Matrice()

//=======================================================================================//

long gcd(long a, long b)
{
if (a == 0)
return b;
else if (b == 0)
return a;

if (a < b)
return gcd(a, b % a);
else
return gcd(b, a % b);
}

/**
* @brief Méthode afficherMatrice, imprime la matrice.
*
Expand All @@ -128,7 +142,20 @@ void Matrice::afficherMatrice(uint8_t format)
{
for (int j = 0; j < this->dimX; j++)
{
cout << this->getElement(j, i) << " ";
double input = this->getElement(j, i);
double integral = std::floor(input);
double frac = input - integral;

if (frac == 0) // Si l'élément est un entier
{
cout << integral << " ";
}
else // Si l'élément est un nombre décimal
{
double num, denom;
Fractionizer::fractionize(input, num, denom);
cout << num << "/" << denom << " ";
}
}
cout << endl;
}
Expand Down Expand Up @@ -326,15 +353,23 @@ Matrice* Matrice::sousMatrice(uint8_t colonneDebut, uint8_t ligneDebut, const ch
Matrice* matriceSousMatrice = new Matrice(nomDeLaMatriceSousMatrice, this->dimX - 1, this->dimY - 1, NULLE);
if (matriceSousMatrice)
{
uint8_t k = 0;
uint8_t kx = 0, ky = 0;
for (int i = 0; i < this->dimY; i++)
{
for (int j = 0; j < this->dimX; j++)
if (i != ligneDebut)
{
if (i != ligneDebut && j != colonneDebut)
for (int j = 0; j < this->dimX; j++)
{
matriceSousMatrice->setElement(k % matriceSousMatrice->dimX, k / matriceSousMatrice->dimY, this->getElement(j, i));
k++;
if (j != colonneDebut)
{
matriceSousMatrice->setElement(kx, ky, this->getElement(j, i));
kx++;
if (kx >= matriceSousMatrice->dimX)
{
kx = 0;
ky++;
}
}
}
}
}
Expand Down Expand Up @@ -372,8 +407,8 @@ double Matrice::calculerDeterminant()
Matrice* matriceSousMatrice = this->sousMatrice(i, 0, "sousMatrice");
if (matriceSousMatrice)
{
if (i % 2 == 0) /*--->*/ determinant = determinant + this->elements[i] * matriceSousMatrice->calculerDeterminant();
else /*-------------->*/ determinant = determinant - this->elements[i] * matriceSousMatrice->calculerDeterminant();
if (i % 2 == 0) /*--->*/ determinant = determinant + this->getElement(i, 0) * matriceSousMatrice->calculerDeterminant();
else /*-------------->*/ determinant = determinant - this->getElement(i, 0) * matriceSousMatrice->calculerDeterminant();

delete matriceSousMatrice;
}
Expand Down Expand Up @@ -413,11 +448,11 @@ Matrice* Matrice::inverserMatrice(const char* nomDeLaMatriceInverse)
{
for (int j = 0; j < this->dimX; j++)
{
Matrice* matriceSousMatrice = this->sousMatrice(j, i, "sousMatrice");
Matrice* matriceSousMatrice = this->sousMatrice(i, j, "sousMatrice");
if (matriceSousMatrice)
{
double coeff = pow(-1, i + j) * matriceSousMatrice->calculerDeterminant();
matriceInverse->setElement(j, i, coeff * (1 / determinant));
matriceInverse->setElement(j, i, coeff / determinant);
delete matriceSousMatrice;
}
else
Expand Down

0 comments on commit 805b66e

Please sign in to comment.