Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,14 @@
(define-syntax-rule (deg->radian deg) (* deg 1/180 PI))
(define-syntax-rule (radian->deg rad) (* 180 (/ PI) rad))
(define (mean-angles angles)
(radian->deg
(angle
(for/sum ((a angles)) (make-polar 1 (deg->radian a))))))
(mean-angles '( 350 10))
→ -0
(mean-angles '[90 180 270 360])
→ -90
(mean-angles '[10 20 30])
→ 20

View file

@ -0,0 +1,25 @@
' FB 1.05.0 Win64
Const PI As Double = 3.1415926535897932
Function MeanAngle(angles() As Double) As Double
Dim As Integer length = Ubound(angles) - Lbound(angles) + 1
Dim As Double sinSum = 0.0
Dim As Double cosSum = 0.0
For i As Integer = LBound(angles) To UBound(angles)
sinSum += Sin(angles(i) * PI / 180.0)
cosSum += Cos(angles(i) * PI / 180.0)
Next
Return Atan2(sinSum / length, cosSum / length) * 180.0 / PI
End Function
Dim As Double angles1(1 To 2) = {350, 10}
Dim As Double angles2(1 To 4) = {90, 180, 270, 360}
Dim As Double angles3(1 To 3) = {10, 20, 30}
Print Using "Mean for angles 1 is : ####.## degrees"; MeanAngle(angles1())
Print Using "Mean for angles 2 is : ####.## degrees"; MeanAngle(angles2())
Print Using "Mean for angles 3 is : ####.## degrees"; MeanAngle(angles3())
Print
Print "Press any key to quit the program"
Sleep

View file

@ -0,0 +1,17 @@
import math, complex
proc rect(r, phi): Complex = (r * cos(phi), sin(phi))
proc phase(c): float = arctan2(c.im, c.re)
proc radians(x): float = (x * Pi) / 180.0
proc degrees(x): float = (x * 180.0) / Pi
proc meanAngle(deg): float =
var c: Complex
for d in deg:
c += rect(1.0, radians(d))
degrees(phase(c / float(deg.len)))
echo "The 1st mean angle is: ", meanAngle([350.0, 10.0]), " degrees"
echo "The 2nd mean angle is: ", meanAngle([90.0, 180.0, 270.0, 360.0]), " degrees"
echo "The 3rd mean angle is: ", meanAngle([10.0, 20.0, 30.0]), " degrees"

View file

@ -0,0 +1,24 @@
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, ai_rad
integer l=length(angles)
for i=1 to l do
ai_rad = angles[i]*PI/180
x += cos(ai_rad)
y += sin(ai_rad)
end for
if abs(x)<1e-16 then return "not meaningful" end if
return sprintf("%9.5f",atan2(y,x)*180/PI)
end function
constant AngleLists = {{350,10},{90,180,270,360},{10,20,30},{180},{0,180}}
sequence ai
for i=1 to length(AngleLists) do
ai = AngleLists[i]
printf(1,"%+16s: Mean Angle is %s\n",{sprint(ai),MeanAngle(ai)})
end for
{} = wait_key()

View file

@ -0,0 +1,10 @@
func mean_angle(angles) {
Math.atan2(
Math.avg(angles.map{ .deg2rad.sin }...),
Math.avg(angles.map{ .deg2rad.cos }...),
) -> 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";
}

View file

@ -0,0 +1,19 @@
def pi: 4 * (1|atan);
def deg2rad: . * pi / 180;
def rad2deg: if . == null then null else . * 180 / pi end;
# Input: [x,y] (special handling of x==0)
# Output: [r, theta] where theta may be null
def to_polar:
if .[0] == 0
then [1, if .[1] > 5e-14 then pi/2 elif .[1] < -5e-14 then -pi/2 else null end]
else [1, ((.[1]/.[0]) | atan)]
end;
def from_polar: .[1] | [ cos, sin];
def abs: if . < 0 then - . else . end;
def summation(f): map(f) | add;

View file

@ -0,0 +1,15 @@
# input: degrees
def mean_angle:
def round:
if . == null then null
elif . < 0 then -1 * ((- .) | round) | if . == -0 then 0 else . end
else ((. + 3e-14) | floor) as $x
| if ($x - .) | abs < 3e-14 then $x else . end
end;
map( [1, deg2rad] | from_polar)
| [ summation(.[0]), summation(.[1]) ]
| to_polar
| .[1]
| rad2deg
| round;

View file

@ -0,0 +1,2 @@
([350, 10], [90, 180, 270, 360], [10, 20, 30])
| "The mean angle of \(.) is: \(mean_angle)"

View file

@ -0,0 +1,4 @@
jq -r -n -f Mean_angle.jq
The mean angle of [350,10] is: 0
The mean angle of [90,180,270,360] is: null
The mean angle of [10,20,30] is: 20