nub.ts 5.15 KiB
import { Debug } from "./base";
import { Dichotomie } from "./dichotomie";
import { ComputeNode } from "./compute-node";
import { Result } from "./util/result";
import { ParamValues, ParamValueMode } from "./param/param-values";
import { ParamDefinition } from ".";
/**
 * Classe abstraite de Noeud de calcul : classe de base pour tous les calculs
export abstract class Nub extends ComputeNode {
    private _dichoStartIntervalMaxSteps: number = 100;
    /**
     * résultat de Calc()/CalcSerie()
    protected _result: Result;
     * paramétrage de la dichotomie
    /**
     * étapes de recherche de l'intervalle de départ
    set dichoStartIntervalMaxSteps(n: number) {
        this._dichoStartIntervalMaxSteps = n;
    /**
     * Formule utilisée pour le calcul analytique (solution directe ou méthode de résolution spécifique)
    public abstract Equation(sVarCalc: string): Result;
    /**
     * Calcul d'une équation quelque soit l'inconnue à calculer
     * @param sVarCalc nom de la variable à calculer
     * @param rInit valeur initiale de la variable à calculer dans le cas de la dichotomie
     * @param rPrec précision de calcul
    public Calc(sVarCalc: string, rInit?: number, rPrec: number = 0.001): Result {
        const computedVar = this.getParameter(sVarCalc);
        if (rInit === undefined) {
            rInit = computedVar.v;
        if (computedVar.isAnalytical()) {
            this._result = this.Equation(sVarCalc);
            return this._result;
        const resSolve: Result = this.Solve(sVarCalc, rInit, rPrec);
        if (!resSolve.ok) {
            this._result = resSolve;
            return this._result;
        const sAnalyticalPrm: string = this.getFirstAnalyticalParameter().symbol;
        computedVar.setValue(resSolve.vCalc);
        const res: Result = this.Equation(sAnalyticalPrm);
        res.vCalc = resSolve.vCalc;
        this._result = res;
        return res;
    /**
     * effectue une série de calculs sur un paramètre
     * @param rPrec précision de calcul
     * @param rInit solution approximative du paramètre
     * @param sDonnee éventuel symbole du paramètre à calculer
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
public CalcSerie(rPrec: number = 0.001, rInit?: number, sDonnee?: string): Result { const res = new Result(); this._result = res; let variatedParam: ParamDefinition; let computedParam: ParamDefinition; for (const p of this.parameterIterator) { switch (p.valueMode) { case ParamValueMode.LISTE: case ParamValueMode.MINMAX: if (variatedParam == undefined) variatedParam = p; else throw new Error(`CalcSerie() : il y plusieurs paramètres à varier (au moins ${variatedParam.symbol} et ${p.symbol})`); break; case ParamValueMode.CALCUL: if (sDonnee == undefined) { if (computedParam == undefined) computedParam = p; else throw new Error(`CalcSerie() : il y plusieurs paramètres à calculer (au moins ${computedParam.symbol} et ${p.symbol})`); } break; } } if (sDonnee) var computedSymbol: string = sDonnee; else { if (computedParam == undefined) throw new Error(`CalcSerie() : aucun paramètre à calculer`); computedSymbol = computedParam.symbol; } if (rInit === undefined) rInit = computedParam.v; if (variatedParam == undefined) this._result = this.Calc(computedSymbol, rInit, rPrec); // résultat dans this._result else { const res = new Result(); variatedParam.paramValues.initIterator(); while (variatedParam.paramValues.hasNext) { variatedParam.paramValues.next; this.Calc(computedSymbol, rInit, rPrec); // résultat dans this._result if (this._result.ok) { res.addResultElement(this._result.resultElement); res.addLog(this._result.log); rInit = this._result.resultElement.vCalc; } res.globalLog.addLog(this._result.globalLog); } this._result = res; } return this._result; } /** * Résoud l'équation par une méthode numérique * @param sVarCalc nom de la variable à calculer * @param rInit valeur initiale de la variable à calculer dans le cas de la dichotomie * @param rPrec précision de calcul */ private Solve(sVarCalc: string, rInit: number, rPrec: number): Result { const dicho: Dichotomie = new Dichotomie(this, sVarCalc, this.DBG); dicho.startIntervalMaxSteps = this._dichoStartIntervalMaxSteps; const target = this.getFirstAnalyticalParameter();
141142143144145146147148
return dicho.Dichotomie(target.v, rPrec, rInit); } public get result(): Result { return this._result; } }