37 lines
1.2 KiB
Nix
37 lines
1.2 KiB
Nix
{ lib, ... }:
|
|
with lib;
|
|
with builtins; rec {
|
|
readLines = f: init (filter isString (split "\n" (readFile f)));
|
|
reSplit = re: s: filter isString (split re s);
|
|
matchAll = re: s: filter isList (split re s);
|
|
splitWhitespace = reSplit "[[:space:]]+";
|
|
abs = i: if i >= 0 then i else i * -1;
|
|
delta = x: y: abs (x - y);
|
|
listSum = foldl' add 0;
|
|
listSumWith = f: l: foldl' add 0 (map f l);
|
|
deleteAt = l: i: (take i l) ++ (drop (i + 1) l);
|
|
eq = x: y: x == y;
|
|
greaterThan = x: y: x > y;
|
|
listRange = l: range 0 ((length l) - 1);
|
|
invert = f: x: !(f x);
|
|
elemAtMid = l: elemAt l ((length l) / 2);
|
|
removeAll = rl: (flip pipe) (map remove rl);
|
|
pipe' = flip pipe;
|
|
inRange = i: l: (i >= 0) && (i < length l);
|
|
|
|
transpose = l: map (i: map ((flip elemAt) i) l) (listRange (head l));
|
|
matVecMul = m: v: map (r: foldl' add 0 (zipListsWith mul v r)) m;
|
|
vecSum = zipListsWith add;
|
|
matIndex = i: pipe' (map (flip elemAt) i);
|
|
inRangeMatrix = i: m:
|
|
if i == [ ] then
|
|
true
|
|
else
|
|
(isList m) && (inRange (head i) m)
|
|
&& (inRangeMatrix (tail i) (elemAt m (head i)));
|
|
getMatrixIndices = m:
|
|
crossLists (x: y: [ x y ]) [
|
|
(listRange m)
|
|
(listRange (head m))
|
|
]; # fixme: only 2d, not arbitrary tensors.
|
|
}
|