September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -1,6 +1,15 @@
import Data.Complex (cis, phase)
meanAngle
:: RealFloat c
=> [c] -> c
meanAngle = (/ pi) . (* 180) . phase . sum . map (cis . (/ 180) . (* pi))
main = mapM_ (\angles -> putStrLn $ "The mean angle of " ++ show angles ++ " is: " ++ show (meanAngle angles) ++ " degrees")
[[350, 10], [90, 180, 270, 360], [10, 20, 30]]
main :: IO ()
main =
mapM_
(\angles ->
putStrLn $
"The mean angle of " ++
show angles ++ " is: " ++ show (meanAngle angles) ++ " degrees")
[[350, 10], [90, 180, 270, 360], [10, 20, 30]]

View file

@ -1,60 +1,30 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class RAvgMeanAngle {
public class AverageMeanAngle {
private static final List<List<Double>> samples;
static {
samples = new ArrayList<>();
samples.add(Arrays.asList(350.0, 10.0));
samples.add(Arrays.asList(90.0, 180.0, 270.0, 360.0));
samples.add(Arrays.asList(10.0, 20.0, 30.0));
samples.add(Arrays.asList(370.0));
samples.add(Arrays.asList(180.0));
}
public RAvgMeanAngle() {
return;
}
public double getMeanAngle(List<Double> sample) {
double x_component = 0.0;
double y_component = 0.0;
double avg_d, avg_r;
for (double angle_d : sample) {
double angle_r;
angle_r = Math.toRadians(angle_d);
x_component += Math.cos(angle_r);
y_component += Math.sin(angle_r);
}
x_component /= sample.size();
y_component /= sample.size();
avg_r = Math.atan2(y_component, x_component);
avg_d = Math.toDegrees(avg_r);
return avg_d;
}
public static void main(String[] args) {
runSample(args);
return;
}
public static void runSample(String[] args) {
RAvgMeanAngle main = new RAvgMeanAngle();
for (List<Double> sample : samples) {
double meanAngle = main.getMeanAngle(sample);
System.out.printf("The mean angle of %s is:%n%12.6f%n%n", sample, meanAngle);
public static void main(String[] args) {
printAverageAngle(350.0, 10.0);
printAverageAngle(90.0, 180.0, 270.0, 360.0);
printAverageAngle(10.0, 20.0, 30.0);
printAverageAngle(370.0);
printAverageAngle(180.0);
}
return;
}
private static void printAverageAngle(double... sample) {
double meanAngle = getMeanAngle(sample);
System.out.printf("The mean angle of %s is %s%n", Arrays.toString(sample), meanAngle);
}
public static double getMeanAngle(double... anglesDeg) {
double x = 0.0;
double y = 0.0;
for (double angleD : anglesDeg) {
double angleR = Math.toRadians(angleD);
x += Math.cos(angleR);
y += Math.sin(angleR);
}
double avgR = Math.atan2(y / anglesDeg.length, x / anglesDeg.length);
return Math.toDegrees(avgR);
}
}

View file

@ -0,0 +1,17 @@
// version 1.0.5-2
fun meanAngle(angles: DoubleArray): Double {
val sinSum = angles.sumByDouble { Math.sin(it * Math.PI / 180.0) }
val cosSum = angles.sumByDouble { Math.cos(it * Math.PI / 180.0) }
return Math.atan2(sinSum / angles.size, cosSum / angles.size) * 180.0 / Math.PI
}
fun main(args: Array<String>) {
val angles1 = doubleArrayOf(350.0, 10.0)
val angles2 = doubleArrayOf(90.0, 180.0, 270.0, 360.0)
val angles3 = doubleArrayOf(10.0, 20.0, 30.0)
val fmt = "%.2f degrees" // format results to 2 decimal places
println("Mean for angles 1 is ${fmt.format(meanAngle(angles1))}")
println("Mean for angles 2 is ${fmt.format(meanAngle(angles2))}")
println("Mean for angles 3 is ${fmt.format(meanAngle(angles3))}")
}

View file

@ -1,11 +1,11 @@
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)
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}))

View file

@ -1,12 +1,12 @@
import math, complex
proc rect(r, phi): Complex = (r * cos(phi), sin(phi))
proc phase(c): float = arctan2(c.im, c.re)
proc rect(r, phi: float): Complex = (r * cos(phi), sin(phi))
proc phase(c: Complex): float = arctan2(c.im, c.re)
proc radians(x): float = (x * Pi) / 180.0
proc degrees(x): float = (x * 180.0) / Pi
proc radians(x: float): float = (x * Pi) / 180.0
proc degrees(x: float): float = (x * 180.0) / Pi
proc meanAngle(deg): float =
proc meanAngle(deg: openArray[float]): float =
var c: Complex
for d in deg:
c += rect(1.0, radians(d))

View file

@ -0,0 +1,24 @@
/*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.*/
xl = 350 10 ; say showit(xl, meanAngleD(xl) )
xl = 90 180 270 360 ; say showit(xl, meanAngleD(xl) )
xl = 10 20 30 ; say showit(xl, meanAngleD(xl) )
exit /*stick a fork in it, we're done.*/
/*----------------------------------MEANANGD subroutine-----------------*/
meanAngleD: procedure; parse arg xl; numeric digits digits()+digits()%4
sum.=0
n=words(xl)
do j=1 to n
sum.0sin+=rxCalcSin(word(xl,j))
sum.0cos+=rxCalcCos(word(xl,j))
End
If sum.0cos=0 Then
Return sign(sum.0sin/n)*90
Else
Return rxCalcArcTan((sum.0sin/n)/(sum.0cos/n))
showit: procedure expose showDig; numeric digits showDig; parse arg a,mA
return left('angles='a,30) 'mean angle=' format(mA,,showDig,0)/1
::requires rxMath library;

View file

@ -0,0 +1,7 @@
function Get-MeanAngle ([double[]]$Angles)
{
$x = ($Angles | ForEach-Object {[Math]::Cos($_ * [Math]::PI / 180)} | Measure-Object -Average).Average
$y = ($Angles | ForEach-Object {[Math]::Sin($_ * [Math]::PI / 180)} | Measure-Object -Average).Average
[Math]::Atan2($y, $x) * 180 / [Math]::PI
}

View file

@ -0,0 +1 @@
@(350, 10), @(90, 180, 270, 360), @(10, 20, 30) | ForEach-Object {Get-MeanAngle $_}

View file

@ -0,0 +1,23 @@
(import (srfi 1 lists)) ;; use 'fold' from library
(define (average l)
(/ (fold + 0 l) (length l)))
(define pi 3.14159265358979323846264338327950288419716939937510582097)
(define (radians a)
(* pi 1/180 a))
(define (degrees a)
(* 180 (/ 1 pi) a))
(define (mean-angle angles)
(let* ((angles (map radians angles))
(cosines (map cos angles))
(sines (map sin angles)))
(degrees (atan (average sines) (average cosines)))))
(for-each (lambda (angles)
(display "The mean angle of ") (display angles)
(display " is ") (display (mean-angle angles)) (newline))
'((350 10) (90 180 270 360) (10 20 30)))

View file

@ -1,10 +1,10 @@
func mean_angle(angles) {
Math.atan2(
atan2(
Math.avg(angles.map{ .deg2rad.sin }...),
Math.avg(angles.map{ .deg2rad.cos }...),
) -> rad2deg;
) -> rad2deg
}
[[350,10], [90,180,270,360], [10,20,30]].each { |angles|
say "The mean angle of #{angles.dump} is: #{ '%.2f' % mean_angle(angles)} degrees";
say "The mean angle of #{angles.dump} is: #{ '%.2f' % mean_angle(angles)} degrees"
}

View file

@ -0,0 +1,14 @@
mata
function meanangle(a) {
return(arg(sum(exp(C(0,a)))))
}
deg=acos(-1)/180
meanangle((350,10)*deg)/deg
-1.61481e-15
meanangle((90,180,270,360)*deg)/deg
0
meanangle((10,20,30)*deg)/deg
20
end

View file

@ -0,0 +1,7 @@
fcn meanA(a1,a2,etc){
as:=vm.arglist.pump(List,"toFloat","toRad");
n:=as.len();
(as.apply("sin").sum(0.0)/n)
.atan2(as.apply("cos").sum(0.0)/n)
.toDeg()
}