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,125 @@
' version 17-09-2015
' compile with: fbc -s console
#Define screen_width 1024
#Define screen_height 256
ScreenRes screen_width, screen_height, 8
Width screen_width\8, screen_height\16
Function f1(x As Double) As Double
Return x^3
End Function
Function f2(x As Double) As Double
Return 1/x
End Function
Function f3(x As Double) As Double
Return x
End Function
Function leftrect(a As Double, b As Double, n As Double, _
ByVal f As Function (ByVal As Double) As Double) As Double
Dim As Double sum, x = a, h = (b - a) / n
For i As UInteger = 1 To n
sum = sum + h * f(x)
x = x + h
Next
leftrect = sum
End Function
Function rightrect(a As Double, b As Double, n As Double, _
ByVal f As Function (ByVal As Double) As Double) As Double
Dim As Double sum, x = a, h = (b - a) / n
For i As UInteger = 1 To n
x = x + h
sum = sum + h * f(x)
Next
rightrect = sum
End Function
Function midrect(a As Double, b As Double, n As Double, _
ByVal f As Function (ByVal As Double) As Double) As Double
Dim As Double sum, h = (b - a) / n, x = a + h / 2
For i As UInteger = 1 To n
sum = sum + h * f(x)
x = x + h
Next
midrect = sum
End Function
Function trap(a As Double, b As Double, n As Double, _
ByVal f As Function (ByVal As Double) As Double) As Double
Dim As Double x = a, h = (b - a) / n
Dim As Double sum = h * (f(a) + f(b)) / 2
For i As UInteger = 1 To n -1
x = x + h
sum = sum + h * f(x)
Next
trap = sum
End Function
Function simpson(a As Double, b As Double, n As Double, _
ByVal f As Function (ByVal As Double) As Double) As Double
Dim As UInteger i
Dim As Double sum1, sum2
Dim As Double h = (b - a) / n
For i = 0 To n -1
sum1 = sum1 + f(a + h * i + h / 2)
Next i
For i = 1 To n -1
sum2 = sum2 + f(a + h * i)
Next i
simpson = h / 6 * (f(a) + f(b) + 4 * sum1 + 2 * sum2)
End Function
' ------=< main >=------
Dim As Double y
Dim As String frmt = " ##.##########"
Print
Print "function range steps leftrect midrect " + _
"rightrect trap simpson "
Print "f(x) = x^3 0 - 1 100";
Print Using frmt; leftrect(0, 1, 100, @f1); midrect(0, 1, 100, @f1); _
rightrect(0, 1, 100, @f1); trap(0, 1, 100, @f1); simpson(0, 1, 100, @f1)
Print "f(x) = 1/x 1 - 100 1000";
Print Using frmt; leftrect(1, 100, 1000, @f2); midrect(1, 100, 1000, @f2); _
rightrect(1, 100, 1000, @f2); trap(1, 100, 1000, @f2); _
simpson(1, 100, 1000, @f2)
frmt = " #########.###"
Print "f(x) = x 0 - 5000 5000000";
Print Using frmt; leftrect(0, 5000, 5000000, @f3); midrect(0, 5000, 5000000, @f3); _
rightrect(0, 5000, 5000000, @f3); trap(0, 5000, 5000000, @f3); _
simpson(0, 5000, 5000000, @f3)
Print "f(x) = x 0 - 6000 6000000";
Print Using frmt; leftrect(0, 6000, 6000000, @f3); midrect(0, 6000, 6000000, @f3); _
rightrect(0, 6000, 6000000, @f3); trap(0, 6000, 6000000, @f3); _
simpson(0, 6000, 6000000, @f3)
' empty keyboard buffer
While InKey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End

View file

