axb.ts 2.18 KB
Newer Older
Mathias Chouet's avatar
Mathias Chouet committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { YAXBParams } from "./axb_params";
import { CalculatorType } from "./compute-node";
import { Nub } from "./nub";
import { ParamCalculability } from "./param/param-definition";
import { Message, MessageCode } from "./util/message";
import { Result } from "./util/result";

/**
 * Y = A.X + B
 */
export class YAXB extends Nub {

    constructor(prms: YAXBParams, dbg: boolean = false) {
        super(prms, dbg);
        this._calcType = CalculatorType.YAXB;
        this._defaultCalculatedParam = prms.Y;
        this.resetDefaultCalculatedParam();
    }

    /** paramètres castés au bon type */
    get prms(): YAXBParams {
        return this._prms as YAXBParams;
    }

    public Equation(sVarCalc: string): Result {
        let v: number;

        switch (sVarCalc) {
            case "Y":
                v = this.prms.A.v * this.prms.X.v + this.prms.B.v;
                break;

            case "A":
                if (this.prms.X.v === 0) {
                    const m = new Message(MessageCode.ERROR_DIVISION_BY_ZERO);
                    m.extraVar.symbol = "X";
                    return new Result(m);
                }
                v = (this.prms.Y.v - this.prms.B.v) / this.prms.X.v;
                break;

            case "X":
                if (this.prms.A.v === 0) {
                    const m = new Message(MessageCode.ERROR_DIVISION_BY_ZERO);
                    m.extraVar.symbol = "A";
                    return new Result(m);
                }
                v = (this.prms.Y.v - this.prms.B.v) / this.prms.A.v;
                break;

            case "B":
                v = this.prms.Y.v - (this.prms.A.v * this.prms.X.v);
                break;

            default:
                throw new Error("AXB.Equation() : invalid variable name " + sVarCalc);
        }

        return new Result(v, this);
    }

    /** paramétrage de la calculabilité des paramètres */
    protected setParametersCalculability() {
        this.prms.Y.calculability = ParamCalculability.EQUATION;
        this.prms.A.calculability = ParamCalculability.EQUATION;
        this.prms.X.calculability = ParamCalculability.EQUATION;
        this.prms.B.calculability = ParamCalculability.EQUATION;
    }

}