langs a-z

This commit is contained in:
Ingy döt Net 2013-04-10 22:43:41 -07:00
parent db842d013d
commit d066446780
11389 changed files with 98361 additions and 1020 deletions

View file

@ -0,0 +1,25 @@
let pi = 3.14159_26535_89793_23846_2643
let deg2rad d =
d *. pi /. 180.0
let rad2deg r =
r *. 180.0 /. pi
let mean_angle angles =
let rad_angles = List.map deg2rad angles in
let sum_sin = List.fold_left (fun sum a -> sum +. sin a) 0.0 rad_angles
and sum_cos = List.fold_left (fun sum a -> sum +. cos a) 0.0 rad_angles
in
rad2deg (atan2 sum_sin sum_cos)
let test angles =
Printf.printf "The mean angle of [%s] is: %g degrees\n"
(String.concat "; " (List.map (Printf.sprintf "%g") angles))
(mean_angle angles)
let () =
test [350.0; 10.0];
test [90.0; 180.0; 270.0; 360.0];
test [10.0; 20.0; 30.0];
;;

View file

@ -0,0 +1,7 @@
open Complex
let mean_angle angles =
let sum =
List.fold_left (fun sum a -> add sum (polar 1.0 (deg2rad a))) zero angles
in
rad2deg (arg sum)

View file

@ -0,0 +1,3 @@
meanAngle(v)=atan(sum(i=1,#v,sin(v[i]))/sum(i=1,#v,cos(v[i])))%(2*Pi)
meanDegrees(v)=meanAngle(v*Pi/180)*180/Pi
apply(meanDegrees,[[350, 10], [90, 180, 270, 360], [10, 20, 30]])

View file

@ -0,0 +1,18 @@
averages: procedure options (main); /* 31 August 2012 */
declare b1(2) fixed initial (350, 10);
declare b2(4) fixed initial (90, 180, 270, 360);
declare b3(3) fixed initial (10, 20, 30);
put edit ( b1) (f(7));
put edit ( ' mean=', mean(b1) ) (a, f(7) );
put skip edit ( b3) (f(7));
put edit ( ' mean=', mean(b3) ) (a, f(7) );
put skip edit ( b2) (f(7));
put edit ( ' mean=', mean(b2) ) (a, f(7) );
mean: procedure (a) returns (fixed);
declare a(*) float (18);
return ( atand(sum(sind(a))/hbound(a), sum(cosd(a))/hbound(a) ) );
end mean;
end averages;

View file

@ -0,0 +1,14 @@
sub deg2rad { $^d * pi / 180 }
sub rad2deg { $^r * 180 / pi }
sub phase ($c) {
my ($mag,$ang) = $c.polar;
return NaN if $mag < 1e-16;
$ang;
}
sub meanAngle { rad2deg phase [+] map { cis deg2rad $_ }, @^angles }
say meanAngle($_).fmt("%.2f\tis the mean angle of "), $_ for
[350, 10],
[90, 180, 270, 360],
[10, 20, 30];

View file

@ -0,0 +1,22 @@
include c:\cxpl\codes;
def Pi = 3.14159265358979323846;
def D2R = Pi/180.0; \coefficient to convert degrees to radians
func real MeanAng(A); \Return the mean of the given list of angles
int A;
real X, Y;
int I;
[X:= 0.0; Y:= 0.0;
for I:= 1 to A(0) do
[X:= X + Cos(D2R*float(A(I)));
Y:= Y + Sin(D2R*float(A(I)));
];
return ATan2(Y,X)/D2R;
];
[Format(5, 15);
RlOut(0, MeanAng([2, 350, 10])); CrLf(0);
RlOut(0, MeanAng([4, 90, 180, 270, 360])); CrLf(0);
RlOut(0, MeanAng([3, 10, 20, 30])); CrLf(0);
]