/usr/share/pyshared/sympy/geometry/point.py is in python-sympy 0.7.1.rc1-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 | """Geometrical Points.
Contains
--------
Point
"""
from sympy.core import S, sympify
from sympy.core.compatibility import iterable
from sympy.simplify import simplify
from sympy.geometry.exceptions import GeometryError
from sympy.functions.elementary.miscellaneous import sqrt
from entity import GeometryEntity
class Point(GeometryEntity):
"""A point in a 2-dimensional Euclidean space.
Parameters
----------
coords : sequence of 2 coordinate values.
Attributes
----------
coordinates : 2-tuple of numbers or sympy objects.
Stored in `self`. That is self[0] is the first coordinate value, and
self[1] is the second coordinate value.
Raises
------
NotImplementedError
When trying to create a point with more than two dimensions.
When `intersection` is called with object other than a Point.
TypeError
When trying to add or subtract points with different dimensions.
Notes
-----
Currently only 2-dimensional points are supported.
Examples
--------
>>> from sympy.geometry import Point
>>> from sympy.abc import x
>>> Point(1, 2)
Point(1, 2)
>>> Point([1, 2])
Point(1, 2)
>>> Point(0, x)
Point(0, x)
"""
def __new__(cls, *args, **kwargs):
if iterable(args[0]):
coords = tuple([sympify(x) for x in args[0]])
else:
coords = tuple([sympify(x) for x in args])
if len(coords) != 2:
raise NotImplementedError("Only two dimensional points currently supported")
return GeometryEntity.__new__(cls, *coords)
@property
def x(self):
return self[0]
@property
def y(self):
return self[1]
@property
def free_symbols(self):
return self.x.free_symbols.union(self.y.free_symbols)
def _eval_subs(self, old, new):
return type(self)(self.x.subs(old, new), self.y.subs(old, new))
def is_collinear(*points):
"""Is a sequence of points collinear?
Test whether or not a set of points are collinear. Returns True if
the set of points are collinear, or False otherwise.
Parameters
----------
points : sequence of Point
Returns
-------
is_collinear : boolean
Notes
--------------------------
Slope is preserved everywhere on a line, so the slope between
any two points on the line should be the same. Take the first
two points, p1 and p2, and create a translated point v1
with p1 as the origin. Now for every other point we create
a translated point, vi with p1 also as the origin. Note that
these translations preserve slope since everything is
consistently translated to a new origin of p1. Since slope
is preserved then we have the following equality:
v1_slope = vi_slope
=> v1.y/v1.x = vi.y/vi.x (due to translation)
=> v1.y*vi.x = vi.y*v1.x
=> v1.y*vi.x - vi.y*v1.x = 0 (*)
Hence, if we have a vi such that the equality in (*) is False
then the points are not collinear. We do this test for every
point in the list, and if all pass then they are collinear.
Examples
--------
>>> from sympy import Point
>>> from sympy.abc import x
>>> p1, p2 = Point(0, 0), Point(1, 1)
>>> p3, p4, p5 = Point(2, 2), Point(x, x), Point(1, 2)
>>> Point.is_collinear(p1, p2, p3, p4)
True
>>> Point.is_collinear(p1, p2, p3, p5)
False
"""
if len(points) == 0:
return False
if len(points) <= 2:
return True # two points always form a line
points = [Point(a) for a in points]
# XXX Cross product is used now, but that only extends to three
# dimensions. If the concept needs to extend to greater
# dimensions then another method would have to be used
p1 = points[0]
p2 = points[1]
v1 = p2 - p1
for p3 in points[2:]:
v2 = p3 - p1
test = simplify(v1[0]*v2[1] - v1[1]*v2[0])
if simplify(test) != 0:
return False
return True
def is_concyclic(*points):
"""Is a sequence of points concyclic?
Test whether or not a sequence of points are concyclic (i.e., they lie
on a circle).
Parameters
----------
points : sequence of Points
Returns
-------
is_concyclic : boolean
True if points are concyclic, False otherwise.
Notes
-----
No points are not considered to be concyclic. One or two points
are definitely concyclic and three points are conyclic iff they
are not collinear.
For more than three points, create a circle from the first three
points. If the circle cannot be created (i.e., they are collinear)
then all of the points cannot be concyclic. If the circle is created
successfully then simply check the remaining points for containment
in the circle.
Examples
--------
>>> from sympy.geometry import Point
>>> p1, p2 = Point(-1, 0), Point(1, 0)
>>> p3, p4 = Point(0, 1), Point(-1, 2)
>>> Point.is_concyclic(p1, p2, p3)
True
>>> Point.is_concyclic(p1, p2, p3, p4)
False
"""
if len(points) == 0:
return False
if len(points) <= 2:
return True
points = [Point(p) for p in points]
if len(points) == 3:
return (not Point.is_collinear(*points))
try:
from ellipse import Circle
c = Circle(points[0], points[1], points[2])
for point in points[3:]:
if point not in c:
return False
return True
except GeometryError, e:
# Circle could not be created, because of collinearity of the
# three points passed in, hence they are not concyclic.
return False
"""
# This code is from Maple
def f(u):
dd = u[0]**2 + u[1]**2 + 1
u1 = 2*u[0] / dd
u2 = 2*u[1] / dd
u3 = (dd - 2) / dd
return u1,u2,u3
u1,u2,u3 = f(points[0])
v1,v2,v3 = f(points[1])
w1,w2,w3 = f(points[2])
p = [v1 - u1, v2 - u2, v3 - u3]
q = [w1 - u1, w2 - u2, w3 - u3]
r = [p[1]*q[2] - p[2]*q[1], p[2]*q[0] - p[0]*q[2], p[0]*q[1] - p[1]*q[0]]
for ind in xrange(3, len(points)):
s1,s2,s3 = f(points[ind])
test = simplify(r[0]*(s1-u1) + r[1]*(s2-u2) + r[2]*(s3-u3))
if test != 0:
return False
return True
"""
def distance(self, p):
"""The Euclidean distance from self to point p.
Parameters
----------
p : Point
Returns
-------
distance : number or symbolic expression.
Examples
--------
>>> from sympy.geometry import Point
>>> p1, p2 = Point(1, 1), Point(4, 5)
>>> p1.distance(p2)
5
>>> from sympy.abc import x, y
>>> p3 = Point(x, y)
>>> p3.distance(Point(0, 0))
(x**2 + y**2)**(1/2)
"""
return sqrt(sum([(a - b)**2 for a, b in zip(self, p)]))
def midpoint(self, p):
"""The midpoint between self and point p.
Parameters
----------
p : Point
Returns
-------
midpoint : Point
Examples
--------
>>> from sympy.geometry import Point
>>> p1, p2 = Point(1, 1), Point(13, 5)
>>> p1.midpoint(p2)
Point(7, 3)
"""
return Point([simplify((a + b)*S.Half) for a, b in zip(self, p)])
def evalf(self):
"""Evaluate the coordinates of the point.
This method will, where possible, create and return a new Point
where the coordinates are evaluated as floating point numbers.
Returns
-------
point : Point
Examples
--------
>>> from sympy import Point, Rational
>>> p1 = Point(Rational(1, 2), Rational(3, 2))
>>> p1
Point(1/2, 3/2)
>>> p1.evalf()
Point(0.5, 1.5)
"""
return Point([x.evalf() for x in self])
def intersection(self, o):
"""The intersection between this point and another point.
Parameters
----------
other : Point
Returns
-------
intersection : list of Points
Notes
-----
The return value will either be an empty list if there is no
intersection, otherwise it will contain this point.
Examples
--------
>>> from sympy import Point
>>> p1, p2, p3 = Point(0, 0), Point(1, 1), Point(0, 0)
>>> p1.intersection(p2)
[]
>>> p1.intersection(p3)
[Point(0, 0)]
"""
if isinstance(o, Point):
if self == o:
return [self]
return []
return o.intersection(self)
@property
def length(self):
return S.Zero
def __len__(self):
return 1
def __add__(self, other):
"""Add two points, or add a factor to this point's coordinates."""
if isinstance(other, Point):
if len(other.args) == len(self.args):
return Point( [simplify(a + b) for a, b in zip(self, other)] )
else:
raise TypeError("Points must have the same number of dimensions")
else:
raise ValueError('Cannot add non-Point, %s, to a Point' % other)
other = sympify(other)
return Point([simplify(a + other) for a in self])
def __sub__(self, other):
"""Subtract two points, or subtract a factor from this point's
coordinates."""
return self + (-other)
def __mul__(self, factor):
"""Multiply point's coordinates by a factor."""
factor = sympify(factor)
return Point([x*factor for x in self])
def __div__(self, divisor):
"""Divide point's coordinates by a factor."""
divisor = sympify(divisor)
return Point([x/divisor for x in self])
def __neg__(self):
"""Negate the point."""
return Point([-x for x in self])
def __abs__(self):
"""Returns the distance between this point and the origin."""
origin = Point([0]*len(self.args))
return Point.distance(origin, self)
|