J'écris un morceau de code javascript côté client qui prend une fonction et trouve la dérivée de celui-ci, cependant, l'expression rationnelle qui est censée correspondre avec la règle de puissance ne fonctionne pas dans le contexte du programme javascript, bien qu'elle corresponde avec succès lorsqu'elle est utilisée avec un testeur de regex indépendant.JS regex ne correspond pas, même si cela fonctionne avec un testeur regex
Le code est très strict pour le moment, et je prévois de flesh it out plus tard.
Le navigateur sur lequel j'exécute est Midori, et le système d'exploitation est Ubuntu 10.04 (Lucid Lynx). Voici la page HTML utilisé comme l'interface en plus du code:
page:
<html>
<head>
<title>
Derivative Calculator
</title>
<script type="text/javascript" src="derivative.js">
</script>
<body>
<form action="" name=form>
<input type=text name=f /> with respects to
<input type=text name=vr size=7 />
<input type=button value="Derive!" onClick="main(this.form)" /> <br />
<input type=text name=result value="" />
</form>
</body>
</html>
derivative.js:
function main(form) {
form.result.value = derive(form.f.value, form.vr.value);
}
function derive(f, v) {
var atom = [];
atom["sin(" + v + ")"] = "cos(" + v + ")";
atom["cos(" + v + ")"] = "-sin(" + v + ")";
atom["tan(" + v + ")"] = "sec^(2)(" + v + ")";
atom["sec(" + v + ")"] = "sec(" + v + ")*tan(" + v + ")";
atom["1/(cos(" + v + "))"] = "sec(" + v + ")*tan(" + v + ")";
atom["csc(" + v + ")"] = "-csc(" + v + ")*cot(" + v + ")";
atom["1/(sin(" + v + "))"] = "-csc(" + v + ")*cot(" + v + ")";
atom["cot(" + v + ")"] = "-csc^(2)(" + v + ")";
atom["1/(tan(" + v + "))"] = "-csc^(2)(" + v + ")";
atom["sin^(-1)(" + v + ")"] = "1/sqrt(1 - " + v + "^(2))";
atom["arcsin(" + v + ")"] = "1/sqrt(1 - " + v + "^(2))";
atom["cos^(-1)(" + v + ")"] = "-1/sqrt(1 - " + v + "^(2))";
atom["arccos(" + v + ")"] = "-1/sqrt(1 - " + v + "^(2))";
atom["tan^(-1)(" + v + ")"] = "1/(1 + " + v + "^(2))";
atom["arctan(" + v + ")"] = "1/(1 + " + v + "^(2))";
atom["sec^(-1)(" + v + ")"] = "1/(|" + v + "|*sqrt(" + v + "^(2) - 1))";
atom["arcsec(" + v + ")"] = "1/(|" + v + "|*sqrt(" + v + "^(2) - 1))";
atom["csc^(-1)(" + v + ")"] = "-1/(|" + v + "|*sqrt(" + v + "^(2) - 1))";
atom["arccsc(" + v + ")"] = "-1/(|" + v + "|*sqrt(" + v + "^(2) - 1))";
atom["cot^(-1)(" + v + ")"] = "-1/(1 + " + v + "^(2))";
atom["arccot(" + v + ")"] = "-1/(1 + " + v + "^(2))";
atom["ln(" + v + ")"] = "1/(" + v + ")";
atom["e^(" + v + ")"] = "e^(" + v + ")";
atom["ln(|" + v + "|)"] = "1/(" + v + ")";
atom[v] = "1";
var match = "";
if (new Boolean(atom[f]) == true) {
return atom[f];
} else if (f.match(/^[0-9]+$/)) {
return "";
} else if (f.match(/([\S]+)([\s]+)\+([\s]+)([\S]+)/)) {
match = /([\S]+)([\s]+)\+([\s]+)([\S]+)/.exec(f);
return derive(match[1], v) + " + " + derive(match[4], v);
} else if (f.match(new RegExp("^([0-9]+)(" + v + ")$"))) {
match = new RegExp("^([0-9]+)(" + v + ")$").exec(f);
return match[1];
} else if (f.match(new RegExp("^([0-9]+)(" + v + ")\^([0-9]+)$"))) {
match = new RegExp("^([0-9]+)(" + v + ")\^([0-9]+)$").exec(f);
return String((match[1] * (match[3]))) + v + "^" +
String(match[3] - 1);
} else {
return "?";
}
}
En particulier, je vais avoir du mal avec ce modèle :
} else if (f.match(new RegExp("^([0-9]+)(" + v + ")\^([0-9]+)$"))) {
match = new RegExp("^([0-9]+)(" + v + ")\^([0-9]+)$").exec(f);
return String((match[1] * (match[3]))) + v + "^" +
String(match[3] - 1);
Veuillez préciser la chaîne et le motif avec lesquels vous rencontrez des problèmes. On dirait que vous en avez plusieurs, je ne suis pas prêt à passer en revue et à corriger le programme entier, mais je pourrais facilement vous aider avec une chaîne. –
J'ai spécifié le motif avec lequel je suis en difficulté (celui qui est censé correspondre à la règle de puissance). Cependant, étant donné que certains utilisateurs ne sont peut-être pas familiers avec ce que c'est ou ce à quoi il est censé ressembler, j'ai clarifié davantage. –
EDIT: En fait, il y avait une petite erreur mathématique, alors je m'excuse pour toute confusion que j'ai causée. –