Skip to content

Give sgn a derivative, and stop a constant exponent taking the logarithmic rule - #672

Merged
Happypig375 merged 2 commits into
ASC-Community:masterfrom
Rafael-SOWNet:fix/derivative-gaps
Aug 3, 2026
Merged

Happypig375 merged 2 commits into
ASC-Community:masterfrom
Rafael-SOWNet:fix/derivative-gaps

Conversation

@Rafael-SOWNet

Copy link
Copy Markdown
Member

Two ways a derivative came back unusable. Both were found by differentiating
antiderivatives back and checking them at points, rather than by reading code.

sgn had no derivative

// TODO: the Delta function required to be defined, or a piecewise definition
protected override Entity InnerDifferentiate(Variable variable)
    => MathS.Derivative(this, variable);

This is the one to look at first, because it changes a deliberate decision.

Leaving the node unevaluated does not represent the delta either. What it does is make
the derivative of anything containing sgn unevaluable, and that reaches callers: the
antiderivative of abs(x) is sgn(x) * x^2 / 2, and differentiating it back stops at
derivative(sgn(x), x). So ∫abs(x) dx cannot be checked or used numerically.

sgn is flat either side of zero and has no derivative at zero, so its derivative is 0
wherever it exists, and a condition says where that is:

d/dx sgn(x)   →   0 provided not x = 0

That is the stance abs already takes directly below it — sgn(x) provided not x = 0,
likewise undefined at zero. Whichever way the distributional reading is eventually
handled, it is the same question for both, and answering one but not the other is what
was inconsistent.

TestSgnDer asserted the unevaluated form and now asserts this one, with the reasoning
beside it. If you would rather keep the node unevaluated, say so and I will drop this
half — the second half stands on its own.

A constant exponent was read as symbolic

Powf decided whether the exponent was constant with a type test:

: Exponent is Number exp

An exponent written as a sum is not a Number even when it is constant — and the
integral table leaves them exactly that way, (x - 1)^(4 + 1). Read as non-constant it
fell through to the logarithmic rule, which needs a positive base, so:

d/dx [ (x-1)^(4+1) / (4+1) ]   →   ... provided x - 1 > 0

The antiderivative of (x - 1)^4 differentiated back to something undefined for
x < 1
. It now asks Exponent.Evaled instead. Genuinely symbolic exponents are
untouched and still carry their condition:

d/dx x^n x^n * n / x provided x > 0
d/dx 2^x ln(2) * 2^x
d/dx x^x (1 + ln(x)) * x^x provided not x = 0

Testing

UnitTests 3667 passed / 0 failed, FSharpWrapperUnitTests 127 passed / 0 failed, both
netstandard2.0 and net7.0 build.

Nine new tests: the sgn derivative, four antiderivatives of absolute values
differentiating back at points, the power case at a point below the base's root — which
is where the spurious condition bit — and the symbolic exponents that must not change.

Independent of #663 through #671.

🤖 Generated with Claude Code

Two ways a derivative came back unusable, both found by differentiating
antiderivatives back and checking them at points.

sgn had no derivative. It returned an unevaluated node, with a note that the delta
function would be needed first. But leaving it unevaluated does not represent the
delta either -- it only means the derivative of anything containing sgn cannot be
evaluated at all, and that reaches callers: the antiderivative of abs(x) is
sgn(x) * x^2 / 2, and differentiating it back stopped at derivative(sgn(x), x).

sgn is flat either side of zero and has no derivative at zero, so the derivative is
0 wherever it exists, and the condition says where that is. This is the stance abs
already takes right below it, whose derivative is sgn(x) provided not x = 0.
Whichever way the distributional reading is eventually handled, it is the same
question for both. TestSgnDer pinned the unevaluated form and now pins this one,
with the reasoning next to it.

Separately, Powf decided whether the exponent was constant with a type test. An
exponent written as a sum is not a Number even when it is constant, and the integral
table leaves them that way -- (x - 1)^(4 + 1). Read as non-constant it fell to the
logarithmic rule, which needs a positive base, so differentiating the antiderivative
of (x - 1)^4 came back undefined for x < 1. It now asks Evaled instead. Genuinely
symbolic exponents are unaffected and still carry their condition.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@codecov-commenter

codecov-commenter commented Aug 3, 2026 •

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.71%. Comparing base (90c00a8) to head (7b22fc5).
⚠️ Report is 47 commits behind head on master.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #672      +/-   ##
==========================================
- Coverage   80.99%   79.71%   -1.29%     
==========================================
  Files         155      155              
  Lines       13687    12431    -1256     
  Branches     1957     2013      +56     
==========================================
- Hits        11086     9909    -1177     
+ Misses       1990     1934      -56     
+ Partials      611      588      -23     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Happypig375
Happypig375 merged commit 4c11747 into ASC-Community:master Aug 3, 2026
21 of 24 checks passed
@Rafael-SOWNet
Rafael-SOWNet deleted the fix/derivative-gaps branch August 4, 2026 20:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants