eXorithm – Execute Algorithm: View / Run Algorithm derivative

Logo Beta

function derivative ($function, $with_respect_to
{
  // $with_respect_to is a descriptive variable name, but it's too long!
  $x = $with_respect_to
  
  if (is_array$function)) {
      
    // pop the operator off the front of the equation
    $operator = array_shift$function);
    
    switch ($operator) {
        
      case '+'
        // (f+g)' = f' + g'
        return simplify_equationarray'+', derivative$function[0], $x), derivative$function[1], $x)));
  
      case '-'
        // (f-g)' = f' - g'
        return simplify_equationarray'-', derivative$function[0], $x), derivative$function[1], $x)));
  
      case '*'
        // (f*g)' = f'*g + f*g'
        return simplify_equationarray'+'
                     array'*', derivative$function[0], $x), $function[1]),
                     array'*', $function[0], derivative$function[1], $x))
                    ));
  
      case '/'
        // (f/g)' = (f'*g - f*g') / g^2
        return simplify_equationarray'/'
                     array'-'
                           array'*', derivative$function[0], $x), $function[1]),
                           array'*', $function[0], derivative$function[1], $x))
                          ),
                     array'^', $function[1], 2)
                    ));
  
      case '^'
        // (f^g)' = (f^g) * (g'*ln(f) + (g/f)*f')
        return simplify_equationarray'*'
                     array'^', $function[0], $function[1]),
                     array'+'
                           array'*'
                                 derivative$function[1], $x),
                                 array'ln', $function[0])
                                ),
                           array'*'
                                 array'/', $function[1], $function[0]),
                                 derivative$function[0], $x
                                )
                          ),
                    ));
        
      case 'neg'
        // (-f') = -(f')
        return simplify_equationarray'neg', derivative$function[0], $x)));
    
      case 'sqrt'
        // (f^(1/2))' = (f^(1/2)) * ((1/2f)*f')
        return simplify_equationarray'*'
                     array'sqrt', $function[0]),
                     array'*'
                            array'/', 1, array'*', 2, $function[0])),
                            derivative$function[0], $x
                          )
                    ));
  
      case 'log'
        // (log(f,g))' = (1/(f*ln(g))) * f'
        return simplify_equationarray'*'
                     array'/', 1, array'*'
                                         $function[0],
                                         array'ln', $function[1])
                                         )),
                     derivative$function[0], $x
                    ));
  
      case 'ln'
        // (ln(f))' = (1/f) * f'
        return simplify_equationarray'*'
                     array'/', 1, $function[0]),
                     derivative$function[0], $x
                    ));
  
      case 'exp'
        // (exp(f))' = exp(f)
        return simplify_equationarray'exp', $function[0]));
  
      case 'root'
        // (f^(1/g))' = (f^(1/g)) * ((1/g)'*ln(f) + (1/fg)*f')
        return simplify_equationarray'*'
                     array'^', $function[0], array'/', 1, $function[1])),
                     array'+'
                           array'*'
                                 derivativearray'/', 1, $function[1]), $x),
                                 array'ln', $function[0])
                                ),
                           array'*'
                                 array'/', array'/', 1, $function[1]), $function[0]),
                                 derivative$function[0], $x
                                )
                          ),
                    ));
        
        // trig
        // these all use the chain rule (f(g(x)))' = f'(g(x)) * (g(x))'
        
      case 'sin'
        return simplify_equationarray'*'
                     array'cos', $function[0]),
                     derivative$function[0], $x
                    ));
        
      case 'cos'
        return simplify_equationarray'*'
                     array'neg', array'sin', $function[0])),
                     derivative$function[0], $x
                    ));
  
      case 'tan'
        return simplify_equationarray'*'
                     array'/', 1, array'^', array'cos', $function[0]), 2)),
                     derivative$function[0], $x
                    ));
  
      case 'sec'
         return simplify_equationarray'*'
                     array'*'
                           array'sec', $function[0]),
                           array'tan', $function[0])
                          ),
                     derivative$function[0], $x
                    ));
  
      case 'csc'
         return simplify_equationarray'*'
                     array'*'
                           array'neg', array'csc', $function[0])),
                           array'cot', $function[0])
                          ),
                     derivative$function[0], $x
                    ));
  
      case 'cot'
         return simplify_equationarray'*'
                     array'/', -1, array'^', array'sin', $function[0]), 2)),
                     derivative$function[0], $x
                    ));
        
     // below are not done
        
        // hyperbolic trig
      case 'sinh'
  
      case 'cosh'
  
      case 'tanh'
  
      case 'sech'
  
      case 'csch'
  
      case 'coth'
  
        // arc trig
      case 'arcsin'
  
      case 'arccos'
  
      case 'arctan'
  
        // inverse hyperbolic trig
      case 'arsinh'
  
      case 'arcosh'
  
      case 'artanh'
  
      default
        throw new Exception'usupported operator in derivative '$operator);
    }
  } else {
    if (ctype_alpha$function)) {
      if ($function==$x) {
        return 1;
      } else {
        throw new Exception'function contains a variable other than '$x'!');
      }
    } else {
      return 0;
    }
  }
  return $function
}Â