math-exercises 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (98) hide show
  1. package/nodemon.json +1 -2
  2. package/package.json +1 -1
  3. package/src/exercises/calcul/addAndSub.js +34 -0
  4. package/src/exercises/calcul/addAndSub.ts +32 -0
  5. package/src/exercises/calcul/priority.ts +33 -0
  6. package/src/exercises/calculLitteral/distributivity/allIdentities.js +25 -0
  7. package/src/exercises/calculLitteral/distributivity/allIdentities.ts +24 -0
  8. package/src/exercises/calculLitteral/distributivity/doubleDistributivity.js +31 -0
  9. package/src/exercises/calculLitteral/distributivity/doubleDistributivity.ts +31 -0
  10. package/src/exercises/calculLitteral/distributivity/firstIdentity.js +30 -0
  11. package/src/exercises/calculLitteral/distributivity/firstIdentity.ts +31 -0
  12. package/src/exercises/calculLitteral/distributivity/secondIdentity.js +33 -0
  13. package/src/exercises/calculLitteral/distributivity/secondIdentity.ts +33 -0
  14. package/src/exercises/calculLitteral/distributivity/simpleDistributivity.js +33 -0
  15. package/src/exercises/calculLitteral/distributivity/simpleDistributivity.ts +31 -0
  16. package/src/exercises/calculLitteral/distributivity/thirdIdentity.js +30 -0
  17. package/src/exercises/calculLitteral/distributivity/thirdIdentity.ts +32 -0
  18. package/src/exercises/calculLitteral/equation/equationType1Exercise.js +38 -0
  19. package/src/exercises/calculLitteral/equation/equationType1Exercise.ts +39 -0
  20. package/src/exercises/calculLitteral/equation/equationType2Exercise.js +41 -0
  21. package/src/exercises/calculLitteral/equation/equationType2Exercise.ts +41 -0
  22. package/src/exercises/calculLitteral/equation/equationType3Exercise.js +42 -0
  23. package/src/exercises/calculLitteral/equation/equationType3Exercise.ts +43 -0
  24. package/src/exercises/calculLitteral/equation/equationType4Exercise.js +44 -0
  25. package/src/exercises/calculLitteral/equation/equationType4Exercise.ts +51 -0
  26. package/src/exercises/calculLitteral/factorisation/factoType1Exercise.js +28 -23
  27. package/src/exercises/calculLitteral/factorisation/factoType1Exercise.ts +36 -22
  28. package/src/exercises/exercise.js +5 -0
  29. package/src/exercises/exercise.ts +9 -1
  30. package/src/exercises/exercises.ts +27 -0
  31. package/src/exercises/squareRoots/simpifySquareRoot.js +29 -0
  32. package/src/exercises/squareRoots/simpifySquareRoot.ts +25 -0
  33. package/src/expression/expression.js +3 -1
  34. package/src/expression/expression.ts +4 -0
  35. package/src/index.js +8 -6
  36. package/src/index.ts +18 -6
  37. package/src/mathutils/arithmetic/isSquare.js +7 -0
  38. package/src/mathutils/arithmetic/primeFactors.js +22 -0
  39. package/src/numbers/integer/integer.js +15 -0
  40. package/src/numbers/integer/integer.ts +20 -2
  41. package/src/numbers/nombre.js +9 -0
  42. package/src/numbers/nombre.ts +13 -0
  43. package/src/numbers/rationals/rational.js +23 -1
  44. package/src/numbers/rationals/rational.ts +37 -3
  45. package/src/numbers/reals/real.js +15 -0
  46. package/src/numbers/reals/real.ts +15 -1
  47. package/src/numbers/reals/squareRoot.js +85 -0
  48. package/src/numbers/reals/squareRoot.ts +44 -21
  49. package/src/polynomials/affine.js +5 -4
  50. package/src/polynomials/affine.ts +16 -21
  51. package/src/polynomials/polynomial.js +47 -11
  52. package/src/polynomials/polynomial.ts +57 -17
  53. package/src/sets/discreteSet.js +4 -4
  54. package/src/sets/discreteSet.ts +10 -9
  55. package/src/sets/emptySet.ts +5 -5
  56. package/src/sets/intervals/intervals.js +33 -9
  57. package/src/sets/intervals/intervals.ts +41 -12
  58. package/src/sets/mathSet.ts +5 -3
  59. package/src/sets/mathSetInterface.ts +4 -2
  60. package/src/tree/latexParser/latexParse.js +76 -0
  61. package/src/tree/latexParser/latexParse.ts +82 -0
  62. package/src/tree/nodes/functions/sqrtNode.js +18 -0
  63. package/src/tree/nodes/functions/sqrtNode.ts +16 -0
  64. package/src/tree/nodes/node.js +10 -0
  65. package/src/tree/nodes/node.ts +14 -0
  66. package/src/tree/nodes/numbers/numberNode.js +19 -0
  67. package/src/tree/nodes/numbers/numberNode.ts +19 -0
  68. package/src/tree/nodes/operators/addNode.js +18 -0
  69. package/src/tree/nodes/operators/addNode.ts +17 -0
  70. package/src/tree/nodes/operators/divideNode.js +22 -0
  71. package/src/tree/nodes/operators/divideNode.ts +23 -0
  72. package/src/tree/nodes/operators/equalNode.js +18 -0
  73. package/src/tree/nodes/operators/equalNode.ts +17 -0
  74. package/src/tree/nodes/operators/multiplyNode.js +18 -0
  75. package/src/tree/nodes/operators/multiplyNode.ts +18 -0
  76. package/src/{operations/operation.js → tree/nodes/operators/operatorNode.js} +0 -0
  77. package/src/tree/nodes/operators/operatorNode.ts +3 -0
  78. package/src/tree/nodes/operators/oppositeNode.js +18 -0
  79. package/src/tree/nodes/operators/oppositeNode.ts +17 -0
  80. package/src/tree/nodes/operators/powerNode.js +18 -0
  81. package/src/tree/nodes/operators/powerNode.ts +17 -0
  82. package/src/tree/nodes/operators/substractNode.js +18 -0
  83. package/src/tree/nodes/operators/substractNode.ts +17 -0
  84. package/src/tree/nodes/variables/variableNode.js +20 -0
  85. package/src/tree/nodes/variables/variableNode.ts +18 -0
  86. package/src/equations/equations.ts +0 -16
  87. package/src/exercises/calculLitteral/factorisation/factorisation.js +0 -39
  88. package/src/latex/add.js +0 -7
  89. package/src/latex/latex.js +0 -37
  90. package/src/latex/latex.ts +0 -35
  91. package/src/latex/monome.ts +0 -0
  92. package/src/latex/multiply.ts +0 -5
  93. package/src/numbers/number.ts +0 -4
  94. package/src/operations/add.js +0 -9
  95. package/src/operations/add.ts +0 -9
  96. package/src/operations/operation.ts +0 -6
  97. package/src/operations/substract.js +0 -9
  98. package/src/operations/substract.ts +0 -9
