nerdamer
Advanced tools
Comparing version 1.1.10 to 1.1.11
261
Extra.js
@@ -11,3 +11,3 @@ /* | ||
if ((typeof module) !== 'undefined') { | ||
if((typeof module) !== 'undefined') { | ||
var nerdamer = require('./nerdamer.core.js'); | ||
@@ -38,9 +38,9 @@ require('./Calculus'); | ||
//this is what we're looking for | ||
if (this.group === FN && this.fname === fname) | ||
if(this.group === FN && this.fname === fname) | ||
return this.clone(); | ||
var found; | ||
if (this.symbols) | ||
for (var x in this.symbols) { | ||
if(this.symbols) | ||
for(var x in this.symbols) { | ||
found = this.symbols[x].findFunction(fname); | ||
if (found) | ||
if(found) | ||
break; | ||
@@ -53,3 +53,3 @@ } | ||
var __ = core.Extra = { | ||
version: '1.4.1', | ||
version: '1.4.2', | ||
//http://integral-table.com/downloads/LaplaceTable.pdf | ||
@@ -61,3 +61,3 @@ //Laplace assumes all coefficients to be positive | ||
symbol = symbol.clone(); | ||
t = t.toString(); | ||
@@ -72,13 +72,13 @@ //First try a lookup for a speed boost | ||
if (symbol.isConstant() || !symbol.contains(t, true)) { | ||
if(symbol.isConstant() || !symbol.contains(t, true)) { | ||
retval = _.parse(format('({0})/({1})', symbol, s)); | ||
} | ||
else if (g === S && core.Utils.isInt(symbol.power)) { | ||
else if(g === S && core.Utils.isInt(symbol.power)) { | ||
var n = String(symbol.power); | ||
retval = _.parse(format('factorial({0})/({1})^({0}+1)', n, s)); | ||
} | ||
else if (symbol.group === S && symbol.power.equals(1 / 2)) { | ||
else if(symbol.group === S && symbol.power.equals(1 / 2)) { | ||
retval = _.parse(format('sqrt(pi)/(2*({0})^(3/2))', s)); | ||
} | ||
else if (symbol.isComposite()) { | ||
else if(symbol.isComposite()) { | ||
retval = new Symbol(0); | ||
@@ -89,3 +89,3 @@ symbol.each(function (x) { | ||
} | ||
else if (symbol.isE() && (symbol.power.group === S || symbol.power.group === CB)) { | ||
else if(symbol.isE() && (symbol.power.group === S || symbol.power.group === CB)) { | ||
var a = symbol.power.stripVar(t); | ||
@@ -97,6 +97,6 @@ retval = _.parse(format('1/(({1})-({0}))', a, s)); | ||
//support for symbols in fns with arguments in the form a*t or n*t where a = symbolic and n = Number | ||
if (symbol.group === FN && fns.indexOf(symbol.fname) !== -1 && (symbol.args[0].group === S || symbol.args[0].group === CB)) { | ||
if(symbol.group === FN && fns.indexOf(symbol.fname) !== -1 && (symbol.args[0].group === S || symbol.args[0].group === CB)) { | ||
var a = symbol.args[0].stripVar(t); | ||
switch (symbol.fname) { | ||
switch(symbol.fname) { | ||
case 'sin': | ||
@@ -122,3 +122,3 @@ retval = _.parse(format('({0})/(({1})^2+({0})^2)', a, s)); | ||
if (depth_is_lower) { | ||
if(depth_is_lower) { | ||
var integration_depth = core.Settings.integration_depth; //save the depth | ||
@@ -133,3 +133,3 @@ core.Settings.integration_depth = core.Settings.Laplace_integration_depth; //transforms need a little more room | ||
retval = core.Calculus.integrate(integration_expr, u); | ||
if (retval.hasIntegral()) | ||
if(retval.hasIntegral()) | ||
return _.symfunction('laplace', arguments); | ||
@@ -146,3 +146,3 @@ // _.error('Unable to compute transform'); | ||
if (depth_is_lower)//put the integration depth as it was | ||
if(depth_is_lower)//put the integration depth as it was | ||
core.Settings.integration_depth = integration_depth; | ||
@@ -159,6 +159,7 @@ } | ||
//expand and get partial fractions | ||
if(symbol.group === CB) | ||
if(symbol.group === CB) { | ||
symbol = core.Algebra.PartFrac.partfrac(_.expand(symbol), s_); | ||
} | ||
if (symbol.group === S || symbol.group === CB || symbol.isComposite()) { | ||
if(symbol.group === S || symbol.group === CB || symbol.isComposite()) { | ||
var finalize = function () { | ||
@@ -178,3 +179,3 @@ //put back the numerator | ||
den = symbol.getDenom().toUnitMultiplier(); | ||
//TODO: Make it so factor doesn't destroy pi | ||
@@ -184,9 +185,10 @@ //num = core.Algebra.Factor.factor(symbol.getNum()); | ||
if (den.group === CP) { | ||
if(den.group === CP) { | ||
den_p = den.power.clone(); | ||
den.toLinear(); | ||
} | ||
else | ||
else { | ||
den_p = new core.Frac(1); | ||
} | ||
//convert s to a string | ||
@@ -201,3 +203,3 @@ s = s_.toString(); | ||
//check if in the form t^n where n = integer | ||
if ((den.group === S || den.group === CB) && f.x.value === s && f.b.equals(0) && core.Utils.isInt(f.x.power)) { | ||
if((den.group === S || den.group === CB) && f.x.value === s && f.b.equals(0) && core.Utils.isInt(f.x.power)) { | ||
var fact, p; | ||
@@ -211,55 +213,72 @@ p = f.x.power - 1; | ||
} | ||
else if (den.group === CP && den_p.equals(1)) { | ||
// a/(b*s-c) -> ae^(-bt) | ||
if (f.x.isLinear() && !num.contains(s)) { | ||
t = _.divide(t, f.a.clone()); | ||
retval = _.parse(format('(({0})^({3}-1)*e^(-(({2})*({0}))/({1})))/(({3}-1)!*({1})^({3}))', t, f.a, f.b, den_p)); | ||
//wrap it up | ||
finalize(); | ||
else if(den.group === CP && den_p.equals(1)) { | ||
if(f.x.group === core.groups.PL && core.Algebra.degree(den).equals(2)) { | ||
// Possibly in the form 1/(s^2+2*s+1) | ||
// Try factoring to get it in a more familiar form{ | ||
// Apply inverse of F(s-a) | ||
var completed = core.Algebra.sqComplete(den, s); | ||
var u = core.Utils.getU(den); | ||
// Get a for the function above | ||
var a = core.Utils.decompose_fn(completed.a, s, true).b; | ||
var tf = __.LaPlace.inverse(_.parse(`1/((${u})^2+(${completed.c}))`), u, t); | ||
retval = _.multiply(tf, _.parse(`(${m})*e^(-(${a})*(${t}))`)); | ||
} | ||
else { | ||
if (f.x.group === S && f.x.power.equals(2)) { | ||
if (!num.contains(s)) { | ||
retval = _.parse(format('(({1})*sin((sqrt(({2})*({3}))*({0}))/({2})))/sqrt(({2})*({3}))', t, num, f.a, f.b)); | ||
} | ||
// a*s/(b*s^2+c^2) | ||
else { | ||
var a = new Symbol(1); | ||
if (num.group === CB) { | ||
var new_num = new Symbol(1); | ||
num.each(function (x) { | ||
if (x.contains(s)) | ||
new_num = _.multiply(new_num, x); | ||
else | ||
a = _.multiply(a, x); | ||
}); | ||
num = new_num; | ||
} | ||
// a/(b*s-c) -> ae^(-bt) | ||
if(f.x.isLinear() && !num.contains(s)) { | ||
t = _.divide(t, f.a.clone()); | ||
//we need more information about the denominator to decide | ||
var f2 = core.Utils.decompose_fn(num, s, true); | ||
var fn1, fn2, a_has_sin, b_has_cos, a_has_cos, b_has_sin; | ||
fn1 = f2.a; | ||
fn2 = f2.b; | ||
a_has_sin = fn1.containsFunction('sin'); | ||
a_has_cos = fn1.containsFunction('cos'); | ||
b_has_cos = fn2.containsFunction('cos'); | ||
b_has_sin = fn2.containsFunction('sin'); | ||
if (f2.x.value === s && f2.x.isLinear() && !((a_has_sin && b_has_cos) || (a_has_cos || b_has_sin))) { | ||
retval = _.parse(format('(({1})*cos((sqrt(({2})*({3}))*({0}))/({2})))/({2})', t, f2.a, f.a, f.b)); | ||
// Don't add factorial of one or zero | ||
var p = den_p - 1; | ||
var fact = p === 0 || p === 1 ? '1' : `(${den_p}-1)!` | ||
retval = _.parse(format('(({0})^({3}-1)*e^(-(({2})*({0}))/({1})))/(({4})*({1})^({3}))', t, f.a, f.b, den_p, fact)); | ||
//wrap it up | ||
finalize(); | ||
} | ||
else { | ||
if(f.x.group === S && f.x.power.equals(2)) { | ||
if(!num.contains(s)) { | ||
retval = _.parse(format('(({1})*sin((sqrt(({2})*({3}))*({0}))/({2})))/sqrt(({2})*({3}))', t, num, f.a, f.b)); | ||
} | ||
// a*s/(b*s^2+c^2) | ||
else { | ||
if (a_has_sin && b_has_cos) { | ||
var sin, cos; | ||
sin = fn1.findFunction('sin'); | ||
cos = fn2.findFunction('cos'); | ||
//who has the s? | ||
if (sin.args[0].equals(cos.args[0]) && !sin.args[0].contains(s)) { | ||
var b, c, d, e; | ||
b = _.divide(fn2, cos.toUnitMultiplier()).toString(); | ||
c = sin.args[0].toString(); | ||
d = f.b; | ||
e = _.divide(fn1, sin.toUnitMultiplier()); | ||
exp = '(({1})*({2})*cos({3})*sin(sqrt({4})*({0})))/sqrt({4})+({1})*sin({3})*({5})*cos(sqrt({4})*({0}))'; | ||
retval = _.parse(format(exp, t, a, b, c, d, e)); | ||
var a = new Symbol(1); | ||
if(num.group === CB) { | ||
var new_num = new Symbol(1); | ||
num.each(function (x) { | ||
if(x.contains(s)) | ||
new_num = _.multiply(new_num, x); | ||
else | ||
a = _.multiply(a, x); | ||
}); | ||
num = new_num; | ||
} | ||
//we need more information about the denominator to decide | ||
var f2 = core.Utils.decompose_fn(num, s, true); | ||
var fn1, fn2, a_has_sin, b_has_cos, a_has_cos, b_has_sin; | ||
fn1 = f2.a; | ||
fn2 = f2.b; | ||
a_has_sin = fn1.containsFunction('sin'); | ||
a_has_cos = fn1.containsFunction('cos'); | ||
b_has_cos = fn2.containsFunction('cos'); | ||
b_has_sin = fn2.containsFunction('sin'); | ||
if(f2.x.value === s && f2.x.isLinear() && !((a_has_sin && b_has_cos) || (a_has_cos || b_has_sin))) { | ||
retval = _.parse(format('(({1})*cos((sqrt(({2})*({3}))*({0}))/({2})))/({2})', t, f2.a, f.a, f.b)); | ||
} | ||
else { | ||
if(a_has_sin && b_has_cos) { | ||
var sin, cos; | ||
sin = fn1.findFunction('sin'); | ||
cos = fn2.findFunction('cos'); | ||
//who has the s? | ||
if(sin.args[0].equals(cos.args[0]) && !sin.args[0].contains(s)) { | ||
var b, c, d, e; | ||
b = _.divide(fn2, cos.toUnitMultiplier()).toString(); | ||
c = sin.args[0].toString(); | ||
d = f.b; | ||
e = _.divide(fn1, sin.toUnitMultiplier()); | ||
exp = '(({1})*({2})*cos({3})*sin(sqrt({4})*({0})))/sqrt({4})+({1})*sin({3})*({5})*cos(sqrt({4})*({0}))'; | ||
retval = _.parse(format(exp, t, a, b, c, d, e)); | ||
} | ||
} | ||
@@ -272,9 +291,9 @@ } | ||
} | ||
else if (f.x.power.num && f.x.power.num.equals(3) && f.x.power.den.equals(2) && num.contains('sqrt(pi)') && !num.contains(s) && num.isLinear()) { | ||
else if(f.x.power.num && f.x.power.num.equals(3) && f.x.power.den.equals(2) && num.contains('sqrt(pi)') && !num.contains(s) && num.isLinear()) { | ||
var b = _.divide(num.clone(), _.parse('sqrt(pi)')); | ||
retval = _.parse(format('(2*({2})*sqrt({0}))/({1})', t, f.a, b, num)); | ||
} | ||
else if (den_p.equals(2) && f.x.power.equals(2)) { | ||
else if(den_p.equals(2) && f.x.power.equals(2)) { | ||
var a, d, exp; | ||
if (!num.contains(s)) { | ||
if(!num.contains(s)) { | ||
a = _.divide(num, new Symbol(2)); | ||
@@ -285,5 +304,5 @@ exp = '(({1})*sin((sqrt(({2})*({3}))*({0}))/({2})))/(({3})*sqrt(({2})*({3})))-(({1})*({0})*cos((sqrt(({2})*({3}))*({0}))/({2})))/(({2})*({3}))'; | ||
else { | ||
//decompose the numerator to check value of s | ||
// Decompose the numerator to check value of s | ||
f2 = core.Utils.decompose_fn(_.expand(num.clone()), s, true); | ||
if (f2.x.isComposite()) { | ||
if(f2.x.isComposite()) { | ||
var s_terms = []; | ||
@@ -305,6 +324,6 @@ //first collect the factors e.g. (a)(bx)(cx^2+d) | ||
a = new Symbol(-1); | ||
//grab only the ones which have s | ||
for (var i = 0; i < symbols.length; i++) { | ||
// Grab only the ones which have s | ||
for(var i = 0; i < symbols.length; i++) { | ||
var fc = symbols[i]; | ||
if (fc.x.value === s) | ||
if(fc.x.value === s) | ||
s_terms.push(fc); | ||
@@ -314,8 +333,8 @@ else | ||
} | ||
//the following 2 assumptions are made | ||
//1. since the numerator was factored above then each s_term has a unique power | ||
//2. because the terms are sorted by descending powers then the first item | ||
// has the highest power | ||
//we can now check for the next type s(s^2-a^2)/(s^2+a^2)^2 | ||
if (s_terms[0].x.power.equals(2) && s_terms[1].x.power.equals(1) && s_terms[1].b.equals(0) && !s_terms[0].b.equals(0)) { | ||
// The following 2 assumptions are made | ||
// 1. since the numerator was factored above then each s_term has a unique power | ||
// 2. because the terms are sorted by descending powers then the first item | ||
// has the highest power | ||
// We can now check for the next type s(s^2-a^2)/(s^2+a^2)^2 | ||
if(s_terms[0].x.power.equals(2) && s_terms[1].x.power.equals(1) && s_terms[1].b.equals(0) && !s_terms[0].b.equals(0)) { | ||
b = s_terms[0].a.negate(); | ||
@@ -329,3 +348,3 @@ exp = '-(({1})*({2})*({5})*({0})*sin((sqrt(({4})*({5}))*({0}))/({4})))/' + | ||
else { | ||
if (f2.x.isLinear()) { | ||
if(f2.x.isLinear()) { | ||
a = _.divide(f2.a, new Symbol(2)); | ||
@@ -335,4 +354,4 @@ exp = '(({1})*({0})*sin((sqrt(({2})*({3}))*({0}))/({2})))/(({2})*sqrt(({2})*({3})))'; | ||
} | ||
else if (f2.x.power.equals(2)) { | ||
if (f2.b.equals(0)) { | ||
else if(f2.x.power.equals(2)) { | ||
if(f2.b.equals(0)) { | ||
a = _.divide(f2.a, new Symbol(2)); | ||
@@ -355,12 +374,19 @@ exp = '(({1})*sin((sqrt(({2})*({3}))*({0}))/({2})))/(({2})*sqrt(({2})*({3})))+(({1})*({0})*cos((sqrt(({2})*({3}))*({0}))/({2})))/({2})^2'; | ||
else if(symbol.isComposite()) { | ||
retval = new Symbol(0); | ||
symbol.each(function(x) { | ||
retval = _.add(retval, __.LaPlace.inverse(x, s_, t)); | ||
}, true); | ||
// 1/(s+1)^2 | ||
if(den_p.equals(2) && f.x.group === S) { | ||
retval = _.parse(`(${m})*(${t})*e^(-(${f.b})*(${t}))`); | ||
} | ||
else { | ||
retval = new Symbol(0); | ||
symbol.each(function (x) { | ||
retval = _.add(retval, __.LaPlace.inverse(x, s_, t)); | ||
}, true); | ||
} | ||
} | ||
} | ||
if (!retval) | ||
if(!retval) { | ||
retval = _.symfunction('ilt', [input_symbol, s_, t]); | ||
} | ||
@@ -375,6 +401,6 @@ return retval; | ||
//get the frequency map | ||
for (var i = 0, l = arr.length; i < l; i++) { | ||
for(var i = 0, l = arr.length; i < l; i++) { | ||
var e = arr[i], | ||
key = e.toString(); | ||
if (!map[key]) //default it to zero | ||
if(!map[key]) //default it to zero | ||
map[key] = 0; | ||
@@ -387,3 +413,3 @@ map[key]++; //increment | ||
return arr.sort(function (a, b) { | ||
if (!a.isConstant() || !b.isConstant()) | ||
if(!a.isConstant() || !b.isConstant()) | ||
_.error('Unable to sort! All values must be numeric'); | ||
@@ -398,5 +424,5 @@ return a.multiplier.subtract(b.multiplier); | ||
var sum = new Symbol(0); | ||
for (var i = 0, l = arr.length; i < l; i++) { | ||
for(var i = 0, l = arr.length; i < l; i++) { | ||
var xi = arr[i].clone(); | ||
if (x_) { | ||
if(x_) { | ||
sum = _.add(_.pow(_.subtract(xi, x_.clone()), new Symbol(2)), sum); | ||
@@ -413,3 +439,3 @@ } | ||
//handle arrays | ||
if (isVector(args[0])) | ||
if(isVector(args[0])) | ||
return __.Statistics.mean.apply(this, args[0].elements); | ||
@@ -421,3 +447,3 @@ return _.divide(__.Statistics.sum(args), __.Statistics.count(args)); | ||
//handle arrays | ||
if (isVector(args[0])) | ||
if(isVector(args[0])) | ||
return __.Statistics.median.apply(this, args[0].elements); | ||
@@ -427,3 +453,3 @@ try { | ||
var l = args.length; | ||
if (core.Utils.even(l)) { | ||
if(core.Utils.even(l)) { | ||
var mid = l / 2; | ||
@@ -435,3 +461,3 @@ retval = __.Statistics.mean(sorted[mid - 1], sorted[mid]); | ||
} | ||
catch (e) { | ||
catch(e) { | ||
retval = _.symfunction('median', args); | ||
@@ -445,3 +471,3 @@ } | ||
//handle arrays | ||
if (isVector(args[0])) | ||
if(isVector(args[0])) | ||
return __.Statistics.mode.apply(this, args[0].elements); | ||
@@ -452,3 +478,3 @@ | ||
//the mode of 1 item is that item as per issue #310 (verified by Happypig375). | ||
if (core.Utils.keys(map).length === 1) | ||
if(core.Utils.keys(map).length === 1) | ||
retval = args[0]; | ||
@@ -458,6 +484,6 @@ else { | ||
var inverse = {}; | ||
for (var x in map) { | ||
for(var x in map) { | ||
var freq = map[x]; | ||
//check if it's in the inverse already | ||
if (!(freq in inverse)) | ||
if(!(freq in inverse)) | ||
inverse[freq] = x; | ||
@@ -467,3 +493,3 @@ else { | ||
//if it's already an array then just add it | ||
if (isArray(e)) | ||
if(isArray(e)) | ||
e.push(x); | ||
@@ -479,3 +505,3 @@ //convert it to and array | ||
//them to Symbol | ||
if (isArray(max)) { | ||
if(isArray(max)) { | ||
retval = _.symfunction('mode', max.sort()); | ||
@@ -497,3 +523,3 @@ } | ||
//handle arrays | ||
if (isVector(args[0])) | ||
if(isVector(args[0])) | ||
return __.Statistics.variance.apply(this, args[0].elements); | ||
@@ -506,3 +532,3 @@ var k = _.divide(new Symbol(1), __.Statistics.count(args)); | ||
//handle arrays | ||
if (isVector(args[0])) | ||
if(isVector(args[0])) | ||
return __.Statistics.sampleVariance.apply(this, args[0].elements); | ||
@@ -516,3 +542,3 @@ | ||
//handle arrays | ||
if (isVector(args[0])) | ||
if(isVector(args[0])) | ||
return __.Statistics.standardDeviation.apply(this, args[0].elements); | ||
@@ -524,3 +550,3 @@ return _.pow(__.Statistics.variance.apply(__.Statistics, args), new Symbol(1 / 2)); | ||
//handle arrays | ||
if (isVector(args[0])) | ||
if(isVector(args[0])) | ||
return __.Statistics.sampleStandardDeviation.apply(this, args[0].elements); | ||
@@ -629,2 +655,7 @@ return _.pow(__.Statistics.sampleVariance.apply(__.Statistics, args), new Symbol(1 / 2)); | ||
nerdamer.api(); | ||
}()); | ||
}()); | ||
// Added for all.min.js | ||
if((typeof module) !== 'undefined') { | ||
module.exports = nerdamer; | ||
}; |
145
index.d.ts
@@ -118,6 +118,6 @@ export as namespace nerdamer | ||
nerdamer.register({ | ||
name: 'myFunction', | ||
numargs: 2, | ||
visible: true, | ||
build: function(){ return f } | ||
name: 'myFunction', | ||
numargs: 2, | ||
visible: true, | ||
build: function(){ return f } | ||
}) | ||
@@ -128,6 +128,6 @@ | ||
nerdamer.register({ | ||
name: 'D', | ||
visible: true, | ||
numargs: [1, 3], | ||
build: function(){ return core.Calculus.diff } | ||
name: 'D', | ||
visible: true, | ||
numargs: [1, 3], | ||
build: function(){ return core.Calculus.diff } | ||
}) | ||
@@ -195,3 +195,3 @@ */ | ||
*/ | ||
buildFunction(args_array: string[]): (...args: number[]) => number | ||
buildFunction(args_array?: string[]): (...args: number[]) => number | ||
@@ -227,2 +227,7 @@ /** | ||
/** | ||
* Returns the value of the expression as a string or a number | ||
*/ | ||
valueOf(): string | number | ||
/** | ||
* Gets the list of reserved names. This is a list of names already in use by nerdamer excluding variable names. This is not a static list. | ||
@@ -241,2 +246,118 @@ * @param outputType Pass in the string 'decimals' to always get back numers as decimals. Pass in the string 'fractions' to always get back number as fractions. Defaults to decimals. | ||
solveFor(variable: string): Expression | ||
/** | ||
* Forces the expression to displayed with decimals | ||
*/ | ||
toDecimal(prec?: number): string | ||
/** | ||
* Checks to see if the expression's value equals a number. Compares the direct value returned. | ||
* The function will not check for all possible cases. To avoid this call evaluate. | ||
* @example | ||
* nerdamer('sqrt(5)').isNumber() | ||
* // false | ||
* nerdamer('sqrt(5)').evaluate().isNumber() | ||
* // true | ||
*/ | ||
isNumber(): boolean | ||
/** | ||
* Checks if a number evaluates to an imaginary number | ||
* @example | ||
* nerdamer('sqrt(-5)+8').isImaginary() | ||
* // true | ||
* nerdamer('sqrt(5)+8').isImaginary() | ||
* // false | ||
*/ | ||
isImaginary(): boolean | ||
/** | ||
* Adds a value to an expression | ||
* @example | ||
* nerdamer('x').add(3) | ||
*/ | ||
add(symbol: number | string | Expression): Expression | ||
/** | ||
* Subtracts a value from an expression | ||
* @example | ||
* nerdamer('x').subtract(3) | ||
*/ | ||
subtract(symbol: number | string | Expression): Expression | ||
/** | ||
* Multiplies an expression by a value | ||
* @example | ||
* nerdamer('x').multiply(3) | ||
*/ | ||
multiply(symbol: number | string | Expression): Expression | ||
/** | ||
* Divides an expression by a valule | ||
* @example | ||
* nerdamer('9*x').divide(3) | ||
*/ | ||
divide(symbol: number | string | Expression): Expression | ||
/** | ||
* Raises an expression to a power | ||
* @example | ||
* nerdamer('x').pow(3) | ||
*/ | ||
pow(symbol: number | string | Expression): Expression | ||
/** | ||
* Checks if two values are equal | ||
* @param value The value being tested | ||
* @example | ||
* nerdamer('sqrt(9)').eq(3) | ||
* // true | ||
* nerdamer('x').eq('y') | ||
* // false | ||
*/ | ||
eq(value: number | string | Expression): Expression | ||
/** | ||
* Checks if a value is less than another | ||
* @param value The value being tested | ||
* @example | ||
* nerdamer('sqrt(9)').lt(3) | ||
* // false | ||
* nerdamer('8').lt(100) | ||
* // true | ||
*/ | ||
lt(value: number | string | Expression): Expression | ||
/** | ||
* Checks if a value is less than or equal to another | ||
* @param value The value being tested | ||
* @example | ||
* nerdamer('sqrt(9)').lte(3) | ||
* // true | ||
* nerdamer('x').lte(100) | ||
* // false | ||
*/ | ||
lte(value: number | string | Expression): Expression | ||
/** | ||
* Checks if a value is greater than another | ||
* @param value The value being tested | ||
* @example | ||
* nerdamer('sqrt(9)').gt(3) | ||
* // false | ||
* nerdamer('800').gt(100) | ||
* // true | ||
*/ | ||
gt(value: number | string | Expression): Expression | ||
/** | ||
* Checks if a value is greater than or equal to another | ||
* @param value The value being tested | ||
* @example | ||
* nerdamer('sqrt(9)').gte(3) | ||
* // true | ||
* nerdamer('x').gte(100) | ||
* // false | ||
*/ | ||
gte(value: number | string | Expression): Expression | ||
} | ||
@@ -254,5 +375,5 @@ | ||
export function sum(expression: ExpressionParam, | ||
index: string, | ||
lower: ExpressionParam, | ||
upper: ExpressionParam): Expression | ||
index: string, | ||
lower: ExpressionParam, | ||
upper: ExpressionParam): Expression | ||
@@ -259,0 +380,0 @@ /** |
109
package.json
{ | ||
"author": { | ||
"name": "Martin Donk", | ||
"email": "martin.r.donk@gmail.com" | ||
}, | ||
"description": "javascript light-weight symbolic math expression evaluator", | ||
"name": "nerdamer", | ||
"license": "MIT", | ||
"version": "1.1.10", | ||
"homepage": "http://nerdamer.com/", | ||
"directory": { | ||
"lib": "./" | ||
}, | ||
"main": "nerdamer.core.js", | ||
"typings": "index.d.ts", | ||
"keywords": [ | ||
"math", | ||
"matrix", | ||
"complex", | ||
"number", | ||
"parser", | ||
"expression", | ||
"functions", | ||
"numeric", | ||
"equation solver", | ||
"linear algebra", | ||
"vectors", | ||
"symbolic", | ||
"integration", | ||
"differentiation", | ||
"derivative", | ||
"algebra", | ||
"calculus", | ||
"mathematics", | ||
"constants", | ||
"symbolic math" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/jiggzson/nerdamer.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/jiggzson/nerdamer/issues" | ||
}, | ||
"engines": { | ||
"node": ">=0.10.0" | ||
}, | ||
"scripts": { | ||
"test": "jasmine" | ||
}, | ||
"devDependencies": { | ||
"jasmine": "^2.99.0" | ||
} | ||
"author": { | ||
"name": "Martin Donk", | ||
"email": "martin.r.donk@gmail.com" | ||
}, | ||
"description": "javascript light-weight symbolic math expression evaluator", | ||
"name": "nerdamer", | ||
"license": "MIT", | ||
"version": "1.1.11", | ||
"homepage": "http://nerdamer.com/", | ||
"directory": { | ||
"lib": "./" | ||
}, | ||
"main": "nerdamer.core.js", | ||
"typings": "index.d.ts", | ||
"keywords": [ | ||
"math", | ||
"matrix", | ||
"complex", | ||
"number", | ||
"parser", | ||
"expression", | ||
"functions", | ||
"numeric", | ||
"equation solver", | ||
"linear algebra", | ||
"vectors", | ||
"symbolic", | ||
"integration", | ||
"differentiation", | ||
"derivative", | ||
"algebra", | ||
"calculus", | ||
"mathematics", | ||
"constants", | ||
"symbolic math" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/jiggzson/nerdamer.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/jiggzson/nerdamer/issues" | ||
}, | ||
"engines": { | ||
"node": ">=0.10.0" | ||
}, | ||
"scripts": { | ||
"test": "jasmine" | ||
}, | ||
"devDependencies": { | ||
"gulp": "^4.0.2", | ||
"gulp-cli": "^2.3.0", | ||
"gulp-concat": "^2.6.1", | ||
"gulp-uglify": "^3.0.2", | ||
"gulp-watch": "^5.0.1", | ||
"jasmine": "^2.99.0" | ||
} | ||
} |
@@ -701,2 +701,3 @@ /* global expect */ | ||
expect(nerdamer('coeffs(x+A+1,x)').toString()).toEqual('[1+A,1]'); | ||
expect(nerdamer.coeffs('2x+i*x+5', 'x').toString()).toEqual('[5,2+i]'); | ||
}); | ||
@@ -820,3 +821,3 @@ it('should calculate the line function', function () { | ||
it('should calculate nth roots correctly', function() { | ||
expect(nerdamer('roots((-1)^(1/5))').evaluate().text()).toEqual('[0.5877852522924731*i+0.809016994374947,-0.309016994374947+0.9510565162951536*i,-1+1e-16*i,-0.309016994374948-0.9510565162951536*i,-0.5877852522924734*i+0.809016994374947]'); | ||
expect(nerdamer('roots((-1)^(1/5))').evaluate().text()).toEqual('[0.5877852522924731*i+0.809016994374947,-0.309016994374947+0.9510565162951536*i,-1,-0.309016994374948-0.9510565162951536*i,-0.5877852522924734*i+0.809016994374947]'); | ||
expect(nerdamer('roots((2)^(1/3))').evaluate().text()).toEqual('[1.122462048309381,-1.122462048309381]'); | ||
@@ -823,0 +824,0 @@ }); |
@@ -11,687 +11,173 @@ /* global expect */ | ||
it('should differentiate correctly', function () { | ||
// given | ||
var testCases = [ | ||
{ | ||
given: 'diff(cos(x),x)', | ||
expected: '-sin(x)' | ||
}, | ||
{ | ||
given: 'diff(log(x),x)', | ||
expected: 'x^(-1)' | ||
}, | ||
{ | ||
given: 'diff(tan(x),x)', | ||
expected: 'sec(x)^2' | ||
}, | ||
{ | ||
given: 'diff(4*tan(x)*sec(x),x)', | ||
expected: '4*(sec(x)*tan(x)^2+sec(x)^3)' | ||
}, | ||
{ | ||
given: 'diff(sqrt(7),x)', | ||
expected: '0' | ||
}, | ||
{ | ||
given: 'diff(4,x)', | ||
expected: '0' | ||
}, | ||
{ | ||
given: 'diff(x^2,x)', | ||
expected: '2*x' | ||
}, | ||
{ | ||
given: 'diff(2*x^2+4,x)', | ||
expected: '4*x' | ||
}, | ||
{ | ||
given: 'diff(sqrt(x)*x,x)', | ||
expected: '(3/2)*x^(1/2)' | ||
}, | ||
{ | ||
given: 'diff(sqrt(x)-1/sqrt(x),x)', | ||
expected: '(1/2)*x^(-1/2)+(1/2)*x^(-3/2)' | ||
}, | ||
{ | ||
given: 'diff(x^2/3-3/x^2,x)', | ||
expected: '(2/3)*x+6*x^(-3)' | ||
}, | ||
{ | ||
given: 'diff(sqrt(x)*(x^2+1),x)', | ||
expected: '(1/2)*(1+x^2)*x^(-1/2)+2*x^(3/2)' | ||
}, | ||
{ | ||
given: 'diff(e^x/(e^x-1),x)', | ||
expected: '(-1+e^x)^(-1)*e^x-(-1+e^x)^(-2)*e^(2*x)' | ||
}, | ||
{ | ||
given: 'diff(e^x,x)', | ||
expected: 'e^x' | ||
}, | ||
{ | ||
given: 'diff(e^x/x,x)', | ||
expected: '-e^x*x^(-2)+e^x*x^(-1)' | ||
}, | ||
{ | ||
given: 'diff(tan(x)*log(1/cos(x)),x)', | ||
expected: '-(-cos(x)^(-1)*sin(x)*tan(x)+log(cos(x))*sec(x)^2)' | ||
}, | ||
{ | ||
given: 'diff((2*x)^(e),x)', | ||
expected: '2^e*e*x^(-1+e)' | ||
}, | ||
{ | ||
given: 'diff(2*cos(x)*log(x),x)', | ||
expected: '2*(-log(x)*sin(x)+cos(x)*x^(-1))' | ||
}, | ||
{ | ||
given: 'diff(cos(5*x)*log(sec(sqrt(cos(x^(4/5))^2))/y^2)*y,x)', | ||
expected: '(-4/5)*abs(cos(x^(4/5)))^(-1)*cos(x^(4/5))*sec(abs(cos(x^(4/5))))*sin(x^(4/5))*tan(abs(cos(x^(4/5))))*x^(-1/5)*y^(-2)*cos(5*x)' + | ||
'*sec(abs(cos(x^(4/5))))^(-1)*y^3-5*log(sec(abs(cos(x^(4/5))))*y^(-2))*sin(5*x)*y' | ||
}, | ||
{ | ||
given: 'diff(x*cos(x)^log(x),x)', | ||
expected: '(-cos(x)^(-1)*log(x)*sin(x)+log(cos(x))*x^(-1))*cos(x)^log(x)*x+cos(x)^log(x)' | ||
}, | ||
{ | ||
given: 'diff(cos(2*x),x)', | ||
expected: '-2*sin(2*x)' | ||
}, | ||
{ | ||
given: 'diff(cos(x)*tan(x),x)', | ||
expected: '-sin(x)*tan(x)+cos(x)*sec(x)^2' | ||
}, | ||
{ | ||
given: 'diff(sec(sqrt(cos(x^(4/5))^2)),x)', | ||
expected: '(-4/5)*abs(cos(x^(4/5)))^(-1)*cos(x^(4/5))*sec(abs(cos(x^(4/5))))*sin(x^(4/5))*tan(abs(cos(x^(4/5))))*x^(-1/5)' | ||
}, | ||
{ | ||
given: 'diff(log(log(log(cos(t*t)^z))),t)', | ||
expected: '-2*cos(t^2)^(-1)*sin(t^2)*t*z*log(cos(t^2))^(-1)*log(log(cos(t^2))*z)^(-1)*z^(-1)' | ||
}, | ||
{ | ||
given: 'diff(6*log(x)^(3*log(x^2)),x)', | ||
expected: '36*(log(log(x))*x^(-1)+x^(-1))*log(x)^(6*log(x))' | ||
}, | ||
{ | ||
given: 'diff(sinh(x^2)^cos(x),x)', | ||
expected: '(-log(sinh(x^2))*sin(x)+2*cos(x)*cosh(x^2)*sinh(x^2)^(-1)*x)*sinh(x^2)^cos(x)' | ||
}, | ||
{ | ||
given: 'diff(tan(x)*tanh(x),x)', | ||
expected: 'sec(x)^2*tanh(x)+sech(x)^2*tan(x)' | ||
}, | ||
{ | ||
given: 'diff(4*x*tan(x)*7*tanh(x),x)', | ||
expected: '28*(sec(x)^2*tanh(x)*x+sech(x)^2*tan(x)*x+tan(x)*tanh(x))' | ||
}, | ||
{ | ||
given: 'diff(y*tan(y)*7*tanh(y),x)', | ||
expected: '0' | ||
}, | ||
{ | ||
given: 'diff(yx*tan(y)*7*tanh(y),x)', | ||
expected: '0' | ||
}, | ||
{ | ||
given: 'diff(y,x)', | ||
expected: '0' | ||
}, | ||
{ | ||
given: 'diff(x*y,x)', | ||
expected: 'y' | ||
}, | ||
{ | ||
given: 'diff([sin(x), x^2, x],x)', | ||
expected: '[cos(x),2*x,1]' | ||
}, | ||
{ | ||
given: 'diff(sinc(a*x^3+b),x)', | ||
expected: '3*((a*x^3+b)*cos(a*x^3+b)-sin(a*x^3+b))*(a*x^3+b)^(-2)*a*x^2' | ||
} | ||
]; | ||
for (var i = 0; i < testCases.length; ++i) { | ||
// when | ||
var parsed = nerdamer(testCases[i].given); | ||
// then | ||
expect(parsed.toString()).toEqual(testCases[i].expected); | ||
} | ||
expect(nerdamer('diff(cos(x),x)').toString()).toEqual('-sin(x)'); | ||
expect(nerdamer('diff(log(x),x)').toString()).toEqual('x^(-1)'); | ||
expect(nerdamer('diff(tan(x),x)').toString()).toEqual('sec(x)^2'); | ||
expect(nerdamer('diff(4*tan(x)*sec(x),x)').toString()).toEqual('4*(sec(x)*tan(x)^2+sec(x)^3)'); | ||
expect(nerdamer('diff(sqrt(7),x)').toString()).toEqual('0'); | ||
expect(nerdamer('diff(4,x)').toString()).toEqual('0'); | ||
expect(nerdamer('diff(x^2,x)').toString()).toEqual('2*x'); | ||
expect(nerdamer('diff(2*x^2+4,x)').toString()).toEqual('4*x'); | ||
expect(nerdamer('diff(sqrt(x)*x,x)').toString()).toEqual('(3/2)*x^(1/2)'); | ||
expect(nerdamer('diff(sqrt(x)-1/sqrt(x),x)').toString()).toEqual('(1/2)*x^(-1/2)+(1/2)*x^(-3/2)'); | ||
expect(nerdamer('diff(x^2/3-3/x^2,x)').toString()).toEqual('(2/3)*x+6*x^(-3)'); | ||
expect(nerdamer('diff(sqrt(x)*(x^2+1),x)').toString()).toEqual('(1/2)*(1+x^2)*x^(-1/2)+2*x^(3/2)'); | ||
expect(nerdamer('diff(e^x/(e^x-1),x)').toString()).toEqual('(-1+e^x)^(-1)*e^x-(-1+e^x)^(-2)*e^(2*x)'); | ||
expect(nerdamer('diff(e^x,x)').toString()).toEqual('e^x'); | ||
expect(nerdamer('diff(e^x/x,x)').toString()).toEqual('-e^x*x^(-2)+e^x*x^(-1)'); | ||
expect(nerdamer('diff(tan(x)*log(1/cos(x)),x)').toString()).toEqual('-(-cos(x)^(-1)*sin(x)*tan(x)+log(cos(x))*sec(x)^2)'); | ||
expect(nerdamer('diff((2*x)^(e),x)').toString()).toEqual('2^e*e*x^(-1+e)'); | ||
expect(nerdamer('diff(2*cos(x)*log(x),x)').toString()).toEqual('2*(-log(x)*sin(x)+cos(x)*x^(-1))'); | ||
expect(nerdamer('diff(cos(5*x)*log(sec(sqrt(cos(x^(4/5))^2))/y^2)*y,x)').toString()).toEqual('(-4/5)*abs(cos(x^(4/5)))^(-1)*cos(x^(4/5))*sec(abs(cos(x^(4/5))))*sin(x^(4/5))*tan(abs(cos(x^(4/5))))*x^(-1/5)*y^(-2)*cos(5*x)*sec(abs(cos(x^(4/5))))^(-1)*y^3-5*log(sec(abs(cos(x^(4/5))))*y^(-2))*sin(5*x)*y'); | ||
expect(nerdamer('diff(x*cos(x)^log(x),x)').toString()).toEqual('(-cos(x)^(-1)*log(x)*sin(x)+log(cos(x))*x^(-1))*cos(x)^log(x)*x+cos(x)^log(x)'); | ||
expect(nerdamer('diff(cos(2*x),x)').toString()).toEqual('-2*sin(2*x)'); | ||
expect(nerdamer('diff(cos(x)*tan(x),x)').toString()).toEqual('-sin(x)*tan(x)+cos(x)*sec(x)^2'); | ||
expect(nerdamer('diff(sec(sqrt(cos(x^(4/5))^2)),x)').toString()).toEqual('(-4/5)*abs(cos(x^(4/5)))^(-1)*cos(x^(4/5))*sec(abs(cos(x^(4/5))))*sin(x^(4/5))*tan(abs(cos(x^(4/5))))*x^(-1/5)'); | ||
expect(nerdamer('diff(log(log(log(cos(t*t)^z))),t)').toString()).toEqual('-2*cos(t^2)^(-1)*sin(t^2)*t*z*log(cos(t^2))^(-1)*log(log(cos(t^2))*z)^(-1)*z^(-1)'); | ||
expect(nerdamer('diff(6*log(x)^(3*log(x^2)),x)').toString()).toEqual('36*(log(log(x))*x^(-1)+x^(-1))*log(x)^(6*log(x))'); | ||
expect(nerdamer('diff(sinh(x^2)^cos(x),x)').toString()).toEqual('(-log(sinh(x^2))*sin(x)+2*cos(x)*cosh(x^2)*sinh(x^2)^(-1)*x)*sinh(x^2)^cos(x)'); | ||
expect(nerdamer('diff(tan(x)*tanh(x),x)').toString()).toEqual('sec(x)^2*tanh(x)+sech(x)^2*tan(x)'); | ||
expect(nerdamer('diff(4*x*tan(x)*7*tanh(x),x)').toString()).toEqual('28*(sec(x)^2*tanh(x)*x+sech(x)^2*tan(x)*x+tan(x)*tanh(x))'); | ||
expect(nerdamer('diff(y*tan(y)*7*tanh(y),x)').toString()).toEqual('0'); | ||
expect(nerdamer('diff(yx*tan(y)*7*tanh(y),x)').toString()).toEqual('0'); | ||
expect(nerdamer('diff(y,x)').toString()).toEqual('0'); | ||
expect(nerdamer('diff(x*y,x)').toString()).toEqual('y'); | ||
expect(nerdamer('diff([sin(x), x^2, x],x)').toString()).toEqual('[cos(x),2*x,1]'); | ||
expect(nerdamer('diff(sinc(a*x^3+b),x)').toString()).toEqual('3*((a*x^3+b)*cos(a*x^3+b)-sin(a*x^3+b))*(a*x^3+b)^(-2)*a*x^2'); | ||
expect(nerdamer('diff(sqrt(e^x + a),x)').toString()).toEqual('(1/2)*(a+e^x)^(-1/2)*e^x'); | ||
}); | ||
it('should calculate sums correctly', function () { | ||
// given | ||
var testCases = [ | ||
{ | ||
given: 'sum(x+y, x, 0, 3)', | ||
expected: '4*y+6' | ||
}, | ||
{ | ||
given: 'sum(x^2+x, x, 0, 10)', | ||
expected: '440' | ||
}, | ||
{ | ||
given: 'sum(x^2*z^2+x*y-z+1, x, 0, 10)', | ||
expected: '-11*z+385*z^2+11+55*y' | ||
}, | ||
{ | ||
given: 'sum(x^2*z^2+x*y-z+1, z, 0, 10)', | ||
expected: '-44+11*x*y+385*x^2' | ||
}, | ||
{ | ||
given: 'sum(sqrt(x)*sin(x), x, 0, 10)', | ||
expected: '775334583/372372283' | ||
}, | ||
{ | ||
given: 'sum(e^(-x^2*π/9),x,1,100)', | ||
expected: '633863423979/633863423978' | ||
}, | ||
]; | ||
expect(nerdamer('sum(x+y, x, 0, 3)').evaluate().toString()).toEqual('4*y+6'); | ||
expect(nerdamer('sum(x^2+x, x, 0, 10)').evaluate().toString()).toEqual('440'); | ||
expect(nerdamer('sum(x^2*z^2+x*y-z+1, x, 0, 10)').evaluate().toString()).toEqual('-11*z+385*z^2+11+55*y'); | ||
expect(nerdamer('sum(x^2*z^2+x*y-z+1, z, 0, 10)').evaluate().toString()).toEqual('-44+11*x*y+385*x^2'); | ||
expect(nerdamer('sum(sqrt(x)*sin(x), x, 0, 10)').evaluate().toString()).toEqual('775334583/372372283'); | ||
expect(nerdamer('sum(e^(-x^2*π/9),x,1,100)').evaluate().toString()).toEqual('633863423979/633863423978'); | ||
}); | ||
for (var i = 0; i < testCases.length; ++i) { | ||
// when | ||
var parsed = nerdamer(testCases[i].given).evaluate(); | ||
// then | ||
expect(parsed.toString()).toEqual(testCases[i].expected); | ||
} | ||
}); | ||
it('should calculate the definite integral correctly', function () { | ||
//I don't really care to read warnings | ||
nerdamer.set('SILENCE_WARNINGS', true); | ||
// given | ||
var testCases = [ | ||
{ | ||
given: 'defint(cos(x),1,2,x)', | ||
expected: '0.067826442018' | ||
}, | ||
{ | ||
given: 'defint(cos(x)^3*x^2-1,-1,9)', | ||
expected: '8.543016466395' | ||
}, | ||
{ | ||
given: 'defint(cos(x^x),1,2,x)', | ||
expected: '-0.27113666621' | ||
}, | ||
{ | ||
given: 'defint(cos(x^log(sin(x))),2,3,x)', | ||
expected: '0.805604089074' | ||
}, | ||
{ | ||
given: 'defint(log(2*cos(x/2)),-π,π,x)', | ||
expected: '0' | ||
}, | ||
{ | ||
given: 'defint(log(cos(x/2)),-π,π,x)', | ||
expected: '-4.355172180607' | ||
}, | ||
{ | ||
given: 'defint(log(x+1), -1, 1, x)', | ||
expected: '-0.6137056388801095' | ||
}, | ||
{ | ||
given: 'defint(log(x), 0, 1, x)', | ||
expected: '-1' | ||
}, | ||
{ | ||
given: 'defint((x^2-3)/(-x^3+9x+1), 1, 3, x)', | ||
expected: '0.732408192445406585' | ||
}, | ||
{ | ||
given: 'defint(x*(x-5)^(1/2),5,8)', | ||
expected: '23.555890982936999348' | ||
} | ||
]; | ||
for (var i = 0; i < testCases.length; ++i) { | ||
// when | ||
var parsed = nerdamer(testCases[i].given, null, 'numer'); | ||
// then | ||
expect(round(parsed.text(), 14)).toEqual(round(testCases[i].expected), 14); | ||
} | ||
expect(round(nerdamer('defint(cos(x),1,2,x)').evaluate(), 14)).toEqual(round(0.067826442018, 14)); | ||
expect(round(nerdamer('defint(cos(x)^3*x^2-1,-1,9)').evaluate(), 14)).toEqual(round(8.543016466395, 14)); | ||
expect(round(nerdamer('defint(cos(x^x),1,2,x)').evaluate(), 14)).toEqual(round(-0.27113666621, 14)); | ||
expect(round(nerdamer('defint(cos(x^log(sin(x))),2,3,x)').evaluate(), 14)).toEqual(round(0.805604089074, 14)); | ||
expect(round(nerdamer('defint(log(2*cos(x/2)),-π,π,x)').evaluate(), 14)).toEqual(round(-0, 14)); | ||
expect(round(nerdamer('defint(log(cos(x/2)),-π,π,x)').evaluate(), 14)).toEqual(round(-4.355172180607, 14)); | ||
expect(round(nerdamer('defint(log(x+1), -1, 1, x)').evaluate(), 14)).toEqual(round(-0.6137056388801095, 14)); | ||
expect(round(nerdamer('defint(log(x), 0, 1, x)').evaluate(), 14)).toEqual(round(-1, 14)); | ||
expect(round(nerdamer('defint((x^2-3)/(-x^3+9x+1), 1, 3, x)').evaluate(), 14)).toEqual(round(0.732408192445406585, 14)); | ||
expect(round(nerdamer('defint(x*(x-5)^(1/2),5,8)').evaluate(), 14)).toEqual(round(23.555890982936999348, 14)); | ||
}); | ||
xit('should calculate limits correctly', function () { | ||
// given | ||
var testCases = [ | ||
{ | ||
given: 'limit((2-2*x^2)/(x-1), x, 1)', | ||
expected: '-4' | ||
}, | ||
{ | ||
given: 'limit(1/2*(x^2 - 1)/(x^2 + 1), x, 3)', | ||
expected: '2/5' | ||
}, | ||
{ | ||
given: 'limit(tan(3*x)/tan(x), x, pi/2)', | ||
expected: '1/3' | ||
}, | ||
{ | ||
given: 'limit(cos(sin(x)+2), x, Infinity)', | ||
expected: '[cos(1),cos(3)]' | ||
}, | ||
{ | ||
given: 'limit(x/(3*abs(4*x)),x, 0)', | ||
expected: '[-1/12,1/12]' | ||
}, | ||
{ | ||
given: 'limit((4x^2-x)/(3x^2+x),x,∞)', | ||
expected: '4/3' | ||
}, | ||
{ | ||
given: 'limit((x^(1/2)+x^(-1/2))/(x^(1/2)-x^(-1/2)),x,Infinity)', | ||
expected: '1' | ||
}, | ||
{ | ||
given: 'limit((2sin(x)-sin(2x))/(x-sin(x)),x,0)', | ||
expected: '6' | ||
}, | ||
{ | ||
given: 'limit((3*sin(x)-sin(2*x))/(x-sin(x)),x,0)', | ||
expected: 'Infinity' | ||
}, | ||
{ | ||
given: 'limit(x/(x+1)^2, x, -1)', | ||
expected: '-Infinity' | ||
}, | ||
{ | ||
given: 'limit((x+1)^(1+1/x)-x^(1+x),x, Infinity)', | ||
expected: '-Infinity' | ||
}, | ||
{ | ||
given: 'limit((2*x+log(x))/(x*log(x)),x,Infinity)', | ||
expected: '0' | ||
}, | ||
{ | ||
given: 'limit(log(x),x, 0)', | ||
expected: 'Infinity' | ||
}, | ||
{ | ||
given: 'limit(e^(-x)+2,x,Infinity)', | ||
expected: '2' | ||
} | ||
]; | ||
for (var i = 0; i < testCases.length; ++i) { | ||
// when | ||
var parsed = nerdamer(testCases[i].given); | ||
it('should calculate limits correctly', function () { | ||
expect(nerdamer('limit((2-2*x^2)/(x-1), x, 1)').toString()).toEqual('-4'); | ||
expect(nerdamer('limit(1/2*(x^2 - 1)/(x^2 + 1), x, 3)').toString()).toEqual('2/5'); | ||
expect(nerdamer('limit(tan(3*x)/tan(x), x, pi/2)').toString()).toEqual('1/3'); | ||
expect(nerdamer('limit(x/(3*abs(4*x)),x, 0)').toString()).toEqual('[-1/12,1/12]'); | ||
expect(nerdamer('limit((4x^2-x)/(3x^2+x),x,∞)').toString()).toEqual('4/3'); | ||
expect(nerdamer('limit((x^(1/2)+x^(-1/2))/(x^(1/2)-x^(-1/2)),x,Infinity)').toString()).toEqual('1'); | ||
expect(nerdamer('limit((2*x+log(x))/(x*log(x)),x,Infinity)').toString()).toEqual('0'); | ||
expect(nerdamer('limit(e^(-x)+2,x,Infinity)').toString()).toEqual('2'); | ||
expect(nerdamer('limit((x+1)^(1+1/x)-x^(1+x),x, Infinity)').toString()).toEqual('-Infinity'); | ||
expect(nerdamer('limit(x/(x+1)^2, x, -1)').toString()).toEqual('-Infinity'); | ||
// then | ||
expect(parsed.toString()).toEqual(testCases[i].expected); | ||
} | ||
// Revisit. | ||
/* | ||
expect(nerdamer('limit(cos(sin(x)+2), x, Infinity)').toString()).toEqual('[cos(1),cos(3)]'); | ||
expect(nerdamer('limit((2sin(x)-sin(2x))/(x-sin(x)),x,0)').toString()).toEqual('6'); | ||
expect(nerdamer('limit((3*sin(x)-sin(2*x))/(x-sin(x)),x,0)').toString()).toEqual('Infinity'); | ||
expect(nerdamer('limit(log(x),x, 0)').toString()).toEqual('Infinity'); | ||
*/ | ||
}); | ||
it('should integrate properly', function () { | ||
// given | ||
var testCases = [ | ||
{ | ||
given: 'integrate(sin(x), x)', | ||
expected: '-cos(x)' | ||
}, | ||
{ | ||
given: 'integrate((22/7)^x,x)', | ||
expected: '(log(1/7)+log(22))^(-1)*22^x*7^(-x)' | ||
}, | ||
{ | ||
given: 'integrate(cos(x), x)', | ||
expected: 'sin(x)' | ||
}, | ||
{ | ||
given: 'integrate(2*x^2+x, x)', | ||
expected: '(1/2)*x^2+(2/3)*x^3' | ||
}, | ||
{ | ||
given: 'integrate(log(x), x)', | ||
expected: '-x+log(x)*x' | ||
}, | ||
{ | ||
given: 'integrate(sqrt(x), x)', | ||
expected: '(2/3)*x^(3/2)' | ||
}, | ||
{ | ||
given: 'integrate(asin(a*x), x)', | ||
expected: 'a^(-1)*sqrt(-a^2*x^2+1)+asin(a*x)*x' | ||
}, | ||
{ | ||
given: 'integrate(a/x, x)', | ||
expected: 'a*log(x)' | ||
}, | ||
{ | ||
given: 'integrate(x*e^x, x)', | ||
expected: '-e^x+e^x*x' | ||
}, | ||
{ | ||
given: 'integrate(x^3*log(x), x)', | ||
expected: '(-1/16)*x^4+(1/4)*log(x)*x^4' | ||
}, | ||
{ | ||
given: 'integrate(x^2*sin(x), x)', | ||
expected: '-cos(x)*x^2+2*cos(x)+2*sin(x)*x' | ||
}, | ||
{ | ||
given: 'integrate(sin(x)*log(cos(x)), x)', | ||
expected: '-cos(x)*log(cos(x))+cos(x)' | ||
}, | ||
{ | ||
given: 'integrate(x*asin(x), x)', | ||
expected: '(-1/4)*asin(x)+(1/2)*asin(x)*x^2+(1/4)*cos(asin(x))*sin(asin(x))' | ||
}, | ||
{ | ||
given: 'integrate(q/((2-3*x^2)^(1/2)), x)', | ||
expected: 'asin(3*sqrt(6)^(-1)*x)*q*sqrt(3)^(-1)' | ||
}, | ||
{ | ||
given: 'integrate(1/(a^2+x^2), x)', | ||
expected: 'a^(-1)*atan(a^(-1)*x)' | ||
}, | ||
{ | ||
given: 'integrate(11/(a+5*r*x)^2,x)', | ||
expected: '(-11/5)*(5*r*x+a)^(-1)*r^(-1)' | ||
}, | ||
{ | ||
given: 'integrate(cos(x)*sin(x), x)', | ||
expected: '(-1/2)*cos(x)^2' | ||
}, | ||
{ | ||
given: 'integrate(x*cos(x)*sin(x), x)', | ||
expected: '(-1/2)*cos(x)^2*x+(1/4)*cos(x)*sin(x)+(1/4)*x' | ||
}, | ||
{ | ||
given: 'integrate(t/(a*x+b), x)', | ||
expected: 'a^(-1)*log(a*x+b)*t' | ||
}, | ||
{ | ||
given: 'integrate(x*(x+a)^3, x)', | ||
expected: '(1/2)*a^3*x^2+(1/5)*x^5+(3/4)*a*x^4+a^2*x^3' | ||
}, | ||
{ | ||
given: 'integrate(4*x/(x^2+a^2), x)', | ||
expected: '2*log(a^2+x^2)' | ||
}, | ||
{ | ||
given: 'integrate(1/(x^2+3*a^2), x)', | ||
expected: 'a^(-1)*atan(a^(-1)*sqrt(3)^(-1)*x)*sqrt(3)^(-1)' | ||
}, | ||
{ | ||
given: 'integrate(8*x^3/(6*x^2+3*a^2), x)', | ||
expected: '8*((-1/24)*a^2*log(2*x^2+a^2)+(1/12)*x^2)' | ||
}, | ||
{ | ||
given: 'integrate(10*q/(4*x^2+24*x+20), x)', | ||
expected: '10*((-1/16)*log(5+x)+(1/16)*log(1+x))*q' | ||
}, | ||
{ | ||
given: 'integrate(x/(x+a)^2, x)', | ||
expected: '(a+x)^(-1)*a+log(a+x)' | ||
}, | ||
{ | ||
given: 'integrate(sqrt(x-a), x)', | ||
expected: '(2/3)*(-a+x)^(3/2)' | ||
}, | ||
{ | ||
given: 'integrate(x^n*log(x), x)', | ||
expected: '(1+n)^(-1)*log(x)*x^(1+n)-(1+n)^(-2)*x^(1+n)' | ||
}, | ||
{ | ||
given: 'integrate(3*a*sec(x)^2, x)', | ||
expected: '3*a*tan(x)' | ||
}, | ||
{ | ||
given: 'integrate(a/(x^2+b*x+a*x+a*b),x)', //uglier for but still correct | ||
expected: '(((-a^(-1)*b+1)^(-1)*a^(-1)*b+1)*a^(-1)*log(b+x)-(-a^(-1)*b+1)^(-1)*a^(-1)*log(a+x))*a' | ||
}, | ||
{ | ||
given: 'integrate(log(a*x+b),x)', | ||
expected: '((a*x+b)*log(a*x+b)-a*x-b)*a' | ||
}, | ||
{ | ||
given: 'integrate(x*log(x),x)', | ||
expected: '(-1/4)*x^2+(1/2)*log(x)*x^2' | ||
}, | ||
{ | ||
given: 'integrate(log(a*x)/x,x)', | ||
expected: '(1/2)*log(a*x)^2' | ||
}, | ||
{ | ||
given: 'integrate(log(x)^2,x)', | ||
expected: '-2*log(x)*x+2*x+log(x)^2*x' | ||
}, | ||
{ | ||
given: 'integrate(t*log(x)^3,x)', | ||
expected: '(-3*log(x)^2*x-6*x+6*log(x)*x+log(x)^3*x)*t' | ||
}, | ||
{ | ||
given: 'integrate(e^x*sin(x),x)', | ||
expected: '(1/2)*(-cos(x)*e^x+e^x*sin(x))' | ||
}, | ||
{ | ||
given: 'integrate(e^x*sin(x),x)', | ||
expected: '(1/2)*(-cos(x)*e^x+e^x*sin(x))' | ||
}, | ||
{ | ||
given: 'integrate(e^(2*x)*sin(x),x)', | ||
expected: '(4/5)*((-1/4)*cos(x)*e^(2*x)+(1/2)*e^(2*x)*sin(x))' | ||
}, | ||
{ | ||
given: 'integrate(e^(2*x)*sin(x)*x,x)', | ||
expected: '(-3/25)*e^(2*x)*sin(x)+(4/25)*cos(x)*e^(2*x)+(4/5)*((-1/4)*cos(x)*e^(2*x)+(1/2)*e^(2*x)*sin(x))*x' | ||
}, | ||
{ | ||
given: 'integrate(x*log(x)^2,x)', | ||
expected: '(-1/2)*log(x)*x^2+(1/2)*log(x)^2*x^2+(1/4)*x^2' | ||
}, | ||
{ | ||
given: 'integrate(x^2*log(x)^2,x)', | ||
expected: '(-2/9)*log(x)*x^3+(1/3)*log(x)^2*x^3+(2/27)*x^3' | ||
}, | ||
{ | ||
given: 'integrate(x^2*e^(a*x),x)', | ||
expected: '-2*(-a^(-2)*e^(a*x)+a^(-1)*e^(a*x)*x)*a^(-1)+a^(-1)*e^(a*x)*x^2' | ||
}, | ||
{ | ||
given: 'integrate(8*e^(a*x^2),x)', | ||
expected: '4*erf(sqrt(-a)*x)*sqrt(-a)^(-1)*sqrt(pi)' | ||
}, | ||
{ | ||
given: 'integrate(5*x*e^(-8*a*x^2),x)', | ||
expected: '(-5/16)*a^(-1)*e^(-8*a*x^2)' | ||
}, | ||
{ | ||
given: 'integrate(x^2*sin(x),x)', | ||
expected: '-cos(x)*x^2+2*cos(x)+2*sin(x)*x' | ||
}, | ||
{ | ||
given: 'integrate(8*tan(b*x)^2,x)', | ||
expected: '8*(-x+b^(-1)*tan(b*x))' | ||
}, | ||
{ | ||
given: 'integrate(sec(a*x)^3,x)', | ||
expected: '(1/2)*a^(-1)*log(sec(a*x)+tan(a*x))+(1/2)*a^(-1)*sec(a*x)*tan(a*x)' | ||
}, | ||
{ | ||
given: 'integrate(sec(a*x)*tan(a*x),x)', | ||
expected: 'a^(-1)*sec(a*x)' | ||
}, | ||
{ | ||
given: 'integrate(3*a*cot(a*x)^4, x)', | ||
expected: '3*((-1/3)*a^(-1)*cot(a*x)^3+a^(-1)*cot(a*x)+x)*a' | ||
}, | ||
{ | ||
given: 'integrate(3*a*csc(a*x)^4, x)', | ||
expected: '3*((-1/3)*a^(-1)*cot(a*x)*csc(a*x)^2+(-2/3)*a^(-1)*cot(a*x))*a' | ||
}, | ||
{ | ||
given: 'integrate(1/8*a*2/(x^3+13*x^2+47*x+35),x)', | ||
expected: '(1/4)*((-1/8)*log(5+x)+(1/12)*log(7+x)+(1/24)*log(1+x))*a' | ||
}, | ||
{ | ||
given: 'integrate(a*2/(x^2+x),x)', | ||
expected: '2*(-log(1+x)+log(x))*a' | ||
}, | ||
{ | ||
given: 'integrate((x+7)/(x+1)^3,x)', | ||
expected: '(-1/2)*(1+x)^(-1)+(-7/2)*(1+x)^(-2)+(-1/2)*(1+x)^(-2)*x' | ||
}, | ||
{ | ||
given: 'integrate((3*x+2)/(x^2+x),x)', | ||
expected: '2*log(x)+log(1+x)' | ||
}, | ||
{ | ||
given: 'integrate([sin(x), x^2, x],x)', | ||
expected: '[-cos(x),(1/3)*x^3,(1/2)*x^2]' | ||
}, | ||
{ | ||
given: 'integrate(sinh(x),x)', | ||
expected: 'cosh(x)' | ||
}, | ||
{ | ||
given: 'integrate(cosh(x),x)', | ||
expected: 'sinh(x)' | ||
}, | ||
{ | ||
given: 'integrate(tanh(x),x)', | ||
expected: 'log(cosh(x))' | ||
}, | ||
{ | ||
given: 'integrate(sinh(x)*x,x)', | ||
expected: '-sinh(x)+cosh(x)*x' | ||
}, | ||
{ | ||
given: 'integrate((x^6+x^2-7)/(x^2+11), x)', | ||
expected: '(-11/3)*x^3+(1/5)*x^5+122*x-1349*atan(sqrt(11)^(-1)*x)*sqrt(11)^(-1)' | ||
}, | ||
{ | ||
given: 'integrate(x^6/(x^2+11), x)', | ||
expected: '(-11/3)*x^3+(1/5)*x^5+121*x-1331*atan(sqrt(11)^(-1)*x)*sqrt(11)^(-1)' | ||
}, | ||
{ | ||
given: 'integrate(x^2/(x^2+11))', | ||
expected: '-11*atan(sqrt(11)^(-1)*x)*sqrt(11)^(-1)+x' | ||
}, | ||
{ | ||
given: 'integrate(tan(x)*csc(x), x)', | ||
expected: 'log(sec(x)+tan(x))' | ||
}, | ||
{ | ||
given: 'integrate(sinh(x)*e^x, x)', | ||
expected: '(-1/2)*x+(1/4)*e^(2*x)' | ||
}, | ||
{ | ||
given: 'integrate(sinh(x)*cos(x), x)', | ||
expected: '(-1/4)*e^(-x)*sin(x)+(1/4)*cos(x)*e^(-x)+(1/4)*cos(x)*e^x+(1/4)*e^x*sin(x)' | ||
}, | ||
{ | ||
given: 'integrate(cos(x^2), x)', | ||
expected: 'integrate(cos(x^2),x)' | ||
}, | ||
{ | ||
given: 'integrate(sqrt(a-x^2)*x^2, x)', | ||
expected: '((-1/16)*cos(2*asin(sqrt(a)^(-1)*x))*sin(2*asin(sqrt(a)^(-1)*x))+(1/8)*asin(sqrt(a)^(-1)*x))*a^2' | ||
}, | ||
{ | ||
given: 'integrate((1-x^2)^(3/2), x)', | ||
expected: '(-3/16)*cos(2*asin(x))*sin(2*asin(x))+(-x^2+1)^(3/2)*x+(3/8)*asin(x)' | ||
}, | ||
{ | ||
given: 'integrate((1-x^2)^(3/2)*x^2, x)', | ||
expected: '(-1/32)*cos(2*asin(x))*sin(2*asin(x))+(-1/48)*cos(2*asin(x))^2*sin(2*asin(x))+(1/16)*asin(x)+(1/48)*sin(2*asin(x))' | ||
}, | ||
{ | ||
given: 'integrate(cos(x)^2*sin(x)^4, x)', | ||
expected: '(-1/32)*cos(2*x)*sin(2*x)+(-1/48)*sin(2*x)+(1/16)*x+(1/48)*cos(2*x)^2*sin(2*x)' | ||
}, | ||
{ | ||
given: 'integrate(log(a*x+1)/x^2, x)', | ||
expected: '(-log(1+a*x)+log(x))*a-log(1+a*x)*x^(-1)' | ||
}, | ||
{ | ||
given: 'integrate(x^2*(1-x^2)^(5/2), x)', | ||
expected: '(-1/128)*cos(2*asin(x))^3*sin(2*asin(x))+(-1/48)*cos(2*asin(x))^2*sin(2*asin(x))+(-3/256)*cos(2*asin(x))*sin(2*asin(x))+(1/48)*sin(2*asin(x))+(5/128)*asin(x)' | ||
}, | ||
{ | ||
given: 'integrate(1/tan(a*x)^n, x)', | ||
expected: 'integrate(tan(a*x)^(-n),x)' | ||
}, | ||
{ | ||
given: 'integrate(sin(x)^2*cos(x)*tan(x), x)', | ||
expected: '(-3/4)*cos(x)+(1/12)*cos(3*x)' | ||
}, | ||
{ | ||
given: 'integrate(cos(x)^2/sin(x),x)', | ||
expected: '-log(cot(x)+csc(x))+cos(x)' | ||
}, | ||
{ | ||
given: 'integrate(cos(x)/x,x)', | ||
expected: 'Ci(x)' | ||
}, | ||
{ | ||
given: 'integrate(sin(x)/x,x)', | ||
expected: 'Si(x)' | ||
}, | ||
{ | ||
given: 'integrate(log(x)^3/x,x)', | ||
expected: '(1/4)*log(x)^4' | ||
}, | ||
{ | ||
given: 'integrate(tan(x)^2*sec(x), x)', | ||
expected: '(-1/2)*log(sec(x)+tan(x))+(1/2)*sec(x)*tan(x)' | ||
}, | ||
{ | ||
given: 'integrate(tan(x)/cos(x),x)', | ||
expected: 'cos(x)^(-1)' | ||
}, | ||
{ | ||
given: 'integrate(sin(x)^3/x,x)', | ||
expected: '(-1/4)*Si(3*x)+(3/4)*Si(x)' | ||
}, | ||
{ | ||
given: 'integrate(tan(x)/sec(x)*sin(x)/tan(x),x)', | ||
expected: '(-1/2)*cos(x)^2' | ||
}, | ||
{ | ||
given: 'integrate(log(x)^n/x,x)', | ||
expected: '(1+n)^(-1)*log(x)^(1+n)' | ||
}, | ||
{ | ||
given: 'integrate(1/(x^2+9)^3,x)', | ||
expected: '(1/729)*((1/4)*cos(atan((1/3)*x))^3*sin(atan((1/3)*x))+(3/8)*atan((1/3)*x)+(3/8)*cos(atan((1/3)*x))*sin(atan((1/3)*x)))' | ||
}, | ||
{ | ||
given: 'integrate(1/(a*x^2+b)^3,x)', | ||
expected: '((1/4)*cos(atan(sqrt(a)^(-1)*sqrt(b)^(-1)*x))^3*sin(atan(sqrt(a)^(-1)*sqrt(b)^(-1)*x))+(3/8)*atan(sqrt(a)^(-1)*sqrt(b)^(-1)*x)+(3/8)*cos(atan(sqrt(a)^(-1)*sqrt(b)^(-1)*x))*sin(atan(sqrt(a)^(-1)*sqrt(b)^(-1)*x)))*b^(-3)' | ||
}, | ||
{ | ||
given: 'integrate(asin(x)/sqrt(2-2x^2),x)', | ||
expected: '(1/2)*asin(x)^2*sqrt(2)^(-1)' | ||
}, | ||
{ | ||
given: 'integrate(atan(x)/(2+2*x^2),x)', | ||
expected: '(1/4)*atan(x)^2' | ||
}, | ||
{ | ||
given: 'integrate(1/(sqrt(1-1/x^2)*x^2), x)', | ||
expected: 'asin(sqrt(-x^(-2)+1))' | ||
}, | ||
{ | ||
given: 'integrate(1/(sqrt(1-1/x^2)*x), x)', | ||
expected: '(-1/2)*log(1+sqrt(-x^(-2)+1))+(1/2)*log(-1+sqrt(-x^(-2)+1))' | ||
}, | ||
{ | ||
given: 'integrate(exp(2*log(x)),x)', | ||
expected: '(1/3)*x^3' | ||
} | ||
]; | ||
for (var i = 0; i < testCases.length; ++i) { | ||
// when | ||
var parsed = nerdamer(testCases[i].given); | ||
// then | ||
expect(parsed.toString()).toEqual(testCases[i].expected); | ||
} | ||
expect(nerdamer('integrate(sin(x), x)').toString()).toEqual('-cos(x)'); | ||
expect(nerdamer('integrate((22/7)^x,x)').toString()).toEqual('(log(1/7)+log(22))^(-1)*22^x*7^(-x)'); | ||
expect(nerdamer('integrate(cos(x), x)').toString()).toEqual('sin(x)'); | ||
expect(nerdamer('integrate(2*x^2+x, x)').toString()).toEqual('(1/2)*x^2+(2/3)*x^3'); | ||
expect(nerdamer('integrate(log(x), x)').toString()).toEqual('-x+log(x)*x'); | ||
expect(nerdamer('integrate(sqrt(x), x)').toString()).toEqual('(2/3)*x^(3/2)'); | ||
expect(nerdamer('integrate(asin(a*x), x)').toString()).toEqual('a^(-1)*sqrt(-a^2*x^2+1)+asin(a*x)*x'); | ||
expect(nerdamer('integrate(a/x, x)').toString()).toEqual('a*log(x)'); | ||
expect(nerdamer('integrate(x*e^x, x)').toString()).toEqual('-e^x+e^x*x'); | ||
expect(nerdamer('integrate(x^3*log(x), x)').toString()).toEqual('(-1/16)*x^4+(1/4)*log(x)*x^4'); | ||
expect(nerdamer('integrate(x^2*sin(x), x)').toString()).toEqual('-cos(x)*x^2+2*cos(x)+2*sin(x)*x'); | ||
expect(nerdamer('integrate(sin(x)*log(cos(x)), x)').toString()).toEqual('-cos(x)*log(cos(x))+cos(x)'); | ||
expect(nerdamer('integrate(x*asin(x), x)').toString()).toEqual('(-1/4)*asin(x)+(1/2)*asin(x)*x^2+(1/4)*cos(asin(x))*sin(asin(x))'); | ||
expect(nerdamer('integrate(q/((2-3*x^2)^(1/2)), x)').toString()).toEqual('asin(3*sqrt(6)^(-1)*x)*q*sqrt(3)^(-1)'); | ||
expect(nerdamer('integrate(1/(a^2+x^2), x)').toString()).toEqual('a^(-1)*atan(a^(-1)*x)'); | ||
expect(nerdamer('integrate(11/(a+5*r*x)^2,x)').toString()).toEqual('(-11/5)*(5*r*x+a)^(-1)*r^(-1)'); | ||
expect(nerdamer('integrate(cos(x)*sin(x), x)').toString()).toEqual('(-1/2)*cos(x)^2'); | ||
expect(nerdamer('integrate(x*cos(x)*sin(x), x)').toString()).toEqual('(-1/2)*cos(x)^2*x+(1/4)*cos(x)*sin(x)+(1/4)*x'); | ||
expect(nerdamer('integrate(t/(a*x+b), x)').toString()).toEqual('a^(-1)*log(a*x+b)*t'); | ||
expect(nerdamer('integrate(x*(x+a)^3, x)').toString()).toEqual('(1/2)*a^3*x^2+(1/5)*x^5+(3/4)*a*x^4+a^2*x^3'); | ||
expect(nerdamer('integrate(4*x/(x^2+a^2), x)').toString()).toEqual('2*log(a^2+x^2)'); | ||
expect(nerdamer('integrate(1/(x^2+3*a^2), x)').toString()).toEqual('a^(-1)*atan(a^(-1)*sqrt(3)^(-1)*x)*sqrt(3)^(-1)'); | ||
expect(nerdamer('integrate(8*x^3/(6*x^2+3*a^2), x)').toString()).toEqual('8*((-1/24)*a^2*log(2*x^2+a^2)+(1/12)*x^2)'); | ||
expect(nerdamer('integrate(10*q/(4*x^2+24*x+20), x)').toString()).toEqual('10*((-1/16)*log(5+x)+(1/16)*log(1+x))*q'); | ||
expect(nerdamer('integrate(x/(x+a)^2, x)').toString()).toEqual('(a+x)^(-1)*a+log(a+x)'); | ||
expect(nerdamer('integrate(sqrt(x-a), x)').toString()).toEqual('(2/3)*(-a+x)^(3/2)'); | ||
expect(nerdamer('integrate(x^n*log(x), x)').toString()).toEqual('(1+n)^(-1)*log(x)*x^(1+n)-(1+n)^(-2)*x^(1+n)'); | ||
expect(nerdamer('integrate(3*a*sec(x)^2, x)').toString()).toEqual('3*a*tan(x)'); | ||
expect(nerdamer('integrate(a/(x^2+b*x+a*x+a*b),x)').toString()).toEqual('(((-a^(-1)*b+1)^(-1)*a^(-1)*b+1)*a^(-1)*log(b+x)-(-a^(-1)*b+1)^(-1)*a^(-1)*log(a+x))*a'); | ||
expect(nerdamer('integrate(log(a*x+b),x)').toString()).toEqual('((a*x+b)*log(a*x+b)-a*x-b)*a'); | ||
expect(nerdamer('integrate(x*log(x),x)').toString()).toEqual('(-1/4)*x^2+(1/2)*log(x)*x^2'); | ||
expect(nerdamer('integrate(log(a*x)/x,x)').toString()).toEqual('(1/2)*log(a*x)^2'); | ||
expect(nerdamer('integrate(log(x)^2,x)').toString()).toEqual('-2*log(x)*x+2*x+log(x)^2*x'); | ||
expect(nerdamer('integrate(t*log(x)^3,x)').toString()).toEqual('(-3*log(x)^2*x-6*x+6*log(x)*x+log(x)^3*x)*t'); | ||
expect(nerdamer('integrate(e^x*sin(x),x)').toString()).toEqual('(1/2)*(-cos(x)*e^x+e^x*sin(x))'); | ||
expect(nerdamer('integrate(e^x*sin(x),x)').toString()).toEqual('(1/2)*(-cos(x)*e^x+e^x*sin(x))'); | ||
expect(nerdamer('integrate(e^(2*x)*sin(x),x)').toString()).toEqual('(4/5)*((-1/4)*cos(x)*e^(2*x)+(1/2)*e^(2*x)*sin(x))'); | ||
expect(nerdamer('integrate(e^(2*x)*sin(x)*x,x)').toString()).toEqual('(-3/25)*e^(2*x)*sin(x)+(4/25)*cos(x)*e^(2*x)+(4/5)*((-1/4)*cos(x)*e^(2*x)+(1/2)*e^(2*x)*sin(x))*x'); | ||
expect(nerdamer('integrate(x*log(x)^2,x)').toString()).toEqual('(-1/2)*log(x)*x^2+(1/2)*log(x)^2*x^2+(1/4)*x^2'); | ||
expect(nerdamer('integrate(x^2*log(x)^2,x)').toString()).toEqual('(-2/9)*log(x)*x^3+(1/3)*log(x)^2*x^3+(2/27)*x^3'); | ||
expect(nerdamer('integrate(x^2*e^(a*x),x)').toString()).toEqual('-2*(-a^(-2)*e^(a*x)+a^(-1)*e^(a*x)*x)*a^(-1)+a^(-1)*e^(a*x)*x^2'); | ||
expect(nerdamer('integrate(8*e^(a*x^2),x)').toString()).toEqual('4*erf(sqrt(-a)*x)*sqrt(-a)^(-1)*sqrt(pi)'); | ||
expect(nerdamer('integrate(5*x*e^(-8*a*x^2),x)').toString()).toEqual('(-5/16)*a^(-1)*e^(-8*a*x^2)'); | ||
expect(nerdamer('integrate(x^2*sin(x),x)').toString()).toEqual('-cos(x)*x^2+2*cos(x)+2*sin(x)*x'); | ||
expect(nerdamer('integrate(8*tan(b*x)^2,x)').toString()).toEqual('8*(-x+b^(-1)*tan(b*x))'); | ||
expect(nerdamer('integrate(sec(a*x)^3,x)').toString()).toEqual('(1/2)*a^(-1)*log(sec(a*x)+tan(a*x))+(1/2)*a^(-1)*sec(a*x)*tan(a*x)'); | ||
expect(nerdamer('integrate(sec(a*x)*tan(a*x),x)').toString()).toEqual('a^(-1)*sec(a*x)'); | ||
expect(nerdamer('integrate(3*a*cot(a*x)^4, x)').toString()).toEqual('3*((-1/3)*a^(-1)*cot(a*x)^3+a^(-1)*cot(a*x)+x)*a'); | ||
expect(nerdamer('integrate(3*a*csc(a*x)^4, x)').toString()).toEqual('3*((-1/3)*a^(-1)*cot(a*x)*csc(a*x)^2+(-2/3)*a^(-1)*cot(a*x))*a'); | ||
expect(nerdamer('integrate(1/8*a*2/(x^3+13*x^2+47*x+35),x)').toString()).toEqual('(1/4)*((-1/8)*log(5+x)+(1/12)*log(7+x)+(1/24)*log(1+x))*a'); | ||
expect(nerdamer('integrate(a*2/(x^2+x),x)').toString()).toEqual('2*(-log(1+x)+log(x))*a'); | ||
expect(nerdamer('integrate((x+7)/(x+1)^3,x)').toString()).toEqual('(-1/2)*(1+x)^(-1)+(-7/2)*(1+x)^(-2)+(-1/2)*(1+x)^(-2)*x'); | ||
expect(nerdamer('integrate((3*x+2)/(x^2+x),x)').toString()).toEqual('2*log(x)+log(1+x)'); | ||
expect(nerdamer('integrate([sin(x), x^2, x],x)').toString()).toEqual('[-cos(x),(1/3)*x^3,(1/2)*x^2]'); | ||
expect(nerdamer('integrate(sinh(x),x)').toString()).toEqual('cosh(x)'); | ||
expect(nerdamer('integrate(cosh(x),x)').toString()).toEqual('sinh(x)'); | ||
expect(nerdamer('integrate(tanh(x),x)').toString()).toEqual('log(cosh(x))'); | ||
expect(nerdamer('integrate(sinh(x)*x,x)').toString()).toEqual('-sinh(x)+cosh(x)*x'); | ||
expect(nerdamer('integrate((x^6+x^2-7)/(x^2+11), x)').toString()).toEqual('(-11/3)*x^3+(1/5)*x^5+122*x-1349*atan(sqrt(11)^(-1)*x)*sqrt(11)^(-1)'); | ||
expect(nerdamer('integrate(x^6/(x^2+11), x)').toString()).toEqual('(-11/3)*x^3+(1/5)*x^5+121*x-1331*atan(sqrt(11)^(-1)*x)*sqrt(11)^(-1)'); | ||
expect(nerdamer('integrate(x^2/(x^2+11))').toString()).toEqual('-11*atan(sqrt(11)^(-1)*x)*sqrt(11)^(-1)+x'); | ||
expect(nerdamer('integrate(tan(x)*csc(x), x)').toString()).toEqual('log(sec(x)+tan(x))'); | ||
expect(nerdamer('integrate(sinh(x)*e^x, x)').toString()).toEqual('(-1/2)*x+(1/4)*e^(2*x)'); | ||
expect(nerdamer('integrate(sinh(x)*cos(x), x)').toString()).toEqual('(-1/4)*e^(-x)*sin(x)+(1/4)*cos(x)*e^(-x)+(1/4)*cos(x)*e^x+(1/4)*e^x*sin(x)'); | ||
expect(nerdamer('integrate(cos(x^2), x)').toString()).toEqual('integrate(cos(x^2),x)'); | ||
expect(nerdamer('integrate(sqrt(a-x^2)*x^2, x)').toString()).toEqual('((-1/16)*cos(2*asin(sqrt(a)^(-1)*x))*sin(2*asin(sqrt(a)^(-1)*x))+(1/8)*asin(sqrt(a)^(-1)*x))*a^2'); | ||
expect(nerdamer('integrate((1-x^2)^(3/2), x)').toString()).toEqual('(-3/16)*cos(2*asin(x))*sin(2*asin(x))+(-x^2+1)^(3/2)*x+(3/8)*asin(x)'); | ||
expect(nerdamer('integrate((1-x^2)^(3/2)*x^2, x)').toString()).toEqual('(-1/32)*cos(2*asin(x))*sin(2*asin(x))+(-1/48)*cos(2*asin(x))^2*sin(2*asin(x))+(1/16)*asin(x)+(1/48)*sin(2*asin(x))'); | ||
expect(nerdamer('integrate(cos(x)^2*sin(x)^4, x)').toString()).toEqual('(-1/32)*cos(2*x)*sin(2*x)+(-1/48)*sin(2*x)+(1/16)*x+(1/48)*cos(2*x)^2*sin(2*x)'); | ||
expect(nerdamer('integrate(log(a*x+1)/x^2, x)').toString()).toEqual('(-log(1+a*x)+log(x))*a-log(1+a*x)*x^(-1)'); | ||
expect(nerdamer('integrate(x^2*(1-x^2)^(5/2), x)').toString()).toEqual('(-1/128)*cos(2*asin(x))^3*sin(2*asin(x))+(-1/48)*cos(2*asin(x))^2*sin(2*asin(x))+(-3/256)*cos(2*asin(x))*sin(2*asin(x))+(1/48)*sin(2*asin(x))+(5/128)*asin(x)'); | ||
expect(nerdamer('integrate(1/tan(a*x)^n, x)').toString()).toEqual('integrate(tan(a*x)^(-n),x)'); | ||
expect(nerdamer('integrate(sin(x)^2*cos(x)*tan(x), x)').toString()).toEqual('(-3/4)*cos(x)+(1/12)*cos(3*x)'); | ||
expect(nerdamer('integrate(cos(x)^2/sin(x),x)').toString()).toEqual('-log(cot(x)+csc(x))+cos(x)'); | ||
expect(nerdamer('integrate(cos(x)/x,x)').toString()).toEqual('Ci(x)'); | ||
expect(nerdamer('integrate(sin(x)/x,x)').toString()).toEqual('Si(x)'); | ||
expect(nerdamer('integrate(log(x)^3/x,x)').toString()).toEqual('(1/4)*log(x)^4'); | ||
expect(nerdamer('integrate(tan(x)^2*sec(x), x)').toString()).toEqual('(-1/2)*log(sec(x)+tan(x))+(1/2)*sec(x)*tan(x)'); | ||
expect(nerdamer('integrate(tan(x)/cos(x),x)').toString()).toEqual('cos(x)^(-1)'); | ||
expect(nerdamer('integrate(sin(x)^3/x,x)').toString()).toEqual('(-1/4)*Si(3*x)+(3/4)*Si(x)'); | ||
expect(nerdamer('integrate(tan(x)/sec(x)*sin(x)/tan(x),x)').toString()).toEqual('(-1/2)*cos(x)^2'); | ||
expect(nerdamer('integrate(log(x)^n/x,x)').toString()).toEqual('(1+n)^(-1)*log(x)^(1+n)'); | ||
expect(nerdamer('integrate(1/(x^2+9)^3,x)').toString()).toEqual('(1/729)*((1/4)*cos(atan((1/3)*x))^3*sin(atan((1/3)*x))+(3/8)*atan((1/3)*x)+(3/8)*cos(atan((1/3)*x))*sin(atan((1/3)*x)))'); | ||
expect(nerdamer('integrate(asin(x)/sqrt(2-2x^2),x)').toString()).toEqual('(1/2)*asin(x)^2*sqrt(2)^(-1)'); | ||
expect(nerdamer('integrate(atan(x)/(2+2*x^2),x)').toString()).toEqual('(1/4)*atan(x)^2'); | ||
expect(nerdamer('integrate(1/(sqrt(1-1/x^2)*x^2), x)').toString()).toEqual('asin(sqrt(-x^(-2)+1))'); | ||
expect(nerdamer('integrate(1/(sqrt(1-1/x^2)*x), x)').toString()).toEqual('(-1/2)*log(1+sqrt(-x^(-2)+1))+(1/2)*log(-1+sqrt(-x^(-2)+1))'); | ||
expect(nerdamer('integrate(exp(2*log(x)),x)').toString()).toEqual('(1/3)*x^3'); | ||
}); | ||
}); |
@@ -8,180 +8,48 @@ /* global expect */ | ||
describe('calculus', function () { | ||
describe('Extra Calculus', function () { | ||
it('should transform Laplace correctly', function () { | ||
// given | ||
var testCases = [ | ||
{ | ||
given: 'laplace(5, t, s)', | ||
expected: '5*s^(-1)' | ||
}, | ||
{ | ||
given: 'laplace(a*t, t, s)', | ||
expected: 'a*s^(-2)' | ||
}, | ||
{ | ||
given: 'laplace(cos(a*t), t, s)', | ||
expected: '(a^2+s^2)^(-1)*s' | ||
}, | ||
{ | ||
given: 'laplace(cos(x), t, s)', | ||
expected: 'cos(x)*s^(-1)' | ||
}, | ||
{ | ||
given: 'laplace(sinh(a*t), t, s)', | ||
expected: '(-a^2+s^2)^(-1)*a' | ||
}, | ||
{ | ||
given: 'laplace(a*t^2, t, s)', | ||
expected: '2*a*s^(-3)' | ||
}, | ||
{ | ||
given: 'laplace(2*sqrt(t), t, s)', | ||
expected: 's^(-3/2)*sqrt(pi)' | ||
}, | ||
{ | ||
given: 'laplace(x*e^(a*t), t, s)', | ||
expected: '(-a+s)^(-1)*x' | ||
}, | ||
{ | ||
given: 'laplace(x*(sin(a*t)-a*t*cos(a*t)), t, s)', | ||
expected: '((a^2+s^2)^(-1)*a-((1+a^2*s^(-2))^(-2)*s^(-2)-(1+a^2*s^(-2))^(-2)*a^2*s^(-4))*a)*x' | ||
}, | ||
{ | ||
given: 'laplace(sin(a*t), t, s)', | ||
expected: '(a^2+s^2)^(-1)*a' | ||
}, | ||
{ | ||
given: 'laplace(t*sin(a*t), t, s)', | ||
expected: '2*(1+a^2*s^(-2))^(-2)*a*s^(-3)' | ||
}, | ||
{ | ||
given: 'laplace(sin(a*t+b), t, s)', | ||
expected: '(1+a^2*s^(-2))^(-1)*a*cos(b)*s^(-2)+(1+a^2*s^(-2))^(-1)*s^(-1)*sin(b)' | ||
}, | ||
{ | ||
given: 'laplace(t^2*e^(a*t), t, s)', | ||
expected: '-2*(-s+a)^(-3)' | ||
}, | ||
{ | ||
given: 'laplace(6*t*e^(-9*t)*sin(6*t), t, s)', | ||
expected: '-72*(-9-s)^(-3)*(1+36*(-9-s)^(-2))^(-2)' | ||
}, | ||
{ | ||
//NOTE: this unit test was incorrect before. I don't know how this was missed. | ||
given: 'laplace(sinh(t)*e^t, t, s)', | ||
expected: '(-1/2)*(-s+2)^(-1)+(-1/2)*s^(-1)' | ||
} | ||
]; | ||
expect(nerdamer('laplace(5, t, s)').toString()).toEqual('5*s^(-1)'); | ||
expect(nerdamer('laplace(a*t, t, s)').toString()).toEqual('a*s^(-2)'); | ||
expect(nerdamer('laplace(cos(a*t), t, s)').toString()).toEqual('(a^2+s^2)^(-1)*s'); | ||
expect(nerdamer('laplace(cos(x), t, s)').toString()).toEqual('cos(x)*s^(-1)'); | ||
expect(nerdamer('laplace(sinh(a*t), t, s)').toString()).toEqual('(-a^2+s^2)^(-1)*a'); | ||
expect(nerdamer('laplace(a*t^2, t, s)').toString()).toEqual('2*a*s^(-3)'); | ||
expect(nerdamer('laplace(2*sqrt(t), t, s)').toString()).toEqual('s^(-3/2)*sqrt(pi)'); | ||
expect(nerdamer('laplace(x*e^(a*t), t, s)').toString()).toEqual('(-a+s)^(-1)*x'); | ||
expect(nerdamer('laplace(x*(sin(a*t)-a*t*cos(a*t)), t, s)').toString()).toEqual('((a^2+s^2)^(-1)*a-((1+a^2*s^(-2))^(-2)*s^(-2)-(1+a^2*s^(-2))^(-2)*a^2*s^(-4))*a)*x'); | ||
expect(nerdamer('laplace(sin(a*t), t, s)').toString()).toEqual('(a^2+s^2)^(-1)*a'); | ||
expect(nerdamer('laplace(t*sin(a*t), t, s)').toString()).toEqual('2*(1+a^2*s^(-2))^(-2)*a*s^(-3)'); | ||
expect(nerdamer('laplace(sin(a*t+b), t, s)').toString()).toEqual('(1+a^2*s^(-2))^(-1)*a*cos(b)*s^(-2)+(1+a^2*s^(-2))^(-1)*s^(-1)*sin(b)'); | ||
expect(nerdamer('laplace(t^2*e^(a*t), t, s)').toString()).toEqual('-2*(-s+a)^(-3)'); | ||
expect(nerdamer('laplace(6*t*e^(-9*t)*sin(6*t), t, s)').toString()).toEqual('-72*(-9-s)^(-3)*(1+36*(-9-s)^(-2))^(-2)'); | ||
expect(nerdamer('laplace(sinh(t)*e^t, t, s)').toString()).toEqual('(-1/2)*(-s+2)^(-1)+(-1/2)*s^(-1)'); | ||
}); | ||
for (var i = 0; i < testCases.length; ++i) { | ||
// when | ||
var parsed = nerdamer(testCases[i].given); | ||
// then | ||
expect(parsed.toString()).toEqual(testCases[i].expected); | ||
} | ||
it('should invert a Laplace transform correctly', function () { | ||
expect(nerdamer('ilt(a/(b*x), x, t)').toString()).toEqual('a*b^(-1)'); | ||
expect(nerdamer('ilt(a*6/(b*s^6),s,t)').toString()).toEqual('(1/20)*a*b^(-1)*t^5'); | ||
expect(nerdamer('ilt(3*s/(4*s^2+5),s,t)').toString()).toEqual('(3/4)*cos((1/2)*sqrt(5)*t)'); | ||
expect(nerdamer('ilt(2/(3*s^2+1),s,t)').toString()).toEqual('2*sin((1/3)*sqrt(3)*t)*sqrt(3)^(-1)'); | ||
expect(nerdamer('ilt(5*sqrt(pi)/(3*s^(3/2)),s,t)').toString()).toEqual('(10/3)*sqrt(t)'); | ||
expect(nerdamer('ilt(3/(7*s^2+1)^2, s, t)').toString()).toEqual('(-3/14)*cos((1/7)*sqrt(7)*t)*t+(3/2)*sin((1/7)*sqrt(7)*t)*sqrt(7)^(-1)'); | ||
expect(nerdamer('ilt(5*s/(s^2+4)^2, s, t)').toString()).toEqual('(5/4)*sin(2*t)*t'); | ||
expect(nerdamer('ilt(8*s^2/(2*s^2+3)^2, s, t)').toString()).toEqual('2*sin((1/2)*sqrt(6)*t)*sqrt(6)^(-1)+cos((1/2)*sqrt(6)*t)*t'); | ||
expect(nerdamer('ilt((6*s^2-1)/(4*s^2+1)^2, s, t)').toString()).toEqual('(1/8)*sin((1/2)*t)+(5/16)*cos((1/2)*t)*t'); | ||
expect(nerdamer('ilt((5*(sin(1)*s+3*cos(1)))/(s^2+9),s, t)').toString()).toEqual('5*cos(1)*sin(3*t)+5*cos(3*t)*sin(1)'); | ||
expect(nerdamer('ilt(((s+1)*(s+2)*(s+3))^(-1), s, t)').toString()).toEqual('(1/2)*e^(-3*t)+(1/2)*e^(-t)-e^(-2*t)'); | ||
expect(nerdamer('ilt(1/(s^2+s+1),s,t)').toString()).toEqual('2*e^((-1/2)*t)*sin((1/2)*sqrt(3)*t)*sqrt(3)^(-1)'); | ||
expect(nerdamer('ilt(1/(s^2+2s+1),s,t)').toString()).toEqual('e^(-t)*t'); | ||
}); | ||
it('should invert a Laplace transform correctly', function () { | ||
// given | ||
var testCases = [ | ||
{ | ||
given: 'ilt(a/(b*x), x, t)', | ||
expected: 'a*b^(-1)' | ||
}, | ||
{ | ||
given: 'ilt(a*6/(b*s^6),s,t)', | ||
expected: '(1/20)*a*b^(-1)*t^5' | ||
}, | ||
{ | ||
given: 'ilt(3*s/(4*s^2+5),s,t)', | ||
expected: '(3/4)*cos((1/2)*sqrt(5)*t)' | ||
}, | ||
{ | ||
given: 'ilt(2/(3*s^2+1),s,t)', | ||
expected: '2*sin((1/3)*sqrt(3)*t)*sqrt(3)^(-1)' | ||
}, | ||
{ | ||
given: 'ilt(5*sqrt(pi)/(3*s^(3/2)),s,t)', | ||
expected: '(10/3)*sqrt(t)' | ||
}, | ||
{ | ||
given: 'ilt(3/(7*s^2+1)^2, s, t)', | ||
expected: '(-3/14)*cos((1/7)*sqrt(7)*t)*t+(3/2)*sin((1/7)*sqrt(7)*t)*sqrt(7)^(-1)' | ||
}, | ||
{ | ||
given: 'ilt(5*s/(s^2+4)^2, s, t)', | ||
expected: '(5/4)*sin(2*t)*t' | ||
}, | ||
{ | ||
given: 'ilt(8*s^2/(2*s^2+3)^2, s, t)', | ||
expected: '2*sin((1/2)*sqrt(6)*t)*sqrt(6)^(-1)+cos((1/2)*sqrt(6)*t)*t' | ||
}, | ||
{ | ||
given: 'ilt((6*s^2-1)/(4*s^2+1)^2, s, t)', | ||
expected: '(1/8)*sin((1/2)*t)+(5/16)*cos((1/2)*t)*t' | ||
}, | ||
{ | ||
given: 'ilt((5*(sin(1)*s+3*cos(1)))/(s^2+9),s, t)', | ||
expected: '5*cos(1)*sin(3*t)+5*cos(3*t)*sin(1)' | ||
}, | ||
{ | ||
//TODO: Although this is computed correctly we need to get rid of the silly factorial(0)^(-1) | ||
given: 'ilt(((s+1)*(s+2)*(s+3))^(-1), s, t)', | ||
expected: '(1/2)*e^(-3*t)*factorial(0)^(-1)+(1/2)*e^(-t)*factorial(0)^(-1)-e^(-2*t)*factorial(0)^(-1)' | ||
} | ||
]; | ||
for (var i = 0; i < testCases.length; ++i) { | ||
// when | ||
var parsed = nerdamer(testCases[i].given); | ||
// then | ||
expect(parsed.toString()).toEqual(testCases[i].expected); | ||
} | ||
}); | ||
it('should calculate mode correctly', function () { | ||
// given | ||
var testCases = [ | ||
{ | ||
given: 'mode(r,r,r,r)', | ||
expected: 'r' | ||
}, | ||
{ | ||
given: 'mode(1,2)', | ||
expected: 'mode(1,2)' | ||
}, | ||
{ | ||
given: 'mode(1,2,3,1,2)', | ||
expected: 'mode(1,2)' | ||
}, | ||
{ | ||
given: 'mode(1,1,2)', | ||
expected: '1' | ||
}, | ||
{ | ||
given: 'mode(a,a,b,c,a,b,d)', | ||
expected: 'a' | ||
}, | ||
{ | ||
given: 'mode(x, r+1, 21, tan(x), r+1)', | ||
expected: '1+r' | ||
}, | ||
{ | ||
given: 'mode(x, r+1, 21, tan(x), r+1, x)', | ||
expected: 'mode(1+r,x)' | ||
} | ||
]; | ||
for (var i = 0; i < testCases.length; ++i) { | ||
// when | ||
var parsed = nerdamer(testCases[i].given); | ||
// then | ||
expect(parsed.toString()).toEqual(testCases[i].expected); | ||
} | ||
expect(nerdamer('mode(r,r,r,r)').toString()).toEqual('r'); | ||
expect(nerdamer('mode(1,2)').toString()).toEqual('mode(1,2)'); | ||
expect(nerdamer('mode(1,2,3,1,2)').toString()).toEqual('mode(1,2)'); | ||
expect(nerdamer('mode(1,1,2)').toString()).toEqual('1'); | ||
expect(nerdamer('mode(a,a,b,c,a,b,d)').toString()).toEqual('a'); | ||
expect(nerdamer('mode(x, r+1, 21, tan(x), r+1)').toString()).toEqual('1+r'); | ||
expect(nerdamer('mode(x, r+1, 21, tan(x), r+1, x)').toString()).toEqual('mode(1+r,x)'); | ||
}); | ||
}); | ||
@@ -187,2 +187,7 @@ /* global expect */ | ||
decimalTeX: '\\sqrt[b]{a}' | ||
}, | ||
{ | ||
given: 'x_aa_bb+y_cc_dd', | ||
TeX: 'x_{aa_{bb}}+y_{cc_{dd}}', | ||
decimalTeX: 'x_{aa_{bb}}+y_{cc_{dd}}' | ||
} | ||
@@ -265,46 +270,11 @@ ]; | ||
it('should explicitly convert to LaTeX', function () { | ||
// given | ||
var testCases = [ | ||
{ | ||
given: 'realpart(a)', | ||
expected: '\\operatorname{Re}\\left(a\\right)' | ||
}, | ||
{ | ||
given: 'imagpart(a)', | ||
expected: '\\operatorname{Im}\\left(a\\right)' | ||
}, | ||
{ | ||
given: 'diff(cos(x),x)', | ||
expected: '\\frac{d}{d x}\\left({\\mathrm{cos}\\left(x\\right)}\\right)' | ||
}, | ||
{ | ||
given: 'integrate(cos(x),x)', | ||
expected: '\\int {\\mathrm{cos}\\left(x\\right)}\\, dx' | ||
}, | ||
{ | ||
given: '2*(sqrt(3)+sqrt(2))', | ||
expected: '2 \\cdot \\left(\\sqrt{3} + \\sqrt{2}\\right)' | ||
}, | ||
// Redundant sign in powers | ||
{ | ||
given: '(a+1)(x+a)^(-5)+1', | ||
expected: '\\frac{a + 1}{{\\left(x + a\\right)}^{5}} + 1' | ||
}, | ||
{ | ||
given: 'a*x^-3+1/a', | ||
expected: '\\frac{a}{{x}^{3}} + \\frac{1}{a}' | ||
}, | ||
{ | ||
given: 'a*x^+3+1/a', | ||
expected: 'a \\cdot {x}^{3} + \\frac{1}{a}' | ||
} | ||
]; | ||
for(var i = 0; i < testCases.length; ++i) { | ||
// when | ||
var teX = nerdamer.convertToLaTeX(testCases[i].given); | ||
// then | ||
expect(teX).toEqual(testCases[i].expected); | ||
} | ||
expect(nerdamer.convertToLaTeX('realpart(a)')).toEqual('\\operatorname{Re}\\left(a\\right)'); | ||
expect(nerdamer.convertToLaTeX('imagpart(a)')).toEqual('\\operatorname{Im}\\left(a\\right)'); | ||
expect(nerdamer.convertToLaTeX('diff(cos(x),x)')).toEqual('\\frac{d}{d x}\\left({\\mathrm{cos}\\left(x\\right)}\\right)'); | ||
expect(nerdamer.convertToLaTeX('integrate(cos(x),x)')).toEqual('\\int {\\mathrm{cos}\\left(x\\right)}\\, dx'); | ||
expect(nerdamer.convertToLaTeX('2*(sqrt(3)+sqrt(2))')).toEqual('2 \\cdot \\left(\\sqrt{3} + \\sqrt{2}\\right)'); | ||
expect(nerdamer.convertToLaTeX('(a+1)(x+a)^(-5)+1')).toEqual('\\frac{a + 1}{{\\left(x + a\\right)}^{5}} + 1'); | ||
expect(nerdamer.convertToLaTeX('a*x^-3+1/a')).toEqual('\\frac{a}{{x}^{3}} + \\frac{1}{a}'); | ||
expect(nerdamer.convertToLaTeX('a*x^+3+1/a')).toEqual('a \\cdot {x}^{3} + \\frac{1}{a}'); | ||
expect(nerdamer.convertToLaTeX('x^2/y-x')).toEqual('\\frac{{x}^{2}}{y} - x'); | ||
}); | ||
@@ -311,0 +281,0 @@ |
@@ -10,304 +10,75 @@ /* global expect */ | ||
it('should solve correctly', function () { | ||
// given | ||
var testCases = [ | ||
{ | ||
given: 'solve(x=y/3416.3333333333344, y)', | ||
expected: '[(1073228064103962/314146179365)*x]' | ||
}, | ||
{ | ||
given: 'solve(x, x)', | ||
expected: '[0]' | ||
}, | ||
{ | ||
given: 'solve(5*y^x=8, x)', | ||
expected: '[log(8/5)*log(y)^(-1)]' | ||
}, | ||
{ | ||
given: 'solve(x^y+8=a*b, x)', | ||
expected: '[(-8+a*b)^y^(-1)]' | ||
}, | ||
{ | ||
given: 'solve(x^2, x)', | ||
expected: '[0]' | ||
}, | ||
{ | ||
given: 'solve(x^3, x)', | ||
expected: '[0]' | ||
}, | ||
{ | ||
given: 'solve(x+1, x)', | ||
expected: '[-1]' | ||
}, | ||
{ | ||
given: 'solve(x^2+1, x)', | ||
expected: '[i,-i]' | ||
}, | ||
{ | ||
given: 'solve(2*x^2+1, x)', | ||
expected: '[(1/2)*i*sqrt(2),(-1/2)*i*sqrt(2)]' | ||
}, | ||
{ | ||
given: 'solve(3*(x+5)*(x-4), x)', | ||
expected: '[-5,4]' | ||
}, | ||
{ | ||
given: 'solve(3*(x+a)*(x-b), x)', | ||
expected: '[-a,b]' | ||
}, | ||
{ | ||
given: 'solve(a*x^2+b, x)', | ||
expected: '[a^(-1)*i*sqrt(a)*sqrt(b),-a^(-1)*i*sqrt(a)*sqrt(b)]' | ||
}, | ||
{ | ||
given: 'solve(x^2+2*x+1, x)', | ||
expected: '[-1]' | ||
}, | ||
{ | ||
given: 'solve(-5 sqrt(14)x-14x^2 sqrt(83)-10=0,x)', | ||
expected: '[(-1/28)*sqrt(-560*sqrt(83)+350)*sqrt(83)^(-1)+(-5/28)*sqrt(14)*sqrt(83)^(-1),(-5/28)*sqrt(14)*sqrt(83)^(-1)+(1/28)*sqrt(-560*sqrt(83)+350)*sqrt(83)^(-1)]' | ||
}, | ||
{ | ||
given: 'solve(-5*sqrt(14)x-14x^2*sqrt(83)-10x=0,x)', | ||
expected: '[(-5/14)*(2+sqrt(14))*sqrt(83)^(-1),0]' | ||
}, | ||
{ | ||
given: 'solve(8*x^3-26x^2+3x+9,x)', | ||
expected: '[3/4,-1/2,3]' | ||
}, | ||
{ | ||
given: 'solve(a*x^2+b*x+c, x)', | ||
expected: '[(1/2)*(-b+sqrt(-4*a*c+b^2))*a^(-1),(1/2)*(-b-sqrt(-4*a*c+b^2))*a^(-1)]' | ||
}, | ||
{ | ||
//NOTE: this test has duplicates | ||
given: 'solve(sqrt(x^3)+sqrt(x^2)-sqrt(x)=0,x)', | ||
expected: '[0,78202389238903801/240831735646702201]' | ||
}, | ||
{ | ||
given: 'solve(x^3-10x^2+31x-30,x)', | ||
expected: '[3,5,2]' | ||
}, | ||
{ | ||
given: 'solve(sqrt(x)+sqrt(2x+1)=5,x)', | ||
expected: '[4]' | ||
}, | ||
{ | ||
given: 'solve(x=2/(3-x),x)', | ||
expected: '[1,2]' | ||
}, | ||
{ | ||
given: 'solve(1/x=a,x)', | ||
expected: '[a^(-1)]' | ||
}, | ||
{ | ||
given: 'solve(sqrt(x^2-1),x)', | ||
expected: '[1,-1]' | ||
}, | ||
{ | ||
given: 'solve(m*x^9+n,x)', | ||
expected: '[2*m^(-1/9)*n^(1/9),2*e^((2/9)*i*pi)*m^(-1/9)*n^(1/9),2*e^((4/9)*i*pi)*m^(-1/9)*n^(1/9),'+ | ||
'2*e^((2/3)*i*pi)*m^(-1/9)*n^(1/9),2*e^((8/9)*i*pi)*m^(-1/9)*n^(1/9),2*e^((10/9)*i*pi)*m^(-1/9)*n^(1/9),'+ | ||
'2*e^((4/3)*i*pi)*m^(-1/9)*n^(1/9),2*e^((14/9)*i*pi)*m^(-1/9)*n^(1/9),2*e^((16/9)*i*pi)*m^(-1/9)*n^(1/9)]' | ||
}, | ||
{ | ||
given: 'solve(sqrt(97)x^2-sqrt(13)x+sqrt(14)x+sqrt(43)x^2+sqrt(3)*sqrt(101)=0,x)', | ||
expected: '[(-1/2)*(sqrt(43)+sqrt(97))^(-1)*sqrt(14)+(1/2)*(sqrt(43)+sqrt(97))^(-1)*sqrt((-sqrt(13)+sqrt(14))^2-'+ | ||
'4*(sqrt(43)+sqrt(97))*sqrt(101)*sqrt(3))+(1/2)*(sqrt(43)+sqrt(97))^(-1)*sqrt(13),'+ | ||
'(-1/2)*(sqrt(43)+sqrt(97))^(-1)*sqrt((-sqrt(13)+sqrt(14))^2-4*(sqrt(43)+sqrt(97))*sqrt(101)*sqrt(3))+'+ | ||
'(-1/2)*(sqrt(43)+sqrt(97))^(-1)*sqrt(14)+(1/2)*(sqrt(43)+sqrt(97))^(-1)*sqrt(13)]' | ||
}, | ||
//The tests below were disabled. Too long and extremely difficult to verify. | ||
/* | ||
{ | ||
given: 'solve(cos(x), x)', | ||
expected: '[(-1/2)*pi,(-3/2)*pi,(-9/2)*pi,(1/2)*pi,(3/2)*pi,-1352180071/26085593,-1908340923/110444324,-2517548877/94277729,'+ | ||
'-411557987/20154304,-529064097/48116095,-571845701/72809656,-742972117/31532716,-988823039/7771658,1100909798/10157405,'+ | ||
'1186473006/3058025,2330164408/10230543,363357032/2541981,3842632193/22862576,486282493/4127694,486282493/6879490,'+ | ||
'529064097/48116095,571845701/72809656,694771162/3482717,700190513/17830205,742972117/31532716,8001566871/92617376,828535325/15070342,871316929/8533809]' | ||
}, | ||
{ | ||
given: 'solve(cos(x)*x+1-cos(x), x)', | ||
expected: '[-157611237/7736072,-2656223529/19001015,-299590117/27042575,-302136356/39039211,-308954356/17824291,-36091008/7390861,'+ | ||
'-414635924/29467903,-617639565/11230682,-88830662/83379401,0,1015553327/14364225,1055263167/12216227,'+ | ||
'1168204159/10777362,150965503/34192001,1887835580/8523467,193836127/24237444,2053282505/45052302,332594509/8463821,'+ | ||
'3498970568/8735403,392182006/35998715,406482779/5074820,412628401/2886817,467039565/19859218,651649517/11856939,738326717/6103909,961315151/5614415]' | ||
expect(nerdamer('solve(x=y/3416.3333333333344, y)').toString()).toEqual('[(1073228064103962/314146179365)*x]'); | ||
expect(nerdamer('solve(x, x)').toString()).toEqual('[0]'); | ||
expect(nerdamer('solve(5*y^x=8, x)').toString()).toEqual('[log(8/5)*log(y)^(-1)]'); | ||
expect(nerdamer('solve(x^y+8=a*b, x)').toString()).toEqual('[(-8+a*b)^y^(-1)]'); | ||
expect(nerdamer('solve(x^2, x)').toString()).toEqual('[0]'); | ||
expect(nerdamer('solve(x^3, x)').toString()).toEqual('[0]'); | ||
expect(nerdamer('solve(x+1, x)').toString()).toEqual('[-1]'); | ||
expect(nerdamer('solve(x^2+1, x)').toString()).toEqual('[i,-i]'); | ||
expect(nerdamer('solve(2*x^2+1, x)').toString()).toEqual('[(1/2)*i*sqrt(2),(-1/2)*i*sqrt(2)]'); | ||
expect(nerdamer('solve(3*(x+5)*(x-4), x)').toString()).toEqual('[-5,4]'); | ||
expect(nerdamer('solve(3*(x+a)*(x-b), x)').toString()).toEqual('[-a,b]'); | ||
expect(nerdamer('solve(a*x^2+b, x)').toString()).toEqual('[a^(-1)*i*sqrt(a)*sqrt(b),-a^(-1)*i*sqrt(a)*sqrt(b)]'); | ||
expect(nerdamer('solve(x^2+2*x+1, x)').toString()).toEqual('[-1]'); | ||
expect(nerdamer('solve(-5 sqrt(14)x-14x^2 sqrt(83)-10=0,x)').toString()).toEqual('[(-1/28)*sqrt(-560*sqrt(83)+350)*sqrt(83)^(-1)+(-5/28)*sqrt(14)*sqrt(83)^(-1),(-5/28)*sqrt(14)*sqrt(83)^(-1)+(1/28)*sqrt(-560*sqrt(83)+350)*sqrt(83)^(-1)]'); | ||
expect(nerdamer('solve(-5*sqrt(14)x-14x^2*sqrt(83)-10x=0,x)').toString()).toEqual('[(-5/14)*(2+sqrt(14))*sqrt(83)^(-1),0]'); | ||
expect(nerdamer('solve(8*x^3-26x^2+3x+9,x)').toString()).toEqual('[3/4,-1/2,3]'); | ||
expect(nerdamer('solve(a*x^2+b*x+c, x)').toString()).toEqual('[(1/2)*(-b+sqrt(-4*a*c+b^2))*a^(-1),(1/2)*(-b-sqrt(-4*a*c+b^2))*a^(-1)]'); | ||
expect(nerdamer('solve(sqrt(x^3)+sqrt(x^2)-sqrt(x)=0,x)').toString()).toEqual('[0,78202389238903801/240831735646702201]'); | ||
expect(nerdamer('solve(x^3-10x^2+31x-30,x)').toString()).toEqual('[3,5,2]'); | ||
expect(nerdamer('solve(sqrt(x)+sqrt(2x+1)=5,x)').toString()).toEqual('[4]'); | ||
expect(nerdamer('solve(x=2/(3-x),x)').toString()).toEqual('[1,2]'); | ||
expect(nerdamer('solve(1/x=a,x)').toString()).toEqual('[a^(-1)]'); | ||
expect(nerdamer('solve(sqrt(x^2-1),x)').toString()).toEqual('[1,-1]'); | ||
expect(nerdamer('solve(m*x^9+n,x)').toString()).toEqual('[2*m^(-1/9)*n^(1/9),2*e^((2/9)*i*pi)*m^(-1/9)*n^(1/9),2*e^((4/9)*i*pi)*m^(-1/9)*n^(1/9),2*e^((2/3)*i*pi)*m^(-1/9)*n^(1/9),2*e^((8/9)*i*pi)*m^(-1/9)*n^(1/9),2*e^((10/9)*i*pi)*m^(-1/9)*n^(1/9),2*e^((4/3)*i*pi)*m^(-1/9)*n^(1/9),2*e^((14/9)*i*pi)*m^(-1/9)*n^(1/9),2*e^((16/9)*i*pi)*m^(-1/9)*n^(1/9)]'); | ||
expect(nerdamer('solve(sqrt(97)x^2-sqrt(13)x+sqrt(14)x+sqrt(43)x^2+sqrt(3)*sqrt(101)=0,x)').toString()).toEqual('[(-1/2)*(sqrt(43)+sqrt(97))^(-1)*sqrt(14)+(1/2)*(sqrt(43)+sqrt(97))^(-1)*sqrt((-sqrt(13)+sqrt(14))^2-4*(sqrt(43)+sqrt(97))*sqrt(101)*sqrt(3))+(1/2)*(sqrt(43)+sqrt(97))^(-1)*sqrt(13),(-1/2)*(sqrt(43)+sqrt(97))^(-1)*sqrt((-sqrt(13)+sqrt(14))^2-4*(sqrt(43)+sqrt(97))*sqrt(101)*sqrt(3))+(-1/2)*(sqrt(43)+sqrt(97))^(-1)*sqrt(14)+(1/2)*(sqrt(43)+sqrt(97))^(-1)*sqrt(13)]'); | ||
expect(nerdamer('solve(a*y^2*x^3-1, x)').toString()).toEqual('[((-1/2)*abs(a^(-1)*y^(-2))+(1/2)*a^(-1)*y^(-2))^(1/3)+((1/2)*a^(-1)*y^(-2)+(1/2)*abs(a^(-1)*y^(-2)))^(1/3),(((-1/2)*abs(a^(-1)*y^(-2))+(1/2)*a^(-1)*y^(-2))^(1/3)+((1/2)*a^(-1)*y^(-2)+(1/2)*abs(a^(-1)*y^(-2)))^(1/3))*((1/2)*i*sqrt(3)+1/2),(((-1/2)*abs(a^(-1)*y^(-2))+(1/2)*a^(-1)*y^(-2))^(1/3)+((1/2)*a^(-1)*y^(-2)+(1/2)*abs(a^(-1)*y^(-2)))^(1/3))*((1/2)*i*sqrt(3)+1/2)^2]'); | ||
expect(nerdamer('solve((1/2)*sqrt(-4*x+4*y)-2+y, y)').toString()).toEqual('[(-1/2)*(-5+sqrt(-4*x+9)),(-1/2)*(-5-sqrt(-4*x+9))]'); | ||
expect(nerdamer('solve(log(a*x-c)-b=21, x)').toString()).toEqual('[-(-c-e^(21+b))*a^(-1)]'); | ||
expect(nerdamer('solve(x/(x-a)+4,x)').toString()).toEqual('[(4/5)*a]'); | ||
expect(nerdamer('solve(3*sin(a^2*x-b)-4,x)').toString()).toEqual('[a^(-2)*asin(4/3)]'); | ||
expect(nerdamer('solve(a*log(x^2-4)-4,x)').toString()).toEqual('[(1/2)*sqrt(16+4*e^(4*a^(-1))),(-1/2)*sqrt(16+4*e^(4*a^(-1)))]'); | ||
expect(nerdamer('solve(x/(x^2+2*x+1)+4,x)').toString()).toEqual('[(1/8)*sqrt(17)-9/8,(-1/8)*sqrt(17)-9/8]'); | ||
expect(nerdamer('solve((a*x^2+1),x)').toString()).toEqual('[a^(-1)*sqrt(-a),-a^(-1)*sqrt(-a)]'); | ||
expect(nerdamer('solve(sqrt(x)-2x+x^2,x)').toString()).toEqual('[(-1/2)*sqrt(5)+3/2,0,1,832040/2178309]'); | ||
expect(nerdamer('solve((2x+x^2)^2-x,x)').toString()).toEqual('[0,((-1/6)*sqrt(3)^(-1)*sqrt(59)+43/54)^(1/3)+((1/6)*sqrt(3)^(-1)*sqrt(59)+43/54)^(1/3)-4/3,(((-1/6)*sqrt(3)^(-1)*sqrt(59)+43/54)^(1/3)+((1/6)*sqrt(3)^(-1)*sqrt(59)+43/54)^(1/3)-4/3)*((1/2)*i*sqrt(3)+1/2),(((-1/6)*sqrt(3)^(-1)*sqrt(59)+43/54)^(1/3)+((1/6)*sqrt(3)^(-1)*sqrt(59)+43/54)^(1/3)-4/3)*((1/2)*i*sqrt(3)+1/2)^2]'); | ||
expect(nerdamer('solve((5*x^4-2)/(x+1)/(x^2-1),x)').toString()).toEqual('[72425485/91070226,-72425485/91070226,(316684236/398209345)*i,(-316684236/398209345)*i]'); | ||
expect(nerdamer('solve(0=(x^(2)-2)/(e^(x)-1), x)').toString()).toEqual('[sqrt(2),-sqrt(2)]'); | ||
expect(nerdamer('solve(4/y^2=x^2+1,y)').toString()).toEqual('[(1/2)*(-1-x^2)^(-1)*sqrt(16+16*x^2),(-1/2)*(-1-x^2)^(-1)*sqrt(16+16*x^2)]'); | ||
expect(nerdamer('solve(1/(x+x^2), x)').toString()).toEqual('[]'); | ||
expect(nerdamer('solve(1/(x+x^2-1), x)').toString()).toEqual('[]'); | ||
expect(nerdamer('solve(-1+11000*(-100*(10+x)^(-1)+20)^(-2)*(10+x)^(-2), x)').toString()).toEqual('[(-1/2)*sqrt(110)-5,(1/2)*sqrt(110)-5]'); | ||
expect(nerdamer('solve(x^3+y^3=3, x)').toString()).toEqual('[((-1/2)*y^3+3/2+abs((-1/2)*y^3+3/2))^(1/3)+((-1/2)*y^3-abs((-1/2)*y^3+3/2)+3/2)^(1/3),(((-1/2)*y^3+3/2+abs((-1/2)*y^3+3/2))^(1/3)+((-1/2)*y^3-abs((-1/2)*y^3+3/2)+3/2)^(1/3))*((1/2)*i*sqrt(3)+1/2),(((-1/2)*y^3+3/2+abs((-1/2)*y^3+3/2))^(1/3)+((-1/2)*y^3-abs((-1/2)*y^3+3/2)+3/2)^(1/3))*((1/2)*i*sqrt(3)+1/2)^2]'); | ||
expect(nerdamer('solve(sqrt(10x+186)=x+9,x)').toString()).toEqual('[7]'); | ||
expect(nerdamer('solve(x^3+8=x^2+6,x)').toString()).toEqual('[-1,1+i,-i+1]'); | ||
expect(nerdamer('solve(x^3-10x^2+31x-30,x)').toString()).toEqual('[3,5,2]'); | ||
expect(nerdamer('solve(8x^3-26x^2+3x+9,x)').toString()).toEqual('[3/4,-1/2,3]'); | ||
expect(nerdamer('solve(x^3-1/2x^2-13/2x-3,x)').toString()).toEqual('[-2,3,-1/2]'); | ||
expect(nerdamer('solve(x^3+2x^2+3x-4=0,x)').evaluate().text()).toEqual('[0.776045435028538,0.388022717514269+0.6720750612256601*i,-0.388022717514269+0.6720750612256599*i]'); | ||
expect(nerdamer('solve(x*log(x),x)').toString()).toEqual('[1]'); | ||
expect(nerdamer('solve((9x+x^2)^3+10800x+40x^4+4440x^2+720x^3+20(9*x+x^2)^2+8000,x) ').toString()).toEqual('[-5,-4]'); | ||
expect(nerdamer('solve((x^3-4)/(x^3+7x-11),x)').evaluate().text()).toEqual('[1.587401051968199,0.7937005259840910+1.3747296369986024*i,-0.793700525984099+1.374729636998602*i]'); | ||
expect(nerdamer('solve((93222358/131836323)*(-2*y+549964829/38888386)=10, y)').toString()).toEqual('[1/3625267041734188]'); | ||
expect(nerdamer('solve(sqrt(x)+sqrt(2x+1)=5,x) ').toString()).toEqual('[4]'); | ||
expect(nerdamer('solve(sqrt(x)-1,x) ').toString()).toEqual('[1]'); | ||
expect(nerdamer('solve(sqrt(x)+1,x)').toString()).toEqual('[]'); | ||
expect(nerdamer('solve((x-1)*(x+1)*x=3x,x)').toString()).toEqual('[0,2,-2]'); | ||
expect(nerdamer('solve(sqrt(x^2+1),x)').toString()).toEqual('[i,-i]'); | ||
expect(nerdamer('solve(sqrt(x^2-1),x)').toString()).toEqual('[1,-1]'); | ||
expect(nerdamer('solve(((x+1)*((x+1)+1))/2=n,x)').toString()).toEqual('[-3/2+sqrt(1/4+2*n),-3/2-sqrt(1/4+2*n)]'); | ||
expect(nerdamer('solve(sqrt(10x+186)=x+9,x)').toString()).toEqual('[7]'); | ||
expect(nerdamer('solve(x^3+8=x^2+6,x)').toString()).toEqual('[-1,1+i,-i+1]'); | ||
expect(nerdamer('solve(x^2=x^-2,x)').toString()).toEqual('[1,-1,i,-i]'); | ||
expect(nerdamer('solve((x+1)(x+1)x=3x,x)').toString()).toEqual('[0,-1+sqrt(3),-1-sqrt(3)]'); | ||
}, | ||
{ | ||
given: 'solve(a*x^3+b*x+c, x)', | ||
expected: '[(-1/3)*(27*a^2*c+sqrt(108*a^3*b^3+729*a^4*c^2))^(1/3)*2^(-1/3)*a^(-1)'+ | ||
'+(27*a^2*c+sqrt(108*a^3*b^3+729*a^4*c^2))^(-1/3)*2^(1/3)*b,(-1/2)*(-i*sqrt(3)+1)'+ | ||
'*(27*a^2*c+sqrt(108*a^3*b^3+729*a^4*c^2))^(-1/3)*2^(1/3)*a*b*a^(-1)+(1/6)*'+ | ||
'(1+i*sqrt(3))*(27*a^2*c+sqrt(108*a^3*b^3+729*a^4*c^2))^(1/3)*2^(-1/3)*a^(-1),'+ | ||
'(-1/2)*(1+i*sqrt(3))*(27*a^2*c+sqrt(108*a^3*b^3+729*a^4*c^2))^(-1/3)*2^(1/3)*'+ | ||
'a*b*a^(-1)+(1/6)*(-i*sqrt(3)+1)*(27*a^2*c+sqrt(108*a^3*b^3+729*a^4*c^2))^(1/3)*2^(-1/3)*a^(-1)]' | ||
}, | ||
*/ | ||
{ | ||
given: 'solve(a*y^2*x^3-1, x)', | ||
expected: '[((-1/2)*abs(a^(-1)*y^(-2))+(1/2)*a^(-1)*y^(-2))^(1/3)+((1/2)*a^(-1)*y^(-2)+(1/2)*abs(a^(-1)*y^(-2)))^(1/3),(((-1/2)*abs(a^(-1)*y^(-2))+(1/2)*a^(-1)*y^(-2))^(1/3)+((1/2)*a^(-1)*y^(-2)+(1/2)*abs(a^(-1)*y^(-2)))^(1/3))*((1/2)*i*sqrt(3)+1/2),(((-1/2)*abs(a^(-1)*y^(-2))+(1/2)*a^(-1)*y^(-2))^(1/3)+((1/2)*a^(-1)*y^(-2)+(1/2)*abs(a^(-1)*y^(-2)))^(1/3))*((1/2)*i*sqrt(3)+1/2)^2]' | ||
}, | ||
//The tests below are incorrect and have no solutions | ||
/* | ||
{ | ||
given: 'solve(log(x,2)+log(x,3)=log(x,5), x)', | ||
expected: '[1]' | ||
}, | ||
{ | ||
given: 'solve(log(x)-log(x,0.5)=log(x,-3), x)', | ||
expected: '[1]' | ||
}, | ||
*/ | ||
{ | ||
given: 'solve((1/2)*sqrt(-4*x+4*y)-2+y, y)', | ||
expected: '[(-1/2)*(-5+sqrt(-4*x+9)),(-1/2)*(-5-sqrt(-4*x+9))]' | ||
}, | ||
{ | ||
given: 'solve(log(a*x-c)-b=21, x)', | ||
expected: '[-(-c-e^(21+b))*a^(-1)]' | ||
}, | ||
{ | ||
given: 'solve(x/(x-a)+4,x)', | ||
expected: '[(4/5)*a]' | ||
}, | ||
{ | ||
given: 'solve(3*sin(a^2*x-b)-4,x)', | ||
expected: '[a^(-2)*asin(4/3)]' | ||
}, | ||
{ | ||
given: 'solve(a*log(x^2-4)-4,x)', | ||
expected: '[(1/2)*sqrt(16+4*e^(4*a^(-1))),(-1/2)*sqrt(16+4*e^(4*a^(-1)))]' | ||
}, | ||
{ | ||
given: 'solve(x/(x^2+2*x+1)+4,x)', | ||
expected: '[(1/8)*sqrt(17)-9/8,(-1/8)*sqrt(17)-9/8]' | ||
}, | ||
{ | ||
given: 'solve((a*x^2+1),x)', | ||
expected: '[a^(-1)*sqrt(-a),-a^(-1)*sqrt(-a)]' | ||
}, | ||
{ | ||
//NOTE: 4503599627370497/4503599627370496 result can be safely removed since it has rounding errors | ||
//NOTE: this test has duplicate solutions. The last two are duplicates of the first but have rounding errors | ||
given: 'solve(sqrt(x)-2x+x^2,x)', | ||
expected: '[(-1/2)*sqrt(5)+3/2,0,1,832040/2178309]' | ||
}, | ||
// { | ||
// given: 'solve((2x+x^2)^2-x,x)', | ||
// expected: '[0,1097^(1/3)*729^(-1/3)+4*(-1)^(1/3)*729^(-1/3)+4/3,((1/2)*i*sqrt(3)+1/2)*(1097^(1/3)*729^(-1/3)+4*(-1)^(1/3)*729^(-1/3)+4/3),((1/2)*i*sqrt(3)+1/2)^2*(1097^(1/3)*729^(-1/3)+4*(-1)^(1/3)*729^(-1/3)+4/3)]' | ||
// }, | ||
{ | ||
given: 'solve((5*x^4-2)/(x+1)/(x^2-1),x)', | ||
expected: '[72425485/91070226,-72425485/91070226,(316684236/398209345)*i,(-316684236/398209345)*i]' | ||
}, | ||
{ | ||
given: 'solve(0=(x^(2)-2)/(e^(x)-1), x)', | ||
expected: '[sqrt(2),-sqrt(2)]' | ||
}, | ||
{ | ||
given: 'solve(4/y^2=x^2+1,y)', | ||
expected: '[(1/2)*(-1-x^2)^(-1)*sqrt(16+16*x^2),(-1/2)*(-1-x^2)^(-1)*sqrt(16+16*x^2)]' | ||
}, | ||
{ | ||
given: 'solve(1/(x+x^2), x)', | ||
expected: '[]' | ||
}, | ||
{ | ||
given: 'solve(1/(x+x^2-1), x)', | ||
expected: '[]' | ||
}, | ||
{ | ||
given: 'solve(-1+11000*(-100*(10+x)^(-1)+20)^(-2)*(10+x)^(-2), x)', | ||
expected: '[(-1/2)*sqrt(110)-5,(1/2)*sqrt(110)-5]' | ||
}, | ||
{ | ||
given: 'solve(x^3+y^3=3, x)', | ||
expected: '[((-1/2)*y^3+3/2+abs((-1/2)*y^3+3/2))^(1/3)+((-1/2)*y^3-abs((-1/2)*y^3+3/2)+3/2)^(1/3),(((-1/2)*y^3+3/2+abs((-1/2)*y^3+3/2))^(1/3)+((-1/2)*y^3-abs((-1/2)*y^3+3/2)+3/2)^(1/3))*((1/2)*i*sqrt(3)+1/2),(((-1/2)*y^3+3/2+abs((-1/2)*y^3+3/2))^(1/3)+((-1/2)*y^3-abs((-1/2)*y^3+3/2)+3/2)^(1/3))*((1/2)*i*sqrt(3)+1/2)^2]' | ||
}, | ||
{ | ||
given: 'solve(sqrt(10x+186)=x+9,x)', | ||
expected: '[7]' | ||
}, | ||
{ | ||
given: 'solve(x^3+8=x^2+6,x)', | ||
expected: '[-1,1+i,-i+1]' | ||
}, | ||
{ | ||
given: 'solve(x^3-10x^2+31x-30,x)', | ||
expected: '[3,5,2]' | ||
}, | ||
{ | ||
given: 'solve(8x^3-26x^2+3x+9,x)', | ||
expected: '[3/4,-1/2,3]' | ||
}, | ||
{ | ||
given: 'solve(x^3-1/2x^2-13/2x-3,x)', | ||
expected: '[-2,3,-1/2]' | ||
}, | ||
// { | ||
// given: '', | ||
// expected: '' | ||
// }, | ||
// { | ||
// given: '', | ||
// expected: '' | ||
// }, | ||
]; | ||
for (var i = 0; i < testCases.length; ++i) { | ||
// when | ||
var parsed = nerdamer(testCases[i].given); | ||
// then | ||
expect(parsed.toString()).toEqual(testCases[i].expected); | ||
} | ||
}); | ||
it('should solve system of equations correctly', function () { | ||
// given | ||
var testCases = [ | ||
{ | ||
given: ['x+y=1', '2*x=6', '4*z+y=6'], | ||
expected: 'x,3,y,-2,z,2' | ||
}, | ||
{ | ||
given: ['x+y=a', 'x-y=b', 'z+y=c'], | ||
expected: 'x,0.5*a+0.5*b,y,-0.5*b+0.5*a,z,-0.5*a+0.5*b+c', | ||
variables: ['x', 'y', 'z'] | ||
}, | ||
{ | ||
given: ['x-2*y=-3', 'x+y-z+2*d=8', '5*d-1=19', 'z+d=7'], | ||
expected: 'd,4,x,1,y,2,z,3' | ||
}, | ||
{ | ||
given: 'x^2+4=x-y', | ||
expected: '(1/2)*(1+sqrt(-15-4*y)),(1/2)*(-sqrt(-15-4*y)+1)' | ||
}, | ||
//non-linear systems | ||
{ | ||
given: ['x+y=3','y^3-x=7'], | ||
expected: 'x,1,y,2' | ||
}, | ||
{ | ||
given: ['x^2+y=3','x+y+z=6', 'z^2-y=7'], | ||
expected: 'x,1,y,2,z,3' | ||
}, | ||
{ | ||
given: ['x*y-cos(z)=-3', '3*z^3-y^2+1=12', '3*sin(x)*cos(y)-x^3=-4'], | ||
expected: 'x,1.10523895006979,y,-2.98980336936266,z,1.88015428627437' | ||
} | ||
]; | ||
for (var i = 0; i < testCases.length; ++i) { | ||
// when | ||
var testCase = testCases[i], | ||
parsed; | ||
if (testCase.variables) | ||
parsed = nerdamer.solveEquations(testCase.given, testCase.variables); | ||
else | ||
parsed = nerdamer.solveEquations(testCase.given); | ||
// then | ||
expect(parsed.toString()).toEqual(testCases[i].expected); | ||
} | ||
expect(nerdamer.solveEquations(['x+y=1', '2*x=6', '4*z+y=6']).toString()).toEqual('x,3,y,-2,z,2'); | ||
expect(nerdamer.solveEquations(['x+y=a', 'x-y=b', 'z+y=c'], ['x', 'y', 'z']).toString()).toEqual('x,0.5*a+0.5*b,y,-0.5*b+0.5*a,z,-0.5*a+0.5*b+c'); | ||
expect(nerdamer.solveEquations(['x-2*y=-3', 'x+y-z+2*d=8', '5*d-1=19', 'z+d=7']).toString()).toEqual('d,4,x,1,y,2,z,3'); | ||
expect(nerdamer.solveEquations('x^2+4=x-y').toString()).toEqual('(1/2)*(1+sqrt(-15-4*y)),(1/2)*(-sqrt(-15-4*y)+1)'); | ||
expect(nerdamer.solveEquations(['x+y=3', 'y^3-x=7']).toString()).toEqual('x,1,y,2'); | ||
expect(nerdamer.solveEquations(['x^2+y=3', 'x+y+z=6', 'z^2-y=7']).toString()).toEqual('x,1,y,2,z,3'); | ||
expect(nerdamer.solveEquations(['x*y-cos(z)=-3', '3*z^3-y^2+1=12', '3*sin(x)*cos(y)-x^3=-4']).toString()).toEqual('x,1.10523895006979,y,-2.98980336936266,z,1.88015428627437'); | ||
}); | ||
@@ -319,3 +90,3 @@ /** #55: nerdamer.solveEquation quits working */ | ||
expect(nerdamer.solveEquations("x+1=2", "x").toString()).toEqual('1'); | ||
}); | ||
}); | ||
it('should parse equations correctly', function () { | ||
@@ -325,9 +96,9 @@ expect(nerdamer("-(a+1)=(a+3)^2").toString()).toEqual('-1-a=(3+a)^2'); | ||
//NOTE: contains duplicates | ||
it('should solve functions with factorials', function() { | ||
it('should solve functions with factorials', function () { | ||
expect(nerdamer('solve(x!-x^2,x)').text('decimals', 20)).toEqual('[-2.200391782610595,-4.010232827899529,-2.938361683501947,1,1.000000000000001,3.562382285390900,3.562382285390896,0.9999999999999910,1.000000000000000]'); | ||
}); | ||
it('should solve for variables other than x', function() { | ||
}); | ||
it('should solve for variables other than x', function () { | ||
expect(nerdamer('solve(2*a^(2)+4*a*6=128, a)').toString()).toEqual('[4,-16]'); | ||
}); | ||
it('should solve nonlinear system of equations with multiple parameter functions', function() { | ||
it('should solve nonlinear system of equations with multiple parameter functions', function () { | ||
var ans = nerdamer.solveEquations([ | ||
@@ -342,5 +113,12 @@ `y=x * 2`, | ||
xit('should solve factors', function() { | ||
it('should solve factors', function () { | ||
expect(nerdamer('solve((x-1)*(-a*c-a*x+c*x+x^2),x)').text()).toEqual('[1,-c,a]'); | ||
}); | ||
it('should solve circle equations', function() { | ||
var eq1 ="x^2+y^2=1"; | ||
var eq2 ="x+y=1"; | ||
var sol = nerdamer.solveEquations([eq1, eq2]); | ||
expect(sol.toString()).toEqual('x,1,0,y,0,1'); | ||
}); | ||
}); |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Uses eval
Supply chain riskPackage uses eval() which is a dangerous function. This prevents the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
1800310
29
6
26303
1
2
1