Skip to content
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

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

Open
dragoncoder047 opened this issue Oct 5, 2024 · 0 comments
Open

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

dragoncoder047 opened this issue Oct 5, 2024 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@dragoncoder047
Copy link

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.
@arnog arnog self-assigned this Oct 6, 2024
@arnog arnog added the bug Something isn't working label Oct 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

No branches or pull requests

2 participants