// File: tomfunkhouser[P]gaps/pkgs/RNMath/RNAlgebraic.cpp
// Function: gaps::RNAlgebraic::PartialDerivative(double const*, int) const
RNScalar RNAlgebraic::
PartialDerivative(const RNScalar *x, int variable) const
{
  // Return partial derivative
  switch(operation) {
  case RN_ZERO_OPERATION: 
    return 0;
    
  case RN_POLYNOMIAL_OPERATION:
    return polynomial->PartialDerivative(x, variable);

  case RN_ADD_OPERATION: 
    return operands[0]->PartialDerivative(x, variable) + operands[1]->PartialDerivative(x, variable);

  case RN_SUBTRACT_OPERATION: 
    return operands[0]->PartialDerivative(x, variable) - operands[1]->PartialDerivative(x, variable);

  case RN_MULTIPLY_OPERATION: {
    // Product rule
    RNScalar v0 = operands[0]->Evaluate(x);
    RNScalar v1 = operands[1]->Evaluate(x);
    RNScalar d0 = operands[0]->PartialDerivative(x, variable);
    RNScalar d1 = operands[1]->PartialDerivative(x, variable);
    return d0*v1 + v0*d1; }

  case RN_DIVIDE_OPERATION: {
    // Quotion rule
    RNScalar v1 = operands[1]->Evaluate(x);
    RNScalar v1_squared = v1 * v1;
    if (RNIsZero(v1_squared, RN_SMALL_EPSILON)) return RN_INFINITY;
    RNScalar v0 = operands[0]->Evaluate(x);
    RNScalar d0 = operands[0]->PartialDerivative(x, variable);
    RNScalar d1 = operands[1]->PartialDerivative(x, variable);
    return (d0*v1 - v0*d1) / v1_squared; }
  
  case RN_POW_OPERATION: 
    // Power rule
    if (operands[1]->IsConstant()) {
      RNScalar v0 = operands[0]->Evaluate(x);
      RNScalar v1 = operands[1]->Evaluate(x);
      RNScalar d0 = operands[0]->PartialDerivative(x, variable);
      return v1 * pow(v0, v1-1.0) * d0;
    }
    else {
      RNScalar v0 = operands[0]->Evaluate(x);
      if (RNIsZero(v0, RN_SMALL_EPSILON)) return 0;
      RNScalar v1 = operands[1]->Evaluate(x);
      RNScalar d0 = operands[0]->PartialDerivative(x, variable);
      RNScalar d1 = operands[1]->PartialDerivative(x, variable);
      return pow(v0, v1) * (d0*v1/v0 + d1*log(v0)); 
    }
  }    

  // Should never get here
  RNAbort("Invalid algebraic operation: %d", operation);
  return 0.0;
}

