Skip to content

bug: derivative of Abs is weird and not evaluate-able #200

Open
@dragoncoder047

Description

@dragoncoder047

Description

I need to find the derivative of an arbitrary expression for a calculator project I'm working on using this library. When I give it ln(|x|) it borks and gives a strange expression involving Piecewise and unsubstituted matches (_1), instead of the expected 1/(|x|). This causes the derivative to fail to compile to javascript and fail to evaluate.

Steps to Reproduce

import {
  ComputeEngine,
  version
} from "https://unpkg.com/@cortex-js/compute-engine@0.26.2?module";
const ce = new ComputeEngine();
console.log("Using CE v" + version);

const expr1 = ce.parse("\\ln |x|");
console.log("should be Ln(Abs(x)) -->", JSON.stringify(expr1)); // OK

const deriv = ce.box(["D", expr1, "x"]).evaluate();
console.log("should be 1/Abs(x) -->", JSON.stringify(deriv)); // BAD
console.log("should be \\frac{1}{\\vert x\\vert} -->", deriv.toLatex()); // BAD
try {
  console.log("should be 1/Math.abs(_.x) -->", deriv.compile().toString()); // BAD
} catch (e) {
  console.error(e);
}

ce.assign("x", 1 / 10);
console.log("should be 10 -->", JSON.stringify(deriv.evaluate())); // BAD

Actual Result

Using CE v0.26.2
should be Ln(Abs(x)) --> ["Ln",["Abs","x"]]
should be 1/Abs(x) --> ["Divide",["Piecewise",["Tuple",["Multiply","_1",["Power","_1",-1]],["Less",0,"_1"]]],["Abs","x"]]
should be \frac{1}{\vert x\vert} --> \frac{1}{\vert x\vert}(\mathrm{Piecewise}(\mathrm{Pair}(\frac{\operatorname{\_1}}{\operatorname{\_1}}, 0\lt\operatorname{\_1})))
Error: Cannot compile invalid expression: "Error(ErrorCode(incompatible-type, number, function)) / |x|"
should be 10 --> ["Multiply",10,["Piecewise",["Tuple",["Multiply","_1",["Power","_1",-1]],["Less",0,"_1"]]]]

The latex also looks strange for the derivative. (Github refuses to render it inline here, so paste it into katex).

Expected Result

See above, it's in the console.log()'s

Environment

See above for version

No, it doesn't seem to work in any version that has symbolic derivative functionality.

Also of note

  • Derivative of plain old ln(x) is just fine. The absolute value messes derivative up:
import { ComputeEngine, version } from "https://unpkg.com/@cortex-js/compute-engine@0.26.2?module";
const ce = new ComputeEngine();
console.log("Using CE v" + version);

const expr1 = ce.parse("\\ln x");
console.log("should be Ln(x) -->", JSON.stringify(expr1)); // OK

const deriv = ce.box(["D", expr1, "x"]).evaluate();
console.log("should be Divide(1, x) -->", JSON.stringify(deriv)); // OK

try {
  console.log("should be 1/_.x -->", deriv.compile().toString()); // OK
} catch (e) {
  console.error(e);
}
Using CE v0.26.2
should be Ln(x) --> ["Ln","x"]
should be Divide(1, x) --> ["Divide",1,"x"]
should be 1/_.x --> 1 / _.x
  • The only occurrence of Piecewise in this repository is in the definition of the derivative of Abs; maybe that's why it's messed up - because Piecewise is not implemented.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions