def evaluate_quadratic(J, g, s, diag=None):
 if (s.ndim == 1):
  Js = J.dot(s)
  q = np.dot(Js, Js)
  if (diag is not None):
   q += np.dot((s * diag), s)
 else:
  Js = J.dot(s.T)
  q = np.sum((Js ** 2), axis=0)
  if (diag is not None):
   q += np.sum((diag * (s ** 2)), axis=1)
 l = np.dot(s, g)
 return ((0.5 * q) + l)

def evaluate_quadratic ( J , g , s , diag = None ) : if ( s . ndim == 1 ) : Js = J . dot ( s ) q = np . dot ( Js , Js ) if ( diag is not None ) : q += np . dot ( ( s * diag ) , s ) else : Js = J . dot ( s . T ) q = np . sum ( ( Js ** 2 ) , axis = 0 ) if ( diag is not None ) : q += np . sum ( ( diag * ( s ** 2 ) ) , axis = 1 ) l = np . dot ( s , g ) return ( ( 0.5 * q ) + l )
compute values of a quadratic function arising in least squares .