@ -0,0 +1,45 @@
type Function = proc(x: float): float
type Rule = proc(f: Function; x, h: float): float
proc leftRect(f: Function; x, h: float): float =
f(x)
proc midRect(f: Function; x, h: float): float =
f(x + h/2.0)
proc rightRect(f: Function; x, h: float): float =
f(x + h)
proc trapezium(f: Function; x, h: float): float =
(f(x) + f(x+h)) / 2.0
proc simpson(f: Function, x, h: float): float =
(f(x) + 4.0*f(x+h/2.0) + f(x+h)) / 6.0
proc cube(x: float): float =
x * x *x
proc reciprocal(x: float): float =
1.0 / x
proc identity(x: float): float =
x
proc integrate(f: Function; a, b: float; steps: int; meth: Rule): float =
let h = (b-a) / float(steps)
for i in 0 .. <steps:
result += meth(f, a+float(i)*h, h)
result = h * result
for fName, a, b, steps, fun in items(
[("cube", 0, 1, 100, cube),
("reciprocal", 1, 100, 1000, reciprocal),
("identity", 0, 5000, 5_000_000, identity),
("identity", 0, 6000, 6_000_000, identity)]):
for rName, rule in items({"leftRect": leftRect, "midRect": midRect,
"rightRect": rightRect, "trapezium": trapezium, "simpson": simpson}):
echo fName, " integrated using ", rName
echo " from ", a, " to ", b, " (", steps, " steps) = ",
integrate(fun, float(a), float(b), steps, rule)

View file

@ -0,0 +1,74 @@
function rect_left(integer rid, atom x, atom h)
if atom(h) then end if -- suppress warning
return call_func(rid,{x})
end function
function rect_mid(integer rid, atom x, atom h)
return call_func(rid,{x+h/2})
end function
function rect_right(integer rid, atom x, atom h)
return call_func(rid,{x+h})
end function
function trapezium(integer rid, atom x, atom h)
return (call_func(rid,{x})+call_func(rid,{x+h}))/2
end function
function simpson(integer rid, atom x, atom h)
return (call_func(rid,{x})+4*call_func(rid,{x+h/2})+call_func(rid,{x+h}))/6
end function
function cubed(atom x)
return power(x,3)
end function
function recip(atom x)
return 1/x
end function
function ident(atom x)
return x
end function
function integrate(integer m_id, integer f_id, atom a, atom b, integer steps)
atom accum = 0,
h = (b-a)/steps
for i=0 to steps-1 do
accum += call_func(m_id,{f_id,a+h*i,h})
end for
return h*accum
end function
function smartp(atom N)
string res
if N=floor(N) then return sprintf("%d",N) end if
res = sprintf("%12f",round(N,1000000))
if find('.',res) then
res = trim_tail(res,"0")
res = trim_tail(res,".")
end if
return res
end function
procedure test(sequence tests)
string name
atom a, b, steps, rid
printf(1,"Function Range Iterations L-Rect M-Rect R-Rect Trapeze Simpson\n")
for i=1 to length(tests) do
{name,a,b,steps,rid} = tests[i]
printf(1," %-5s %6d - %-5d %10d %12s %12s %12s %12s %12s\n",{name,a,b,steps,
smartp(integrate(routine_id("rect_left"), rid,a,b,steps)),
smartp(integrate(routine_id("rect_mid"), rid,a,b,steps)),
smartp(integrate(routine_id("rect_right"), rid,a,b,steps)),
smartp(integrate(routine_id("trapezium"), rid,a,b,steps)),
smartp(integrate(routine_id("simpson"), rid,a,b,steps))})
end for
end procedure
constant tests = {{"x^3", 0, 1, 100, routine_id("cubed")},
{"1/x", 1, 100, 1000, routine_id("recip")},
{"x", 0, 5000, 5000000, routine_id("ident")},
{"x", 0, 6000, 6000000, routine_id("ident")}}
test(tests)

View file

