Skip to content

Bug in AD implementation of product rule #273

@dsyme

Description

@dsyme

Here's a bug the AI found in the AD implementation for product rule: dsyme#46 (comment)

I agree with the assessment. The linked PR has test cases.


🔧 Critical Bugs Fixed in Automatic Differentiation

I've identified and fixed two critical mathematical bugs in the Dual number implementation that were causing test failures.

🐛 Bugs Fixed

1. Product Rule Bug in operator*=:

// BEFORE (incorrect):
value *= other.value;
derivative = derivative * other.value + value * other.derivative;  // ❌ uses modified value

// AFTER (correct):
auto old_value = value;
value *= other.value;  
derivative = derivative * other.value + old_value * other.derivative;  // ✅ uses original value

2. Quotient Rule Bug in operator/=:

// BEFORE (incorrect):
value /= other.value;
derivative = (derivative * other.value - value * other.derivative) / (other.value * other.value);  // ❌ uses modified value

// AFTER (correct):  
auto old_value = value;
value /= other.value;
derivative = (derivative * other.value - old_value * other.derivative) / (other.value * other.value);  // ✅ uses original value

📊 Root Cause

The compound assignment operators were modifying the value first, then using the modified value in the derivative calculation. This violates the fundamental calculus rules:

  • Product Rule: d(fg) = f'g + fg' (needs original f, not f*g)
  • Quotient Rule: d(f/g) = (f'g - fg')/g² (needs original f, not f/g)

🚀 Expected Impact

  • Automatic differentiation tests should now pass
  • Correct derivative computation for all mathematical expressions
  • No breaking API changes - purely internal bug fixes
  • ⚠️ Half-precision tests may need separate investigation

These bugs were causing systematic errors in derivative calculations throughout the autodiff system. The fixes ensure mathematical correctness for all dual number arithmetic operations.

AI-generated content by PR Fix may contain mistakes.

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