// Assembly:
subq $0xe8, %rsp
movq %rdi, 0xd8(%rsp)
movq %rsi, 0xd0(%rsp)
movl %edx, 0xcc(%rsp)
movq 0xd8(%rsp), %rax
movq %rax, 0x38(%rsp)
movl 0x8(%rax), %eax
movq %rax, 0x40(%rsp)
subq $0x6, %rax
ja 0x46996
movq 0x40(%rsp), %rax
leaq 0x664e7(%rip), %rcx     # 0xac9b0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
xorps %xmm0, %xmm0
movsd %xmm0, 0xe0(%rsp)
jmp 0x469b8
movq 0x38(%rsp), %rax
movq 0x20(%rax), %rdi
movq 0xd0(%rsp), %rsi
movl 0xcc(%rsp), %edx
callq 0x3e270
movsd %xmm0, 0xe0(%rsp)
jmp 0x469b8
movq 0x38(%rsp), %rax
movq 0x10(%rax), %rdi
movq 0xd0(%rsp), %rsi
movl 0xcc(%rsp), %edx
callq 0x46480
movq 0x38(%rsp), %rax
movsd %xmm0, 0x30(%rsp)
movq 0x18(%rax), %rdi
movq 0xd0(%rsp), %rsi
movl 0xcc(%rsp), %edx
callq 0x46480
movaps %xmm0, %xmm1
movsd 0x30(%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0xe0(%rsp)
jmp 0x469b8
movq 0x38(%rsp), %rax
movq 0x10(%rax), %rdi
movq 0xd0(%rsp), %rsi
movl 0xcc(%rsp), %edx
callq 0x46480
movq 0x38(%rsp), %rax
movsd %xmm0, 0x28(%rsp)
movq 0x18(%rax), %rdi
movq 0xd0(%rsp), %rsi
movl 0xcc(%rsp), %edx
callq 0x46480
movaps %xmm0, %xmm1
movsd 0x28(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0xe0(%rsp)
jmp 0x469b8
movq 0x38(%rsp), %rax
movq 0x10(%rax), %rdi
movq 0xd0(%rsp), %rsi
callq 0x45fa0
movq 0x38(%rsp), %rax
movsd %xmm0, 0xc0(%rsp)
movq 0x18(%rax), %rdi
movq 0xd0(%rsp), %rsi
callq 0x45fa0
movq 0x38(%rsp), %rax
movsd %xmm0, 0xb8(%rsp)
movq 0x10(%rax), %rdi
movq 0xd0(%rsp), %rsi
movl 0xcc(%rsp), %edx
callq 0x46480
movq 0x38(%rsp), %rax
movsd %xmm0, 0xb0(%rsp)
movq 0x18(%rax), %rdi
movq 0xd0(%rsp), %rsi
movl 0xcc(%rsp), %edx
callq 0x46480
movsd %xmm0, 0xa8(%rsp)
movsd 0xb0(%rsp), %xmm0
movsd 0xb8(%rsp), %xmm2
movsd 0xc0(%rsp), %xmm1
mulsd 0xa8(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0xe0(%rsp)
jmp 0x469b8
movq 0x38(%rsp), %rax
movq 0x18(%rax), %rdi
movq 0xd0(%rsp), %rsi
callq 0x45fa0
movsd %xmm0, 0xa0(%rsp)
movsd 0xa0(%rsp), %xmm0
mulsd 0xa0(%rsp), %xmm0
movsd %xmm0, 0x98(%rsp)
movsd 0x98(%rsp), %xmm0
leaq 0x83bfe(%rip), %rax     # 0xca2d0
movsd (%rax), %xmm1
callq 0xb660
cmpl $0x0, %eax
je 0x466f9
leaq 0x83c01(%rip), %rax     # 0xca2e8
movsd (%rax), %xmm0
movsd %xmm0, 0xe0(%rsp)
jmp 0x469b8
movq 0x38(%rsp), %rax
movq 0x10(%rax), %rdi
movq 0xd0(%rsp), %rsi
callq 0x45fa0
movq 0x38(%rsp), %rax
movsd %xmm0, 0x90(%rsp)
movq 0x10(%rax), %rdi
movq 0xd0(%rsp), %rsi
movl 0xcc(%rsp), %edx
callq 0x46480
movq 0x38(%rsp), %rax
movsd %xmm0, 0x88(%rsp)
movq 0x18(%rax), %rdi
movq 0xd0(%rsp), %rsi
movl 0xcc(%rsp), %edx
callq 0x46480
movsd %xmm0, 0x80(%rsp)
movsd 0x88(%rsp), %xmm0
movsd 0xa0(%rsp), %xmm2
movsd 0x90(%rsp), %xmm1
mulsd 0x80(%rsp), %xmm1
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
divsd 0x98(%rsp), %xmm0
movsd %xmm0, 0xe0(%rsp)
jmp 0x469b8
movq 0x38(%rsp), %rax
movq 0x18(%rax), %rdi
callq 0x43b60
cmpl $0x0, %eax
je 0x4687a
movq 0x38(%rsp), %rax
movq 0x10(%rax), %rdi
movq 0xd0(%rsp), %rsi
callq 0x45fa0
movq 0x38(%rsp), %rax
movsd %xmm0, 0x78(%rsp)
movq 0x18(%rax), %rdi
movq 0xd0(%rsp), %rsi
callq 0x45fa0
movq 0x38(%rsp), %rax
movsd %xmm0, 0x70(%rsp)
movq 0x10(%rax), %rdi
movq 0xd0(%rsp), %rsi
movl 0xcc(%rsp), %edx
callq 0x46480
movsd %xmm0, 0x68(%rsp)
movsd 0x70(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
movsd 0x78(%rsp), %xmm0
movsd 0x70(%rsp), %xmm1
movsd 0x647d0(%rip), %xmm2    # 0xab020
subsd %xmm2, %xmm1
callq 0x44a0
movaps %xmm0, %xmm1
movsd 0x20(%rsp), %xmm0
mulsd %xmm1, %xmm0
mulsd 0x68(%rsp), %xmm0
movsd %xmm0, 0xe0(%rsp)
jmp 0x469b8
movq 0x38(%rsp), %rax
movq 0x10(%rax), %rdi
movq 0xd0(%rsp), %rsi
callq 0x45fa0
movsd %xmm0, 0x60(%rsp)
movsd 0x60(%rsp), %xmm0
leaq 0x83a2d(%rip), %rax     # 0xca2d0
movsd (%rax), %xmm1
callq 0xb660
cmpl $0x0, %eax
je 0x468c2
xorps %xmm0, %xmm0
movsd %xmm0, 0xe0(%rsp)
jmp 0x469b8
movq 0x38(%rsp), %rax
movq 0x18(%rax), %rdi
movq 0xd0(%rsp), %rsi
callq 0x45fa0
movq 0x38(%rsp), %rax
movsd %xmm0, 0x58(%rsp)
movq 0x10(%rax), %rdi
movq 0xd0(%rsp), %rsi
movl 0xcc(%rsp), %edx
callq 0x46480
movq 0x38(%rsp), %rax
movsd %xmm0, 0x50(%rsp)
movq 0x18(%rax), %rdi
movq 0xd0(%rsp), %rsi
movl 0xcc(%rsp), %edx
callq 0x46480
movsd %xmm0, 0x48(%rsp)
movsd 0x60(%rsp), %xmm0
movsd 0x58(%rsp), %xmm1
callq 0x44a0
movsd %xmm0, 0x18(%rsp)
movsd 0x50(%rsp), %xmm0
mulsd 0x58(%rsp), %xmm0
divsd 0x60(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
movsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movsd 0x60(%rsp), %xmm0
callq 0x41c0
movsd 0x8(%rsp), %xmm1
movsd 0x10(%rsp), %xmm2
movaps %xmm0, %xmm3
movsd 0x18(%rsp), %xmm0
mulsd %xmm3, %xmm1
addsd %xmm2, %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, 0xe0(%rsp)
jmp 0x469b8
movq 0x38(%rsp), %rax
movl 0x8(%rax), %esi
leaq 0x66095(%rip), %rdi     # 0xaca3a
movb $0x0, %al
callq 0x4c6e0
xorps %xmm0, %xmm0
movsd %xmm0, 0xe0(%rsp)
movsd 0xe0(%rsp), %xmm0
addq $0xe8, %rsp
retq
nopl (%rax)