@@ -0,0 +1,13 @@
1
+ import { Node } from "../tree/nodes/node";
2
+ import { NumberNode } from "../tree/nodes/numbers/numberNode";
3
+
4
+ export enum NumberType {
5
+ Integer,
6
+ Rational,
7
+ Real,
8
+ }
9
+ export interface Nombre {
10
+ value: number;
11
+ tex: string;
12
+ toTree: () => Node;
13
+ }
@@ -2,18 +2,40 @@
2
2
  exports.__esModule = true;
3
3
  exports.Rational = void 0;
4
4
  var gcd_1 = require("../../mathutils/arithmetic/gcd");
5
+ var numberNode_1 = require("../../tree/nodes/numbers/numberNode");
6
+ var divideNode_1 = require("../../tree/nodes/operators/divideNode");
7
+ var integer_1 = require("../integer/integer");
5
8
  var Rational = /** @class */ (function () {
6
9
  function Rational(numerator, denumerator) {
7
10
  this.num = numerator;
8
11
  this.denum = denumerator;
12
+ this.value = numerator / denumerator;
9
13
  this.isSimplified = Math.abs((0, gcd_1.gcd)(numerator, denumerator)) === 1;
14
+ this.tex = "\\frac{".concat(this.num, "}{").concat(this.denum, "}");
10
15
  }
11
16
  Rational.prototype.toTex = function () {
12
17
  return "\\frac{".concat(this.num, "}{").concat(this.denum, "}");
13
18
  };
19
+ Rational.prototype.add = function (expression) {
20
+ return this;
21
+ };
22
+ Rational.prototype.multiply = function (expression) {
23
+ return this;
24
+ };
25
+ Rational.prototype.opposite = function () {
26
+ return new Rational(-this.num, this.denum);
27
+ };
28
+ Rational.prototype.toTree = function () {
29
+ return new divideNode_1.DivideNode(new numberNode_1.NumberNode(this.num), new numberNode_1.NumberNode(this.denum));
30
+ };
14
31
  Rational.prototype.simplify = function () {
32
+ var sign = (this.num > 0 && this.denum > 0) || (this.num < 0 && this.denum < 0)
33
+ ? 1
34
+ : -1;
15
35
  var div = Math.abs((0, gcd_1.gcd)(this.num, this.denum));
16
- return new Rational(this.num / div, this.denum / div);
36
+ if (Math.abs(this.denum) === div)
37
+ return new integer_1.Integer(this.num / this.denum);
38
+ return new Rational((sign * Math.abs(this.num)) / div, Math.abs(this.denum) / div);
17
39
  };
18
40
  return Rational;
19
41
  }());
