From 06682ab0035a14626c261b148ba7679f1f1e4f60 Mon Sep 17 00:00:00 2001 From: "Val Erastov (xibyte)" Date: Sat, 20 Jun 2020 14:28:40 -0700 Subject: [PATCH] typify polynomial --- .../constr/{polynomial.js => polynomial.ts} | 67 ++++++++++++++----- 1 file changed, 52 insertions(+), 15 deletions(-) rename web/app/sketcher/constr/{polynomial.js => polynomial.ts} (88%) diff --git a/web/app/sketcher/constr/polynomial.js b/web/app/sketcher/constr/polynomial.ts similarity index 88% rename from web/app/sketcher/constr/polynomial.js rename to web/app/sketcher/constr/polynomial.ts index c17d2f12..7359f428 100644 --- a/web/app/sketcher/constr/polynomial.js +++ b/web/app/sketcher/constr/polynomial.ts @@ -1,5 +1,6 @@ import {eqEps} from "../../brep/geom/tolerance"; -import {compositeFn} from "../../../../modules/gems/func"; +import {compositeFn} from "gems/func"; +import {SolverParam} from "./solverParam"; export class Polynomial { @@ -131,7 +132,7 @@ export class Polynomial { asResidual() { - const paramsSet = new Set(); + const paramsSet: Set = new Set(); for (let m of this.monomials) { for (let t of m.terms) { @@ -316,9 +317,16 @@ export class Monomial { } } -export class ToThePowerFunction { +export abstract class PolynomialFunction { - degree = 1; + id: string; + abstract get degree(): number; + abstract apply(x: number): number; + abstract merge(fn: PolynomialFunction): PolynomialFunction; + abstract derivative1(x: number): number +} + +export class ToThePowerFunction extends PolynomialFunction { static get(degree) { switch (degree) { @@ -327,23 +335,41 @@ export class ToThePowerFunction { case 2: return POW_2_FN; case 3: return POW_3_FN; case 4: return POW_4_FN; - default: return new ToThePowerFunction(degree, x => { - let val = 1; - for (let i = 0; i < degree; ++i) { - val *= x; + default: return new ToThePowerFunction(degree, + x => { + let val = 1; + for (let i = 0; i < degree; ++i) { + val *= x; + } + return val + }, + x => { + let val = 1; + for (let i = 0; i < degree - 1; ++i) { + val *= x; + } + return degree * val } - return val - }) + ) } } + _degree: number; + fn: (number) => number; + d1: (number) => number; + constructor(degree, fn, d1) { - this.degree = degree; + super(); + this._degree = degree; this.fn = fn; this.d1 = d1; this.id = '^' + degree; } + get degree() { + return this._degree; + } + apply(x) { return this.fn(x); } @@ -372,11 +398,14 @@ export class ToThePowerFunction { } -export class FreeFunction { +export class FreeFunction extends PolynomialFunction { - fn; + fn: (number) => number; + d1: (number) => number; + linearSubstitutionFn: (k: number, param: any, b :number) => Polynomial; constructor(fn, d1, id, linearSubstitutionFn) { + super(); this.fn = fn; this.d1 = d1; this.id = id; @@ -405,9 +434,13 @@ export class FreeFunction { } -export class CosineOfSum { +export class CosineOfSum extends PolynomialFunction { + + k: number; + b: number; constructor(k, b) { + super(); this.k = k; this.b = b; this.id = 'cos(' + k + 'x + ' + b + ')'; @@ -439,9 +472,13 @@ export class CosineOfSum { } -export class SineOfSum { +export class SineOfSum extends PolynomialFunction { + + k: number; + b: number; constructor(k, b) { + super(); this.k = k; this.b = b; this.id = 'sin(' + k + 'x + ' + b + ')';