
NB. Unified Inch Screw Threads per ASME B1.1-2003
NB. UN Class 3 Threads
NB.
NB. --- Tolerances ---
NB. EXTERNAL THREADS
NB. Major Dia Tol: eDt  = 0.060*P^(2/3)
NB. Pitch Dia Tol: ePDt = 0.750*(0.0015*D^(1/3)+0.0015*LE^(1/2)+0.015*P^(2/3))
NB. Minor Dia Tol: UN:  eDmt  = ePDt + 0.21650635P (layout and check)
NB. 			   UNR: eDmtr = ePDt + 0.10825318P (layout and check)
NB.
NB. INTERNAL THREADS
NB. Major Dia Tol: iDt  = 0.14433757P + iPDt
NB. Pitch Dia Tol: iPDt = 0.975*ePDt
NB. Minor Dia Tol: iDmt = [0.05*P^(2/3)+0.03*P/D]-0.002
NB.                  iDmt = min(0.394*P, iDmt)
NB.                  for n = [13..80]: iDmt = max(0.23*P-1.5P^2, iDmt)
NB.                  for n =  [0..12]: iDmt = max(0.120*P, iDmt)
NB.
NB. --- Limits of Size ---
NB. EXTERNAL THREADS
NB. Max Major Dia: eDmx = D
NB. Min Major Dia: eDmn = D - eDt
NB.
NB. Max Pitch Dia: ePDmx = PD
NB. Min Pitch Dia: ePDmn = PD - ePDt
NB.
NB. Max Minor Dia: UN:  eDmmx = Dm
NB.                UNR: eDmmx = Dm - H/8
NB. Min Minor Dia: eDmmn = ePDmn - 0.64951905*P
NB.
NB. INTERNAL THREADS
NB. Max Major Dia: iDmx = Compute from Fig 3
NB. Min Major Dia: iDmn = D
NB.
NB. Max Pitch Dia: iPDmx = PD + iPDt
NB. Min Pitch Dia: iPDmn = PD 
NB.
NB. Max Minor Dia: iDmmx = iDmmn + iDmt (round to 4 decimal places)
NB. Min Minor Dia: iDmmn = iPDmn - 0.43301122*P
NB.   (rnd to 3 decimal places for D >= 0.138 and add a zero)
NB.
NB. --- Length of Engagement ---
NB. UNC, UNF:   LE = D
NB. 4, 6, 8-UN: LE = D
NB. UNEF, 12, 16, 20, 28, 32-UN: LE = 9P
NB. UNS: LE = 9P

