Skip to content
Snippets Groups Projects
Commit e7a7c76d authored by Ettayeb Yassine's avatar Ettayeb Yassine
Browse files

first question

parent 4abc713d
No related branches found
No related tags found
No related merge requests found
main.js 0 → 100644
// comptage d'instances
let instancesCreated = 0;
let instancesDestroyed = 0;
// classe de base pour toutes les expressions
class Expression {
constructor() {
instancesCreated++;
}
// méthode d'affichage
toString() {
return "";
}
// méthode de dérivation
derive(variableName) {
return new Nombre(0);
}
// méthode de simplification
simplify() {
return this;
}
destroy() {
instancesDestroyed++;
}
}
// classe représentant un nombre constant
class Nombre extends Expression {
constructor(value) {
super();
this.value = value;
}
toString() {
return this.value.toString();
}
// dérivée d'un nb cst = 0
derive(variableName) {
return new Nombre(0);
}
// nb est déjà simplifié
simplify() {
return this;
}
}
// classe représentant une variable
class Variable extends Expression {
constructor(name) {
super();
this.name = name;
}
toString() {
return this.name;
}
// dérivée d'une var est 1 si c'est la variable par rapport à laquelle
// on dérive, 0 sinon
derive(variableName) {
return this.name === variableName ? new Nombre(1) : new Nombre(0);
}
simplify() {
return this;
}
}
// classe de base pour toutes les opérations
class Operation extends Expression {
constructor(leftOperand, rightOperand) {
super();
this.leftOperand = leftOperand;
this.rightOperand = rightOperand;
}
}
class Addition extends Operation {
constructor(leftOperand, rightOperand) {
super(leftOperand, rightOperand);
}
toString() {
return `(${this.leftOperand.toString()} + ${this.rightOperand.toString()})`;
}
// dérivée d'une somme = somme des dérivées
derive(variableName) {
return new Addition(
this.leftOperand.derive(variableName),
this.rightOperand.derive(variableName)
);
}
// simplification addition
simplify() {
const leftSimplified = this.leftOperand.simplify();
const rightSimplified = this.rightOperand.simplify();
// si les deux opérandes sont des nombres, on peut calculer le résultat
if (leftSimplified instanceof Nombre && rightSimplified instanceof Nombre) {
return new Nombre(leftSimplified.value + rightSimplified.value);
}
// si l'un des opérandes est 0, on retourne l'autre
if (leftSimplified instanceof Nombre && leftSimplified.value === 0) {
return rightSimplified;
}
if (rightSimplified instanceof Nombre && rightSimplified.value === 0) {
return leftSimplified;
}
// sinon, on crée une nouvelle addition avec les opérandes simplifiés
return new Addition(leftSimplified, rightSimplified);
}
}
class Multiplication extends Operation {
constructor(leftOperand, rightOperand) {
super(leftOperand, rightOperand);
}
toString() {
return `(${this.leftOperand.toString()} * ${this.rightOperand.toString()})`;
}
// (f*g)' = f'*g + f*g'
derive(variableName) {
const leftDerivative = this.leftOperand.derive(variableName);
const rightDerivative = this.rightOperand.derive(variableName);
return new Addition(
new Multiplication(leftDerivative, this.rightOperand),
new Multiplication(this.leftOperand, rightDerivative)
);
}
simplify() {
const leftSimplified = this.leftOperand.simplify();
const rightSimplified = this.rightOperand.simplify();
// si les deux opérandes sont des nombres, on peut calculer le résultat
if (leftSimplified instanceof Nombre && rightSimplified instanceof Nombre) {
return new Nombre(leftSimplified.value * rightSimplified.value);
}
// si l'un des opérandes est 0, le résultat est 0
if ((leftSimplified instanceof Nombre && leftSimplified.value === 0) ||
(rightSimplified instanceof Nombre && rightSimplified.value === 0)) {
return new Nombre(0);
}
// si l'un des opérandes est 1, on retourne l'autre
if (leftSimplified instanceof Nombre && leftSimplified.value === 1) {
return rightSimplified;
}
if (rightSimplified instanceof Nombre && rightSimplified.value === 1) {
return leftSimplified;
}
// sinon, on crée une nouvelle multiplication avec les opérandes simplifiés
return new Multiplication(leftSimplified, rightSimplified);
}
}
function test() {
const x = new Variable("x");
const y = new Variable("y");
const deux = new Nombre(2);
const trois = new Nombre(3);
// expression: 2*x + 3*y
const expr = new Addition(
new Multiplication(deux, x),
new Multiplication(trois, y)
);
console.log("Expression originale:", expr.toString());
// dérivation par rapport à x
const derivedX = expr.derive("x");
console.log("Dérivée par rapport à x:", derivedX.toString());
// dérivation par rapport à y
const derivedY = expr.derive("y");
console.log("Dérivée par rapport à y:", derivedY.toString());
// simplification de la dérivée par rapport à x
const simplifiedDerivedX = derivedX.simplify();
console.log("Dérivée simplifiée par rapport à x:", simplifiedDerivedX.toString());
// simplification de la dérivée par rapport à y
const simplifiedDerivedY = derivedY.simplify();
console.log("Dérivée simplifiée par rapport à y:", simplifiedDerivedY.toString());
}
test();
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment