September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1,129 @@
* Closest Pair Problem 10/03/2017
CLOSEST CSECT
USING CLOSEST,R13 base register
B 72(R15) skip savearea
DC 17F'0' savearea
STM R14,R12,12(R13) save previous context
ST R13,4(R15) link backward
ST R15,8(R13) link forward
LR R13,R15 set addressability
LA R6,1 i=1
LA R7,2 j=2
BAL R14,DDCALC dd=(px(i)-px(j))^2+(py(i)-py(j))^2
BAL R14,DDSTORE ddmin=dd; ii=i; jj=j
LA R6,1 i=1
DO WHILE=(C,R6,LE,N) do i=1 to n
LA R7,1 j=1
DO WHILE=(C,R7,LE,N) do j=1 to n
BAL R14,DDCALC dd=(px(i)-px(j))^2+(py(i)-py(j))^2
IF CP,DD,GT,=P'0' THEN if dd>0 then
IF CP,DD,LT,DDMIN THEN if dd<ddmin then
BAL R14,DDSTORE ddmin=dd; ii=i; jj=j
ENDIF , endif
ENDIF , endif
LA R7,1(R7) j++
ENDDO , enddo j
LA R6,1(R6) i++
ENDDO , enddo i
ZAP WPD,DDMIN ddmin
DP WPD,=PL8'2' ddmin/2
ZAP SQRT2,WPD(8) sqrt2=ddmin/2
ZAP SQRT1,DDMIN sqrt1=ddmin
DO WHILE=(CP,SQRT1,NE,SQRT2) do while sqrt1<>sqrt2
ZAP SQRT1,SQRT2 sqrt1=sqrt2
ZAP WPD,DDMIN ddmin
DP WPD,SQRT1 /sqrt1
ZAP WP1,WPD(8) ddmin/sqrt1
AP WP1,SQRT1 +sqrt1
ZAP WPD,WP1 ~
DP WPD,=PL8'2' /2
ZAP SQRT2,WPD(8) sqrt2=(sqrt1+(ddmin/sqrt1))/2
ENDDO , enddo while
MVC PG,=CL80'the minimum distance '
ZAP WP1,SQRT2 sqrt2
BAL R14,EDITPK edit
MVC PG+21(L'WC),WC output
XPRNT PG,L'PG print buffer
XPRNT =CL22'is between the points:',22
MVC PG,PGP init buffer
L R1,II ii
SLA R1,4 *16
LA R4,PXY-16(R1) @px(ii)
MVC WP1,0(R4) px(ii)
BAL R14,EDITPK edit
MVC PG+3(L'WC),WC output
MVC WP1,8(R4) py(ii)
BAL R14,EDITPK edit
MVC PG+21(L'WC),WC output
XPRNT PG,L'PG print buffer
MVC PG,PGP init buffer
L R1,JJ jj
SLA R1,4 *16
LA R4,PXY-16(R1) @px(jj)
MVC WP1,0(R4) px(jj)
BAL R14,EDITPK edit
MVC PG+3(L'WC),WC output
MVC WP1,8(R4) py(jj)
BAL R14,EDITPK edit
MVC PG+21(L'WC),WC output
XPRNT PG,L'PG print buffer
L R13,4(0,R13) restore previous savearea pointer
LM R14,R12,12(R13) restore previous context
XR R15,R15 rc=0
BR R14 exit
DDCALC EQU * ---- dd=(px(i)-px(j))^2+(py(i)-py(j))^2
LR R1,R6 i
SLA R1,4 *16
LA R4,PXY-16(R1) @px(i)
LR R1,R7 j
SLA R1,4 *16
LA R5,PXY-16(R1) @px(j)
ZAP WP1,0(8,R4) px(i)
ZAP WP2,0(8,R5) px(j)
SP WP1,WP2 px(i)-px(j)
ZAP WPS,WP1 =
MP WP1,WPS (px(i)-px(j))*(px(i)-px(j))
ZAP WP2,8(8,R4) py(i)
ZAP WP3,8(8,R5) py(j)
SP WP2,WP3 py(i)-py(j)
ZAP WPS,WP2 =
MP WP2,WPS (py(i)-py(j))*(py(i)-py(j))
AP WP1,WP2 (px(i)-px(j))^2+(py(i)-py(j))^2
ZAP DD,WP1 dd=(px(i)-px(j))^2+(py(i)-py(j))^2
BR R14 ---- return
DDSTORE EQU * ---- ddmin=dd; ii=i; jj=j
ZAP DDMIN,DD ddmin=dd
ST R6,II ii=i
ST R7,JJ jj=j
BR R14 ---- return
EDITPK EQU * ----
MVC WM,MASK set mask
EDMK WM,WP1 edit and mark
BCTR R1,0 -1
MVC 0(1,R1),WM+17 set sign
MVC WC,WM len17<-len18
BR R14 ---- return
N DC A((PGP-PXY)/16)
PXY DC PL8'0.654682',PL8'0.925557',PL8'0.409382',PL8'0.619391'
DC PL8'0.891663',PL8'0.888594',PL8'0.716629',PL8'0.996200'
DC PL8'0.477721',PL8'0.946355',PL8'0.925092',PL8'0.818220'
DC PL8'0.624291',PL8'0.142924',PL8'0.211332',PL8'0.221507'
DC PL8'0.293786',PL8'0.691701',PL8'0.839186',PL8'0.728260'
PGP DC CL80' [+xxxxxxxxx.xxxxxx,+xxxxxxxxx.xxxxxx]'
MASK DC C' ',7X'20',X'21',X'20',C'.',6X'20',C'-' CL18 15num
II DS F
JJ DS F
DD DS PL8
DDMIN DS PL8
SQRT1 DS PL8
SQRT2 DS PL8
WP1 DS PL8
WP2 DS PL8
WP3 DS PL8
WPS DS PL8
WPD DS PL16
WM DS CL18
WC DS CL17
PG DS CL80
YREGS
END CLOSEST

View file

@ -1,16 +1,23 @@
import Data.List
import System.Random
import Control.Monad
import Control.Arrow
import Data.Ord
import Data.List (minimumBy, tails, unfoldr, foldl1') --'
vecLeng [[a,b],[p,q]] = sqrt $ (a-p)^2+(b-q)^2
import System.Random (newStdGen, randomRs)
findClosestPair = foldl1' ((minimumBy (comparing vecLeng). ). (. return). (:)) .
concatMap (\(x:xs) -> map ((x:).return) xs) . init . tails
import Control.Arrow ((&&&))
import Data.Ord (comparing)
vecLeng [[a, b], [p, q]] = sqrt $ (a - p) ^ 2 + (b - q) ^ 2
findClosestPair =
foldl1'' ((minimumBy (comparing vecLeng) .) . (. return) . (:)) .
concatMap (\(x:xs) -> map ((x :) . return) xs) . init . tails
testCP = do
g <- newStdGen
let pts :: [[Double]]
pts = take 1000. unfoldr (Just. splitAt 2) $ randomRs(-1,1) g
print . (id &&& vecLeng ) . findClosestPair $ pts
g <- newStdGen
let pts :: [[Double]]
pts = take 1000 . unfoldr (Just . splitAt 2) $ randomRs (-1, 1) g
print . (id &&& vecLeng) . findClosestPair $ pts
main = testCP
foldl1'' = foldl1'

View file

@ -0,0 +1,16 @@
function closestpair(P::Vector{Vector{T}}) where T <: Number
N = length(P)
if N < 2 return (Inf, ()) end
mindst = norm(P[1] - P[2])
minpts = (P[1], P[2])
for i in 1:N-1, j in i+1:N
tmpdst = norm(P[i] - P[j])
if tmpdst < mindst
mindst = tmpdst
minpts = (P[i], P[j])
end
end
return mindst, minpts
end
closestpair([[0, -0.3], [1., 1.], [1.5, 2], [2, 2], [3, 3]])

View file

@ -0,0 +1,79 @@
// version 1.1.2
typealias Point = Pair<Double, Double>
fun distance(p1: Point, p2: Point) = Math.hypot(p1.first- p2.first, p1.second - p2.second)
fun bruteForceClosestPair(p: List<Point>): Pair<Double, Pair<Point, Point>> {
val n = p.size
if (n < 2) throw IllegalArgumentException("Must be at least two points")
var minPoints = p[0] to p[1]
var minDistance = distance(p[0], p[1])
for (i in 0 until n - 1)
for (j in i + 1 until n) {
val dist = distance(p[i], p[j])
if (dist < minDistance) {
minDistance = dist
minPoints = p[i] to p[j]
}
}
return minDistance to Pair(minPoints.first, minPoints.second)
}
fun optimizedClosestPair(xP: List<Point>, yP: List<Point>): Pair<Double, Pair<Point, Point>> {
val n = xP.size
if (n <= 3) return bruteForceClosestPair(xP)
val xL = xP.take(n / 2)
val xR = xP.drop(n / 2)
val xm = xP[n / 2 - 1].first
val yL = yP.filter { it.first <= xm }
val yR = yP.filter { it.first > xm }
val (dL, pairL) = optimizedClosestPair(xL, yL)
val (dR, pairR) = optimizedClosestPair(xR, yR)
var dmin = dR
var pairMin = pairR
if (dL < dR) {
dmin = dL
pairMin = pairL
}
val yS = yP.filter { Math.abs(xm - it.first) < dmin }
val nS = yS.size
var closest = dmin
var closestPair = pairMin
for (i in 0 until nS - 1) {
var k = i + 1
while (k < nS && (yS[k].second - yS[i].second < dmin)) {
val dist = distance(yS[k], yS[i])
if (dist < closest) {
closest = dist
closestPair = Pair(yS[k], yS[i])
}
k++
}
}
return closest to closestPair
}
fun main(args: Array<String>) {
val points = listOf(
listOf(
5.0 to 9.0, 9.0 to 3.0, 2.0 to 0.0, 8.0 to 4.0, 7.0 to 4.0,
9.0 to 10.0, 1.0 to 9.0, 8.0 to 2.0, 0.0 to 10.0, 9.0 to 6.0
),
listOf(
0.654682 to 0.925557, 0.409382 to 0.619391, 0.891663 to 0.888594,
0.716629 to 0.996200, 0.477721 to 0.946355, 0.925092 to 0.818220,
0.624291 to 0.142924, 0.211332 to 0.221507, 0.293786 to 0.691701,
0.839186 to 0.728260
)
)
for (p in points) {
val (dist, pair) = bruteForceClosestPair(p)
println("Closest pair (brute force) is ${pair.first} and ${pair.second}, distance $dist")
val xP = p.sortedBy { it.first }
val yP = p.sortedBy { it.second }
val (dist2, pair2) = optimizedClosestPair(xP, yP)
println("Closest pair (optimized) is ${pair2.first} and ${pair2.second}, distance $dist2\n")
}
}

View file

@ -0,0 +1,15 @@
class Point{
fcn init(_x,_y){ var[const] x=_x, y=_y; }
fcn distance(p){ (p.x-x).hypot(p.y-y) }
fcn toString { String("Point(",x,",",y,")") }
}
// find closest two points using brute ugly force:
// find all combinations of two points, measure distance, pick smallest
fcn closestPoints(points){
pairs:=Utils.Helpers.pickNFrom(2,points);
triples:=pairs.apply(fcn([(p1,p2)]){ T(p1,p2,p1.distance(p2)) });
triples.reduce(fcn([(_,_,d1)]p1,[(_,_,d2)]p2){
if(d1 < d2) p1 else p2
});
}

View file

@ -0,0 +1,15 @@
points:=T( 5.0, 9.0, 9.0, 3.0,
2.0, 0.0, 8.0, 4.0,
7.0, 4.0, 9.0, 10.0,
1.0, 9.0, 8.0, 2.0,
0.0, 10.0, 9.0, 6.0 ).pump(List,Void.Read,Point);
closestPoints(points).println(); //-->L(Point(8,4),Point(7,4),1)
points:=T( 0.654682, 0.925557, 0.409382, 0.619391,
0.891663, 0.888594, 0.716629, 0.9962,
0.477721, 0.946355, 0.925092, 0.81822,
0.624291, 0.142924, 0.211332, 0.221507,
0.293786, 0.691701, 0.839186, 0.72826)
.pump(List,Void.Read,Point);
closestPoints(points).println();