Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -4,10 +4,11 @@ MsgBox, % MeanAngle(Angles[1]) "`n"
. MeanAngle(Angles[3])
MeanAngle(a, x=0, y=0) {
c := ATan(1) / 45
for k, v in a
static c := ATan(1) / 45
for k, v in a {
x += Cos(v * c) / a.MaxIndex()
, y += Sin(v * c) / a.MaxIndex()
y += Sin(v * c) / a.MaxIndex()
}
return atan2(x, y) / c
}

View file

@ -1,10 +1,10 @@
import std.stdio, std.algorithm, std.complex;
import std.math: PI;
auto radians(T)(in T d) pure nothrow { return d * PI / 180; }
auto degrees(T)(in T r) pure nothrow { return r * 180 / PI; }
auto radians(T)(in T d) pure nothrow @nogc { return d * PI / 180; }
auto degrees(T)(in T r) pure nothrow @nogc { return r * 180 / PI; }
real meanAngle(T)(in T[] D) pure nothrow {
real meanAngle(T)(in T[] D) pure nothrow @nogc {
immutable t = reduce!((a, d) => a + d.radians.expi)(0.complex, D);
return (t / D.length).arg.degrees;
}

View file

@ -0,0 +1,26 @@
include std/console.e
include std/mathcons.e
sequence AngleList = {{350,10},{90,180,270,360},{10,20,30}}
function atan2(atom y, atom x)
return 2*arctan((sqrt(power(x,2)+power(y,2)) - x)/y)
end function
function MeanAngle(sequence angles)
atom x = 0, y = 0
integer l = length(angles)
for i = 1 to length(angles) do
x += cos(angles[i] * PI / 180)
y += sin(angles[i] * PI / 180)
end for
return atan2(y / l, x / l) * 180 / PI
end function
for i = 1 to length(AngleList) do
printf(1,"Mean Angle for set %d: %3.5f\n",{i,MeanAngle(AngleList[i])})
end for
if getc(0) then end if

View file

@ -1,22 +1,28 @@
package main
import (
"fmt"
"math"
"math/cmplx"
"fmt"
"math"
"math/cmplx"
)
func deg2rad(d float64) float64 { return d * math.Pi / 180 }
func rad2deg(r float64) float64 { return r * 180 / math.Pi }
func mean_angle(deg []float64) float64 {
sum := 0i
for _, x := range deg { sum += cmplx.Rect(1, deg2rad(x)) }
return rad2deg(cmplx.Phase(sum))
sum := 0i
for _, x := range deg {
sum += cmplx.Rect(1, deg2rad(x))
}
return rad2deg(cmplx.Phase(sum))
}
func main() {
for _, angles := range [][]float64 {{350, 10}, {90, 180, 270, 360}, {10, 20, 30}} {
fmt.Printf("The mean angle of %v is: %f degrees\n", angles, mean_angle(angles))
}
for _, angles := range [][]float64{
{350, 10},
{90, 180, 270, 360},
{10, 20, 30},
} {
fmt.Printf("The mean angle of %v is: %f degrees\n", angles, mean_angle(angles))
}
}

View file

@ -1,9 +1,9 @@
func mean_angle(deg []float64) float64 {
var ss, sc float64
for _, x := range deg {
s, c := math.Sincos(x * math.Pi / 180)
ss += s
sc += c
}
return math.Atan2(ss, sc) * 180 / math.Pi
var ss, sc float64
for _, x := range deg {
s, c := math.Sincos(x * math.Pi / 180)
ss += s
sc += c
}
return math.Atan2(ss, sc) * 180 / math.Pi
}

View file

@ -0,0 +1,17 @@
-- file: trigdeg.fs
deg2rad deg = deg*pi/180.0
rad2deg rad = rad*180.0/pi
sind = sin . deg2rad
cosd = cos . deg2rad
tand = tan . deg2rad
atand = rad2deg . atan
atan2d y x = rad2deg (atan2 y x )
avg_angle angles = atan2d y x
where
y = mean (map sind angles)
x = mean (map cosd angles)
-- End of trigdeg.fs --------

View file

@ -0,0 +1,13 @@
to mean_angle :angles
local "avgsin
make "avgsin quotient apply "sum map "sin :angles count :angles
local "avgcos
make "avgcos quotient apply "sum map "cos :angles count :angles
output (arctan :avgcos :avgsin)
end
foreach [[350 10] [90 180 270 360] [10 20 30]] [
print (sentence [The average of \(] ? [\) is] (mean_angle ?))
]
bye

View file

@ -0,0 +1,71 @@
program MeanAngle;
{$IFDEF DELPHI}
{$APPTYPE CONSOLE}
{$ENDIF}
uses
math;// sincos and atan2
type
tAngles = array of double;
function MeanAngle(const a:tAngles;cnt:longInt):double;
// calculates mean angle.
// returns 0.0 if direction is not sure.
const
eps = 1e-10;
var
i : LongInt;
s,c,
Sumsin,SumCos : extended;
begin
IF cnt = 0 then
Begin
MeanAngle := 0.0;
EXIT;
end;
SumSin:= 0;
SumCos:= 0;
For i := Cnt-1 downto 0 do
Begin
sincos(DegToRad(a[i]),s,c);
Sumsin := sumSin+s;
SumCos := sumCos+c;
end;
s := SumSin/cnt;
c := sumCos/cnt;
IF c > eps then
MeanAngle := RadToDeg(arctan2(s,c))
else
// Not meaningful
MeanAngle := 0.0;
end;
Procedure OutMeanAngle(const a:tAngles;cnt:longInt);
var
i : longInt;
Begin
IF cnt > 0 then
Begin
write('The mean angle of [');
For i := 0 to Cnt-2 do
write(a[i]:0:2,',');
write(a[Cnt-1]:0:2,'] => ');
writeln(MeanAngle(a,cnt):0:16);
end;
end;
var
a:tAngles;
Begin
setlength(a,4);
a[0] := 350;a[1] := 10;
OutMeanAngle(a,2);
a[0] := 90;a[1] := 180;a[2] := 270;a[3] := 360;
OutMeanAngle(a,4);
a[0] := 10;a[1] := 20;a[2] := 30;
OutMeanAngle(a,3);
setlength(a,0);
end.

View file

@ -0,0 +1,17 @@
sub Pi () { 3.1415926535897932384626433832795028842 }
sub meanangle {
my($x, $y) = (0,0);
($x,$y) = ($x + sin($_), $y + cos($_)) for @_;
my $atan = atan2($x,$y);
$atan += 2*Pi while $atan < 0; # Ghetto fmod
$atan -= 2*Pi while $atan > 2*Pi;
$atan;
}
sub meandegrees {
meanangle( map { $_ * Pi/180 } @_ ) * 180/Pi;
}
print "The mean angle of [@$_] is: ", meandegrees(@$_), " degrees\n"
for ([350,10], [90,180,270,360], [10,20,30]);

View file

@ -1,57 +1,56 @@
/*REXX program to compute the mean angle (angles expressed in degrees).*/
numeric digits 50 /*use fifty digits of precision. */
showDigs=10 /* ... but only show ten digits. */
#=350 10 ; say showit(#, meanAngleD(#))
#=90 180 270 360 ; say showit(#, meanAngleD(#))
#=10 20 30 ; say showit(#, meanAngleD(#))
/*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.*/
# = 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; arg x; _sin=0; _cos=0
numeric digits $d()+$d()%4
n=words(x); do j=1 for n
xr = d2r(d2d(word(x,j)))
_sin = _sin + sin(xr)
_cos = _cos + cos(xr)
end /*j*/
return r2d(atan2(_sin/n, _cos/n))
/*═════════════════════════════general 1-line subs══════════════════════*/
/*The 1-line subs were broken up into multiple lines for easier reading.*/
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)//(2*pi())
p: return word(arg(1),1)
$d: return p(arg(1) digits())
$fuzz: return min(arg(1),max(1,$d()-arg(2)))
showit: procedure expose showDigs; numeric digits showDigs; arg a,ma
return left('angles='a,30) 'mean angle=' format(ma,,showDigs,0)/1
acos: procedure; arg x; if x<-1 | x>1 then call $81r -1,1,x,"ACOS"
return .5*pi() - asin(x) /* $81r subroutine not included here.*/
atan: if abs(arg(1))=1 then return pi()*.25*sign(arg(1))
return asin(arg(1) / sqrt(1+arg(1)**2))
asin: procedure; arg x; if x<-1 | x>1 then call $81r -1,1,x,"ASIN"
s=x*x; if abs(x)>=.7 then return sign(x)*acos(sqrt(1-s),'-ASIN')
z=x; o=x; p=z; do j=2 by 2; o=o*s*(j-1)/j; z=z+o/(j+1)
if z=p then leave; p=z; end; return z
atan2: procedure; arg y,x; pi=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)));otherwise z=s*atan(y/x);end; return z
cos: procedure; arg x; x=r2r(x); a=abs(x); numeric fuzz $fuzz(5,3)
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=2*pi()/3 then return -.5; return .sincos(1,1,-1)
sin: procedure; 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)
.sincos: parse arg z,_,i; x=x*x; p=z; do k=2 by 2; _=-_*x/(k*(k+i)); z=z+_
if z=p then leave; p=z; end; return z
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits()
numeric digits 11; g=.sqrtGuess(); do j=0 while p>9;m.j=p; p=p%2+1
end; do k=j+5 to 0 by -1; if m.k>11 then numeric digits m.k
g=.5*(g+x/g); end; numeric digits d; return g/1
.sqrtGuess: if x<0 then call er 02,x p($.ff 'SQRT') "negative"
/*The above IF statement was left here to show checking for x<0.*/
numeric form; m.=11; p=d+d%4+2
parse value format(x,2,1,,0) 'E0' with g 'E' _ .;return g*.5'E'_%2
pi: return ,
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148
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──────────────────────────────────────────*/
.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)
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)
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
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───────────────────*/
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)))
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──────────────────*/
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

View file

@ -0,0 +1,19 @@
trait MeanAnglesComputation {
import scala.math.{Pi, atan2, cos, sin}
def meanAngle(angles: List[Double], convFactor: Double = 180.0 / Pi) = {
val sums = angles.foldLeft((.0, .0))((r, c) => {
val rads = c / convFactor
(r._1 + sin(rads), r._2 + cos(rads))
})
val result = atan2(sums._1, sums._2)
(result + (if (result.signum == -1) 2 * Pi else 0.0)) * convFactor
}
}
object MeanAngles extends App with MeanAnglesComputation {
assert(meanAngle(List(350, 10), 180.0 / math.Pi).round == 360, "Unexpected result with 350, 10")
assert(meanAngle(List(90, 180, 270, 360)).round == 270, "Unexpected result with 90, 180, 270, 360")
assert(meanAngle(List(10, 20, 30)).round == 20, "Unexpected result with 10, 20, 30")
println("Successfully completed without errors.")
}