New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

math.derivative() throws unreasonable TypeError #1063

Closed
luiscarbonell opened this Issue Mar 15, 2018 · 3 comments

Comments

Projects
None yet
3 participants
@luiscarbonell

luiscarbonell commented Mar 15, 2018

OS: Ubuntu 16.04
Node.js Version: v8.10.0

Node.js Code:

let math = require('mathjs')

math.derivative("t0 + (t1 - t0) / (1 + e^(-t2 * (x - t3)))^(1 / t4)","t2") // TypeError

// OR

math.derivative("t0 + (t1 - t0) / (1 + e^(-t2 * (x - t3)))^(1 / t4)","t3") // TypeError

// OR

math.derivative("t0 + (t1 - t0) / (1 + e^(-t2 * (x - t3)))^(1 / t4)","x") // TypeError

Error thrown:

TypeError: Unexpected type of argument in function _derivative (expected: OperatorNode or ConstantNode or SymbolNode or ParenthesisNode or FunctionNode or FunctionAssignmentNode, actual: undefined, index: 0)
    at createError ([path_to_project]/node_modules/typed-function/typed-function.js:555:19)
    at generic ([path_to_project]/node_modules/typed-function/typed-function.js:1077:15)
    at _derivative ([path_to_project]/node_modules/typed-function/typed-function.js:1092:24)
    at OperatorNode, Object ([path_to_project]/node_modules/mathjs/lib/function/algebra/derivative.js:582:54)
    at _derivative ([path_to_project]/node_modules/typed-function/typed-function.js:1085:85)
    at OperatorNode, Object ([path_to_project]/node_modules/mathjs/lib/function/algebra/derivative.js:669:15)
    at _derivative ([path_to_project]/node_modules/typed-function/typed-function.js:1085:85)

===

Note that all other terms in the typed function are accurately derived and do not throw any errors.

let math = require('mathjs')

math.derivative("t0 + (t1 - t0) / (1 + e^(-t2 * (x - t3)))^(1 / t4)","t1") // OK

// AND

math.derivative("t0 + (t1 - t0) / (1 + e^(-t2 * (x - t3)))^(1 / t4)","t4") // OK
@javazen

This comment has been minimized.

javazen commented Mar 17, 2018

I have what looks like a similar issue. In 4.0.0, when I call:
math.derivative("1 - (-B)","B"); // with or without {simplify:false}

This error is thrown:
Uncaught TypeError: Unexpected type of argument in function _derivative (expected: OperatorNode or ConstantNode or SymbolNode or ParenthesisNode or FunctionNode or FunctionAssignmentNode, actual: undefined, index: 0)

When stepping thru, line 47033 (replace deriv of whole expr with subtraction of the derivs of the terms) seemed to work, and it also seems to handle the paren, calling for the deriv of the content.

But then at line 47028, when it tries to replace the deriv of the unary operator with minus the deriv of the arg (B) it does not work. Inside the "generic dispatcher" at line 26035, iStart and iEnd are each 6 and there are only tests 0-5, so it can't dispatch anywhere and it throws the error.

@josdejong josdejong added the bug label Mar 24, 2018

@josdejong

This comment has been minimized.

Owner

josdejong commented Mar 24, 2018

Thanks for the many examples, especially the very short one @javazen , that makes it easier to pinpoint the issue easier.

The code at line 26035 going to the generic function is not the cause of the issue, this is simply typed-function which tries to find a matching signature for a function call in two stages, first a fast one (0-5) and then an generic one (remaining signatures, empty if you have 6 or less signatures), and if no match is found it throws an error.

I think we have to look into the _derivative function calls that are in the stack trace, see whether we call a function the wrong way or with an uninitialized parameter or something

@josdejong josdejong closed this in 229ff3d Apr 11, 2018

@josdejong

This comment has been minimized.

Owner

josdejong commented Apr 11, 2018

I found an even simpler expression to replicate the issue: math.derivative('-x', 'x'), and have fixed it in 229ff3d.

Should be fixed now in v4.1.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment