This commit is contained in:
Ingy döt Net 2013-10-27 22:24:23 +00:00
parent 6f050a029e
commit 776bba907c
3887 changed files with 59894 additions and 7280 deletions

View file

@ -0,0 +1,13 @@
#!/usr/bin/awk -f
{
PI = atan2(0,-1);
x=0.0; y=0.0;
for (i=1; i<=NF; i++) {
p = $i*PI/180.0;
x += sin(p);
y += cos(p);
}
p = atan2(x,y)*180.0/PI;
if (p<0) p += 360;
print p;
}

View file

@ -4,7 +4,7 @@ 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; }
real meanAngle(T)(in T[] D) /*pure nothrow*/ {
real meanAngle(T)(in T[] D) pure nothrow {
immutable t = reduce!((a, d) => a + d.radians.expi)(0.complex, D);
return (t / D.length).arg.degrees;
}

View file

@ -5,11 +5,11 @@ from_degrees( Angles ) ->
Radians = [radians(X) || X <- Angles],
Sines = [math:sin(X) || X <- Radians],
Coses = [math:cos(X) || X <- Radians],
erlang:round( degrees(math:atan2( average(Sines), average(Coses) )) ).
degrees( math:atan2( average(Sines), average(Coses) ) ).
task() ->
Angles = [[350, 10], [90, 180, 270, 360], [10, 20, 30]],
[io:fwrite( "Mean angle of ~p is: ~p~n", [X, from_degrees(X)] ) || X <- Angles].
[io:fwrite( "Mean angle of ~p is: ~p~n", [X, erlang:round(from_degrees(X))] ) || X <- Angles].
average( List ) -> lists:sum( List ) / erlang:length( List ).

View file

@ -0,0 +1,36 @@
!-*- mode: compilation; default-directory: "/tmp/" -*-
!Compilation started at Mon Jun 3 18:07:59
!
!a=./f && make $a && OMP_NUM_THREADS=2 $a
!gfortran -std=f2008 -Wall -fopenmp -ffree-form -fall-intrinsics -fimplicit-none f.f08 -o f
! -7.80250048E-06 350 10
! 90.0000000 90 180 270 360
! 19.9999962 10 20 30
!
!Compilation finished at Mon Jun 3 18:07:59
program average_angles
!real(kind=8), parameter :: TAU = 6.283185307179586232 ! http://tauday.com/
!integer, dimension(13), parameter :: test_data = (/2,350,10, 4,90,180,270,360, 3,10,20,30, 0/)
!integer :: i, j, n
!complex(kind=16) :: some
!real(kind=8) :: angle
real, parameter :: TAU = 6.283185307179586232 ! http://tauday.com/
integer, dimension(13), parameter :: test_data = (/2,350,10, 4,90,180,270,360, 3,10,20,30, 0/)
integer :: i, j, n
complex :: some
real :: angle
i = 1
n = int(test_data(i))
do while (0 .lt. n)
some = 0
do j = 1, n
angle = (TAU/360)*test_data(i+j)
some = some + cmplx(cos(angle), sin(angle))
end do
some = some / n
write(6,*)(360/TAU)*atan2(aimag(some), real(some)),test_data(i+1:i+n)
i = i + n + 1
n = int(test_data(i))
end do
end program average_angles

View file

@ -0,0 +1,22 @@
package main
import (
"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))
}
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))
}
}

View file

@ -0,0 +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
}

View file

@ -0,0 +1,4 @@
import static java.lang.Math.*
def meanAngle = {
atan2( it.sum { sin(it * PI / 180) } / it.size(), it.sum { cos(it * PI / 180) } / it.size()) * 180 / PI
}

View file

@ -0,0 +1,8 @@
def verifyAngle = { angles ->
def ma = meanAngle(angles)
printf("Mean Angle for $angles: %5.2f%n", ma)
round(ma * 100) / 100.0
}
assert verifyAngle([350, 10]) == -0
assert verifyAngle([90, 180, 270, 360]) == -90
assert verifyAngle([10, 20, 30]) == 20

View file

@ -0,0 +1,9 @@
procedure main(A)
write("Mean angle is ",meanAngle(A))
end
procedure meanAngle(A)
every (sumSines := 0.0) +:= sin(dtor(!A))
every (sumCosines := 0.0) +:= cos(dtor(!A))
return rtod(atan(sumSines/*A,sumCosines/*A))
end

View file

@ -0,0 +1,3 @@
function u = mean_angle(phi)
u = angle(mean(exp(i*pi*phi/180)))*180/pi;
end

View file

@ -0,0 +1,23 @@
<?php
$samples = array(
'1st' => array(350, 10),
'2nd' => array(90, 180, 270, 360),
'3rd' => array(10, 20, 30)
);
foreach($samples as $key => $sample){
echo 'Mean angle for ' . $key . ' sample: ' . meanAngle($sample) . ' degrees.' . PHP_EOL;
}
function meanAngle ($angles){
$y_part = $x_part = 0;
$size = count($angles);
for ($i = 0; $i < $size; $i++){
$x_part += cos(deg2rad($angles[$i]));
$y_part += sin(deg2rad($angles[$i]));
}
$x_part /= $size;
$y_part /= $size;
return rad2deg(atan2($y_part, $x_part));
}
?>

View file

@ -1,4 +1,4 @@
require 'complex'
require 'complex' # Superfluous in Ruby >= 2.0; complex is added to core.
def deg2rad(d)
d * Math::PI / 180