Download positional_overlap.py from PureOne/neoryae-aegis: direct link, hf CLI and curl.
- Browser
- Download file 5.65 kB
-
https://huggingface.co/datasets/PureOne/neoryae-aegis/resolve/main/positional_overlap.py
- Command line
-
hf download hf://datasets/PureOne/neoryae-aegis/positional_overlap.py
-
curl -L -o positional_overlap.py https://huggingface.co/datasets/PureOne/neoryae-aegis/resolve/main/positional_overlap.py
5.65 kB
| """Exact-formula positional line-overlap certificates, evaluated in float64. | |
| For two segments, Q is the measure of unoriented lines meeting both, with | |
| line measure dtheta*dt for theta in [0,pi). The endpoint formula is exact | |
| mathematically; its implementation and all returned numbers use floating | |
| point arithmetic, not outward-rounded interval arithmetic. | |
| The certificates are NECESSARY conditions for an opaque barrier of the | |
| unit-radius disk. Passing them does not establish opacity. Positive-length | |
| collinear overlaps must be merged first, to avoid counting length twice. | |
| """ | |
| import math | |
| import numpy as np | |
| def _cross(u,v): | |
| return float(u[0]*v[1]-u[1]*v[0]) | |
| def _point(x): | |
| p=np.asarray(x,dtype=float) | |
| if p.shape!=(2,) or not np.isfinite(p).all(): | |
| raise ValueError("Endpoints must be finite two-dimensional points") | |
| return p | |
| def _distance(p,q): | |
| return math.hypot(float(p[0]-q[0]),float(p[1]-q[1])) | |
| def _four_point(a,b,c,d): | |
| return abs(math.fsum([_distance(a,c),_distance(b,d), | |
| -_distance(a,d),-_distance(b,c)])) | |
| def pair_overlap(first,second,relative_tolerance=1e-11): | |
| """Evaluate Q by a finite sum of absolute four-endpoint distance terms. | |
| Split both segments at the intersection of their supporting lines when | |
| that intersection is interior. On each resulting parameter rectangle, | |
| the mixed derivative of distance has constant sign. | |
| Collinear positive-length overlaps are rejected. Numerically ambiguous | |
| near-collinear positive overlaps within relative_tolerance are rejected | |
| as well. A shared endpoint and isolated zero-length pieces are allowed. | |
| """ | |
| a,b=map(_point,first) | |
| c,d=map(_point,second) | |
| e,f=b-a,d-c | |
| le,lf=_distance(a,b),_distance(c,d) | |
| if le==0 or lf==0: | |
| return 0.0 | |
| scale=max(le,lf,_distance(a,c),_distance(a,d), | |
| _distance(b,c),_distance(b,d)) | |
| tol=relative_tolerance*scale | |
| determinant=_cross(e,f) | |
| near_parallel=abs(determinant)<=relative_tolerance*le*lf | |
| near_collinear=(near_parallel and | |
| abs(_cross(c-a,e))/le<=tol and | |
| abs(_cross(d-a,e))/le<=tol) | |
| if near_collinear: | |
| unit=e/le | |
| pc,pd=float(np.dot(c-a,unit)),float(np.dot(d-a,unit)) | |
| overlap=min(le,max(pc,pd))-max(0.,min(pc,pd)) | |
| if overlap>tol: | |
| raise ValueError("Collinear or numerically near-collinear positive-length overlap: merge these pieces first") | |
| return 0.0 | |
| if determinant==0: | |
| return _four_point(a,b,c,d) | |
| t=_cross(c-a,f)/determinant | |
| u=_cross(c-a,e)/determinant | |
| # The average reduces roundoff when an intersection is interior to both. | |
| p=.5*((a+t*e)+(c+u*f)) | |
| first_pieces=[(a,b)] | |
| second_pieces=[(c,d)] | |
| if relative_tolerance<t<1-relative_tolerance: | |
| first_pieces=[(a,p),(p,b)] | |
| if relative_tolerance<u<1-relative_tolerance: | |
| second_pieces=[(c,p),(p,d)] | |
| return math.fsum(_four_point(x,y,z,w) | |
| for x,y in first_pieces for z,w in second_pieces) | |
| def positional_certificate(segments,certified_max_hits=None): | |
| """Return pair energy and maximum-spanning-forest necessary bounds. | |
| If the input is an actual opaque barrier with total length L, then | |
| L >= pi + .5*maximum_spanning_forest_value. | |
| If a separate proof establishes N(line)<=M for almost every line, | |
| L >= pi + pair_energy/M. | |
| 'certified_max_hits' records that EXTERNAL assumption. This function | |
| never certifies a line-intersection maximum by sampling or otherwise. | |
| The universal automatic maximum is the number of nondegenerate pieces. | |
| """ | |
| pieces=[tuple(map(_point,s)) for s in segments] | |
| n=len(pieces) | |
| if n==0: | |
| raise ValueError("At least one segment is required") | |
| lengths=[_distance(a,b) for a,b in pieces] | |
| n_active=sum(x>0 for x in lengths) | |
| pairs=[] | |
| for i in range(n): | |
| for j in range(i+1,n): | |
| pairs.append((i,j,pair_overlap(pieces[i],pieces[j]))) | |
| parent=list(range(n)) | |
| def find(x): | |
| while parent[x]!=x: | |
| parent[x]=parent[parent[x]] | |
| x=parent[x] | |
| return x | |
| forest=[] | |
| for i,j,q in sorted(pairs,key=lambda item:item[2],reverse=True): | |
| ri,rj=find(i),find(j) | |
| if ri!=rj: | |
| parent[ri]=rj | |
| forest.append((i,j,q)) | |
| energy=math.fsum(q for i,j,q in pairs) | |
| forest_value=math.fsum(q for i,j,q in forest) | |
| result={"segment_count":n,"nondegenerate_segment_count":n_active, | |
| "total_segment_length":math.fsum(lengths), | |
| "pair_overlaps":[{"i":i,"j":j,"Q":q} for i,j,q in pairs], | |
| "pair_energy":energy, | |
| "maximum_spanning_forest_edges":[{"i":i,"j":j,"Q":q} for i,j,q in forest], | |
| "maximum_spanning_forest_value":forest_value, | |
| "forest_necessary_lower_bound":math.pi+.5*forest_value, | |
| "automatic_hit_bound":n_active, | |
| "automatic_pair_necessary_lower_bound":math.pi+energy/n_active if n_active else None, | |
| "opacity_established":False, | |
| "arithmetic":"float64 evaluation of exact geometric formulas"} | |
| if certified_max_hits is not None: | |
| if not isinstance(certified_max_hits,int) or not 1<=certified_max_hits<=n_active: | |
| raise ValueError("Externally certified M must be an integer between 1 and the number of nondegenerate pieces") | |
| result.update(externally_assumed_max_hits=certified_max_hits, | |
| conditional_pair_necessary_lower_bound=math.pi+energy/certified_max_hits, | |
| max_hits_assumption_verified_by_this_code=False) | |
| return result | |