@@ -1,22 +1,56 @@
1
+ import { Expression } from "../../expression/expression";
1
2
  import { gcd } from "../../mathutils/arithmetic/gcd";
3
+ import { Node } from "../../tree/nodes/node";
4
+ import { NumberNode } from "../../tree/nodes/numbers/numberNode";
5
+ import { DivideNode } from "../../tree/nodes/operators/divideNode";
6
+ import { Integer } from "../integer/integer";
7
+ import { Nombre } from "../nombre";
2
8
 
3
- export class Rational {
9
+ export class Rational implements Nombre {
4
10
  num: number;
5
11
  denum: number;
12
+ tex: string;
13
+ value: number;
6
14
  isSimplified: boolean;
7
15
 
8
16
  constructor(numerator: number, denumerator: number) {
9
17
  this.num = numerator;
10
18
  this.denum = denumerator;
19
+ this.value = numerator / denumerator;
11
20
  this.isSimplified = Math.abs(gcd(numerator, denumerator)) === 1;
21
+ this.tex = `\\frac{${this.num}}{${this.denum}}`;
12
22
  }
13
23
 
14
24
  toTex() {
15
25
  return `\\frac{${this.num}}{${this.denum}}`;
16
26
  }
17
27
 
18
- simplify(): Rational {
28
+ add(expression: Expression): Expression {
29
+ return this;
30
+ }
31
+
32
+ multiply(expression: Expression): Expression {
33
+ return this;
34
+ }
35
+
36
+ opposite(): Rational {
37
+ return new Rational(-this.num, this.denum);
38
+ }
39
+
40
+ toTree(): Node {
41
+ return new DivideNode(new NumberNode(this.num), new NumberNode(this.denum));
42
+ }
43
+
44
+ simplify(): Nombre {
45
+ const sign =
46
+ (this.num > 0 && this.denum > 0) || (this.num < 0 && this.denum < 0)
47
+ ? 1
48
+ : -1;
19
49
  const div = Math.abs(gcd(this.num, this.denum));
20
- return new Rational(this.num / div, this.denum / div);
50
+ if (Math.abs(this.denum) === div) return new Integer(this.num / this.denum);
51
+ return new Rational(
52
+ (sign * Math.abs(this.num)) / div,
53
+ Math.abs(this.denum) / div
54
+ );
21
55
  }
22
56
  }
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ exports.__esModule = true;
3
+ exports.Real = void 0;
4
+ var numberNode_1 = require("../../tree/nodes/numbers/numberNode");
5
+ var Real = /** @class */ (function () {
6
+ function Real(value, tex) {
7
+ this.value = value;
8
+ this.tex = tex;
9
+ }
10
+ Real.prototype.toTree = function () {
11
+ return new numberNode_1.NumberNode(this.value);
12
+ };
13
+ return Real;
14
+ }());
15
+ exports.Real = Real;
@@ -1 +1,15 @@
1
- export interface Real {}
1
+ import { Node } from "../../tree/nodes/node";
2
+ import { NumberNode } from "../../tree/nodes/numbers/numberNode";
3
+ import { Nombre } from "../nombre";
4
+
5
+ export class Real implements Nombre {
6
+ value: number;
7
+ tex: string;
8
+ constructor(value: number, tex: string) {
9
+ this.value = value;
10
+ this.tex = tex;
11
+ }
12
+ toTree(): Node {
13
+ return new NumberNode(this.value);
14
+ }
15
+ }
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ exports.__esModule = true;
18
+ exports.SquareRoot = exports.SquareRootConstructor = void 0;
19
+ var isSquare_1 = require("../../mathutils/arithmetic/isSquare");
20
+ var primeFactors_1 = require("../../mathutils/arithmetic/primeFactors");
21
+ var randint_1 = require("../../mathutils/random/randint");
22
+ var numberNode_1 = require("../../tree/nodes/numbers/numberNode");
23
+ var multiplyNode_1 = require("../../tree/nodes/operators/multiplyNode");
24
+ var real_1 = require("./real");
25
+ var sqrtNode_1 = require("../../tree/nodes/functions/sqrtNode");
26
+ var SquareRootConstructor = /** @class */ (function () {
27
+ function SquareRootConstructor() {
28
+ }
29
+ /**
30
+ * @returns simplifiable square root type sqrt(c)=a*sqrt(b)
31
+ */
32
+ SquareRootConstructor.randomSimplifiable = function (_a) {
33
+ var _b = _a.allowPerfectSquare, allowPerfectSquare = _b === void 0 ? false : _b, _c = _a.maxSquare, maxSquare = _c === void 0 ? 11 : _c;
34
+ var a = (0, randint_1.randint)(2, maxSquare);
35
+ var b;
36
+ var bMin = allowPerfectSquare ? 1 : 2;
37
+ do {
38
+ b = (0, randint_1.randint)(bMin, maxSquare);
39
+ } while (b % (a * a) === 0 || (0, isSquare_1.isSquare)(b));
40
+ return new SquareRoot(a * a * b);
41
+ };
42
+ return SquareRootConstructor;
43
+ }());
44
+ exports.SquareRootConstructor = SquareRootConstructor;
45
+ var SquareRoot = /** @class */ (function (_super) {
46
+ __extends(SquareRoot, _super);
47
+ function SquareRoot(operand) {
48
+ var _this = _super.call(this, Math.sqrt(operand), "\\sqrt{".concat(operand, "}")) || this;
49
+ _this.operand = operand;
50
+ return _this;
51
+ }
52
+ SquareRoot.prototype.simplify = function () {
53
+ var factors = (0, primeFactors_1.primeFactors)(this.operand);
54
+ // finds primes with even exponents
55
+ var multiples = [1];
56
+ for (var i = 0; i < factors.length - 1; i++) {
57
+ if (factors[i] === factors[i + 1]) {
58
+ multiples.push(factors[i]);
59
+ factors.splice(i, 2);
60
+ i--;
61
+ }
62
+ }
63
+ var outsideSqrt = multiples.reduce(function (x, y) { return x * y; });
64
+ var insideSqrt = factors.length === 0 ? 1 : factors.reduce(function (x, y) { return x * y; });
65
+ var simplified = insideSqrt !== 1
66
+ ? new real_1.Real(outsideSqrt * Math.sqrt(insideSqrt), "".concat(outsideSqrt === 1 ? "" : "".concat(outsideSqrt), "\\sqrt{").concat(insideSqrt, "}"))
67
+ : new real_1.Real(outsideSqrt, outsideSqrt + "");
68
+ simplified.toTree = function () {
69
+ return insideSqrt !== 1
70
+ ? outsideSqrt === 1
71
+ ? new sqrtNode_1.SqrtNode(new numberNode_1.NumberNode(insideSqrt))
72
+ : new multiplyNode_1.MultiplyNode(new numberNode_1.NumberNode(outsideSqrt), new sqrtNode_1.SqrtNode(new numberNode_1.NumberNode(insideSqrt)))
73
+ : new numberNode_1.NumberNode(outsideSqrt);
74
+ };
75
+ return simplified;
76
+ };
77
+ SquareRoot.prototype.toTex = function () {
78
+ return this.tex;
79
+ };
80
+ SquareRoot.prototype.toTree = function () {
81
+ return new sqrtNode_1.SqrtNode(new numberNode_1.NumberNode(this.operand));
82
+ };
83
+ return SquareRoot;
84
+ }(real_1.Real));
85
+ exports.SquareRoot = SquareRoot;
@@ -1,13 +1,20 @@
1
1
  import { isSquare } from "../../mathutils/arithmetic/isSquare";
2
2
  import { primeFactors } from "../../mathutils/arithmetic/primeFactors";
3
3
  import { randint } from "../../mathutils/random/randint";
4
+ import { Node } from "../../tree/nodes/node";
5
+ import { NumberNode } from "../../tree/nodes/numbers/numberNode";
6
+ import { MultiplyNode } from "../../tree/nodes/operators/multiplyNode";
4
7
  import { Real } from "./real";
8
+ import { SqrtNode } from "../../tree/nodes/functions/sqrtNode";
5
9
 
6
10
  export abstract class SquareRootConstructor {
7
11
  /**
8
12
  * @returns simplifiable square root type sqrt(c)=a*sqrt(b)
9
13
  */
10
- randomSimplifiable({ allowPerfectSquare = false, maxSquare = 11 }): SquareRoot {
14
+ static randomSimplifiable({
15
+ allowPerfectSquare = false,
16
+ maxSquare = 11,
17
+ }): SquareRoot {
11
18
  const a = randint(2, maxSquare);
12
19
  let b;
13
20
  let bMin = allowPerfectSquare ? 1 : 2;
@@ -18,37 +25,53 @@ export abstract class SquareRootConstructor {
18
25
  }
19
26
  }
20
27
 
21
- export class SquareRoot implements Real {
22
- tex: string;
23
-
24
- constructor(operand: string | number) {
25
- this.tex = `\\sqrt{${operand}}`;
28
+ export class SquareRoot extends Real {
29
+ operand: number;
30
+ constructor(operand: number) {
31
+ super(Math.sqrt(operand), `\\sqrt{${operand}}`);
32
+ this.operand = operand;
26
33
  }
27
34
 
28
- simplify() {
29
- const factors = primeFactors(b);
30
-
31
- /**
32
- * finds primes with even exponents
33
- */
35
+ simplify(): Real {
36
+ const factors = primeFactors(this.operand);
37
+ // finds primes with even exponents
34
38
  const multiples = [1];
35
39
  for (let i = 0; i < factors.length - 1; i++) {
36
40
  if (factors[i] === factors[i + 1]) {
37
41
  multiples.push(factors[i]);
38
- factors.splice(i, 2);
42
+ factors.splice(i, 2);
43
+ i--;
39
44
  }
40
45
  }
46
+ const outsideSqrt = multiples.reduce((x, y) => x * y);
47
+ const insideSqrt =
48
+ factors.length === 0 ? 1 : factors.reduce((x, y) => x * y);
49
+
50
+ const simplified =
51
+ insideSqrt !== 1
52
+ ? new Real(
53
+ outsideSqrt * Math.sqrt(insideSqrt),
54
+ `${outsideSqrt === 1 ? "" : `${outsideSqrt}`}\\sqrt{${insideSqrt}}`
55
+ )
56
+ : new Real(outsideSqrt, outsideSqrt + "");
57
+ simplified.toTree = (): Node => {
58
+ return insideSqrt !== 1
59
+ ? outsideSqrt === 1
60
+ ? new SqrtNode(new NumberNode(insideSqrt))
61
+ : new MultiplyNode(
62
+ new NumberNode(outsideSqrt),
63
+ new SqrtNode(new NumberNode(insideSqrt))
64
+ )
65
+ : new NumberNode(outsideSqrt);
66
+ };
67
+ return simplified;
41
68
  }
42
69
 
43
70
  toTex(): string {
44
71
  return this.tex;
45
72
  }
46
- }
47
73
 
48
- const outsideSqrtB = multiples.reduce((x, y) => x * y); // A should be muliply be all those numbers
49
- const insideSqrtB = factors.length === 0 ? "" : factors.reduce((x, y) => x * y); // here is what remains in the squareroot
50
-
51
- if (b === 1) answer = `${a}`;
52
- else if (factors.length === 0) answer = `${a * outsideSqrtB}`;
53
- // if the is no leftover in the squareroot, it just vanish
54
- else answer = `${a * outsideSqrtB}\\sqrt{${insideSqrtB}}`;
74
+ toTree(): Node {
75
+ return new SqrtNode(new NumberNode(this.operand));
76
+ }
77
+ }
@@ -20,18 +20,19 @@ var rational_1 = require("../numbers/rationals/rational");
20
20
  var intervals_1 = require("../sets/intervals/intervals");
21
21
  var polynomial_1 = require("./polynomial");
22
22
  var discreteSet_1 = require("../sets/discreteSet");
23
+ var integer_1 = require("../numbers/integer/integer");
23
24
  var AffineConstructor = /** @class */ (function () {
24
25
  function AffineConstructor() {
25
26
  }
26
27
  AffineConstructor.random = function (domainA, domainB) {
27
- if (domainA === void 0) { domainA = new intervals_1.Interval("[[-10; 10]]").difference(new discreteSet_1.DiscreteSet([0])); }
28
+ if (domainA === void 0) { domainA = new intervals_1.Interval("[[-10; 10]]").difference(new discreteSet_1.DiscreteSet([new integer_1.Integer(0)])); }
28
29
  if (domainB === void 0) { domainB = new intervals_1.Interval("[[-10; 10]]"); }
29
30
  var a = domainA.getRandomElement();
30
31
  var b = domainB.getRandomElement();
31
- return new Affine(a, b);
32
+ return new Affine(a.value, b.value);
32
33
  };
33
34
  AffineConstructor.differentRandoms = function (nb, domainA, domainB) {
34
- if (domainA === void 0) { domainA = new intervals_1.Interval("[[-10; 10]]").difference(new discreteSet_1.DiscreteSet([0])); }
35
+ if (domainA === void 0) { domainA = new intervals_1.Interval("[[-10; 10]]").difference(new discreteSet_1.DiscreteSet([new integer_1.Integer(0)])); }
35
36
  if (domainB === void 0) { domainB = new intervals_1.Interval("[[-10; 10]]"); }
36
37
  var res = [];
37
38
  var _loop_1 = function (i) {
@@ -60,7 +61,7 @@ var Affine = /** @class */ (function (_super) {
60
61
  return _this;
61
62
  }
62
63
  Affine.prototype.getRoot = function () {
63
- return new rational_1.Rational(-this.b, this.a).simplify().toTex();
64
+ return new rational_1.Rational(-this.b, this.a).simplify();
64
65
  };
65
66
  Affine.prototype.toString = function () {
66
67
  return _super.prototype.toTex.call(this);
@@ -1,32 +1,34 @@
1
- import { Expression } from "../expression/expression";
2
- import { add } from "../latex/add";
3
- import { NumberType } from "../numbers/number";
4
1
  import { Rational } from "../numbers/rationals/rational";
5
2
  import { Interval } from "../sets/intervals/intervals";
6
- import { randint } from "../mathutils/random/randint";
7
3
  import { Polynomial } from "./polynomial";
8
- import { Latex } from "../latex/latex";
9
4
  import { DiscreteSet } from "../sets/discreteSet";
10
5
  import { MathSet } from "../sets/mathSet";
6
+ import { Integer } from "../numbers/integer/integer";
7
+ import { Node, NodeType } from "../tree/nodes/node";
8
+ import { MultiplyNode } from "../tree/nodes/operators/multiplyNode";
9
+ import { NumberNode } from "../tree/nodes/numbers/numberNode";
10
+ import { VariableNode } from "../tree/nodes/variables/variableNode";
11
+ import { AddNode } from "../tree/nodes/operators/addNode";
12
+ import { Nombre } from "../numbers/nombre";
11
13
 
12
14
  export abstract class AffineConstructor {
13
15
  static random(
14
- domainA: MathSet<number> = new Interval("[[-10; 10]]").difference(
15
- new DiscreteSet([0])
16
+ domainA: MathSet = new Interval("[[-10; 10]]").difference(
17
+ new DiscreteSet([new Integer(0)])
16
18
  ),
17
- domainB: MathSet<number> = new Interval("[[-10; 10]]")
19
+ domainB: MathSet = new Interval("[[-10; 10]]")
18
20
  ): Affine {
19
21
  const a = domainA.getRandomElement();
20
22
  const b = domainB.getRandomElement();
21
- return new Affine(a, b);
23
+ return new Affine(a.value, b.value);
22
24
  }
23
25
 
24
26
  static differentRandoms(
25
27
  nb: number,
26
- domainA: MathSet<number> = new Interval("[[-10; 10]]").difference(
27
- new DiscreteSet([0])
28
+ domainA: MathSet = new Interval("[[-10; 10]]").difference(
29
+ new DiscreteSet([new Integer(0)])
28
30
  ),
29
- domainB: MathSet<number> = new Interval("[[-10; 10]]")
31
+ domainB: MathSet = new Interval("[[-10; 10]]")
30
32
  ): Affine[] {
31
33
  const res: Affine[] = [];
32
34
  for (let i = 0; i < nb; i++) {
@@ -38,13 +40,6 @@ export abstract class AffineConstructor {
38
40
  }
39
41
  return res;
40
42
  }
41
-
42
- // static add(aff1: Affine, aff2: Affine | number): Affine {
43
- // if(typeof aff2)
44
- // }
45
- // static randomWithIntegerRoot(): Affine {
46
- // const root = randint(-10, 10);
47
- // }
48
43
  }
49
44
 
50
45
  export class Affine extends Polynomial {
@@ -59,8 +54,8 @@ export class Affine extends Polynomial {
59
54
  this.variable = variable;
60
55
  }
61
56
 
62
- getRoot(): string {
63
- return new Rational(-this.b, this.a).simplify().toTex();
57
+ getRoot(): Nombre {
58
+ return new Rational(-this.b, this.a).simplify();
64
59
  }
65
60
 
66
61
  toString(): string {
@@ -1,6 +1,12 @@
1
1
  "use strict";
2
2
  exports.__esModule = true;
3
3
  exports.Polynomial = void 0;
4
+ var numberNode_1 = require("../tree/nodes/numbers/numberNode");
5
+ var addNode_1 = require("../tree/nodes/operators/addNode");
6
+ var multiplyNode_1 = require("../tree/nodes/operators/multiplyNode");
7
+ var oppositeNode_1 = require("../tree/nodes/operators/oppositeNode");
8
+ var powerNode_1 = require("../tree/nodes/operators/powerNode");
9
+ var variableNode_1 = require("../tree/nodes/variables/variableNode");
4
10
  var Polynomial = /** @class */ (function () {
5
11
  /**
6
12
  *
@@ -11,8 +17,9 @@ var Polynomial = /** @class */ (function () {
11
17
  if (variable === void 0) { variable = "x"; }
12
18
  if (coefficients.length === 0)
13
19
  throw Error("coeffs must be not null");
14
- if (coefficients[coefficients.length - 1] === 0)
20
+ if (coefficients[coefficients.length - 1] === 0) {
15
21
  throw Error("n-th coeff must be not null");
22
+ }
16
23
  this.coefficients = coefficients;
17
24
  this.variable = variable;
18
25
  this.degree = coefficients.length - 1;
@@ -21,6 +28,7 @@ var Polynomial = /** @class */ (function () {
21
28
  return (P.degree === this.degree &&
22
29
  this.coefficients.every(function (coeff, i) { return coeff === P.coefficients[i]; }));
23
30
  };
31
+ Polynomial.prototype.getRoots = function () { };
24
32
  Polynomial.prototype.add = function (P) {
25
33
  if (P.variable !== this.variable)
26
34
  throw Error("Can't add two polynomials with different variables");
@@ -34,6 +42,9 @@ var Polynomial = /** @class */ (function () {
34
42
  }
35
43
  return new Polynomial(res, this.variable);
36
44
  };
45
+ Polynomial.prototype.times = function (nb) {
46
+ return new Polynomial(this.coefficients.map(function (coeff) { return coeff * nb; }), this.variable);
47
+ };
37
48
  Polynomial.prototype.multiply = function (Q) {
38
49
  if (Q.variable !== this.variable)
39
50
  throw Error("Can't multiply two polynomials with different variables");
@@ -52,14 +63,42 @@ var Polynomial = /** @class */ (function () {
52
63
  Polynomial.prototype.opposite = function () {
53
64
  return new Polynomial(this.coefficients.map(function (coeff) { return -coeff; }), this.variable);
54
65
  };
66
+ Polynomial.prototype.toTree = function () {
67
+ var _this = this;
68
+ var recursive = function (cursor) {
69
+ var coeff = _this.coefficients[cursor];
70
+ if (coeff === 0)
71
+ return recursive(cursor - 1);
72
+ if (cursor === 0) {
73
+ return new numberNode_1.NumberNode(coeff);
74
+ }
75
+ var monome = cursor > 1
76
+ ? new powerNode_1.PowerNode(new variableNode_1.VariableNode(_this.variable), new numberNode_1.NumberNode(cursor))
77
+ : new variableNode_1.VariableNode(_this.variable);
78
+ var res;
79
+ if (coeff === 1)
80
+ res = monome;
81
+ else if (coeff === -1)
82
+ res = new oppositeNode_1.OppositeNode(monome);
83
+ else
84
+ res = new multiplyNode_1.MultiplyNode(new numberNode_1.NumberNode(coeff), monome);
85
+ var nextCoeff;
86
+ for (var i = cursor - 1; i > -1; i--) {
87
+ if (_this.coefficients[i]) {
88
+ nextCoeff = _this.coefficients[i];
89
+ break;
90
+ }
91
+ }
92
+ if (nextCoeff) {
93
+ return new addNode_1.AddNode(res, recursive(cursor - 1));
94
+ }
95
+ else {
96
+ return res;
97
+ }
98
+ };
99
+ return recursive(this.degree);
100
+ };
55
101
  Polynomial.prototype.toTex = function () {
56
- // if (this.degree == 0) return "" + this.b;
57
- // function getMonome(power: number): string {
58
- // if (power === 0) return "";
59
- // if (power === 1) return this.variable;
60
- // else return `${this.variable}^{${power}}`;
61
- // }
62
- // let s = this.coefficients[this.degree] + this.variable + this.degree > 1 ? `^{${this.degree}}` : "";
63
102
  var s = "";
64
103
  for (var i = this.degree; i > -1; i--) {
65
104
  var coeff = this.coefficients[i];
@@ -87,9 +126,6 @@ var Polynomial = /** @class */ (function () {
87
126
  s += this.variable;
88
127
  else
89
128
  s += "".concat(this.variable, "^{").concat(i, "}");
90
- // latex.add(this.coefficients[i]);
91
- // s += addLatex(this.coefficients[i]);
92
- // s += this.coefficients[i] + this.variable + `^{${i}}`;
93
129
  }
94
130
  return s;
95
131
  };
@@ -1,5 +1,12 @@
1
1
  import { Expression } from "../expression/expression";
2
- import { Latex } from "../latex/latex";
2
+ import { Node } from "../tree/nodes/node";
3
+ import { NumberNode } from "../tree/nodes/numbers/numberNode";
4
+ import { AddNode } from "../tree/nodes/operators/addNode";
5
+ import { MultiplyNode } from "../tree/nodes/operators/multiplyNode";
6
+ import { OppositeNode } from "../tree/nodes/operators/oppositeNode";
7
+ import { PowerNode } from "../tree/nodes/operators/powerNode";
8
+ import { SubstractNode } from "../tree/nodes/operators/substractNode";
9
+ import { VariableNode } from "../tree/nodes/variables/variableNode";
3
10
 
4
11
  export class Polynomial implements Expression {
5
12
  degree: number;
@@ -16,8 +23,9 @@ export class Polynomial implements Expression {
16
23
  */
17
24
  constructor(coefficients: number[], variable: string = "x") {
18
25
  if (coefficients.length === 0) throw Error("coeffs must be not null");
19
- if (coefficients[coefficients.length - 1] === 0)
26
+ if (coefficients[coefficients.length - 1] === 0) {
20
27
  throw Error("n-th coeff must be not null");
28
+ }
21
29
  this.coefficients = coefficients;
22
30
  this.variable = variable;
23
31
  this.degree = coefficients.length - 1;
@@ -28,7 +36,7 @@ export class Polynomial implements Expression {
28
36
  this.coefficients.every((coeff, i) => coeff === P.coefficients[i])
29
37
  );
30
38
  }
31
-
39
+ getRoots() {}
32
40
  add(P: Polynomial): Polynomial {
33
41
  if (P.variable !== this.variable)
34
42
  throw Error("Can't add two polynomials with different variables");
@@ -38,13 +46,19 @@ export class Polynomial implements Expression {
38
46
  P.coefficients[P.degree] === -this.coefficients[this.degree]
39
47
  ? P.degree - 1
40
48
  : Math.max(P.degree, this.degree);
49
+
41
50
  const res: number[] = [];
42
51
  for (let i = 0; i < newDegree + 1; i++) {
43
52
  res[i] = P.coefficients[i] + this.coefficients[i];
44
53
  }
45
54
  return new Polynomial(res, this.variable);
46
55
  }
47
-
56
+ times(nb: number): Polynomial {
57
+ return new Polynomial(
58
+ this.coefficients.map((coeff) => coeff * nb),
59
+ this.variable
60
+ );
61
+ }
48
62
  multiply(Q: Polynomial): Polynomial {
49
63
  if (Q.variable !== this.variable)
50
64
  throw Error("Can't multiply two polynomials with different variables");
@@ -64,22 +78,52 @@ export class Polynomial implements Expression {
64
78
  return new Polynomial(res, this.variable);
65
79
  }
66
80
 
67
- opposite() {
81
+ opposite(): Polynomial {
68
82
  return new Polynomial(
69
83
  this.coefficients.map((coeff) => -coeff),
70
84
  this.variable
71
85
  );
72
86
  }
73
87
 
74
- toTex(): string {
75
- // if (this.degree == 0) return "" + this.b;
76
- // function getMonome(power: number): string {
77
- // if (power === 0) return "";
78
- // if (power === 1) return this.variable;
79
- // else return `${this.variable}^{${power}}`;
80
- // }
88
+ toTree(): Node {
89
+ const recursive = (cursor: number): Node => {
90
+ const coeff = this.coefficients[cursor];
91
+ if (coeff === 0) return recursive(cursor - 1);
92
+
93
+ if (cursor === 0) {
94
+ return new NumberNode(coeff);
95
+ }
96
+
97
+ const monome =
98
+ cursor > 1
99
+ ? new PowerNode(
100
+ new VariableNode(this.variable),
101
+ new NumberNode(cursor)
102
+ )
103
+ : new VariableNode(this.variable);
104
+
105
+ let res: Node;
106
+ if (coeff === 1) res = monome;
107
+ else if (coeff === -1) res = new OppositeNode(monome);
108
+ else res = new MultiplyNode(new NumberNode(coeff), monome);
109
+
110
+ let nextCoeff;
111
+ for (let i = cursor - 1; i > -1; i--) {
112
+ if (this.coefficients[i]) {
113
+ nextCoeff = this.coefficients[i];
114
+ break;
115
+ }
116
+ }
117
+ if (nextCoeff) {
118
+ return new AddNode(res, recursive(cursor - 1));
119
+ } else {
120
+ return res;
121
+ }
122
+ };
123
+ return recursive(this.degree);
124
+ }
81
125
 
82
- // let s = this.coefficients[this.degree] + this.variable + this.degree > 1 ? `^{${this.degree}}` : "";
126
+ toTex(): string {
83
127
  let s = "";
84
128
  for (let i = this.degree; i > -1; i--) {
85
129
  const coeff = this.coefficients[i];
@@ -101,10 +145,6 @@ export class Polynomial implements Expression {
101
145
  if (i === 0) continue;
102
146
  if (i === 1) s += this.variable;
103
147
  else s += `${this.variable}^{${i}}`;
104
-
105
- // latex.add(this.coefficients[i]);
106
- // s += addLatex(this.coefficients[i]);
107
- // s += this.coefficients[i] + this.variable + `^{${i}}`;
108
148
  }
109
149
  return s;
110
150
  }