Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
12
Task/Averages-Mean-angle/Clojure/averages-mean-angle-1.clj
Normal file
12
Task/Averages-Mean-angle/Clojure/averages-mean-angle-1.clj
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
(defn mean-fn
|
||||
[k coll]
|
||||
(let [n (count coll)
|
||||
trig (get {:sin #(Math/sin %) :cos #(Math/cos %)} k)]
|
||||
(* (/ 1 n) (reduce + (map trig coll)))))
|
||||
|
||||
(defn mean-angle
|
||||
[degrees]
|
||||
(let [radians (map #(Math/toRadians %) degrees)
|
||||
a (mean-fn :sin radians)
|
||||
b (mean-fn :cos radians)]
|
||||
(Math/toDegrees (Math/atan2 a b))))
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
(mean-angle [350 10])
|
||||
;=> -1.614809932057922E-15
|
||||
|
||||
(mean-angle [90 180 270 360])
|
||||
;=> -90.0
|
||||
|
||||
(mean-angle [10 20 30])
|
||||
;=> 19.999999999999996
|
||||
|
|
@ -1 +1 @@
|
|||
avgAngleD=: (_1 { [: (**|)&.+.@(+/ % #)&.(*.inv) 1,.])&.(1r180p1&*)
|
||||
avgAngleD=: 360|(_1 { [: (**|)&.+.@(+/ % #)&.(*.inv) 1,.])&.(1r180p1&*)
|
||||
|
|
|
|||
|
|
@ -3,4 +3,4 @@ toComplex=: *.inv NB. maps integer pairs
|
|||
mean=: +/ % # NB. calculate arithmetic mean
|
||||
roundComplex=: (* * |)&.+. NB. discard an extraneous least significant bit of precision from a complex value whose magnitude is in the vicinity of 1
|
||||
avgAngleR=: _1 { [: roundComplex@mean&.toComplex 1 ,. ] NB. calculate average angle in radians
|
||||
avgAngleD=: avgAngleR&.rfd
|
||||
avgAngleD=: 360|avgAngleR&.rfd NB. calculate average angle in degrees
|
||||
|
|
|
|||
|
|
@ -4,3 +4,7 @@
|
|||
0
|
||||
avgAngleD 10 20 30
|
||||
20
|
||||
avgAngleD 20 350
|
||||
5
|
||||
avgAngleD 10 340
|
||||
355
|
||||
|
|
|
|||
18
Task/Averages-Mean-angle/JavaScript/averages-mean-angle.js
Normal file
18
Task/Averages-Mean-angle/JavaScript/averages-mean-angle.js
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
function sum(a) {
|
||||
s = 0;
|
||||
for (var i in a) s += a[i];
|
||||
return s;
|
||||
}
|
||||
|
||||
function degToRad(a) {
|
||||
return Math.PI/180*a;
|
||||
}
|
||||
|
||||
function meanAngleDeg(a) {
|
||||
return 180/Math.PI*Math.atan2(sum(a.map(degToRad).map(Math.sin))/a.length,sum(a.map(degToRad).map(Math.cos))/a.length);
|
||||
}
|
||||
|
||||
var a = [350, 10], b = [90, 180, 270, 360], c =[10, 20, 30];
|
||||
console.log(meanAngleDeg(a));
|
||||
console.log(meanAngleDeg(b));
|
||||
console.log(meanAngleDeg(c));
|
||||
13
Task/Averages-Mean-angle/Lua/averages-mean-angle.lua
Normal file
13
Task/Averages-Mean-angle/Lua/averages-mean-angle.lua
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
function meanAngle (angleList)
|
||||
local sumSin, sumCos = 0, 0
|
||||
for i, angle in pairs(angleList) do
|
||||
sumSin = sumSin + math.sin(math.rad(angle))
|
||||
sumCos = sumCos + math.cos(math.rad(angle))
|
||||
end
|
||||
local result = math.deg(math.atan2(sumSin, sumCos))
|
||||
return string.format("%.2f", result)
|
||||
end
|
||||
|
||||
print(meanAngle({350, 10}))
|
||||
print(meanAngle({90, 180, 270, 360}))
|
||||
print(meanAngle({10, 20, 30}))
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
MODULE MeanAngle;
|
||||
IMPORT
|
||||
M := LRealMath,
|
||||
Out;
|
||||
CONST
|
||||
toRads = M.pi / 180;
|
||||
toDegs = 180 / M.pi;
|
||||
VAR
|
||||
grades: ARRAY 64 OF LONGREAL;
|
||||
|
||||
PROCEDURE Mean(g: ARRAY OF LONGREAL): LONGREAL;
|
||||
VAR
|
||||
i: INTEGER;
|
||||
sumSin, sumCos: LONGREAL;
|
||||
BEGIN
|
||||
i := 0;sumSin := 0.0;sumCos := 0.0;
|
||||
WHILE g[i] # 0.0 DO
|
||||
sumSin := sumSin + M.sin(g[i] * toRads);
|
||||
sumCos := sumCos + M.cos(g[i] * toRads);
|
||||
INC(i)
|
||||
END;
|
||||
RETURN M.arctan2(sumSin / i,sumCos / i);
|
||||
END Mean;
|
||||
|
||||
BEGIN
|
||||
grades[0] := 350.0;grades[1] := 10.0;
|
||||
Out.LongRealFix(Mean(grades) * toDegs,15,9);Out.Ln;
|
||||
grades[0] := 90.0;grades[1] := 180.0;grades[2] := 270.0;grades[3] := 360.0;
|
||||
Out.LongRealFix(Mean(grades) * toDegs,15,9);Out.Ln;
|
||||
grades[0] := 10.0;grades[1] := 20.0;grades[2] := 30.0;grades[3] := 0.0;
|
||||
Out.LongRealFix(Mean(grades) * toDegs,15,9);Out.Ln;
|
||||
END MeanAngle.
|
||||
|
|
@ -1,56 +1,57 @@
|
|||
/*REXX program computes the mean angle (angles expressed in degrees). */
|
||||
numeric digits 50 /*use fifty digits of precision, */
|
||||
showDig=10 /*··· but only display 10 digits.*/
|
||||
/*REXX program computes the mean angle (the angles expressed in degrees). */
|
||||
numeric digits 50 /*use 50 decimal digits of precision,*/
|
||||
showDig=10 /* but only display ten decimal digits.*/
|
||||
# = 350 10 ; say showit(#, meanAngleD(#) )
|
||||
# = 90 180 270 360 ; say showit(#, meanAngleD(#) )
|
||||
# = 10 20 30 ; say showit(#, meanAngleD(#) )
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────MEANANGD subroutine─────────────────*/
|
||||
meanAngleD: procedure; parse arg x; numeric digits digits()+digits()%4
|
||||
_sin=0; _cos=0; n=words(x); do j=1 for n; !=d2r(word(x,j))
|
||||
_sin = _sin + sin(!)
|
||||
_cos = _cos + cos(!)
|
||||
end /*j*/
|
||||
return r2d(atan2(_sin/n, _cos/n))
|
||||
/*─────────────────────────────one─line subroutines──────────────────────────────────────────*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────subroutines──────────────────────────────────────────────*/
|
||||
.sinCos: arg z,_,i; x=x*x; do k=2 by 2 until p=z; p=z; _=-_*x/(k*(k+i)); z=z+_; end; return z
|
||||
$fuzz: return min(arg(1), max(1, digits() - arg(2) ) )
|
||||
acos: procedure; parse arg x; return pi() * .5 - asin(x)
|
||||
acos: procedure; parse arg x; return pi() * .5 - asin(x)
|
||||
atan: parse arg x; if abs(x)=1 then return pi()*.25 * sign(x); return asin(x/sqrt(1 + x*x))
|
||||
d2d: return arg(1) // 360
|
||||
d2r: return r2r(d2d(arg(1)) / 180 * pi() )
|
||||
r2d: return d2d((r2r(arg(1)) / pi()) * 180)
|
||||
r2r: return arg(1) // (pi() * 2)
|
||||
d2r: return r2r(d2d(arg(1)) / 180 * pi() )
|
||||
r2d: return d2d((r2r(arg(1)) / pi()) * 180)
|
||||
r2r: return arg(1) // (pi() * 2)
|
||||
p: return word(arg(1), 1)
|
||||
pi: pi=3.1415926535897932384626433832795028841971693993751058209749445923078164062862;return pi
|
||||
/*───────────────────────────────────ASIN subroutine────────────────────*/
|
||||
asin: procedure; parse arg x 1 z 1 o 1 p; xx=x*x
|
||||
|
||||
asin: procedure; parse arg x 1 z 1 o 1 p; xx=x*x
|
||||
if xx>=.5 then return sign(x) * acos(sqrt(1-xx))
|
||||
do j=2 by 2 until p=z; p=z; o=o*xx*(j-1)/j; z=z+o/(j+1); end
|
||||
return z /* [↑] compute until no noise.*/
|
||||
/*───────────────────────────────────ATAN2 subroutine───────────────────*/
|
||||
do j=2 by 2 until p=z; p=z; o=o*xx*(j-1)/j; z=z+o/(j+1); end
|
||||
return z /* [↑] compute until no more noise. */
|
||||
|
||||
atan2: procedure; parse arg y,x; call pi; s=sign(y)
|
||||
select
|
||||
when x=0 then z=s * pi * .5
|
||||
when x<0 then if y=0 then z=pi; else z=s*(pi-abs(atan(y/x)))
|
||||
when x=0 then z=s * pi * .5
|
||||
when x<0 then if y=0 then z=pi; else z=s*(pi-abs(atan(y/x)))
|
||||
otherwise z=s * atan(y/x)
|
||||
end /*select*/; return z
|
||||
/*───────────────────────────────────COS subroutine─────────────────────*/
|
||||
cos: procedure; parse arg x; x=r2r(x); numeric fuzz $fuzz(5, 3)
|
||||
a=abs(x); if a=0 then return 1; if a=pi then return -1
|
||||
if a=pi*.5 | a=pi*1.5 then return 0; if a=pi/3 then return .5
|
||||
if a=pi*2/3 then return -.5; return .sinCos(1, 1, -1)
|
||||
/*───────────────────────────────────SHOWIT subroutine──────────────────*/
|
||||
|
||||
cos: procedure; parse arg x; x=r2r(x); numeric fuzz $fuzz(6, 3)
|
||||
a=abs(x); if a=0 then return 1; if a=pi then return -1
|
||||
if a=pi*.5 | a=pi*1.5 then return 0; if a=pi/3 then return .5
|
||||
if a=pi*2/3 then return -.5; return .sinCos(1, 1, -1)
|
||||
|
||||
|
||||
meanAngleD: procedure; parse arg x; numeric digits digits()+digits()%4
|
||||
_sin=0; _cos=0; n=words(x); do j=1 for n; !=d2r(word(x,j))
|
||||
_sin=_sin + sin(!)
|
||||
_cos=_cos + cos(!)
|
||||
end /*j*/
|
||||
return r2d(atan2(_sin/n, _cos/n))
|
||||
|
||||
showit: procedure expose showDig; numeric digits showDig; parse arg a,mA
|
||||
return left('angles='a,30) 'mean angle=' format(mA,,showDig,0)/1
|
||||
/*───────────────────────────────────COS subroutine─────────────────────*/
|
||||
|
||||
sin: procedure; parse arg x; x=r2r(x); numeric fuzz $fuzz(5, 3)
|
||||
if x=pi*.5 then return 1; if x==pi*1.5 then return -1
|
||||
if abs(x)=pi | x=0 then return 0; return .sinCos(x, x, +1)
|
||||
/*───────────────────────────────────SQRT subroutine────────────────────*/
|
||||
sqrt: procedure; parse arg x,i; if x=0 then return 0; d=digits(); m.=11
|
||||
if x<0 then i='i'; numeric digits 11; numeric form; p=d+d%4+2
|
||||
parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g=g*.5'E'_%2
|
||||
do j=0 while p>9; m.j=p; p=p%2+1; end /*j*/
|
||||
do k=j+5 to 0 by -1; if m.k>11 then numeric digits m.k
|
||||
g=.5*(g+x/g); end /*k*/; numeric digits d; return g/1
|
||||
|
||||
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); i=; m.=9
|
||||
numeric digits 9; numeric form; h=d+6; if x<0 then do; x=-x; i='i'; end
|
||||
parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g=g*.5'e'_%2
|
||||
do j=0 while h>9; m.j=h; h=h%2+1; end /*j*/
|
||||
do k=j+5 to 0 by -1; numeric digits m.k; g=(g+x/g)*.5; end /*k*/
|
||||
numeric digits d; return (g/1)i /*make complex if X < 0.*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue