trigo.ts 5.62 KB
Newer Older
Mathias Chouet's avatar
Mathias Chouet committed
1
2
3
import { CalculatorType } from "./compute-node";
import { Nub } from "./nub";
import { ParamCalculability } from "./param/param-definition";
Mathias Chouet's avatar
Mathias Chouet committed
4
import { ParamDomainValue } from "./param/param-domain";
Mathias Chouet's avatar
Mathias Chouet committed
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { TrigoParams } from "./trigo_params";
import { Interval } from "./util/interval";
import { Observer } from "./util/observer";
import { Result } from "./util/result";

export enum TrigoOperation {
    COS,
    SIN,
    TAN,
    COSH,
    SINH,
    TANH
}

export enum TrigoUnit {
    DEG,    // Degrees
    RAD     // Radians
}

/**
 * cos, sin, tan, cosh, sinh, tanh
 */
export class Trigo extends Nub implements Observer {

    constructor(prms: TrigoParams, dbg: boolean = false) {
        super(prms, dbg);
        this._calcType = CalculatorType.Trigo;
        this._defaultCalculatedParam = prms.Y;
        this.resetDefaultCalculatedParam();
        this.properties.addObserver(this);
        this.properties.setPropValue("trigoOperation", TrigoOperation.COS);
        this.properties.setPropValue("trigoUnit", TrigoUnit.DEG);
    }

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

    public get operation(): TrigoOperation {
        return this.properties.getPropValue("trigoOperation");
    }

    public set operation(o: TrigoOperation) {
        this.properties.setPropValue("trigoOperation", o);
    }

    public get unit(): TrigoUnit {
        return this.properties.getPropValue("trigoUnit");
    }

    public set unit(u: TrigoUnit) {
        this.properties.setPropValue("trigoUnit", u);
    }

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

        switch (sVarCalc) {
            case "Y":
                let xv = this.prms.X.v;
                if (this.unit === TrigoUnit.DEG) {
                    xv = xv * Math.PI / 180;
                }
                switch (this.operation) {
                    case TrigoOperation.COS:
                        v = Math.cos(xv);
                        break;
                    case TrigoOperation.SIN:
                        v = Math.sin(xv);
                        break;
                    case TrigoOperation.TAN:
                        v = Math.tan(xv);
                        break;
                    case TrigoOperation.COSH:
                        v = Math.cosh(xv);
                        break;
                    case TrigoOperation.SINH:
                        v = Math.sinh(xv);
                        break;
                    case TrigoOperation.TANH:
                        v = Math.tanh(xv);
                        break;
                    default:
                        throw new Error("Trigo.Equation() : invalid operation " + this.operation);
                }
                break;

            case "X":
                const yv = this.prms.Y.v;
                switch (this.operation) {
                    case TrigoOperation.COS:
                        v = Math.acos(yv);
                        break;
                    case TrigoOperation.SIN:
                        v = Math.asin(yv);
                        break;
                    case TrigoOperation.TAN:
                        v = Math.atan(yv);
                        break;
                    case TrigoOperation.COSH:
                        v = Math.acosh(yv);
                        break;
                    case TrigoOperation.SINH:
                        v = Math.asinh(yv);
                        break;
                    case TrigoOperation.TANH:
                        v = Math.atanh(yv);
                        break;
                    default:
                        throw new Error("Trigo.Equation() : invalid operation " + this.operation);
                }
                if (this.unit === TrigoUnit.DEG) {
                    v = v / Math.PI * 180;
                }
                break;

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

        return new Result(v, this);
    }

    // interface Observer

    public update(sender: any, data: any) {
        if (data.action === "propertyChange") {
            switch (data.name) {
                case "trigoOperation":
                case "trigoUnit":
                    // adjust definition domains
                    const un = this.unit;
                    switch (this.operation) {
                        case TrigoOperation.COS:
                        case TrigoOperation.SIN:
                            // [-1,1]
                            this.prms.Y.domain.interval.setInterval(new Interval(-1, 1));
                            break;
                        case TrigoOperation.TANH:
                            // ]-1,1[
                            // @TODO manage boundaries exclusion in Interval
                            this.prms.Y.domain.interval.setInterval(new Interval(-0.99999999, 0.99999999));
                            break;
                        case TrigoOperation.TAN:
                        case TrigoOperation.SINH:
                            // R
                            this.prms.Y.setDomain(ParamDomainValue.ANY);
                            break;
                        case TrigoOperation.COSH:
                            // [1,+∞[
                            this.prms.Y.domain.interval.setInterval(new Interval(1, Infinity));
                            break;
                    }
            }
        }
    }

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

}