diff --git a/spec/base.spec.ts b/spec/base.spec.ts index 95e92b3a1e2d8d55ce7a23608aa2d7de79311467..6986c540fcaf7116080775b4bad8b60db89abe1e 100644 --- a/spec/base.spec.ts +++ b/spec/base.spec.ts @@ -4,11 +4,10 @@ import { nub, res } from "./nubtest"; describe('Class Nub: ', () => { beforeEach(() => { - nub.sVarsEq = ["C"]; res.vCalc = 3; }); describe('Calc(): ', () => { - it('should return a result equal to 3', () => { + it('should return a result.vCalc equal to 3', () => { expect(nub.Calc("C")).toEqual(res); }); }); diff --git a/spec/dichotomie.spec.ts b/spec/dichotomie.spec.ts new file mode 100644 index 0000000000000000000000000000000000000000..a2c6d9b54c90955641ee9c64e39468d3eb20605d --- /dev/null +++ b/spec/dichotomie.spec.ts @@ -0,0 +1,14 @@ +/// <reference path="../node_modules/@types/jasmine/index.d.ts" /> + +import { nub, res } from "./nubtest"; +import { Dichotomie } from "../src/dichotomie" + +let dicho: Dichotomie = new Dichotomie(nub, "A"); + +describe('Class Dichotomie: ', () => { + describe('Dichotomie(3, 1E-6, 0): ', () => { + it('should return a result close to 1', () => { + expect(dicho.Dichotomie(3, 1E-6, 0).vCalc).toBeCloseTo(1,1E-6); + }); + }); +}); diff --git a/spec/nubtest.ts b/spec/nubtest.ts index a85d5badb691aa16664ca0a73d49016eabd41016..ef81b541c26edea19746383808123783103995b3 100644 --- a/spec/nubtest.ts +++ b/spec/nubtest.ts @@ -1,6 +1,10 @@ -import { Nub, Result } from "../src/base"; +import { Nub, Result, IParametres } from "../src/base"; export class NubTest extends Nub { + constructor(v: IParametres) { + super(v); + this.sVarsEq = ["C"]; + } Equation(): Result { let res: Result = new Result(); res.vCalc = this.v["A"] + this.v["B"]; @@ -8,4 +12,4 @@ export class NubTest extends Nub { } } export let nub = new NubTest({ "A": 1, "B": 2, "C": null }); -export let res = new Result; +export let res = new Result; \ No newline at end of file diff --git a/spec/tsconfig.spec.json b/spec/tsconfig.spec.json index a6acd911cce162abab7b9e14f43c415274b71163..3c8b0e620de9b8aeaa4767036ab8239a504b8f08 100644 --- a/spec/tsconfig.spec.json +++ b/spec/tsconfig.spec.json @@ -13,7 +13,6 @@ // ], "include": [ "../src/**/*.ts", - "../spec/**/*.ts", - "../typings/**/*.d.ts" + "../spec/**/*.ts" ] } diff --git a/src/dichotomie.ts b/src/dichotomie.ts index 6dabd736f0743fb8f242ccd47601dab36d7c56f7..88aea0bcbe9249157b75ccf602fe5c903e4f6ad8 100644 --- a/src/dichotomie.ts +++ b/src/dichotomie.ts @@ -2,11 +2,11 @@ import { Debug, Nub, Result } from "./base"; export class Dichotomie extends Debug { /** Pas de parcours de l'intervalle pour initialisation dichotomie */ - readonly IDEFINT = 100; + readonly IDEFINT = 100; /** Nombre d'itérations maximum de la dichotomie */ - readonly IDICMAX = 100; - - + readonly IDICMAX = 100; + + /** * Construction de la classe. * @param nub Noeud de calcul contenant la méthode de calcul Equation @@ -25,7 +25,7 @@ export class Dichotomie extends Debug { set vX(vCalc) { this.nub.v[this.sVarCalc] = vCalc; } - + /** * Méthode simulant l'opérateur booléen xor * @see http://www.howtocreate.co.uk/xor.html @@ -57,7 +57,7 @@ export class Dichotomie extends Debug { * @param rInit Valeur initiale de l'inconnue à rechercher */ Dichotomie(rTarget: number, rTol: number, rInit: number): Result { - let res: Result + let res: Result; let XminInit = 1E-8; this.vX = XminInit; @@ -66,7 +66,7 @@ export class Dichotomie extends Debug { let XmaxInit: number = Math.max(1, rInit) * 100; this.vX = XmaxInit; let v2 = this.Calcul().vCalc; - + let DX = (XmaxInit - XminInit) / this.IDEFINT; let nIterMax = Math.floor(Math.max(XmaxInit - rInit, rInit - XminInit) / DX + 1); let Xmin = rInit; @@ -76,7 +76,7 @@ export class Dichotomie extends Debug { this.debug("rInit: " + rInit); this.vX = rInit; let v = this.Calcul().vCalc; - + v1 = v; v2 = v; this.debug(nIterMax); @@ -113,9 +113,11 @@ export class Dichotomie extends Debug { } if (this.XOR(rTarget > v1, rTarget >= v2)) { break; } } + + // Gestion de l'absence de solution dans l'intervalle de recherche if (nIter >= this.IDEFINT) { - this.debug("in if"); - // Pas d'intervalle trouvé avec au moins une solution + this.debug("nIter >= this.IDEFINT"); + if (v2 < rTarget && v1 < rTarget) { // Cote de l'eau trop basse pour passer le débit il faut ouvrir un autre ouvrage this.vX = XmaxInit; @@ -128,32 +130,32 @@ export class Dichotomie extends Debug { res.extraVar["flag"] = -1; // la valeur cible n'est pas dans l'intervalle de recherche return res; } - else { - // Dichotomie - this.debug("in dicho"); - let X = rInit; - for (nIter = 1; nIter <= this.IDICMAX; nIter++) { - this.vX = X; - v = this.Calcul().vCalc; - if (rTarget != 0 && Math.abs(X1 - X2) <= rTol) { break; } - if (this.XOR(rTarget < v, v1 <= v2)) { - // rTarget < IQ et v(X1) > v(X2) ou pareil en inversant les inégalités - X1 = this.vX; - } - else { - // rTarget < IQ et v(X1) < v(X2) ou pareil en inversant les inégalités - X2 = this.vX; - } - X = (X2 + X1) * 0.5; - this.debug((X)); + + // Dichotomie + this.debug("start dicho"); + let X = rInit; + for (nIter = 1; nIter <= this.IDICMAX; nIter++) { + this.vX = X; + v = this.Calcul().vCalc; + if (rTarget != 0 && Math.abs(X1 - X2) <= rTol) { break; } + if (this.XOR(rTarget < v, v1 <= v2)) { + // rTarget < IQ et v(X1) > v(X2) ou pareil en inversant les inégalités + X1 = this.vX; } - if (nIter == this.IDICMAX) { - res = this.Calcul(); - res.extraVar["flag"] = -1; // la valeur cible n'est pas dans l'intervalle de recherche - return res; + else { + // rTarget < IQ et v(X1) < v(X2) ou pareil en inversant les inégalités + X2 = this.vX; } + X = (X2 + X1) * 0.5; + this.debug((X)); + } + if (nIter == this.IDICMAX) { + res = this.Calcul(); + res.extraVar["flag"] = -1; // la valeur cible n'est pas dans l'intervalle de recherche + return res; } - res.vCalc = v + res = new Result(); + res.vCalc = X; return res; } -} \ No newline at end of file +}