Averages/Mean angle: Difference between revisions

→‎{{header|Java}}: code cleanup
(+Stata)
(→‎{{header|Java}}: code cleanup)
Line 785:
{{trans|NetRexx}}
{{works with|Java|7+}}
<lang java5>import java.util.ArrayListArrays;
import java.util.Arrays;
import java.util.List;
 
public class RAvgMeanAngle {
 
public static void main(String[] args) {
private static final List<List<Double>> samples;
double[][] samples = {
{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));},
{370.0},
{180.0}};
 
for (List<Double>double[] sample : samples) {
static {
double meanAngle = main.getMeanAngle(sample);
samples = new ArrayList<>();
System.out.printf("The mean angle of %s is: %n%12.6f%ns%n", Arrays.toString(sample), meanAngle);
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);
 
public static double getMeanAngle(List<Double>double... sampleanglesDeg) {
return avg_d;
double x_componentx = 0.0;
}
double y_componenty = 0.0;
 
for (double angle_dangleD : sampleanglesDeg) {
public static void main(String[] args) {
angle_r double angleR = Math.toRadians(angle_dangleD);
 
x_component x += Math.cos(angle_rangleR);
runSample(args);
y_component y += Math.sin(angle_rangleR);
 
return; }
double avgR = Math.atan2(y / anglesDeg.length, x / anglesDeg.length);
}
avg_d = return Math.toDegrees(avg_ravgR);
 
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);
}
 
return;
}
}</lang>
{{out}}
<pre>The mean angle of [350.0, 10.0] is: -1.614809932057922E-15
The mean angle of [1090.0, 20180.0, 30270.0, 360.0] is: -90.0
-0.000000
The mean angle of [37010.0, 20.0, 30.0] is: 19.999999999999996
 
The mean angle of [90370.0,] 180.0,is 2709.0, 360.0] is:999999999999977
The mean angle of [180.0] is: 180.0</pre>
-90.000000
 
The mean angle of [10.0, 20.0, 30.0] is:
20.000000
 
The mean angle of [370.0] is:
10.000000
 
The mean angle of [180.0] is:
180.000000</pre>
 
=={{header|JavaScript}}==