def intersect_trust_region(x, s, Delta):
 a = np.dot(s, s)
 if (a == 0):
  raise ValueError('`s` is zero.')
 b = np.dot(x, s)
 c = (np.dot(x, x) - (Delta ** 2))
 if (c > 0):
  raise ValueError('`x` is not within the trust region.')
 d = np.sqrt(((b * b) - (a * c)))
 q = (- (b + copysign(d, b)))
 t1 = (q / a)
 t2 = (c / q)
 if (t1 < t2):
  return (t1, t2)
 else:
  return (t2, t1)

def intersect_trust_region ( x , s , Delta ) : a = np . dot ( s , s ) if ( a == 0 ) : raise ValueError ( '`s` is zero.' ) b = np . dot ( x , s ) c = ( np . dot ( x , x ) - ( Delta ** 2 ) ) if ( c > 0 ) : raise ValueError ( '`x` is not within the trust region.' ) d = np . sqrt ( ( ( b * b ) - ( a * c ) ) ) q = ( - ( b + copysign ( d , b ) ) ) t1 = ( q / a ) t2 = ( c / q ) if ( t1 < t2 ) : return ( t1 , t2 ) else : return ( t2 , t1 )
find the intersection of a line with the boundary of a trust region .