thd =: 3 : 0
D =: 0 { y
n =: 1 { y
P =: %n
PD =: D - +: 0.32475953*P

NB. Vector of basic diameters
vD=: 0.06 0.073 0.086 0.099 0.112 0.125 0.138 0.164 0.19 0.216 0.25 0.3125 0.375 0.4375 0.5 0.5625 0.625 0.6875 0.75 0.8125 0.875 0.9375 1 1.0625 1.125 1.1875 1.25 1.3125 1.375 1.4375 1.5 1.5625 1.625 1.6875 1.75 1.8125 4.875 5 5.125 5.25 5.375 5.5 5.625 5.75 5.875 6
UNC=: 0 64 56 48 40 40 32 32 24 24 20 18 16 14 13 12 11 0 10 0 9 0 8 0 7 0 7 0 6 0 6 0 0 0 5 0 0 0 0 0 0 0 0 0 0 0
UNF=: 80 72 64 56 48 44 40 36 32 28 28 24 24 20 20 0 18 0 16 0 14 0 12 0 12 0 12 0 12 0 12 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0


NB. --- Length of Engagement ---
NB. UNC, UNF:   LE = D
NB. 4, 6, 8-UN: LE = D
NB. UNEF, 12, 16, 20, 28, 32-UN: LE = 9P
NB. UNS: LE = 9P
LE =: 9*P						NB. Replace with call to a verb

NB. --- Tolerances ---
NB. EXTERNAL THREADS
eDt  =: 0.060*P^2r3				NB. Major Dia Tol
ePDt =: 0.750*(0.0015*D^(%3))+(0.0015*%:LE)+0.015*P^2r3		NB. Pitch Dia Tol
eDmt  =: ePDt + 0.21650635*P	NB. Minor Dia Tol: UN:   (layout & check)
eDmtr =: ePDt + 0.10825318*P	NB. UNR:  (layout & check)

NB. INTERNAL THREADS
iPDt =: 0.975*ePDt							NB. Pitch Dia Tol
iDt =: iPDt + 0.14433757*P					NB. Major Dia Tol
iDmt =: _0.002 + (0.05*P^2r3)+0.03*P%D		NB. Minor Dia Tol
iDmt =: iDmt <. 0.394*P						NB. Minimum of

if. n <: 12 do.
  iDmt =: iDmt >. 0.120*P					NB. for n = [0..12] maximum of
else.
 iDmt =: iDmt >. (0.23*P)-1.5* *:P			NB. for n = [13..80]
end.

NB. --- Limits of Size ---
NB. EXTERNAL THREADS
eDmx =: D									NB. Max Major Dia
eDmn =: D - eDt								NB. Min Major Dia

ePDmx =: PD									NB. Max Pitch Dia
ePDmn =: PD - ePDt							NB. Min Pitch Dia

eDmmx =: Dm									NB. Max Minor Dia: UN
NB. eDmmxr =: Dm - H/8							NB. UNR 

eDmmn =: ePDmn - 0.64951905*P				NB. Min Minor Dia

NB. INTERNAL THREADS
iPDmx =: PD + iPDt							NB. Max Pitch Dia
iPDmn =: PD									NB. Min Pitch Dia

iDmn =: D									NB. Min Major Dia
iDmx =: iPDmx + (P * %:3)*0.500 - 0.041667	NB. Max Major Dia

iDmmn =: iPDmn - 0.43301122*P	NB. Min Minor Dia
iDmmx =: iDmmn + iDmt 			NB. Max Minor Dia (round to 4 decimal places)
NB.   (rnd to 3 decimal places for D >= 0.138 and add a zero)
)


NB. Convert a quad word (8 bytes) string from disk to a vector of doubles 
qwtod =: _2 & (3!:5)

NB. Convert a vector of doubles to a qword string for writing to disk.
dtoqw =: 2 & (3!:5)

NB. Convert every double word (4 bytes) to a 32 bit signed int  
dwtoi =: _2 & (3!:4)

NB. Convert 32 bit signed int to dword data for writing to disk.
itodw =: 2 & (3!:4)

NB. Convert every quad word (8 bytes) to a 64 bit signed int  
qwtoi =: _3 & (3!:4)

NB. Convert 64 bit signed int to qword data for writing to disk.
itoqw =: 3 & (3!:4)


require 'files'

prime_ascii_to_int32 =: 3 : 0
F=. 'C:\Users\DE51225\Documents\DEE\projecteuler\primes\data\primes'
F=. F, ": y
p1s=: fread F,'.txt'
p1s_no_nl=: p1s -. CR,LF
p1=: ". p1s_no_nl
(itodw p1) fwrite F,'.int32'
)

require 'plot'

Fsin=:'C:\Users\DE51225\Documents\DEE\CS\edwards\soln\wk11\sin_tbl_fsin.dbl'

NB. Read in a sine table and convert to a
NB. vector of doubles
fsin =: qwtod fread Fsin
dx =: 2p1%(<:$fsin)
fx =: +/\ _1p1,(<:$fsin)$dx
NB. plot fx; fsin

NB. diff - Difference between adjacent elements in vector <y>
NB. diff[i] =: y[i+1] - y[i]
NB. Shape of result is one less than shape of <y>
diff =: 3 : '(}. y) - }:y'
