<s> """<STR_LIT>""" <EOL> from __future__ import division , print_function <EOL> from sympy . core import S , sympify <EOL> from sympy . core . compatibility import iterable <EOL> from sympy . core . containers import Tuple <EOL> from sympy . simplify import nsimplify , simplify <EOL> from sympy . geometry . exceptions import GeometryError <EOL> from sympy . functions . elementary . miscellaneous import sqrt <EOL> from sympy . functions . elementary . complexes import im <EOL> from sympy . matrices import Matrix <EOL> from sympy . core . numbers import Float <EOL> from sympy . core . evaluate import global_evaluate <EOL> from sympy . core . add import Add <EOL> from . entity import GeometryEntity <EOL> class Point ( GeometryEntity ) : <EOL> """<STR_LIT>""" <EOL> def __new__ ( cls , * args , ** kwargs ) : <EOL> evaluate = kwargs . get ( '<STR_LIT>' , global_evaluate [ <NUM_LIT:0> ] ) <EOL> if iterable ( args [ <NUM_LIT:0> ] ) : <EOL> if isinstance ( args [ <NUM_LIT:0> ] , Point ) and not evaluate : <EOL> return args [ <NUM_LIT:0> ] <EOL> args = args [ <NUM_LIT:0> ] <EOL> coords = Tuple ( * args ) <EOL> if any ( a . is_number and im ( a ) for a in coords ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if evaluate : <EOL> coords = coords . xreplace ( dict ( <EOL> [ ( f , simplify ( nsimplify ( f , rational = True ) ) ) <EOL> for f in coords . atoms ( Float ) ] ) ) <EOL> if len ( coords ) == <NUM_LIT:2> : <EOL> return Point2D ( coords , ** kwargs ) <EOL> if len ( coords ) == <NUM_LIT:3> : <EOL> return Point3D ( coords , ** kwargs ) <EOL> return GeometryEntity . __new__ ( cls , * coords ) <EOL> is_Point = True <EOL> def __contains__ ( self , item ) : <EOL> return item == self <EOL> def is_concyclic ( * args ) : <EOL> args = list ( set ( args ) ) <EOL> if not all ( isinstance ( p , Point ) for p in args ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> return args [ <NUM_LIT:0> ] . is_concyclic ( * args [ <NUM_LIT:1> : ] ) <EOL> def is_collinear ( * args ) : <EOL> """<STR_LIT>""" <EOL> args = list ( set ( args ) ) <EOL> if not all ( isinstance ( p , Point ) for p in args ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> if len ( args ) == <NUM_LIT:0> : <EOL> return False <EOL> if len ( args ) <= <NUM_LIT:2> : <EOL> return True <EOL> points = [ p - args [ <NUM_LIT:0> ] for p in args [ <NUM_LIT:1> : ] ] <EOL> for p in points [ <NUM_LIT:1> : ] : <EOL> if not Point . is_scalar_multiple ( points [ <NUM_LIT:0> ] , p ) : <EOL> return False <EOL> return True <EOL> def is_scalar_multiple ( p1 , p2 ) : <EOL> """<STR_LIT>""" <EOL> m = Matrix ( [ p1 . args , p2 . args ] ) <EOL> return m . rank ( simplify = True ) < <NUM_LIT:2> <EOL> @ property <EOL> def length ( self ) : <EOL> """<STR_LIT>""" <EOL> return S . Zero <EOL> @ property <EOL> def origin ( self ) : <EOL> """<STR_LIT>""" <EOL> return Point ( [ <NUM_LIT:0> ] * len ( self ) ) <EOL> @ property <EOL> def is_zero ( self ) : <EOL> """<STR_LIT>""" <EOL> return all ( x == S . Zero for x in self . args ) <EOL> @ property <EOL> def ambient_dimension ( self ) : <EOL> """<STR_LIT>""" <EOL> return len ( self ) <EOL> def distance ( self , p ) : <EOL> """<STR_LIT>""" <EOL> return sqrt ( sum ( [ ( a - b ) ** <NUM_LIT:2> for a , b in zip ( <EOL> self . args , p . args if isinstance ( p , Point ) else p ) ] ) ) <EOL> def taxicab_distance ( self , p ) : <EOL> """<STR_LIT>""" <EOL> p = Point ( p ) <EOL> return sum ( abs ( a - b ) for a , b in zip ( self . args , p . args ) ) <EOL> def midpoint ( self , p ) : <EOL> """<STR_LIT>""" <EOL> return Point ( [ simplify ( ( a + b ) * S . Half ) for a , b in zip ( self . args , p . args ) ] ) <EOL> def evalf ( self , prec = None , ** options ) : <EOL> """<STR_LIT>""" <EOL> coords = [ x . evalf ( prec , ** options ) for x in self . args ] <EOL> return Point ( * coords , evaluate = False ) <EOL> n = evalf <EOL> def intersection ( self , o ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( o , Point ) : <EOL> if len ( self ) != len ( o ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if self == o : <EOL> return [ self ] <EOL> return [ ] <EOL> return o . intersection ( self ) <EOL> def dot ( self , p2 ) : <EOL> """<STR_LIT>""" <EOL> p2 = Point ( p2 ) <EOL> return Add ( * [ a * b for a , b in zip ( self , p2 ) ] ) <EOL> def equals ( self , other ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( other , Point ) or len ( self . args ) != len ( other . args ) : <EOL> return False <EOL> return all ( a . equals ( b ) for a , b in zip ( self . args , other . args ) ) <EOL> def __len__ ( self ) : <EOL> return len ( self . args ) <EOL> def __iter__ ( self ) : <EOL> return self . args . __iter__ ( ) <EOL> def __eq__ ( self , other ) : <EOL> if not isinstance ( other , Point ) or len ( self . args ) != len ( other . args ) : <EOL> return False <EOL> return self . args == other . args <EOL> def __hash__ ( self ) : <EOL> return hash ( self . args ) <EOL> def __getitem__ ( self , key ) : <EOL> return self . args [ key ] <EOL> def __add__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> if iterable ( other ) and len ( other ) == len ( self ) : <EOL> return Point ( [ simplify ( a + b ) for a , b in zip ( self , other ) ] ) <EOL> else : <EOL> raise ValueError ( <EOL> "<STR_LIT>" ) <EOL> def __sub__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> return self + ( - other ) <EOL> def __mul__ ( self , factor ) : <EOL> """<STR_LIT>""" <EOL> factor = sympify ( factor ) <EOL> return Point ( [ simplify ( x * factor ) for x in self . args ] ) <EOL> def __div__ ( self , divisor ) : <EOL> """<STR_LIT>""" <EOL> divisor = sympify ( divisor ) <EOL> return Point ( [ simplify ( x / divisor ) for x in self . args ] ) <EOL> __truediv__ = __div__ <EOL> def __neg__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return Point ( [ - x for x in self . args ] ) <EOL> def __abs__ ( self ) : <EOL> """<STR_LIT>""" <EOL> origin = Point ( [ <NUM_LIT:0> ] * len ( self ) ) <EOL> return Point . distance ( origin , self ) <EOL> class Point2D ( Point ) : <EOL> """<STR_LIT>""" <EOL> def __new__ ( cls , * args , ** kwargs ) : <EOL> eval = kwargs . get ( '<STR_LIT>' , global_evaluate [ <NUM_LIT:0> ] ) <EOL> check = True <EOL> if isinstance ( args [ <NUM_LIT:0> ] , Point2D ) : <EOL> if not eval : <EOL> return args [ <NUM_LIT:0> ] <EOL> args = args [ <NUM_LIT:0> ] . args <EOL> check = False <EOL> else : <EOL> if iterable ( args [ <NUM_LIT:0> ] ) : <EOL> args = args [ <NUM_LIT:0> ] <EOL> if len ( args ) != <NUM_LIT:2> : <EOL> raise ValueError ( <EOL> "<STR_LIT>" ) <EOL> coords = Tuple ( * args ) <EOL> if check : <EOL> if any ( a . is_number and im ( a ) for a in coords ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if eval : <EOL> coords = coords . xreplace ( dict ( <EOL> [ ( f , simplify ( nsimplify ( f , rational = True ) ) ) <EOL> for f in coords . atoms ( Float ) ] ) ) <EOL> return GeometryEntity . __new__ ( cls , * coords ) <EOL> def __contains__ ( self , item ) : <EOL> return item == self <EOL> @ property <EOL> def x ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . args [ <NUM_LIT:0> ] <EOL> @ property <EOL> def y ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . args [ <NUM_LIT:1> ] <EOL> @ property <EOL> def bounds ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( self . x , self . y , self . x , self . y ) <EOL> def is_concyclic ( * points ) : <EOL> """<STR_LIT>""" <EOL> if len ( points ) == <NUM_LIT:0> : <EOL> return False <EOL> if len ( points ) <= <NUM_LIT:2> : <EOL> return True <EOL> points = [ Point ( p ) for p in points ] <EOL> if len ( points ) == <NUM_LIT:3> : <EOL> return ( not Point . is_collinear ( * points ) ) <EOL> try : <EOL> from . ellipse import Circle <EOL> c = Circle ( points [ <NUM_LIT:0> ] , points [ <NUM_LIT:1> ] , points [ <NUM_LIT:2> ] ) <EOL> for point in points [ <NUM_LIT:3> : ] : <EOL> if point not in c : <EOL> return False <EOL> return True <EOL> except GeometryError : <EOL> return False <EOL> def rotate ( self , angle , pt = None ) : <EOL> """<STR_LIT>""" <EOL> from sympy import cos , sin , Point <EOL> c = cos ( angle ) <EOL> s = sin ( angle ) <EOL> rv = self <EOL> if pt is not None : <EOL> pt = Point ( pt ) <EOL> rv -= pt <EOL> x , y = rv . args <EOL> rv = Point ( c * x - s * y , s * x + c * y ) <EOL> if pt is not None : <EOL> rv += pt <EOL> return rv <EOL> def scale ( self , x = <NUM_LIT:1> , y = <NUM_LIT:1> , pt = None ) : <EOL> """<STR_LIT>""" <EOL> if pt : <EOL> pt = Point ( pt ) <EOL> return self . translate ( * ( - pt ) . args ) . scale ( x , y ) . translate ( * pt . args ) <EOL> return Point ( self . x * x , self . y * y ) <EOL> def translate ( self , x = <NUM_LIT:0> , y = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> return Point ( self . x + x , self . y + y ) <EOL> def transform ( self , matrix ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> col , row = matrix . shape <EOL> valid_matrix = matrix . is_square and col == <NUM_LIT:3> <EOL> except AttributeError : <EOL> valid_matrix = False <EOL> if not valid_matrix : <EOL> raise ValueError ( "<STR_LIT>" + "<STR_LIT>" ) <EOL> x , y = self . args <EOL> return Point ( * ( Matrix ( <NUM_LIT:1> , <NUM_LIT:3> , [ x , y , <NUM_LIT:1> ] ) * matrix ) . tolist ( ) [ <NUM_LIT:0> ] [ : <NUM_LIT:2> ] ) <EOL> class Point3D ( Point ) : <EOL> """<STR_LIT>""" <EOL> def __new__ ( cls , * args , ** kwargs ) : <EOL> eval = kwargs . get ( '<STR_LIT>' , global_evaluate [ <NUM_LIT:0> ] ) <EOL> if isinstance ( args [ <NUM_LIT:0> ] , ( Point , Point3D ) ) : <EOL> if not eval : <EOL> return args [ <NUM_LIT:0> ] <EOL> args = args [ <NUM_LIT:0> ] . args <EOL> else : <EOL> if iterable ( args [ <NUM_LIT:0> ] ) : <EOL> args = args [ <NUM_LIT:0> ] <EOL> if len ( args ) not in ( <NUM_LIT:2> , <NUM_LIT:3> ) : <EOL> raise TypeError ( <EOL> "<STR_LIT>" ) <EOL> coords = Tuple ( * args ) <EOL> if len ( coords ) == <NUM_LIT:2> : <EOL> coords += ( S . Zero , ) <EOL> if eval : <EOL> coords = coords . xreplace ( dict ( <EOL> [ ( f , simplify ( nsimplify ( f , rational = True ) ) ) <EOL> for f in coords . atoms ( Float ) ] ) ) <EOL> return GeometryEntity . __new__ ( cls , * coords ) <EOL> def __contains__ ( self , item ) : <EOL> return item == self <EOL> @ property <EOL> def x ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . args [ <NUM_LIT:0> ] <EOL> @ property <EOL> def y ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . args [ <NUM_LIT:1> ] <EOL> @ property <EOL> def z ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . args [ <NUM_LIT:2> ] <EOL> def direction_ratio ( self , point ) : <EOL> """<STR_LIT>""" <EOL> return [ ( point . x - self . x ) , ( point . y - self . y ) , ( point . z - self . z ) ] <EOL> def direction_cosine ( self , point ) : <EOL> """<STR_LIT>""" <EOL> a = self . direction_ratio ( point ) <EOL> b = sqrt ( sum ( i ** <NUM_LIT:2> for i in a ) ) <EOL> return [ ( point . x - self . x ) / b , ( point . y - self . y ) / b , <EOL> ( point . z - self . z ) / b ] <EOL> @ staticmethod <EOL> def are_collinear ( * points ) : <EOL> """<STR_LIT>""" <EOL> return Point . is_collinear ( * points ) <EOL> @ staticmethod <EOL> def are_coplanar ( * points ) : <EOL> """<STR_LIT>""" <EOL> from sympy . geometry . plane import Plane <EOL> points = list ( set ( points ) ) <EOL> if len ( points ) < <NUM_LIT:3> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> a , b = points [ : <NUM_LIT:2> ] <EOL> for i , c in enumerate ( points [ <NUM_LIT:2> : ] ) : <EOL> try : <EOL> p = Plane ( a , b , c ) <EOL> for j in ( <NUM_LIT:0> , <NUM_LIT:1> , i ) : <EOL> points . pop ( j ) <EOL> return all ( p . is_coplanar ( i ) for i in points ) <EOL> except ValueError : <EOL> pass <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> def intersection ( self , o ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( o , Point3D ) : <EOL> if self == o : <EOL> return [ self ] <EOL> return [ ] <EOL> return o . intersection ( self ) <EOL> def scale ( self , x = <NUM_LIT:1> , y = <NUM_LIT:1> , z = <NUM_LIT:1> , pt = None ) : <EOL> """<STR_LIT>""" <EOL> if pt : <EOL> pt = Point3D ( pt ) <EOL> return self . translate ( * ( - pt ) . args ) . scale ( x , y , z ) . translate ( * pt . args ) <EOL> return Point3D ( self . x * x , self . y * y , self . z * z ) <EOL> def translate ( self , x = <NUM_LIT:0> , y = <NUM_LIT:0> , z = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> return Point3D ( self . x + x , self . y + y , self . z + z ) <EOL> def transform ( self , matrix ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> col , row = matrix . shape <EOL> valid_matrix = matrix . is_square and col == <NUM_LIT:4> <EOL> except AttributeError : <EOL> valid_matrix = False <EOL> if not valid_matrix : <EOL> raise ValueError ( "<STR_LIT>" + "<STR_LIT>" ) <EOL> from sympy . matrices . expressions import Transpose <EOL> x , y , z = self . args <EOL> m = Transpose ( matrix ) <EOL> return Point3D ( * ( Matrix ( <NUM_LIT:1> , <NUM_LIT:4> , [ x , y , z , <NUM_LIT:1> ] ) * m ) . tolist ( ) [ <NUM_LIT:0> ] [ : <NUM_LIT:3> ] ) </s>
