/usr/share/pyshared/sympy/geometry/util.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 | """Utility functions for geometrical entities.
Contains
--------
intersection
convex_hull
are_similar
"""
from sympy import Symbol, Function, solve
def idiff(eq, y, x, dep=None):
"""Return dy/dx assuming that y and any other variables given in dep
depend on x.
>>> from sympy.abc import x, y, a
>>> from sympy.geometry.util import idiff
>>> idiff(x**2 + y**2 - 4, y, x)
-x/y
>>> idiff(x + a + y, y, x)
-1
>>> idiff(x + a + y, y, x, [a])
-Derivative(a, x) - 1
"""
if not dep:
dep = []
dep = set(dep)
dep.add(y)
f = dict([(s, Function(s.name)(x)) for s in eq.atoms(Symbol) if s != x and s in dep])
dydx = Function(y.name)(x).diff(x)
return solve(eq.subs(f).diff(x), dydx)[0].subs(
[(b, a) for a, b in f.iteritems()])
def _symbol(s, matching_symbol=None):
"""Return s if s is a Symbol, else return either a new Symbol (real=True)
with the same name s or the matching_symbol if s is a string and it matches
the name of the matching_symbol.
>>> from sympy import Symbol
>>> from sympy.geometry.util import _symbol
>>> x = Symbol('x')
>>> _symbol('y')
y
>>> _.is_real
True
>>> _symbol(x)
x
>>> _.is_real is None
True
>>> arb = Symbol('foo')
>>> _symbol('arb', arb) # arb's name is foo so foo will not be returned
arb
>>> _symbol('foo', arb) # now it will
foo
NB: the symbol here may not be the same as a symbol with the same
name defined elsewhere as a result of different assumptions.
"""
if isinstance(s, basestring):
if matching_symbol and matching_symbol.name == s:
return matching_symbol
return Symbol(s, real=True)
elif isinstance(s, Symbol):
return s
else:
raise ValueError('symbol must be string for symbol name or Symbol')
def intersection(*entities):
"""The intersection of a collection of GeometryEntity instances.
Parameters
----------
entities : sequence of GeometryEntity
Returns
-------
intersection : list of GeometryEntity
Raises
------
NotImplementedError
When unable to calculate intersection.
Notes
-----
The intersection of any geometrical entity with itself should return
a list with one item: the entity in question.
An intersection requires two or more entities. If only a single
entity is given then the function will return an empty list.
It is possible for `intersection` to miss intersections that one
knows exists because the required quantities were not fully
simplified internally.
Reals should be converted to Rationals, e.g. Rational(str(real_num))
or else failures due to floating point issues may result.
Examples
--------
>>> from sympy.geometry import Point, Line, Circle, intersection
>>> p1, p2, p3 = Point(0, 0), Point(1, 1), Point(-1, 5)
>>> l1, l2 = Line(p1, p2), Line(p3, p2)
>>> c = Circle(p2, 1)
>>> intersection(l1, p2)
[Point(1, 1)]
>>> intersection(l1, l2)
[Point(1, 1)]
>>> intersection(c, p2)
[]
>>> intersection(c, Point(1, 0))
[Point(1, 0)]
>>> intersection(c, l2)
[Point(-5**(1/2)/5 + 1, 2*5**(1/2)/5 + 1), Point(5**(1/2)/5 + 1, -2*5**(1/2)/5 + 1)]
"""
from entity import GeometryEntity
if len(entities) <= 1:
return []
for i, e in enumerate(entities):
if not isinstance(e, GeometryEntity):
try:
entities[i] = Point(e)
except NotImplementedError:
raise ValueError('%s is not a GeometryEntity and cannot be made into Point' % str(e))
res = entities[0].intersection(entities[1])
for entity in entities[2:]:
newres = []
for x in res:
newres.extend(x.intersection(entity))
res = newres
return res
def convex_hull(*args):
"""The convex hull of a collection of 2-dimensional points.
Parameters
----------
args : a collection of Points
Returns
-------
convex_hull : Polygon
Notes
-----
This can only be performed on a set of non-symbolic points.
See Also
--------
Point
References
----------
[1] http://en.wikipedia.org/wiki/Graham_scan
[2] Andrew's Monotone Chain Algorithm
( A.M. Andrew, "Another Efficient Algorithm for Convex Hulls in Two Dimensions", 1979)
http://softsurfer.com/Archive/algorithm_0109/algorithm_0109.htm
Examples
--------
>>> from sympy.geometry import Point, convex_hull
>>> points = [(1,1), (1,2), (3,1), (-5,2), (15,4)]
>>> convex_hull(*points)
Polygon(Point(-5, 2), Point(1, 1), Point(3, 1), Point(15, 4))
"""
from entity import GeometryEntity
from point import Point
from line import Segment
from polygon import Polygon
p = set()
for e in args:
if not isinstance(e, GeometryEntity):
try:
e = Point(e)
except NotImplementedError:
raise ValueError('%s is not a GeometryEntity and cannot be made into Point' % str(e))
if isinstance(e, Point):
p.add(e)
elif isinstance(e, Segment):
p.update(e.points)
elif isinstance(e, Polygon):
p.update(e.vertices)
else:
raise NotImplementedError('Convex hull for %s not implemented.' % type(e))
p = list(p)
if len(p) == 1:
return p[0]
elif len(p) == 2:
return Segment(p[0], p[1])
def orientation(p, q, r):
'''Return positive if p-q-r are clockwise, neg if ccw, zero if
collinear.'''
return (q[1] - p[1])*(r[0] - p[0]) - (q[0] - p[0])*(r[1] - p[1])
# scan to find upper and lower convex hulls of a set of 2d points.
U = []
L = []
p.sort()
for p_i in p:
while len(U) > 1 and orientation(U[-2], U[-1], p_i) <= 0:
U.pop()
while len(L) > 1 and orientation(L[-2], L[-1], p_i) >= 0:
L.pop()
U.append(p_i)
L.append(p_i)
U.reverse()
convexHull = tuple(L + U[1:-1])
if len(convexHull) == 2:
return Segment(convexHull[0], convexHull[1])
return Polygon(*convexHull)
def are_similar(e1, e2):
"""Are two geometrical entities similar.
Can one geometrical entity be uniformly scaled to the other?
Parameters
----------
e1 : GeometryEntity
e2 : GeometryEntity
Returns
-------
are_similar : boolean
Raises
------
GeometryError
When `e1` and `e2` cannot be compared.
Notes
-----
If the two objects are equal then they are similar.
Examples
--------
>>> from sympy import Point, Circle, Triangle, are_similar
>>> c1, c2 = Circle(Point(0, 0), 4), Circle(Point(1, 4), 3)
>>> t1 = Triangle(Point(0, 0), Point(1, 0), Point(0, 1))
>>> t2 = Triangle(Point(0, 0), Point(2, 0), Point(0, 2))
>>> t3 = Triangle(Point(0, 0), Point(3, 0), Point(0, 1))
>>> are_similar(t1, t2)
True
>>> are_similar(t1, t3)
False
"""
if e1 == e2:
return True
try:
return e1.is_similar(e2)
except AttributeError:
try:
return e2.is_similar(e1)
except AttributeError:
n1 = e1.__class__.__name__
n2 = e2.__class__.__name__
raise GeometryError("Cannot test similarity between %s and %s" % (n1, n2))
|