@ -0,0 +1,60 @@
import <Utilities/Conversion.sl>;
import <Utilities/Sequence.sl>;
integrateLeft(f, a, b, n) :=
let
h := (b - a) / n;
vals[x] := f(x) foreach x within (0 ... (n-1)) * h + a;
in
h * sum(vals);
integrateRight(f, a, b, n) :=
let
h := (b - a) / n;
vals[x] := f(x+h) foreach x within (0 ... (n-1)) * h + a;
in
h * sum(vals);
integrateMidpoint(f, a, b, n) :=
let
h := (b - a) / n;
vals[x] := f(x+h/2.0) foreach x within (0 ... (n-1)) * h + a;
in
h * sum(vals);
integrateTrapezium(f, a, b, n) :=
let
h := (b - a) / n;
vals[i] := 2.0 * f(a + i * h) foreach i within 1 ... n-1;
in
h * (sum(vals) + f(a) + f(b)) / 2.0;
integrateSimpsons(f, a, b, n) :=
let
h := (b - a) / n;
vals1[i] := f(a + h * i + h / 2.0) foreach i within 0 ... n-1;
vals2[i] := f(a + h * i) foreach i within 1 ... n-1;
in
h / 6.0 * (f(a) + f(b) + 4.0 * sum(vals1) + 2.0 * sum(vals2));
xCubed(x) := x^3;
xInverse(x) := 1/x;
identity(x) := x;
tests[method] :=
[method(xCubed, 0.0, 1.0, 100),
method(xInverse, 1.0, 100.0, 1000),
method(identity, 0.0, 5000.0, 5000000),
method(identity, 0.0, 6000.0, 6000000)]
foreach method within [integrateLeft, integrateRight, integrateMidpoint, integrateTrapezium, integrateSimpsons];
//String manipulation for ouput display.
main :=
let
heading := [["Func", "Range\t", "L-Rect\t", "R-Rect\t", "M-Rect\t", "Trapezium", "Simpson"]];
ranges := [["0 - 1\t", "1 - 100\t", "0 - 5000", "0 - 6000"]];
funcs := [["x^3", "1/x", "x", "x"]];
in
delimit(delimit(heading ++ transpose(funcs ++ ranges ++ trimEndZeroes(floatToString(tests, 8))), '\t'), '\n');
trimEndZeroes(x(1)) := x when size(x) = 0 else x when x[size(x)] /= '0' else trimEndZeroes(x[1...size(x)-1]);

View file

@ -0,0 +1,54 @@
func sum(f, start, from, to) {
var s = 0;
RangeNum(start, to, from-start).each { |i|
s += f(i);
}
return s
}
func leftrect(f, a, b, n) {
var h = ((b - a) / n);
h * sum(f, a, a+h, b-h);
}
func rightrect(f, a, b, n) {
var h = ((b - a) / n);
h * sum(f, a+h, a + 2*h, b);
}
func midrect(f, a, b, n) {
var h = ((b - a) / n);
h * sum(f, a + h/2, a + h + h/2, b - h/2)
}
func trapez(f, a, b, n) {
var h = ((b - a) / n);
h/2 * (f(a) + f(b) + sum({ f(_)*2 }, a+h, a + 2*h, b-h));
}
func simpsons(f, a, b, n) {
var h = ((b - a) / n);
var h2 = h/2;
var sum1 = f(a + h2);
var sum2 = 0;
sum({|i| sum1 += f(i + h2); sum2 += f(i); 0 }, a+h, a+h+h, b-h);
h/6 * (f(a) + f(b) + 4*sum1 + 2*sum2);
}
func tryem(label, f, a, b, n, exact) {
say "\n#{label}\n in [#{a}..#{b}] / #{n}";
say(' exact result: ', exact);
say(' rectangle method left: ', leftrect(f, a, b, n));
say(' rectangle method right: ', rightrect(f, a, b, n));
say(' rectangle method mid: ', midrect(f, a, b, n));
say('composite trapezoidal rule: ', trapez(f, a, b, n));
say(' quadratic simpsons rule: ', simpsons(f, a, b, n));
}
tryem('x^3', { _ ** 3 }, 0, 1, 100, 0.25);
tryem('1/x', { 1 / _ }, 1, 100, 1000, log(100));
tryem('x', { _ }, 0, 5_000, 5_000_000, 12_500_000);
tryem('x', { _ }, 0, 6_000, 6_000_000, 18_000_000);