// File: tomfunkhouser[P]gaps/pkgs/RNMath/RNPolynomial.cpp
// Function: gaps::RNPolynomialTerm::PartialDerivative(double const*, int) const
RNScalar RNPolynomialTerm::
PartialDerivative(const RNScalar *x, int variable) const
{
  // Check if has variable
  if (!HasVariable(variable)) return 0.0;

  // Evaluate the derivative with respect to variable
  RNScalar result = c;
  for (int i = 0; i < NVariables(); i++) {
    int k = Variable(i);
    if (variable == k) {
      if (e[i] == 1.0) result *= 1.0;
      else if (e[i] == 2.0) result *= 2.0 * x[k];
      else if ((e[i] < 1.0) && RNIsZero(x[k])) result *= RN_INFINITY;
      else result *= e[i] * pow(x[k], e[i] - 1.0);
    }
    else {
      if (e[i] == 1.0) result *= x[k];
      else if (e[i] == 2.0) result *= x[k] * x[k];
      else if ((e[i] < 0) && RNIsZero(x[k])) result *= RN_INFINITY;
      else result *= pow(x[k], e[i]);
    }
  }
  return result;
}

// Assembly:
subq $0x48, %rsp
movq %rdi, 0x38(%rsp)
movq %rsi, 0x30(%rsp)
movl %edx, 0x2c(%rsp)
movq 0x38(%rsp), %rdi
movq %rdi, 0x10(%rsp)
movl 0x2c(%rsp), %esi
callq 0x3d400
cmpl $0x0, %eax
jne 0x3e348
xorps %xmm0, %xmm0
movsd %xmm0, 0x40(%rsp)
jmp 0x3e629
movq 0x10(%rsp), %rax
movsd 0x10(%rax), %xmm0
movsd %xmm0, 0x20(%rsp)
movl $0x0, 0x1c(%rsp)
movq 0x10(%rsp), %rdi
movl 0x1c(%rsp), %eax
movl %eax, 0xc(%rsp)
callq 0x3f6b0
movl %eax, %ecx
movl 0xc(%rsp), %eax
cmpl %ecx, %eax
jge 0x3e61d
movq 0x10(%rsp), %rdi
movl 0x1c(%rsp), %esi
callq 0x3f6e0
movl %eax, 0x18(%rsp)
movl 0x2c(%rsp), %eax
cmpl 0x18(%rsp), %eax
jne 0x3e4e8
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rax
movslq 0x1c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd 0x6cc65(%rip), %xmm1    # 0xab020
ucomisd %xmm1, %xmm0
jne 0x3e3dc
jp 0x3e3dc
movsd 0x6cc55(%rip), %xmm0    # 0xab020
mulsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
jmp 0x3e4e3
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rax
movslq 0x1c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd 0x6d5e1(%rip), %xmm1    # 0xab9d8
ucomisd %xmm1, %xmm0
jne 0x3e427
jp 0x3e427
movq 0x30(%rsp), %rax
movslq 0x18(%rsp), %rcx
movsd 0x6d5c7(%rip), %xmm0    # 0xab9d8
mulsd (%rax,%rcx,8), %xmm0
mulsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
jmp 0x3e4e1
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rax
movslq 0x1c(%rsp), %rcx
movsd 0x6cbe3(%rip), %xmm0    # 0xab020
ucomisd (%rax,%rcx,8), %xmm0
jbe 0x3e481
movq 0x30(%rsp), %rax
movslq 0x18(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
leaq 0x8be7e(%rip), %rax     # 0xca2d8
movsd (%rax), %xmm1
callq 0xb660
cmpl $0x0, %eax
je 0x3e481
leaq 0x8be79(%rip), %rax     # 0xca2e8
movsd (%rax), %xmm0
mulsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
jmp 0x3e4df
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rcx
movslq 0x1c(%rsp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movsd %xmm0, (%rsp)
movq 0x30(%rsp), %rcx
movslq 0x18(%rsp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movq 0x20(%rax), %rax
movslq 0x1c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm1
movsd 0x6cb62(%rip), %xmm2    # 0xab020
subsd %xmm2, %xmm1
callq 0x44a0
movaps %xmm0, %xmm1
movsd (%rsp), %xmm0
mulsd %xmm1, %xmm0
mulsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
jmp 0x3e4e1
jmp 0x3e4e3
jmp 0x3e60b
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rax
movslq 0x1c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd 0x6cb1d(%rip), %xmm1    # 0xab020
ucomisd %xmm1, %xmm0
jne 0x3e52b
jp 0x3e52b
movq 0x30(%rsp), %rax
movslq 0x18(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
mulsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
jmp 0x3e609
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rax
movslq 0x1c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd 0x6d492(%rip), %xmm1    # 0xab9d8
ucomisd %xmm1, %xmm0
jne 0x3e57d
jp 0x3e57d
movq 0x30(%rsp), %rax
movslq 0x18(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x30(%rsp), %rax
movslq 0x18(%rsp), %rcx
mulsd (%rax,%rcx,8), %xmm0
mulsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
jmp 0x3e607
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rax
movslq 0x1c(%rsp), %rcx
xorps %xmm0, %xmm0
ucomisd (%rax,%rcx,8), %xmm0
jbe 0x3e5d2
movq 0x30(%rsp), %rax
movslq 0x18(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
leaq 0x8bd2d(%rip), %rax     # 0xca2d8
movsd (%rax), %xmm1
callq 0xb660
cmpl $0x0, %eax
je 0x3e5d2
leaq 0x8bd28(%rip), %rax     # 0xca2e8
movsd (%rax), %xmm0
mulsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
jmp 0x3e605
movq 0x10(%rsp), %rax
movq 0x30(%rsp), %rcx
movslq 0x18(%rsp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movq 0x20(%rax), %rax
movslq 0x1c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm1
callq 0x44a0
mulsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
jmp 0x3e607
jmp 0x3e609
jmp 0x3e60b
jmp 0x3e60d
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x3e360
movsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x40(%rsp)
movsd 0x40(%rsp), %xmm0
addq $0x48, %rsp
retq
nopw %cs:(%rax,%rax)