This commit is contained in:
commit
568474d52f
1591 files changed
+528213
No files matched your search
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"tool": "dafny",
|
||||
"version": "4.11.0",
|
||||
"flags": [
|
||||
"verify",
|
||||
"--resource-limit",
|
||||
"20000000"
|
||||
],
|
||||
"source_sha256": "c4ce4ea3415a128e81393b13176e9cbf9f1266c14e2fe3d7588dacdc38cf259a",
|
||||
"verdict": "verified",
|
||||
"summary": "2 verified, 0 errors"
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"tool": "godbolt",
|
||||
"compiler": "mojo_1_2_0",
|
||||
"mojoVersion": "1.2.0",
|
||||
"source_sha256": "cbbcc76b6b043a613dc6983512073400b8b622127210bae94c5ea7011b5ddce3",
|
||||
"stdin": "4 3 1 0 1 1 3 0 11 1 0 2028240960365167 -104 2028240960365167 -104 2028240960365167 -104 2028240960365167 -104 2028240960365167 -104 2028240960365167 -104 2028240960365167 -104 2028240960365167 -104 2028240960365167 -104 2028240960365167 -104 10 3602879701896397 -55 3602879701896397 -55 3602879701896397 -55 3602879701896397 -55 3602879701896397 -55 3602879701896397 -55 3602879701896397 -55 3602879701896397 -55 3602879701896397 -55 3602879701896397 -55 0 end\n",
|
||||
"args": "--target-cpu x86-64-v3 --fp-mode contract=off",
|
||||
"asked": "2026-11-12",
|
||||
"result": {
|
||||
"compiled": true,
|
||||
"buildStderr": "",
|
||||
"executed": true,
|
||||
"exitCode": 0,
|
||||
"stdout": "6.0\n1.000000000000001\n1.0\n0.0",
|
||||
"stderr": "",
|
||||
"timedOut": false,
|
||||
"asm": null
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
function Sum(x: seq<real>): real
|
||||
{
|
||||
if |x| == 0 then 0.0 else Sum(x[..|x| - 1]) + x[|x| - 1]
|
||||
}
|
||||
|
||||
// In exact arithmetic Kahan summation is the plain sum, and its compensation is always 0.
|
||||
method KahanSum(x: seq<real>) returns (total: real)
|
||||
ensures total == Sum(x)
|
||||
{
|
||||
total := 0.0;
|
||||
var c := 0.0;
|
||||
var i := 0;
|
||||
while i < |x|
|
||||
invariant 0 <= i <= |x|
|
||||
invariant total == Sum(x[..i])
|
||||
invariant c == 0.0
|
||||
{
|
||||
assert x[..i + 1][..i] == x[..i];
|
||||
var y := x[i] - c;
|
||||
var t := total + y;
|
||||
c := (t - total) - y;
|
||||
total := t;
|
||||
i := i + 1;
|
||||
}
|
||||
assert x[..|x|] == x;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
double kahan_sum(const double *x, long n) {
|
||||
double total = 0.0, c = 0.0;
|
||||
for (long i = 0; i < n; i++) {
|
||||
double y = x[i] - c;
|
||||
double t = total + y;
|
||||
c = (t - total) - y;
|
||||
total = t;
|
||||
}
|
||||
return total;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
def kahan_sum(x: List[Float64]) -> Float64:
|
||||
var total: Float64 = 0.0
|
||||
var c: Float64 = 0.0
|
||||
for i in range(len(x)):
|
||||
var y = x[i] - c
|
||||
var t = total + y
|
||||
c = (t - total) - y
|
||||
total = t
|
||||
return total
|
||||
@@ -0,0 +1,9 @@
|
||||
def kahan_sum(x):
|
||||
total = 0.0
|
||||
c = 0.0
|
||||
for v in x:
|
||||
y = v - c
|
||||
t = total + y
|
||||
c = (t - total) - y
|
||||
total = t
|
||||
return total
|
||||
@@ -0,0 +1,57 @@
|
||||
{
|
||||
"name": "KahanSum",
|
||||
"summary": "The sum of a list of FLOAT values by compensated summation, whose error bound, to first order, does not grow with the length of the list.",
|
||||
"tags": [
|
||||
"summation",
|
||||
"rounding",
|
||||
"precision"
|
||||
],
|
||||
"sources": [
|
||||
{
|
||||
"title": "Kahan, Pracniques: further remarks on reducing truncation errors, CACM 8(1), 1965",
|
||||
"url": "https://doi.org/10.1145/363707.363723"
|
||||
},
|
||||
{
|
||||
"title": "Higham, Accuracy and Stability of Numerical Algorithms, 2nd edition (SIAM, 2002), section 4.3, Compensated summation"
|
||||
}
|
||||
],
|
||||
"cases": [
|
||||
{
|
||||
"call": "KahanSum([1.0, 2.0, 3.0])"
|
||||
},
|
||||
{
|
||||
"call": "KahanSum([1.0, 1.0e-16, 1.0e-16, 1.0e-16, 1.0e-16, 1.0e-16, 1.0e-16, 1.0e-16, 1.0e-16, 1.0e-16, 1.0e-16])"
|
||||
},
|
||||
{
|
||||
"call": "KahanSum([0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1])"
|
||||
},
|
||||
{
|
||||
"call": "KahanSum([])"
|
||||
}
|
||||
],
|
||||
"references": [
|
||||
{
|
||||
"lang": "python",
|
||||
"level": "plain",
|
||||
"file": "ref/python/plain.py",
|
||||
"note": "The same compensated loop."
|
||||
},
|
||||
{
|
||||
"lang": "c",
|
||||
"level": "plain",
|
||||
"file": "ref/c/plain.c",
|
||||
"note": "The same loop in C. It survives -O2; it would not survive -ffast-math, which lets the compiler treat (t - total) - y as 0."
|
||||
},
|
||||
{
|
||||
"lang": "mojo",
|
||||
"level": "plain",
|
||||
"file": "ref/mojo/plain.mojo",
|
||||
"note": "The same loop in Mojo."
|
||||
}
|
||||
],
|
||||
"proof": {
|
||||
"lang": "dafny",
|
||||
"file": "proof/kahan_sum.dfy",
|
||||
"note": "Over real numbers the compensation C stays 0 and the result is exactly the sum: what Kahan summation corrects is rounding, and nothing else."
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// KahanSum: the sum of a list of FLOAT values by compensated summation
|
||||
// (Kahan, 1965). C holds the low-order part the last addition lost, and the
|
||||
// next addition puts it back, so the error bound, about twice the unit
|
||||
// roundoff times the sum of the absolute values (Higham, section 4.3), does
|
||||
// not grow with the length of the list, to first order. It is relative to
|
||||
// that sum of absolute values, not to the result: KahanSum([1.0e100, 1.0,
|
||||
// -1.0e100]) is 0.0. In exact arithmetic C is always 0 and this is the
|
||||
// plain sum: the compensation exists only because of rounding. A compiler
|
||||
// allowed to reorder FLOAT arithmetic (-ffast-math) can simplify C away.
|
||||
FUNCTION KahanSum(X : LIST OF FLOAT) RETURNS FLOAT
|
||||
DECLARE Total : FLOAT <- 0.0
|
||||
DECLARE C : FLOAT <- 0.0
|
||||
FOR I <- 0 TO LENGTH(X)
|
||||
DECLARE Y : FLOAT <- X[I] - C
|
||||
DECLARE T : FLOAT <- Total + Y
|
||||
C <- (T - Total) - Y
|
||||
Total <- T
|
||||
NEXT I
|
||||
RETURN Total
|
||||
ENDFUNCTION
|
||||
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"tool": "godbolt",
|
||||
"compiler": "mojo_1_2_0",
|
||||
"mojoVersion": "1.2.0",
|
||||
"source_sha256": "37a9981a11db40badf3c1515fa7e52d8ddcf0e062131d08b253320ec3a0f8289",
|
||||
"stdin": "3 0 0 0 0 5 1 5 0 1 2 1 0 1 1 3 0 1 1 5 -1 0 0 1 0 1 0 3 0 1 1 end\n",
|
||||
"args": "--target-cpu x86-64-v3 --fp-mode contract=off",
|
||||
"asked": "2026-11-12",
|
||||
"result": {
|
||||
"compiled": true,
|
||||
"buildStderr": "",
|
||||
"executed": true,
|
||||
"exitCode": 0,
|
||||
"stdout": "2.0\n2.0\n5.0",
|
||||
"stderr": "",
|
||||
"timedOut": false,
|
||||
"asm": null
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
def linear_interp(x0: Float64, y0: Float64, x1: Float64, y1: Float64, x: Float64) -> Float64:
|
||||
return y0 + (y1 - y0) * (x - x0) / (x1 - x0)
|
||||
@@ -0,0 +1,2 @@
|
||||
def linear_interp(x0, y0, x1, y1, x):
|
||||
return y0 + (y1 - y0) * (x - x0) / (x1 - x0)
|
||||
@@ -0,0 +1,38 @@
|
||||
{
|
||||
"name": "LinearInterp",
|
||||
"summary": "The value at X on the straight line through (X0, Y0) and (X1, Y1).",
|
||||
"tags": [
|
||||
"interpolation"
|
||||
],
|
||||
"sources": [
|
||||
{
|
||||
"title": "NIST Digital Library of Mathematical Functions, section 3.3(ii), linear interpolation (equation 3.3.14)",
|
||||
"url": "https://dlmf.nist.gov/3.3.ii"
|
||||
}
|
||||
],
|
||||
"cases": [
|
||||
{
|
||||
"call": "LinearInterp(0.0, 0.0, 10.0, 5.0, 4.0)"
|
||||
},
|
||||
{
|
||||
"call": "LinearInterp(1.0, 2.0, 3.0, 2.0, 2.5)"
|
||||
},
|
||||
{
|
||||
"call": "LinearInterp(0.0, 1.0, 1.0, 3.0, 2.0)"
|
||||
}
|
||||
],
|
||||
"references": [
|
||||
{
|
||||
"lang": "python",
|
||||
"level": "plain",
|
||||
"file": "ref/python/plain.py",
|
||||
"note": "The same formula."
|
||||
},
|
||||
{
|
||||
"lang": "mojo",
|
||||
"level": "plain",
|
||||
"file": "ref/mojo/plain.mojo",
|
||||
"note": "The same formula in Mojo."
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// LinearInterp: the value at X on the straight line through (X0, Y0) and
|
||||
// (X1, Y1). X may lie outside [X0, X1]: that is extrapolation, and the line
|
||||
// carries on. The REQUIRE names the difference the division divides by.
|
||||
FUNCTION LinearInterp(X0 : FLOAT, Y0 : FLOAT, X1 : FLOAT, Y1 : FLOAT, X : FLOAT) RETURNS FLOAT
|
||||
REQUIRE X1 - X0 != 0.0
|
||||
RETURN Y0 + (Y1 - Y0) * (X - X0) / (X1 - X0)
|
||||
ENDFUNCTION
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"name": "numerics",
|
||||
"title": "Numerics",
|
||||
"summary": "Summing, interpolating and integrating with FLOAT values, and, for summing, what rounding does."
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"tool": "godbolt",
|
||||
"compiler": "mojo_1_2_0",
|
||||
"mojoVersion": "1.2.0",
|
||||
"source_sha256": "64af3a830bfe088770c17b6dab589a5944de61b88d95d11c9670da169931c145",
|
||||
"stdin": "3 5 0 0 1 0 1 2 9 0 1 4 1 0 2 1 0 1 0 1 -1 3 0 0 1 -2 1 0 1 -1 end\n",
|
||||
"args": "--target-cpu x86-64-v3 --fp-mode contract=off",
|
||||
"asked": "2026-11-12",
|
||||
"result": {
|
||||
"compiled": true,
|
||||
"buildStderr": "",
|
||||
"executed": true,
|
||||
"exitCode": 0,
|
||||
"stdout": "22.0\n0.5\n0.375",
|
||||
"stderr": "",
|
||||
"timedOut": false,
|
||||
"asm": null
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
def trapezoid(y: List[Float64], h: Float64) -> Float64:
|
||||
var total: Float64 = (y[0] + y[len(y) - 1]) / 2.0
|
||||
for i in range(1, len(y) - 1):
|
||||
total = total + y[i]
|
||||
return total * h
|
||||
@@ -0,0 +1,5 @@
|
||||
def trapezoid(y, h):
|
||||
total = (y[0] + y[len(y) - 1]) / 2.0
|
||||
for i in range(1, len(y) - 1):
|
||||
total += y[i]
|
||||
return total * h
|
||||
@@ -0,0 +1,39 @@
|
||||
{
|
||||
"name": "Trapezoid",
|
||||
"summary": "The integral of samples Y taken H apart, by the trapezoidal rule.",
|
||||
"tags": [
|
||||
"integration",
|
||||
"quadrature"
|
||||
],
|
||||
"sources": [
|
||||
{
|
||||
"title": "NIST Digital Library of Mathematical Functions, section 3.5(i), the composite trapezoidal rule (equation 3.5.2)",
|
||||
"url": "https://dlmf.nist.gov/3.5.i"
|
||||
}
|
||||
],
|
||||
"cases": [
|
||||
{
|
||||
"call": "Trapezoid([0.0, 1.0, 4.0, 9.0, 16.0], 1.0)"
|
||||
},
|
||||
{
|
||||
"call": "Trapezoid([1.0, 1.0], 0.5)"
|
||||
},
|
||||
{
|
||||
"call": "Trapezoid([0.0, 0.25, 1.0], 0.5)"
|
||||
}
|
||||
],
|
||||
"references": [
|
||||
{
|
||||
"lang": "python",
|
||||
"level": "plain",
|
||||
"file": "ref/python/plain.py",
|
||||
"note": "The same sums."
|
||||
},
|
||||
{
|
||||
"lang": "mojo",
|
||||
"level": "plain",
|
||||
"file": "ref/mojo/plain.mojo",
|
||||
"note": "The same sums in Mojo."
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// Trapezoid: the integral of samples Y taken H apart, by the composite
|
||||
// trapezoidal rule: the ends count half, every inside sample counts whole.
|
||||
// The error shrinks with H^2 for a smooth function.
|
||||
FUNCTION Trapezoid(Y : LIST OF FLOAT, H : FLOAT) RETURNS FLOAT
|
||||
REQUIRE LENGTH(Y) >= 2
|
||||
DECLARE Total : FLOAT <- (Y[0] + Y[LENGTH(Y) - 1]) / 2.0
|
||||
FOR I <- 1 TO LENGTH(Y) - 1
|
||||
Total <- Total + Y[I]
|
||||
NEXT I
|
||||
RETURN Total * H
|
||||
ENDFUNCTION
|
||||
Reference in new issue
Block a user