Start pplus-library: 16 verified routines in six modules
Routines written once in pseudo and checked at every level, so programs can use them by name instead of rewriting and re-verifying them: - stats: Mean, Variance (two-pass) - numerics: KahanSum, LinearInterp, Trapezoid - align: LinearGapPenalty, AffineGapPenalty, MatchScore, NeedlemanWunschScore - md: LennardJones, LennardJonesForce, MinimumImage, PairDistance - seq: GCContent, HammingDistance - special: ErfApprox (Abramowitz and Stegun 7.1.26) Each has contracts, cases, reference implementations in Python, Mojo and (for the numerical ones) C at more than one level, and nine have a Dafny proof over exact numbers. status.json and STATUS.md are written by pplus-core's library-check: every check passes or is open/unverified as listed there. The Mojo references still need the pinned compiler's answers (library-verify-mojo, network). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FgvbRdoEheAHqsRmr2WuKZ
This commit is contained in:
103 files changed
+3025
No files matched your search
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"tool": "dafny",
|
||||
"version": "4.11.0",
|
||||
"flags": [
|
||||
"verify",
|
||||
"--resource-limit",
|
||||
"20000000"
|
||||
],
|
||||
"source_sha256": "5a0a04933596877e8f881204d2508c4fd6f4fc5a37aa4d0a0dce95ac4da45730",
|
||||
"verdict": "verified",
|
||||
"summary": "3 verified, 0 errors"
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
method AffineGapPenalty(length: int, open: int, extend: int) returns (result: int)
|
||||
requires length >= 1
|
||||
ensures result == open + (length - 1) * extend
|
||||
{
|
||||
result := open + (length - 1) * extend;
|
||||
}
|
||||
|
||||
// Position by position: the first position opens the gap, every later one extends it.
|
||||
method AffineByPositions(length: int, open: int, extend: int) returns (score: int)
|
||||
requires length >= 1
|
||||
ensures score == open + (length - 1) * extend
|
||||
{
|
||||
score := open;
|
||||
var i := 1;
|
||||
while i < length
|
||||
invariant 1 <= i <= length
|
||||
invariant score == open + (i - 1) * extend
|
||||
{
|
||||
score := score + extend;
|
||||
i := i + 1;
|
||||
}
|
||||
}
|
||||
|
||||
// With the same score for opening and extending, the affine model is the linear one.
|
||||
lemma AffineIsLinearWhenEqual(length: int, gap: int)
|
||||
requires length >= 1
|
||||
ensures gap + (length - 1) * gap == length * gap
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
def affine_gap_penalty(length: Int, open_: Int, extend: Int) -> Int:
|
||||
return open_ + (length - 1) * extend
|
||||
@@ -0,0 +1,2 @@
|
||||
def affine_gap_penalty(length, open_, extend):
|
||||
return open_ + (length - 1) * extend
|
||||
@@ -0,0 +1,44 @@
|
||||
{
|
||||
"name": "AffineGapPenalty",
|
||||
"summary": "The score of a gap of Length positions when opening a gap scores Open and each further position scores Extend.",
|
||||
"tags": [
|
||||
"alignment",
|
||||
"gap model"
|
||||
],
|
||||
"sources": [
|
||||
{
|
||||
"title": "Gotoh, An improved algorithm for matching biological sequences, J. Mol. Biol. 162 (1982)",
|
||||
"url": "https://doi.org/10.1016/0022-2836(82)90398-9"
|
||||
}
|
||||
],
|
||||
"cases": [
|
||||
{
|
||||
"call": "AffineGapPenalty(1, -10, -1)"
|
||||
},
|
||||
{
|
||||
"call": "AffineGapPenalty(5, -10, -1)"
|
||||
},
|
||||
{
|
||||
"call": "AffineGapPenalty(3, -2, -2)"
|
||||
}
|
||||
],
|
||||
"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."
|
||||
}
|
||||
],
|
||||
"proof": {
|
||||
"lang": "dafny",
|
||||
"file": "proof/affine_gap.dfy",
|
||||
"note": "The formula equals the score built position by position, and with Open == Extend it equals the linear model."
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// AffineGapPenalty: the score of a gap of Length positions under an affine
|
||||
// gap model (Gotoh, 1982): Open for the first position and Extend for each
|
||||
// one after it, so one long gap costs less than many short ones. With
|
||||
// Open == Extend it is the linear model.
|
||||
FUNCTION AffineGapPenalty(Length : INTEGER, Open : INTEGER, Extend : INTEGER) RETURNS INTEGER
|
||||
REQUIRE Length >= 1
|
||||
ENSURE RESULT == Open + (Length - 1) * Extend
|
||||
RETURN Open + (Length - 1) * Extend
|
||||
ENDFUNCTION
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"tool": "dafny",
|
||||
"version": "4.11.0",
|
||||
"flags": [
|
||||
"verify",
|
||||
"--resource-limit",
|
||||
"20000000"
|
||||
],
|
||||
"source_sha256": "1d61017b086bed77518f358dcbbc0fd011139347f77beb2d39479d9db7edb2de",
|
||||
"verdict": "verified",
|
||||
"summary": "2 verified, 0 errors"
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
// A gap of `length` positions under a linear model: each position scores `gap`.
|
||||
method LinearGapPenalty(length: int, gap: int) returns (result: int)
|
||||
requires length >= 0
|
||||
ensures result == length * gap
|
||||
{
|
||||
result := length * gap;
|
||||
}
|
||||
|
||||
// The same score, built one position at a time: the product is the model, not a shortcut.
|
||||
method LinearGapByPositions(length: int, gap: int) returns (score: int)
|
||||
requires length >= 0
|
||||
ensures score == length * gap
|
||||
{
|
||||
score := 0;
|
||||
var i := 0;
|
||||
while i < length
|
||||
invariant 0 <= i <= length
|
||||
invariant score == i * gap
|
||||
{
|
||||
score := score + gap;
|
||||
i := i + 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
def linear_gap_penalty(length: Int, gap: Int) -> Int:
|
||||
return length * gap
|
||||
@@ -0,0 +1,2 @@
|
||||
def linear_gap_penalty(length, gap):
|
||||
return length * gap
|
||||
@@ -0,0 +1,43 @@
|
||||
{
|
||||
"name": "LinearGapPenalty",
|
||||
"summary": "The score of a gap of Length positions when every position of a gap scores Gap.",
|
||||
"tags": [
|
||||
"alignment",
|
||||
"gap model"
|
||||
],
|
||||
"sources": [
|
||||
{
|
||||
"title": "Durbin, Eddy, Krogh and Mitchison, Biological Sequence Analysis, section 2.2"
|
||||
}
|
||||
],
|
||||
"cases": [
|
||||
{
|
||||
"call": "LinearGapPenalty(3, -2)"
|
||||
},
|
||||
{
|
||||
"call": "LinearGapPenalty(0, -2)"
|
||||
},
|
||||
{
|
||||
"call": "LinearGapPenalty(10, -1)"
|
||||
}
|
||||
],
|
||||
"references": [
|
||||
{
|
||||
"lang": "python",
|
||||
"level": "plain",
|
||||
"file": "ref/python/plain.py",
|
||||
"note": "The same product."
|
||||
},
|
||||
{
|
||||
"lang": "mojo",
|
||||
"level": "plain",
|
||||
"file": "ref/mojo/plain.mojo",
|
||||
"note": "The same product in Mojo."
|
||||
}
|
||||
],
|
||||
"proof": {
|
||||
"lang": "dafny",
|
||||
"file": "proof/linear_gap.dfy",
|
||||
"note": "The product equals the score built position by position: each of the Length positions adds Gap."
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// LinearGapPenalty: the score of a gap of Length positions under a linear gap
|
||||
// model, where every position of a gap scores Gap (usually negative).
|
||||
FUNCTION LinearGapPenalty(Length : INTEGER, Gap : INTEGER) RETURNS INTEGER
|
||||
REQUIRE Length >= 0
|
||||
ENSURE RESULT == Length * Gap
|
||||
RETURN Length * Gap
|
||||
ENDFUNCTION
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"tool": "dafny",
|
||||
"version": "4.11.0",
|
||||
"flags": [
|
||||
"verify",
|
||||
"--resource-limit",
|
||||
"20000000"
|
||||
],
|
||||
"source_sha256": "374ce6664efd8802bb90e120a1107375c0ab607c9829987b26d73d5f5fd4b009",
|
||||
"verdict": "verified",
|
||||
"summary": "1 verified, 0 errors"
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
method MatchScore(a: char, b: char, match_: int, mismatch: int) returns (r: int)
|
||||
ensures (a == b && r == match_) || (a != b && r == mismatch)
|
||||
{
|
||||
if a == b { r := match_; } else { r := mismatch; }
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
def match_score(a: String, b: String, match_: Int, mismatch: Int) -> Int:
|
||||
if a == b:
|
||||
return match_
|
||||
return mismatch
|
||||
@@ -0,0 +1,2 @@
|
||||
def match_score(a, b, match, mismatch):
|
||||
return match if a == b else mismatch
|
||||
@@ -0,0 +1,43 @@
|
||||
{
|
||||
"name": "MatchScore",
|
||||
"summary": "The score of aligning letter A with letter B: Match when they are the same, Mismatch otherwise.",
|
||||
"tags": [
|
||||
"alignment",
|
||||
"scoring"
|
||||
],
|
||||
"sources": [
|
||||
{
|
||||
"title": "Durbin, Eddy, Krogh and Mitchison, Biological Sequence Analysis, section 2.2"
|
||||
}
|
||||
],
|
||||
"cases": [
|
||||
{
|
||||
"call": "MatchScore('A', 'A', 1, -1)"
|
||||
},
|
||||
{
|
||||
"call": "MatchScore('A', 'G', 1, -1)"
|
||||
},
|
||||
{
|
||||
"call": "MatchScore('T', 'T', 5, -4)"
|
||||
}
|
||||
],
|
||||
"references": [
|
||||
{
|
||||
"lang": "python",
|
||||
"level": "plain",
|
||||
"file": "ref/python/plain.py",
|
||||
"note": "The same test."
|
||||
},
|
||||
{
|
||||
"lang": "mojo",
|
||||
"level": "plain",
|
||||
"file": "ref/mojo/plain.mojo",
|
||||
"note": "The same test in Mojo; a letter is a String of one character."
|
||||
}
|
||||
],
|
||||
"proof": {
|
||||
"lang": "dafny",
|
||||
"file": "proof/match_score.dfy",
|
||||
"note": "The score is Match exactly when the letters agree."
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// MatchScore: the score of aligning letter A with letter B: Match when they
|
||||
// are the same letter, Mismatch otherwise. The simplest substitution score;
|
||||
// a substitution matrix such as BLOSUM62 replaces it for proteins.
|
||||
FUNCTION MatchScore(A : CHAR, B : CHAR, Match : INTEGER, Mismatch : INTEGER) RETURNS INTEGER
|
||||
ENSURE (A == B AND RESULT == Match) OR (A != B AND RESULT == Mismatch)
|
||||
IF A == B THEN
|
||||
RETURN Match
|
||||
ENDIF
|
||||
RETURN Mismatch
|
||||
ENDFUNCTION
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"name": "align",
|
||||
"title": "Sequence alignment",
|
||||
"summary": "Scores for aligning two sequences: gap models, letter scores, and global alignment by dynamic programming."
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
def needleman_wunsch_score(s: String, t: String, match_: Int, mismatch: Int, gap: Int) -> Int:
|
||||
var n = s.count_codepoints()
|
||||
var m = t.count_codepoints()
|
||||
var d = List[List[Int]]()
|
||||
for i in range(n + 1):
|
||||
var row = List[Int]()
|
||||
for j in range(m + 1):
|
||||
row.append(0)
|
||||
d.append(row^)
|
||||
for i in range(n + 1):
|
||||
d[i][0] = i * gap
|
||||
for j in range(1, m + 1):
|
||||
d[0][j] = j * gap
|
||||
for i in range(1, n + 1):
|
||||
for j in range(1, m + 1):
|
||||
var score = mismatch
|
||||
if String(s[codepoint=i - 1]) == String(t[codepoint=j - 1]):
|
||||
score = match_
|
||||
var best = d[i - 1][j - 1] + score
|
||||
if d[i - 1][j] + gap > best:
|
||||
best = d[i - 1][j] + gap
|
||||
if d[i][j - 1] + gap > best:
|
||||
best = d[i][j - 1] + gap
|
||||
d[i][j] = best
|
||||
return d[n][m]
|
||||
@@ -0,0 +1,12 @@
|
||||
def needleman_wunsch_score(s, t, match, mismatch, gap):
|
||||
n, m = len(s), len(t)
|
||||
d = [[0] * (m + 1) for _ in range(n + 1)]
|
||||
for i in range(n + 1):
|
||||
d[i][0] = i * gap
|
||||
for j in range(1, m + 1):
|
||||
d[0][j] = j * gap
|
||||
for i in range(1, n + 1):
|
||||
for j in range(1, m + 1):
|
||||
diagonal = d[i - 1][j - 1] + (match if s[i - 1] == t[j - 1] else mismatch)
|
||||
d[i][j] = max(diagonal, d[i - 1][j] + gap, d[i][j - 1] + gap)
|
||||
return d[n][m]
|
||||
@@ -0,0 +1,10 @@
|
||||
def needleman_wunsch_score(s, t, match, mismatch, gap):
|
||||
m = len(t)
|
||||
prev = [j * gap for j in range(m + 1)]
|
||||
for i in range(1, len(s) + 1):
|
||||
cur = [i * gap] + [0] * m
|
||||
for j in range(1, m + 1):
|
||||
diagonal = prev[j - 1] + (match if s[i - 1] == t[j - 1] else mismatch)
|
||||
cur[j] = max(diagonal, prev[j] + gap, cur[j - 1] + gap)
|
||||
prev = cur
|
||||
return prev[m]
|
||||
@@ -0,0 +1,48 @@
|
||||
{
|
||||
"name": "NeedlemanWunschScore",
|
||||
"summary": "The best score of a global alignment of two sequences with a linear gap model, by dynamic programming.",
|
||||
"tags": [
|
||||
"alignment",
|
||||
"dynamic programming"
|
||||
],
|
||||
"sources": [
|
||||
{
|
||||
"title": "Needleman and Wunsch, A general method applicable to the search for similarities in the amino acid sequence of two proteins, J. Mol. Biol. 48 (1970)",
|
||||
"url": "https://doi.org/10.1016/0022-2836(70)90057-4"
|
||||
}
|
||||
],
|
||||
"cases": [
|
||||
{
|
||||
"call": "NeedlemanWunschScore(\"GATTACA\", \"GCATGCU\", 1, -1, -1)"
|
||||
},
|
||||
{
|
||||
"call": "NeedlemanWunschScore(\"ACGT\", \"ACGT\", 1, -1, -2)"
|
||||
},
|
||||
{
|
||||
"call": "NeedlemanWunschScore(\"\", \"ACG\", 1, -1, -2)"
|
||||
},
|
||||
{
|
||||
"call": "NeedlemanWunschScore(\"AAAA\", \"TTTT\", 2, -3, -1)"
|
||||
}
|
||||
],
|
||||
"references": [
|
||||
{
|
||||
"lang": "python",
|
||||
"level": "plain",
|
||||
"file": "ref/python/plain.py",
|
||||
"note": "The same table, filled row by row."
|
||||
},
|
||||
{
|
||||
"lang": "python",
|
||||
"level": "two-rows",
|
||||
"file": "ref/python/two-rows.py",
|
||||
"note": "Only the previous row is kept: memory grows with the length of T alone, the first step toward Hirschberg's linear-space alignment."
|
||||
},
|
||||
{
|
||||
"lang": "mojo",
|
||||
"level": "plain",
|
||||
"file": "ref/mojo/plain.mojo",
|
||||
"note": "The same table in Mojo, as a list of rows."
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
// NeedlemanWunschScore: the best score of a global alignment of S and T
|
||||
// (Needleman and Wunsch, 1970) with a linear gap model, by dynamic
|
||||
// programming. D[I, J] is the best score of aligning the first I letters of
|
||||
// S with the first J letters of T; each cell takes the best of three moves:
|
||||
// align S[I - 1] with T[J - 1], or put a gap in one sequence or the other.
|
||||
USE align.MatchScore
|
||||
USE align.LinearGapPenalty
|
||||
FUNCTION NeedlemanWunschScore(S : STRING, T : STRING, Match : INTEGER, Mismatch : INTEGER, Gap : INTEGER) RETURNS INTEGER
|
||||
DECLARE N : INTEGER <- LENGTH(S)
|
||||
DECLARE M : INTEGER <- LENGTH(T)
|
||||
DECLARE D : ARRAY[N + 1, M + 1] OF INTEGER
|
||||
FOR I <- 0 TO N + 1
|
||||
D[I, 0] <- LinearGapPenalty(I, Gap)
|
||||
NEXT I
|
||||
FOR J <- 1 TO M + 1
|
||||
D[0, J] <- LinearGapPenalty(J, Gap)
|
||||
NEXT J
|
||||
FOR I <- 1 TO N + 1
|
||||
FOR J <- 1 TO M + 1
|
||||
DECLARE Diagonal : INTEGER <- D[I - 1, J - 1] + MatchScore(S[I - 1], T[J - 1], Match, Mismatch)
|
||||
DECLARE Down : INTEGER <- D[I - 1, J] + Gap
|
||||
DECLARE Across : INTEGER <- D[I, J - 1] + Gap
|
||||
D[I, J] <- MAX(Diagonal, Down, Across)
|
||||
NEXT J
|
||||
NEXT I
|
||||
RETURN D[N, M]
|
||||
ENDFUNCTION
|
||||
@@ -0,0 +1,4 @@
|
||||
def lennard_jones_force(r: Float64, epsilon: Float64, sigma: Float64) -> Float64:
|
||||
var s2 = (sigma / r) * (sigma / r)
|
||||
var s6 = s2 * s2 * s2
|
||||
return 24.0 * epsilon * (2.0 * s6 * s6 - s6) / r
|
||||
@@ -0,0 +1,4 @@
|
||||
def lennard_jones_force(r, epsilon, sigma):
|
||||
s2 = (sigma / r) * (sigma / r)
|
||||
s6 = s2 * s2 * s2
|
||||
return 24.0 * epsilon * (2.0 * s6 * s6 - s6) / r
|
||||
@@ -0,0 +1,38 @@
|
||||
{
|
||||
"name": "LennardJonesForce",
|
||||
"summary": "The force between two particles at distance R under the Lennard-Jones potential: positive pushes them apart.",
|
||||
"tags": [
|
||||
"molecular dynamics",
|
||||
"force"
|
||||
],
|
||||
"sources": [
|
||||
{
|
||||
"title": "Allen and Tildesley, Computer Simulation of Liquids, section 1.4"
|
||||
}
|
||||
],
|
||||
"cases": [
|
||||
{
|
||||
"call": "LennardJonesForce(1.0, 1.0, 1.0)"
|
||||
},
|
||||
{
|
||||
"call": "LennardJonesForce(1.122462048309373, 1.0, 1.0)"
|
||||
},
|
||||
{
|
||||
"call": "LennardJonesForce(2.5, 0.238, 3.4)"
|
||||
}
|
||||
],
|
||||
"references": [
|
||||
{
|
||||
"lang": "python",
|
||||
"level": "multiply",
|
||||
"file": "ref/python/multiply.py",
|
||||
"note": "The same multiplications."
|
||||
},
|
||||
{
|
||||
"lang": "mojo",
|
||||
"level": "multiply",
|
||||
"file": "ref/mojo/multiply.mojo",
|
||||
"note": "The same multiplications in Mojo."
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// LennardJonesForce: the force between two particles at distance R under
|
||||
// the Lennard-Jones potential, F(R) = -dV/dR
|
||||
// = 24 Epsilon / R (2 (Sigma / R)^12 - (Sigma / R)^6).
|
||||
// Positive pushes the particles apart; it is 0 at the bottom of the well.
|
||||
FUNCTION LennardJonesForce(R : FLOAT, Epsilon : FLOAT, Sigma : FLOAT) RETURNS FLOAT
|
||||
REQUIRE R > 0.0
|
||||
DECLARE S2 : FLOAT <- (Sigma / R) * (Sigma / R)
|
||||
DECLARE S6 : FLOAT <- S2 * S2 * S2
|
||||
RETURN 24.0 * Epsilon * (2.0 * S6 * S6 - S6) / R
|
||||
ENDFUNCTION
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"tool": "dafny",
|
||||
"version": "4.11.0",
|
||||
"flags": [
|
||||
"verify",
|
||||
"--resource-limit",
|
||||
"20000000"
|
||||
],
|
||||
"source_sha256": "0dc08be0ccc13bef1bbad5bc4fa8efc54c39005c803c770a79fec941913ffb86",
|
||||
"verdict": "verified",
|
||||
"summary": "14 verified, 0 errors"
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
function Pow(x: real, n: nat): real
|
||||
{
|
||||
if n == 0 then 1.0 else x * Pow(x, n - 1)
|
||||
}
|
||||
|
||||
function LJ(r: real, epsilon: real, sigma: real): real
|
||||
requires r > 0.0
|
||||
{
|
||||
4.0 * epsilon * (Pow(sigma / r, 12) - Pow(sigma / r, 6))
|
||||
}
|
||||
|
||||
lemma PowAdd(x: real, a: nat, b: nat)
|
||||
ensures Pow(x, a + b) == Pow(x, a) * Pow(x, b)
|
||||
{
|
||||
if a > 0 {
|
||||
PowAdd(x, a - 1, b);
|
||||
assert Pow(x, a + b) == x * Pow(x, a - 1 + b);
|
||||
}
|
||||
}
|
||||
|
||||
lemma Pow2(x: real)
|
||||
ensures Pow(x, 2) == x * x
|
||||
{
|
||||
assert Pow(x, 1) == x;
|
||||
}
|
||||
|
||||
lemma Pow6(s: real)
|
||||
ensures Pow(s, 6) == (s * s) * (s * s) * (s * s)
|
||||
{
|
||||
Pow2(s);
|
||||
PowAdd(s, 2, 2);
|
||||
PowAdd(s, 4, 2);
|
||||
}
|
||||
|
||||
lemma Pow12(s: real)
|
||||
ensures Pow(s, 12) == Pow(s, 6) * Pow(s, 6)
|
||||
{
|
||||
PowAdd(s, 6, 6);
|
||||
}
|
||||
|
||||
method LennardJones(r: real, epsilon: real, sigma: real) returns (v: real)
|
||||
requires r > 0.0
|
||||
ensures v == LJ(r, epsilon, sigma)
|
||||
{
|
||||
var s := sigma / r;
|
||||
var s2 := s * s;
|
||||
var s6 := s2 * s2 * s2;
|
||||
Pow6(s);
|
||||
Pow12(s);
|
||||
v := 4.0 * epsilon * (s6 * s6 - s6);
|
||||
}
|
||||
|
||||
// The potential crosses zero at R = Sigma.
|
||||
lemma ZeroAtSigma(epsilon: real, sigma: real)
|
||||
requires sigma > 0.0
|
||||
ensures LJ(sigma, epsilon, sigma) == 0.0
|
||||
{
|
||||
assert sigma / sigma == 1.0;
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
double lennard_jones(double r, double epsilon, double sigma) {
|
||||
double s2 = (sigma / r) * (sigma / r);
|
||||
double s6 = s2 * s2 * s2;
|
||||
return 4.0 * epsilon * (s6 * s6 - s6);
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
def lennard_jones(r: Float64, epsilon: Float64, sigma: Float64) -> Float64:
|
||||
var s2 = (sigma / r) * (sigma / r)
|
||||
var s6 = s2 * s2 * s2
|
||||
return 4.0 * epsilon * (s6 * s6 - s6)
|
||||
@@ -0,0 +1,4 @@
|
||||
def lennard_jones(r, epsilon, sigma):
|
||||
s2 = (sigma / r) * (sigma / r)
|
||||
s6 = s2 * s2 * s2
|
||||
return 4.0 * epsilon * (s6 * s6 - s6)
|
||||
@@ -0,0 +1,2 @@
|
||||
def lennard_jones(r, epsilon, sigma):
|
||||
return 4.0 * epsilon * ((sigma / r) ** 12 - (sigma / r) ** 6)
|
||||
@@ -0,0 +1,63 @@
|
||||
{
|
||||
"name": "LennardJones",
|
||||
"summary": "The Lennard-Jones pair potential at distance R, with well depth Epsilon and zero-crossing distance Sigma.",
|
||||
"tags": [
|
||||
"molecular dynamics",
|
||||
"pair potential"
|
||||
],
|
||||
"sources": [
|
||||
{
|
||||
"title": "Jones, On the determination of molecular fields II, Proc. R. Soc. A 106 (1924)",
|
||||
"url": "https://doi.org/10.1098/rspa.1924.0082"
|
||||
},
|
||||
{
|
||||
"title": "Allen and Tildesley, Computer Simulation of Liquids, section 1.4"
|
||||
}
|
||||
],
|
||||
"cases": [
|
||||
{
|
||||
"call": "LennardJones(1.0, 1.0, 1.0)"
|
||||
},
|
||||
{
|
||||
"call": "LennardJones(1.122462048309373, 1.0, 1.0)"
|
||||
},
|
||||
{
|
||||
"call": "LennardJones(2.5, 0.238, 3.4)"
|
||||
},
|
||||
{
|
||||
"call": "LennardJones(0.9, 1.0, 1.0)"
|
||||
}
|
||||
],
|
||||
"references": [
|
||||
{
|
||||
"lang": "python",
|
||||
"level": "multiply",
|
||||
"file": "ref/python/multiply.py",
|
||||
"note": "The same multiplications."
|
||||
},
|
||||
{
|
||||
"lang": "python",
|
||||
"level": "powers",
|
||||
"file": "ref/python/powers.py",
|
||||
"note": "The textbook formula with ** 12 and ** 6: the same function, rounded differently, held to agree to 12 digits.",
|
||||
"rel": 1e-12
|
||||
},
|
||||
{
|
||||
"lang": "c",
|
||||
"level": "multiply",
|
||||
"file": "ref/c/multiply.c",
|
||||
"note": "The same multiplications in C."
|
||||
},
|
||||
{
|
||||
"lang": "mojo",
|
||||
"level": "multiply",
|
||||
"file": "ref/mojo/multiply.mojo",
|
||||
"note": "The same multiplications in Mojo."
|
||||
}
|
||||
],
|
||||
"proof": {
|
||||
"lang": "dafny",
|
||||
"file": "proof/lennard_jones.dfy",
|
||||
"note": "Over real numbers the multiplications compute the textbook formula exactly, and V(Sigma) = 0."
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// LennardJones: the Lennard-Jones pair potential at distance R,
|
||||
// V(R) = 4 Epsilon ((Sigma / R)^12 - (Sigma / R)^6). Epsilon is the depth of
|
||||
// the well, reached at R = 2^(1/6) Sigma, and V is 0 at R = Sigma. The powers
|
||||
// are built by multiplication from (Sigma / R)^2, as simulation codes do,
|
||||
// which is faster than ^ and rounds a little differently.
|
||||
FUNCTION LennardJones(R : FLOAT, Epsilon : FLOAT, Sigma : FLOAT) RETURNS FLOAT
|
||||
REQUIRE R > 0.0
|
||||
DECLARE S2 : FLOAT <- (Sigma / R) * (Sigma / R)
|
||||
DECLARE S6 : FLOAT <- S2 * S2 * S2
|
||||
RETURN 4.0 * Epsilon * (S6 * S6 - S6)
|
||||
ENDFUNCTION
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"tool": "dafny",
|
||||
"version": "4.11.0",
|
||||
"flags": [
|
||||
"verify",
|
||||
"--resource-limit",
|
||||
"20000000"
|
||||
],
|
||||
"source_sha256": "9881217a957b2d8700997063d9bda129337d7c266a287aeb76a0881a1b0b1b08",
|
||||
"verdict": "verified",
|
||||
"summary": "4 verified, 0 errors"
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
// MinimumImage, over real numbers: the image lies in [-box / 2, box / 2) and is a whole number of box lengths from dx.
|
||||
lemma ScaledBounds(g: real, box: real)
|
||||
requires box > 0.0
|
||||
requires -0.5 <= g < 0.5
|
||||
ensures -box / 2.0 <= g * box < box / 2.0
|
||||
{
|
||||
assert (g + 0.5) * box >= 0.0;
|
||||
assert (0.5 - g) * box > 0.0;
|
||||
}
|
||||
|
||||
method MinimumImage(dx: real, box: real) returns (r: real, k: int)
|
||||
requires box > 0.0
|
||||
ensures r == dx - box * (k as real)
|
||||
ensures -box / 2.0 <= r < box / 2.0
|
||||
{
|
||||
var q := dx / box + 0.5;
|
||||
k := q.Floor;
|
||||
assert (k as real) <= q < (k as real) + 1.0;
|
||||
var g := q - (k as real) - 0.5;
|
||||
assert -0.5 <= g < 0.5;
|
||||
assert dx == (q - 0.5) * box;
|
||||
r := dx - box * (k as real);
|
||||
assert r == g * box;
|
||||
ScaledBounds(g, box);
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
#include <math.h>
|
||||
|
||||
double minimum_image(double dx, double box) {
|
||||
return dx - box * floor(dx / box + 0.5);
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
from std.math import floor
|
||||
|
||||
|
||||
def minimum_image(dx: Float64, box: Float64) -> Float64:
|
||||
return dx - box * floor(dx / box + 0.5)
|
||||
@@ -0,0 +1,5 @@
|
||||
import math
|
||||
|
||||
|
||||
def minimum_image(dx, box):
|
||||
return dx - box * math.floor(dx / box + 0.5)
|
||||
@@ -0,0 +1,55 @@
|
||||
{
|
||||
"name": "MinimumImage",
|
||||
"summary": "A displacement brought to its nearest periodic image in a box of length Box: the minimum image convention.",
|
||||
"tags": [
|
||||
"molecular dynamics",
|
||||
"periodic boundary conditions"
|
||||
],
|
||||
"sources": [
|
||||
{
|
||||
"title": "Allen and Tildesley, Computer Simulation of Liquids, section 1.5.2"
|
||||
}
|
||||
],
|
||||
"cases": [
|
||||
{
|
||||
"call": "MinimumImage(0.75, 1.0)"
|
||||
},
|
||||
{
|
||||
"call": "MinimumImage(-0.75, 1.0)"
|
||||
},
|
||||
{
|
||||
"call": "MinimumImage(2.3, 1.0)"
|
||||
},
|
||||
{
|
||||
"call": "MinimumImage(0.5, 1.0)"
|
||||
},
|
||||
{
|
||||
"call": "MinimumImage(-12.0, 10.0)"
|
||||
}
|
||||
],
|
||||
"references": [
|
||||
{
|
||||
"lang": "python",
|
||||
"level": "plain",
|
||||
"file": "ref/python/plain.py",
|
||||
"note": "The same formula with math.floor."
|
||||
},
|
||||
{
|
||||
"lang": "c",
|
||||
"level": "plain",
|
||||
"file": "ref/c/plain.c",
|
||||
"note": "The same formula with floor from math.h."
|
||||
},
|
||||
{
|
||||
"lang": "mojo",
|
||||
"level": "plain",
|
||||
"file": "ref/mojo/plain.mojo",
|
||||
"note": "The same formula in Mojo, with floor from std.math."
|
||||
}
|
||||
],
|
||||
"proof": {
|
||||
"lang": "dafny",
|
||||
"file": "proof/minimum_image.dfy",
|
||||
"note": "Over real numbers the result lies in [-Box / 2, Box / 2) and differs from Dx by a whole number of box lengths."
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// MinimumImage: the displacement Dx brought to its nearest periodic image in
|
||||
// a box of length Box, the minimum image convention: Dx minus the whole
|
||||
// number of box lengths that brings it into [-Box / 2, Box / 2). FLOOR of
|
||||
// Dx / Box + 0.5 rounds halves upward, so exactly half a box maps to -Box / 2.
|
||||
FUNCTION MinimumImage(Dx : FLOAT, Box : FLOAT) RETURNS FLOAT
|
||||
REQUIRE Box > 0.0
|
||||
RETURN Dx - Box * FLOOR(Dx / Box + 0.5)
|
||||
ENDFUNCTION
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"name": "md",
|
||||
"title": "Molecular simulation",
|
||||
"summary": "Pair potentials, forces and periodic boxes, the inner loop of molecular dynamics."
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
from std.math import floor, sqrt
|
||||
|
||||
|
||||
def pair_distance(a: List[Float64], b: List[Float64], box: Float64) -> Float64:
|
||||
var total: Float64 = 0.0
|
||||
for i in range(3):
|
||||
var d = a[i] - b[i]
|
||||
d = d - box * floor(d / box + 0.5)
|
||||
total = total + d * d
|
||||
return sqrt(total)
|
||||
@@ -0,0 +1,7 @@
|
||||
import math
|
||||
|
||||
|
||||
def pair_distance(a, b, box):
|
||||
d = [x - y for x, y in zip(a, b)]
|
||||
d = [v - box * math.floor(v / box + 0.5) for v in d]
|
||||
return math.hypot(*d)
|
||||
@@ -0,0 +1,40 @@
|
||||
{
|
||||
"name": "PairDistance",
|
||||
"summary": "The distance between two points in a cubic periodic box, each coordinate difference taken by the minimum image convention.",
|
||||
"tags": [
|
||||
"molecular dynamics",
|
||||
"periodic boundary conditions"
|
||||
],
|
||||
"sources": [
|
||||
{
|
||||
"title": "Allen and Tildesley, Computer Simulation of Liquids, section 1.5.2"
|
||||
}
|
||||
],
|
||||
"cases": [
|
||||
{
|
||||
"call": "PairDistance([0.1, 0.1, 0.1], [0.9, 0.9, 0.9], 1.0)"
|
||||
},
|
||||
{
|
||||
"call": "PairDistance([0.0, 0.0, 0.0], [3.0, 4.0, 0.0], 20.0)"
|
||||
},
|
||||
{
|
||||
"call": "PairDistance([1.0, 2.0, 3.0], [1.0, 2.0, 3.0], 5.0)"
|
||||
}
|
||||
],
|
||||
"references": [
|
||||
{
|
||||
"lang": "python",
|
||||
"level": "plain",
|
||||
"file": "ref/python/plain.py",
|
||||
"note": "The same steps, with math.hypot of all three differences at once.",
|
||||
"rel": 1e-15
|
||||
},
|
||||
{
|
||||
"lang": "mojo",
|
||||
"level": "plain",
|
||||
"file": "ref/mojo/plain.mojo",
|
||||
"note": "The same steps in Mojo, with the square root of the sum of squares.",
|
||||
"rel": 1e-15
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// PairDistance: the distance between two points in a cubic periodic box of
|
||||
// side Box, each coordinate difference taken by the minimum image
|
||||
// convention. HYPOT adds the squares without overflowing on the way.
|
||||
USE md.MinimumImage
|
||||
FUNCTION PairDistance(A : LIST OF FLOAT, B : LIST OF FLOAT, Box : FLOAT) RETURNS FLOAT
|
||||
REQUIRE LENGTH(A) == 3 AND LENGTH(B) == 3 AND Box > 0.0
|
||||
DECLARE Dx : FLOAT <- MinimumImage(A[0] - B[0], Box)
|
||||
DECLARE Dy : FLOAT <- MinimumImage(A[1] - B[1], Box)
|
||||
DECLARE Dz : FLOAT <- MinimumImage(A[2] - B[2], Box)
|
||||
RETURN HYPOT(HYPOT(Dx, Dy), Dz)
|
||||
ENDFUNCTION
|
||||
@@ -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,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 does not grow with the length of the list.",
|
||||
"tags": [
|
||||
"summation",
|
||||
"rounding",
|
||||
"precision"
|
||||
],
|
||||
"sources": [
|
||||
{
|
||||
"title": "Kahan, 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, section 4.3"
|
||||
}
|
||||
],
|
||||
"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,17 @@
|
||||
// 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 does not grow with the length of
|
||||
// the list. 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,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,37 @@
|
||||
{
|
||||
"name": "LinearInterp",
|
||||
"summary": "The value at X on the straight line through (X0, Y0) and (X1, Y1).",
|
||||
"tags": [
|
||||
"interpolation"
|
||||
],
|
||||
"sources": [
|
||||
{
|
||||
"title": "Any numerical methods text: linear interpolation"
|
||||
}
|
||||
],
|
||||
"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 what rounding does to each."
|
||||
}
|
||||
@@ -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,38 @@
|
||||
{
|
||||
"name": "Trapezoid",
|
||||
"summary": "The integral of samples Y taken H apart, by the trapezoidal rule.",
|
||||
"tags": [
|
||||
"integration",
|
||||
"quadrature"
|
||||
],
|
||||
"sources": [
|
||||
{
|
||||
"title": "Any numerical methods text: the composite trapezoidal rule"
|
||||
}
|
||||
],
|
||||
"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
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"tool": "dafny",
|
||||
"version": "4.11.0",
|
||||
"flags": [
|
||||
"verify",
|
||||
"--resource-limit",
|
||||
"20000000"
|
||||
],
|
||||
"source_sha256": "f2c5d94943f4b176c99c71e1073bb1c6a8ec6d2eb560d03526aaa3476b9162b1",
|
||||
"verdict": "verified",
|
||||
"summary": "5 verified, 0 errors"
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
function GC(s: string, n: nat): nat
|
||||
requires n <= |s|
|
||||
{
|
||||
if n == 0 then 0 else GC(s, n - 1) + (if s[n - 1] == 'G' || s[n - 1] == 'C' then 1 else 0)
|
||||
}
|
||||
|
||||
lemma GCAtMost(s: string, n: nat)
|
||||
requires n <= |s|
|
||||
ensures GC(s, n) <= n
|
||||
{
|
||||
if n > 0 { GCAtMost(s, n - 1); }
|
||||
}
|
||||
|
||||
method GCContent(s: string) returns (f: real)
|
||||
requires |s| > 0
|
||||
ensures f == (GC(s, |s|) as real) / (|s| as real)
|
||||
ensures 0.0 <= f <= 1.0
|
||||
{
|
||||
var count := 0;
|
||||
var i := 0;
|
||||
while i < |s|
|
||||
invariant 0 <= i <= |s|
|
||||
invariant count == GC(s, i)
|
||||
{
|
||||
if s[i] == 'G' || s[i] == 'C' { count := count + 1; }
|
||||
i := i + 1;
|
||||
}
|
||||
GCAtMost(s, |s|);
|
||||
f := (count as real) / (|s| as real);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
def gc_content(s: String) -> Float64:
|
||||
var count = 0
|
||||
for i in range(s.count_codepoints()):
|
||||
var c = String(s[codepoint=i])
|
||||
if c == "G" or c == "C":
|
||||
count = count + 1
|
||||
return Float64(count) / Float64(s.count_codepoints())
|
||||
@@ -0,0 +1,6 @@
|
||||
def gc_content(s):
|
||||
count = 0
|
||||
for c in s:
|
||||
if c == "G" or c == "C":
|
||||
count += 1
|
||||
return count / len(s)
|
||||
@@ -0,0 +1,43 @@
|
||||
{
|
||||
"name": "GCContent",
|
||||
"summary": "The fraction of the letters of a sequence that are G or C.",
|
||||
"tags": [
|
||||
"sequences",
|
||||
"counting"
|
||||
],
|
||||
"sources": [
|
||||
{
|
||||
"title": "Any molecular biology text: GC content"
|
||||
}
|
||||
],
|
||||
"cases": [
|
||||
{
|
||||
"call": "GCContent(\"GATTACA\")"
|
||||
},
|
||||
{
|
||||
"call": "GCContent(\"GGCC\")"
|
||||
},
|
||||
{
|
||||
"call": "GCContent(\"ATAT\")"
|
||||
}
|
||||
],
|
||||
"references": [
|
||||
{
|
||||
"lang": "python",
|
||||
"level": "plain",
|
||||
"file": "ref/python/plain.py",
|
||||
"note": "The same count."
|
||||
},
|
||||
{
|
||||
"lang": "mojo",
|
||||
"level": "plain",
|
||||
"file": "ref/mojo/plain.mojo",
|
||||
"note": "The same count in Mojo."
|
||||
}
|
||||
],
|
||||
"proof": {
|
||||
"lang": "dafny",
|
||||
"file": "proof/gc_content.dfy",
|
||||
"note": "The count never exceeds the length, so the fraction lies in [0, 1]."
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// GCContent: the fraction of the letters of S that are G or C. Other letters
|
||||
// (N for an unknown base, lowercase) count in the length and not in the count.
|
||||
FUNCTION GCContent(S : STRING) RETURNS FLOAT
|
||||
REQUIRE LENGTH(S) > 0
|
||||
DECLARE Count : INTEGER <- 0
|
||||
FOR I <- 0 TO LENGTH(S)
|
||||
IF S[I] == 'G' OR S[I] == 'C' THEN
|
||||
Count <- Count + 1
|
||||
ENDIF
|
||||
NEXT I
|
||||
RETURN Count / LENGTH(S)
|
||||
ENDFUNCTION
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"tool": "dafny",
|
||||
"version": "4.11.0",
|
||||
"flags": [
|
||||
"verify",
|
||||
"--resource-limit",
|
||||
"20000000"
|
||||
],
|
||||
"source_sha256": "0a1be187ee17e37bf36220aa4c326a907dd0e39eb38710cd77ed05bed2479950",
|
||||
"verdict": "verified",
|
||||
"summary": "5 verified, 0 errors"
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
function Mismatches(s: string, t: string, n: nat): nat
|
||||
requires n <= |s| && n <= |t|
|
||||
{
|
||||
if n == 0 then 0 else Mismatches(s, t, n - 1) + (if s[n - 1] != t[n - 1] then 1 else 0)
|
||||
}
|
||||
|
||||
lemma MismatchesAtMost(s: string, t: string, n: nat)
|
||||
requires n <= |s| && n <= |t|
|
||||
ensures Mismatches(s, t, n) <= n
|
||||
{
|
||||
if n > 0 { MismatchesAtMost(s, t, n - 1); }
|
||||
}
|
||||
|
||||
method HammingDistance(s: string, t: string) returns (count: int)
|
||||
requires |s| == |t|
|
||||
ensures count == Mismatches(s, t, |s|)
|
||||
ensures 0 <= count <= |s|
|
||||
{
|
||||
count := 0;
|
||||
var i := 0;
|
||||
while i < |s|
|
||||
invariant 0 <= i <= |s|
|
||||
invariant count == Mismatches(s, t, i)
|
||||
{
|
||||
if s[i] != t[i] { count := count + 1; }
|
||||
i := i + 1;
|
||||
}
|
||||
MismatchesAtMost(s, t, |s|);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
def hamming_distance(s: String, t: String) -> Int:
|
||||
var count = 0
|
||||
for i in range(s.count_codepoints()):
|
||||
if String(s[codepoint=i]) != String(t[codepoint=i]):
|
||||
count = count + 1
|
||||
return count
|
||||
@@ -0,0 +1,2 @@
|
||||
def hamming_distance(s, t):
|
||||
return sum(1 for a, b in zip(s, t) if a != b)
|
||||
@@ -0,0 +1,43 @@
|
||||
{
|
||||
"name": "HammingDistance",
|
||||
"summary": "How many positions of two sequences of the same length hold different letters.",
|
||||
"tags": [
|
||||
"sequences",
|
||||
"distance"
|
||||
],
|
||||
"sources": [
|
||||
{
|
||||
"title": "Hamming, Error detecting and error correcting codes, Bell System Technical Journal 29 (1950)"
|
||||
}
|
||||
],
|
||||
"cases": [
|
||||
{
|
||||
"call": "HammingDistance(\"GATTACA\", \"GACTATA\")"
|
||||
},
|
||||
{
|
||||
"call": "HammingDistance(\"\", \"\")"
|
||||
},
|
||||
{
|
||||
"call": "HammingDistance(\"AAAA\", \"TTTT\")"
|
||||
}
|
||||
],
|
||||
"references": [
|
||||
{
|
||||
"lang": "python",
|
||||
"level": "plain",
|
||||
"file": "ref/python/plain.py",
|
||||
"note": "The same count."
|
||||
},
|
||||
{
|
||||
"lang": "mojo",
|
||||
"level": "plain",
|
||||
"file": "ref/mojo/plain.mojo",
|
||||
"note": "The same count in Mojo."
|
||||
}
|
||||
],
|
||||
"proof": {
|
||||
"lang": "dafny",
|
||||
"file": "proof/hamming_distance.dfy",
|
||||
"note": "The loop computes the number of mismatched positions, which lies in [0, |S|]."
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// HammingDistance: how many positions of two sequences of the same length
|
||||
// hold different letters. The INVARIANT is what makes the ENSURE provable:
|
||||
// after I positions, at most I mismatches can have been counted.
|
||||
FUNCTION HammingDistance(S : STRING, T : STRING) RETURNS INTEGER
|
||||
REQUIRE LENGTH(S) == LENGTH(T)
|
||||
ENSURE 0 <= RESULT AND RESULT <= LENGTH(S)
|
||||
DECLARE Count : INTEGER <- 0
|
||||
FOR I <- 0 TO LENGTH(S)
|
||||
INVARIANT 0 <= Count AND Count <= I
|
||||
IF S[I] != T[I] THEN
|
||||
Count <- Count + 1
|
||||
ENDIF
|
||||
NEXT I
|
||||
RETURN Count
|
||||
ENDFUNCTION
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"name": "seq",
|
||||
"title": "Sequences",
|
||||
"summary": "Counting and comparing the letters of biological sequences."
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
from std.math import exp
|
||||
|
||||
|
||||
def erf_approx(x: Float64) -> Float64:
|
||||
var sign: Float64 = 1.0
|
||||
if x < 0.0:
|
||||
sign = -1.0
|
||||
var a = abs(x)
|
||||
var t = 1.0 / (1.0 + 0.3275911 * a)
|
||||
var poly = ((((1.061405429 * t - 1.453152027) * t + 1.421413741) * t - 0.284496736) * t + 0.254829592) * t
|
||||
return sign * (1.0 - poly * exp(-a * a))
|
||||
@@ -0,0 +1,5 @@
|
||||
import math
|
||||
|
||||
|
||||
def erf_approx(x):
|
||||
return math.erf(x)
|
||||
@@ -0,0 +1,9 @@
|
||||
import math
|
||||
|
||||
|
||||
def erf_approx(x):
|
||||
sign = -1.0 if x < 0.0 else 1.0
|
||||
a = abs(x)
|
||||
t = 1.0 / (1.0 + 0.3275911 * a)
|
||||
poly = ((((1.061405429 * t - 1.453152027) * t + 1.421413741) * t - 0.284496736) * t + 0.254829592) * t
|
||||
return sign * (1.0 - poly * math.exp(-a * a))
|
||||
@@ -0,0 +1,52 @@
|
||||
{
|
||||
"name": "ErfApprox",
|
||||
"summary": "The error function by Abramowitz and Stegun's formula 7.1.26, within 1.5e-7 of the exact value everywhere.",
|
||||
"tags": [
|
||||
"special functions",
|
||||
"approximation",
|
||||
"statistics"
|
||||
],
|
||||
"sources": [
|
||||
{
|
||||
"title": "Abramowitz and Stegun, Handbook of Mathematical Functions, formula 7.1.26"
|
||||
}
|
||||
],
|
||||
"cases": [
|
||||
{
|
||||
"call": "ErfApprox(0.0)"
|
||||
},
|
||||
{
|
||||
"call": "ErfApprox(0.5)"
|
||||
},
|
||||
{
|
||||
"call": "ErfApprox(-1.0)"
|
||||
},
|
||||
{
|
||||
"call": "ErfApprox(2.0)"
|
||||
},
|
||||
{
|
||||
"call": "ErfApprox(3.5)"
|
||||
}
|
||||
],
|
||||
"references": [
|
||||
{
|
||||
"lang": "python",
|
||||
"level": "plain",
|
||||
"file": "ref/python/plain.py",
|
||||
"note": "The same formula."
|
||||
},
|
||||
{
|
||||
"lang": "python",
|
||||
"level": "libm",
|
||||
"file": "ref/python/libm.py",
|
||||
"note": "math.erf, the C library's correctly rounded erf: the approximation must stay within its stated 1.5e-7 of it.",
|
||||
"abs": 1.5e-07
|
||||
},
|
||||
{
|
||||
"lang": "mojo",
|
||||
"level": "plain",
|
||||
"file": "ref/mojo/plain.mojo",
|
||||
"note": "The same formula in Mojo."
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// ErfApprox: the error function by Abramowitz and Stegun's formula 7.1.26,
|
||||
// with an absolute error below 1.5e-7 for every X. It is an approximation
|
||||
// with a stated bound, not the correctly rounded erf: the libm reference
|
||||
// holds it to that bound. ERF(X) for X < 0 is -ERF(-X), so the sign is set
|
||||
// aside first.
|
||||
FUNCTION ErfApprox(X : FLOAT) RETURNS FLOAT
|
||||
DECLARE Sign : FLOAT <- 1.0
|
||||
IF X < 0.0 THEN
|
||||
Sign <- -1.0
|
||||
ENDIF
|
||||
DECLARE A : FLOAT <- ABS(X)
|
||||
DECLARE T : FLOAT <- 1.0 / (1.0 + 0.3275911 * A)
|
||||
DECLARE Poly : FLOAT <- ((((1.061405429 * T - 1.453152027) * T + 1.421413741) * T - 0.284496736) * T + 0.254829592) * T
|
||||
RETURN Sign * (1.0 - Poly * EXP(-A * A))
|
||||
ENDFUNCTION
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"name": "special",
|
||||
"title": "Special functions",
|
||||
"summary": "Functions with no exact built-in, as approximations with a stated error bound."
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"tool": "dafny",
|
||||
"version": "4.11.0",
|
||||
"flags": [
|
||||
"verify",
|
||||
"--resource-limit",
|
||||
"20000000"
|
||||
],
|
||||
"source_sha256": "2c1cfe5cfd7f6a1f1d930e526ab91e381efef2b847ff56cef529d111c7e97fad",
|
||||
"verdict": "verified",
|
||||
"summary": "9 verified, 0 errors"
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
// Mean, over real numbers. The FLOAT version rounds; this one does not.
|
||||
function Sum(x: seq<real>): real
|
||||
{
|
||||
if |x| == 0 then 0.0 else Sum(x[..|x| - 1]) + x[|x| - 1]
|
||||
}
|
||||
|
||||
method Mean(x: seq<real>) returns (m: real)
|
||||
requires |x| > 0
|
||||
ensures m == Sum(x) / (|x| as real)
|
||||
{
|
||||
var total := 0.0;
|
||||
var i := 0;
|
||||
while i < |x|
|
||||
invariant 0 <= i <= |x|
|
||||
invariant total == Sum(x[..i])
|
||||
{
|
||||
assert x[..i + 1][..i] == x[..i];
|
||||
total := total + x[i];
|
||||
i := i + 1;
|
||||
}
|
||||
assert x[..|x|] == x;
|
||||
m := total / (|x| as real);
|
||||
}
|
||||
|
||||
// Every value at most hi: the sum is at most |x| * hi.
|
||||
lemma SumAtMost(x: seq<real>, hi: real)
|
||||
requires forall i :: 0 <= i < |x| ==> x[i] <= hi
|
||||
ensures Sum(x) <= (|x| as real) * hi
|
||||
{
|
||||
if |x| > 0 { SumAtMost(x[..|x| - 1], hi); }
|
||||
}
|
||||
|
||||
// Every value at least lo: the sum is at least |x| * lo.
|
||||
lemma SumAtLeast(x: seq<real>, lo: real)
|
||||
requires forall i :: 0 <= i < |x| ==> lo <= x[i]
|
||||
ensures (|x| as real) * lo <= Sum(x)
|
||||
{
|
||||
if |x| > 0 { SumAtLeast(x[..|x| - 1], lo); }
|
||||
}
|
||||
|
||||
// In exact arithmetic the mean lies between any lower and upper bound of the values.
|
||||
lemma MeanBetween(x: seq<real>, lo: real, hi: real)
|
||||
requires |x| > 0
|
||||
requires forall i :: 0 <= i < |x| ==> lo <= x[i] <= hi
|
||||
ensures lo <= Sum(x) / (|x| as real) <= hi
|
||||
{
|
||||
SumAtMost(x, hi);
|
||||
SumAtLeast(x, lo);
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
double mean(const double *x, long n) {
|
||||
double total = 0.0;
|
||||
for (long i = 0; i < n; i++) total += x[i];
|
||||
return total / (double)n;
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
def mean(x: List[Float64]) -> Float64:
|
||||
var total: Float64 = 0.0
|
||||
for i in range(len(x)):
|
||||
total = total + x[i]
|
||||
return total / Float64(len(x))
|
||||
@@ -0,0 +1,17 @@
|
||||
def mean(x: List[Float64]) -> Float64:
|
||||
var s0: Float64 = 0.0
|
||||
var s1: Float64 = 0.0
|
||||
var s2: Float64 = 0.0
|
||||
var s3: Float64 = 0.0
|
||||
var n = len(x)
|
||||
var i = 0
|
||||
while i + 4 <= n:
|
||||
s0 = s0 + x[i]
|
||||
s1 = s1 + x[i + 1]
|
||||
s2 = s2 + x[i + 2]
|
||||
s3 = s3 + x[i + 3]
|
||||
i = i + 4
|
||||
while i < n:
|
||||
s0 = s0 + x[i]
|
||||
i = i + 1
|
||||
return ((s0 + s1) + (s2 + s3)) / Float64(n)
|
||||
@@ -0,0 +1,5 @@
|
||||
import math
|
||||
|
||||
|
||||
def mean(x):
|
||||
return math.fsum(x) / len(x)
|
||||
@@ -0,0 +1,5 @@
|
||||
def mean(x):
|
||||
total = 0.0
|
||||
for v in x:
|
||||
total += v
|
||||
return total / len(x)
|
||||
@@ -0,0 +1,69 @@
|
||||
{
|
||||
"name": "Mean",
|
||||
"summary": "The arithmetic mean of a list of numbers.",
|
||||
"tags": [
|
||||
"statistics",
|
||||
"summation"
|
||||
],
|
||||
"sources": [
|
||||
{
|
||||
"title": "Any statistics text: the sample mean, the sum divided by the count"
|
||||
}
|
||||
],
|
||||
"cases": [
|
||||
{
|
||||
"call": "Mean([1.0, 2.0, 3.0, 4.0])"
|
||||
},
|
||||
{
|
||||
"call": "Mean([0.1, 0.1, 0.1])"
|
||||
},
|
||||
{
|
||||
"call": "Mean([-5.0, 5.0])"
|
||||
},
|
||||
{
|
||||
"call": "Mean([1.0e300, 1.0e300])"
|
||||
},
|
||||
{
|
||||
"call": "Mean([2.5])"
|
||||
}
|
||||
],
|
||||
"references": [
|
||||
{
|
||||
"lang": "python",
|
||||
"level": "plain",
|
||||
"file": "ref/python/plain.py",
|
||||
"note": "The same loop: one running total, added to in order."
|
||||
},
|
||||
{
|
||||
"lang": "python",
|
||||
"level": "fsum",
|
||||
"file": "ref/python/fsum.py",
|
||||
"note": "math.fsum rounds the sum once, exactly; the plain loop rounds at every step, so the two can differ in the last digit.",
|
||||
"rel": 1e-15
|
||||
},
|
||||
{
|
||||
"lang": "c",
|
||||
"level": "plain",
|
||||
"file": "ref/c/plain.c",
|
||||
"note": "The same loop in C. Without -ffast-math the compiler keeps the additions in order at every optimization level."
|
||||
},
|
||||
{
|
||||
"lang": "mojo",
|
||||
"level": "plain",
|
||||
"file": "ref/mojo/plain.mojo",
|
||||
"note": "The same loop in Mojo."
|
||||
},
|
||||
{
|
||||
"lang": "mojo",
|
||||
"level": "unrolled",
|
||||
"file": "ref/mojo/unrolled.mojo",
|
||||
"note": "Four running totals, as a vectorizing compiler keeps them: faster on long lists, and the additions happen in a different order, so the last digit can differ.",
|
||||
"rel": 1e-15
|
||||
}
|
||||
],
|
||||
"proof": {
|
||||
"lang": "dafny",
|
||||
"file": "proof/mean.dfy",
|
||||
"note": "Over real numbers: the loop computes Sum(X) / |X|, and the mean lies between the smallest and largest value (rounding can push the FLOAT result just outside, as Mean([0.1, 0.1, 0.1]) shows)."
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// Mean: the arithmetic mean of a list of numbers.
|
||||
// The running total is a FLOAT, rounded at every addition, so the mean of
|
||||
// [0.1, 0.1, 0.1] comes out a little above 0.1. For long lists of values of
|
||||
// very different sizes, sum with numerics.KahanSum instead.
|
||||
FUNCTION Mean(X : LIST OF FLOAT) RETURNS FLOAT
|
||||
REQUIRE LENGTH(X) > 0
|
||||
DECLARE Total : FLOAT <- 0.0
|
||||
FOR I <- 0 TO LENGTH(X)
|
||||
Total <- Total + X[I]
|
||||
NEXT I
|
||||
RETURN Total / LENGTH(X)
|
||||
ENDFUNCTION
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"name": "stats",
|
||||
"title": "Statistics",
|
||||
"summary": "Summaries of a list of measurements: where it sits and how far it spreads."
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
def variance(x: List[Float64]) -> Float64:
|
||||
var m: Float64 = 0.0
|
||||
for i in range(len(x)):
|
||||
m = m + x[i]
|
||||
m = m / Float64(len(x))
|
||||
var total: Float64 = 0.0
|
||||
for i in range(len(x)):
|
||||
total = total + (x[i] - m) * (x[i] - m)
|
||||
return total / Float64(len(x) - 1)
|
||||
@@ -0,0 +1,9 @@
|
||||
def variance(x):
|
||||
m = 0.0
|
||||
for v in x:
|
||||
m += v
|
||||
m /= len(x)
|
||||
total = 0.0
|
||||
for v in x:
|
||||
total += (v - m) * (v - m)
|
||||
return total / (len(x) - 1)
|
||||
@@ -0,0 +1,5 @@
|
||||
import statistics
|
||||
|
||||
|
||||
def variance(x):
|
||||
return statistics.variance(x)
|
||||
@@ -0,0 +1,46 @@
|
||||
{
|
||||
"name": "Variance",
|
||||
"summary": "The sample variance of a list of numbers (dividing by N - 1), computed in two passes.",
|
||||
"tags": [
|
||||
"statistics",
|
||||
"cancellation"
|
||||
],
|
||||
"sources": [
|
||||
{
|
||||
"title": "Chan, Golub and LeVeque, Algorithms for computing the sample variance (1983)",
|
||||
"url": "https://doi.org/10.1080/00031305.1983.10483115"
|
||||
}
|
||||
],
|
||||
"cases": [
|
||||
{
|
||||
"call": "Variance([2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0])"
|
||||
},
|
||||
{
|
||||
"call": "Variance([1.0, 2.0])"
|
||||
},
|
||||
{
|
||||
"call": "Variance([1.0e9 + 4.0, 1.0e9 + 7.0, 1.0e9 + 13.0, 1.0e9 + 16.0])"
|
||||
}
|
||||
],
|
||||
"references": [
|
||||
{
|
||||
"lang": "python",
|
||||
"level": "plain",
|
||||
"file": "ref/python/plain.py",
|
||||
"note": "The same two passes."
|
||||
},
|
||||
{
|
||||
"lang": "python",
|
||||
"level": "statistics",
|
||||
"file": "ref/python/statistics.py",
|
||||
"note": "The standard library's statistics.variance, which works with exact fractions inside: a different method, held to agree to 12 digits.",
|
||||
"rel": 1e-12
|
||||
},
|
||||
{
|
||||
"lang": "mojo",
|
||||
"level": "plain",
|
||||
"file": "ref/mojo/plain.mojo",
|
||||
"note": "The same two passes in Mojo."
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// Variance: the sample variance (dividing by N - 1), in two passes: the mean
|
||||
// first, then the squared distances from it. The one-pass formula
|
||||
// (SUM X^2 - N * MEAN^2) / (N - 1) subtracts two nearly equal large numbers
|
||||
// and loses every digit when the values sit far from 0, as the third case shows.
|
||||
USE stats.Mean
|
||||
FUNCTION Variance(X : LIST OF FLOAT) RETURNS FLOAT
|
||||
REQUIRE LENGTH(X) >= 2
|
||||
DECLARE M : FLOAT <- Mean(X)
|
||||
DECLARE Total : FLOAT <- 0.0
|
||||
FOR I <- 0 TO LENGTH(X)
|
||||
Total <- Total + (X[I] - M) * (X[I] - M)
|
||||
NEXT I
|
||||
RETURN Total / (LENGTH(X) - 1)
|
||||
ENDFUNCTION
|
||||
Reference in new issue
Block a user