mirror of
https://github.com/josdejong/mathjs.git
synced 2026-01-25 15:07:57 +00:00
Support pow() as an argument for derivative() (#1417)
* Support pow() as an argument for derivative() Add support for the pow() function as an argument for math.derivative(). Fixes #1259. * Unit tests for derivatives of pow() * Support for derivate of pow() function
This commit is contained in:
parent
0905122757
commit
9bfdff1d06
@ -300,6 +300,10 @@ function factory (type, config, load, typed) {
|
||||
]), constNodes)
|
||||
}
|
||||
break
|
||||
case 'pow':
|
||||
constNodes[arg1] = constNodes[node.args[1]]
|
||||
// Pass to pow operator node parser
|
||||
return _derivative(new OperatorNode('^', 'pow', [arg0, node.args[1]]), constNodes)
|
||||
case 'exp':
|
||||
// d/dx(e^x) = e^x
|
||||
funcDerivative = new FunctionNode('exp', [arg0.clone()])
|
||||
@ -746,7 +750,7 @@ function factory (type, config, load, typed) {
|
||||
*/
|
||||
function funcArgsCheck (node) {
|
||||
// TODO add min, max etc
|
||||
if ((node.name === 'log' || node.name === 'nthRoot') && node.args.length === 2) {
|
||||
if ((node.name === 'log' || node.name === 'nthRoot' || node.name === 'pow') && node.args.length === 2) {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@ -160,6 +160,16 @@ describe('derivative', function () {
|
||||
compareString(derivativeWithoutSimplify('acoth((2x))', 'x'), '-(2 * 1) / (1 - (2 x) ^ 2)')
|
||||
compareString(derivativeWithoutSimplify('abs(2x)', 'x'), '2 * 1 * abs(2 x) / (2 x)')
|
||||
|
||||
// See power operator tests above
|
||||
compareString(derivativeWithoutSimplify('pow(0, 2^x + x^3 + 2)', 'x'), '0')
|
||||
compareString(derivativeWithoutSimplify('pow(1, 2^x + x^3 + 2)', 'x'), '0')
|
||||
compareString(derivativeWithoutSimplify('pow(10, 2x + 2)', 'x'), '10 ^ (2 x + 2) * log(10) * (2 * 1 + 0)')
|
||||
compareString(derivativeWithoutSimplify('pow(x^x^x^x, 0)', 'x'), '0')
|
||||
compareString(derivativeWithoutSimplify('pow(x + 2, 1)', 'x'), '1 + 0')
|
||||
compareString(derivativeWithoutSimplify('2 * pow(x, 2)', 'x'), '2 * 2 * 1 * x ^ (2 - 1)')
|
||||
compareString(derivativeWithoutSimplify('2 * pow(x, -2)', 'x'), '2 * -2 * 1 * x ^ (-2 - 1)')
|
||||
compareString(derivativeWithoutSimplify('pow(x^3 + x, 5x + 2)', 'x'), '(x ^ 3 + x) ^ (5 x + 2) * ((3 * 1 * x ^ (3 - 1) + 1) * (5 x + 2) / (x ^ 3 + x) + (5 * 1 + 0) * log(x ^ 3 + x))')
|
||||
|
||||
compareString(derivativeWithoutSimplify('exp(2x)', 'x'), '2 * 1 * exp(2 x)')
|
||||
})
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user