function H = trigonometric_hessian1(x)
  ##
  ## The Hessian of the Trigonometric function. See trigonometric1.m
  ## for more information. Not my implementation. Too ugly to
  ## recreate.
  ##
  n  = length(x);
  H  = zeros(n,n);

  cos_sum = sum(cos(x));

  for i = 1 : n
    H(i,i) = sin ( x(i) );
  end

  s = 0;
  for j = 1 : n
    th = cos ( x(j) );
    t = ( n + j ) - H(j,j) - cos_sum - j * th;
    s = s + t;
    for k = 1 : j-1
      H(j,k) = 2 * ( sin ( x(k) ) * ( ( n + j + k ) * H(j,j) - th ) ...
             - H(j,j) * cos ( x(k) ) );
    end
    H(j,j) = ( j * ( j + 2 ) + n ) * H(j,j)^2 + th * ...
             ( th - ( 2 * j + 2 ) * H(j,j) ) + t * ( j * th + H(j,j) );
  end

  for j = 1 : n
    H(j,j) = 2 * ( H(j,j) + cos ( x(j) ) * s );
  end

  for i = 1 : n
    H(i,i+1:n) = H(i+1:n,i);
  end

end
