From f03028ac5db58285b7bee57390c28a6f12c46193 Mon Sep 17 00:00:00 2001 From: Brian Guarraci Date: Wed, 13 Dec 2023 21:47:28 -0700 Subject: [PATCH] add missing lib --- klongpy/lib/README | 8 + klongpy/lib/csv.kg | 40 ++++ klongpy/lib/edt.kg | 24 +++ klongpy/lib/eigenv.kg | 57 ++++++ klongpy/lib/help.kg | 88 ++++++++ klongpy/lib/huffman.kg | 16 ++ klongpy/lib/math.kg | 252 +++++++++++++++++++++++ klongpy/lib/nstat.kg | 442 +++++++++++++++++++++++++++++++++++++++++ klongpy/lib/print.kg | 3 + klongpy/lib/qdict.kg | 12 ++ klongpy/lib/qstr.kg | 14 ++ klongpy/lib/set.kg | 29 +++ klongpy/lib/spline.kg | 43 ++++ klongpy/lib/time.kg | 13 ++ klongpy/lib/util.kg | 14 ++ 15 files changed, 1055 insertions(+) create mode 100644 klongpy/lib/README create mode 100644 klongpy/lib/csv.kg create mode 100644 klongpy/lib/edt.kg create mode 100644 klongpy/lib/eigenv.kg create mode 100644 klongpy/lib/help.kg create mode 100644 klongpy/lib/huffman.kg create mode 100644 klongpy/lib/math.kg create mode 100644 klongpy/lib/nstat.kg create mode 100644 klongpy/lib/print.kg create mode 100644 klongpy/lib/qdict.kg create mode 100644 klongpy/lib/qstr.kg create mode 100644 klongpy/lib/set.kg create mode 100644 klongpy/lib/spline.kg create mode 100644 klongpy/lib/time.kg create mode 100644 klongpy/lib/util.kg diff --git a/klongpy/lib/README b/klongpy/lib/README new file mode 100644 index 0000000..ed1f4bb --- /dev/null +++ b/klongpy/lib/README @@ -0,0 +1,8 @@ +These example lib files are included from the Klong lib files in the original distribution: + +https://t3x.org/klong/index.html + +The plotting libs were removed due to lack of support (so far). + +There are some small modifications to deal with Unicode, but otherwise should be the same. + diff --git a/klongpy/lib/csv.kg b/klongpy/lib/csv.kg new file mode 100644 index 0000000..ced7ade --- /dev/null +++ b/klongpy/lib/csv.kg @@ -0,0 +1,40 @@ +:"comma separated value (CSV) conversion" + +.comment("*****") + +CSV format as supported by this module is as follows: + +- records end with a LF or CR,LF character +- the last record in a file may omit the LF or CR,LF +- fields are separated by commas +- fields may or may not be delimited by quotes (") +- a field delimited by quotes may contain a comma +- a field delimited by quotes may contain a literal " as \" +- two adjacent commas denote an empty field + +The following functions convert between CSV and vector forms: + +csv.split(x) split CSV record in string x into a vector of fields +csv.read() read and split record from From channel +csv.make(x) create csv record (string) from fields in vector x +csv.write(x) create csv record and write to To channel +csv.load() read file of csv records from From channel, returning + a vector of vectors of fields (csv.split each line) + +***** + +.module(:csv) +ctlm:::#(#0cM)-#0c@ +unesc::{[f];:[~[]~f::x?0c\;,/(*f),{1_x}'1_f::f:_x;x]} +escape::{[f];:[~[]~f::x?0c";1_,/{0c\,x}'f:_x;x]} +unquote::{:[0c"~*x;1_(-1)_x;x]} +csv.split::{[c f i k q s];q::0;k::#x;f::[];s::x; + {xy)*a1)} +termq::{[s];s::0.5*msum1(x;z);(s%y)>z} +mkvals::{(,x),(,y)} +next::{[w;v;neq;m;j;k;res];v::x@1;w::*x;neq::*(^w);j::1;m::0;k::0; + {xa" "Grade-Down" + "a=vector; vector of indices of elements of 'a' in ascending order"] + [" b" "More" "a,b=real|char|vector; 1, if 'a' is greater than 'b'"] + [" -a" "Negate" "a=real; negative value"] + [" ~a" "Not" "logical complement"] + [" a+b" "Plus" "sum"] + [" a^b" "Power" "'a' raised to the b'th power"] + [" ?a" "Range" "a=vector; unique elements of 'a' in order of appearance"] + [" %a" "Reciprocal" "1 divided by 'a'"] + [" a:^b" "Reshape" + "a=vector|integer; 'b' reshaped to the dimensions of 'a', row-major"] + [" a!b" "Remainder" "truncated division remainder of a:%b"] + [" |a" "Reverse" "a=vector; reversed vector"] + [" a:+b" "Rotate" + "a=integer, b=vector; 'b' rotated to right by 'a' elements (a<0 = left)"] + [" ^a" "Shape" "a=vector; vector of dimenions of 'a'; 0 for atoms"] + [" #a" "Size" + "magnitude of numbers, size of vectors, code points of characters"] + [" a:#b" "Split" "a=integer, b=vector; split 'b' into 'a'-element subvectors"] + [" a:#b" "Split" + "a=list(integer), b=vector; split into subvectors of sizes in 'a'"] + [" a#b" "Take" + "a=integer, b=vector; extract first (a<0 = last) 'a' elements from 'b'"] + [" a*b" "Times" "a,b=real; product"] + [" +a" "Transpose" "a=vector; transpose of 'a'; 'a' must be symmetric"] + [" :_a" "Undefined" "1, if 'a' is :undefined"]] + +:[[]~.helpdb;.helpdb::op.db;0] +pho::{.d(1_7$*.helpdb@x);.d((.helpdb@x)@2);.p("")} +phf::{.p(*.helpdb@x);{.p(" ", x)}'1_.helpdb@x} +ph::{:[0c ~**.helpdb@x;pho(x);phf(x)]} +fho::{(~[]~(*x)?y)|~[]~(x@1)?y} +fhf::{(~""~y)&~[]~(*x)?y} +fh::{:[0c ~**x;fho(x;y);fhf(x;y)]} +help::{[h];h::x;ph'&{fh(x;h)}'.helpdb;[]} +help.add::{:[.helpdb?*x;1;.helpdb::.helpdb,x]} + +.module(0) diff --git a/klongpy/lib/huffman.kg b/klongpy/lib/huffman.kg new file mode 100644 index 0000000..2f9d3ca --- /dev/null +++ b/klongpy/lib/huffman.kg @@ -0,0 +1,16 @@ +:"Huffman encoder/decoder by Niplav" + +:"Using 'encode' on a text terminal may mess up your TTY settings!" + +frq::{[t];t::x;{(t@*x),#x}'=x} +treewalk::{:[2=#x;,(,y),x@1;.f(x@1;y,0),.f(x@2;y,1)]} +combsmallest::{{,(+/*'x),x}:(2#x),2_x} +gencode::{|'treewalk(*{1<#x}{combsmallest(x@<*'x)}:~|'x;[])} +bin::{(-x)#(&x),{:[x;.f(x:%2),x!2;0]}:(y)} +comp::{,/:#{+/x*'2^|!8}'(8*1+!(#x):%8):_x} +decomp::{,/{bin(8;x)}'#'x} +encode::{[c f b];f::frq(x);c::gencode(f);b::,/{*|c@*(x=*'c)?1}'x; + (#b),(,f),,comp(b)} +decode::{[r o];o::gencode(x@1);r::"";{x}{[p]; + p::*(x{:[&/y=(#y)#x;1;0]}:\*'|'o)?1; + r::r,*o@p;(#*|o@p)_x}:~(*x)#decomp(x@2);r} diff --git a/klongpy/lib/math.kg b/klongpy/lib/math.kg new file mode 100644 index 0000000..814465e --- /dev/null +++ b/klongpy/lib/math.kg @@ -0,0 +1,252 @@ +:"various math functions" +:"atan2 function contributed by Alexander Shendi" + +.comment("*****") + +.fastpow when set, x^y compiles to .pow(x;y) +.md number of mantissa digits +pow(x;y) power function (faster than the Klong primitive) +e Euler's number +pi ratio of a circle's circumference to its diamater +sgn(x) sign of x +rnd(x) round half toward zero +rndn(x;y) round half toward zero after y decimal places +rn(0) random number (argument ignored) +ri(n) random integer in the range [0,n-1] +th(x) return a string representing x with decimal marks (1000's) +gcd(x;y) greatest common divisor +lcm(x;y) least common multiple +exp(x) exponential function +ln(x) natural logarithm +log(n;x) logarithm of x to base n +sin(x) sine function +cos(x) cosine function +tan(x) tangent function +atan2(x;y) arctangent (angle of x+yi) +sqr(x) square root +nrt(n;x) n'th root of x +cadd(a,b;c,d) sum of complex numbers a+bi and c+di +csub(a,b;c,d) difference of complex numbers a+bi and c+di +cmul(a,b;c,d) product of complex numbers a+bi and c+di +cdiv(a,b;c,d) quotient of complex numbers a+bi and c+di +cexp(a,b) e to the complex number a+bi +cabs(a,b) magnitude of the complex number a+bi +fft(x) (not so) fast Fourier transform of the real vector x +gamln(x) ln of Gamma function +gam(x) Gamma function +gamp(a;x) regularized incomplete gamma function P(a,x) +gamq(a;x) regularized incomplete gamma function Q(a,x) +lgam(a;x) lower incomplete gamma function gamma(a,x) +ugam(a;x) upper incomplete gamma function Gamma(a,x) +beta(a;b) Beta function +ibeta(x;a;b) incomplete Beta function Beta(x;a,b), max error: 1e-6 +rbeta(x;a;b) regularized incomplete Beta function I_x(a,b), max error: 1e-6 +matp(x;y) product of matrixes x and y +elim(x) reduce augmented matrix to upper triangular form +subst(x) substitute values in reduced matrix, return vector of variables +gauss(x) solve system of linear equations; x = augmented matrix + e.g. gauss([[1 3 -4 8] [0 2 -2 6] [-1 -2 5 -1]]) +primes(x) return all primes up to and including x +prime(x) test whether x is prime +bisect(fn) find root of function by bisection +regf(fn;x0;x1) find root of function using regula falsi method + +************************************************************************ + +.l("help.kg") + +.module(:math.bisect) +F::{1} +x0::0 +x1::0 +B::0 +fx::0 +findint::{{F(x)<0}{x*2}:~1} +iter::{x;B::(x0+x1)%2;fx::F(B);x0:::[fx<0;B;x0];x1:::[fx<0;x1;B];x1-x0} +bisect::{F::x;x0::0;x1::findint();iter:~0;x0} +.module(0) + +.module(:math.regf) +F::{1} +q::0 +x1::0 +x2::0 +y1::0 +y2::0 +iter::{x1::x@0;x2::x@1;y1::F(x1);y2::F(x2); + q:::[0~y1-y2;x2;((x1*y2)-(x2*y1))%y2-y1];x2,q} +regf::{F::x;(iter:~y,z)@0} +.module(0) + +.module(:math) +e::2.71828182845904523536028747135266249775724709369995 +pi::3.14159265358979323846264338327950288419716939937510 +.md::{[i];i::0;{x<1}{i::i+1;x*10}:~.e;i}() +pi2::pi+pi +pi0.5::pi%2 +pi1.5::1.5*pi +sgn::{:[0=x;0:|x<0;-1;1]} +rn::{x;.rn()} +ri::{_x*.rn()} +rnd::{:[x<0;-1;1]*_0.5+#x} +rndn::{rnd(x*10^y)%10^y} +th::{{x,",",y}/|'|3:#|$_x} +gcd::{:[0~y;x:|0~x;y:|x0;%0:|(0.10;0;%0] + :|y=_y;ex(x;_y);exp(y*ln(x))]} +pow::.pow +.fastpow::1 +uc::{:[x<0;-uc(-x);x-pi2*_(#x)%pi2]} +sin.tlr::{[xx yy r d a];xx::x;yy::3;a::0; + {d::(xx^yy)%*/1+!yy;r:::[a;x+d;x-d];a::~a;yy::2+yy;r}:~x} +sin::{[xx];xx::uc(x); + xx:::[(~0>xx)&~xx>pi0.5;xx + :|(~pi0.5>xx)&~xx>pi1.5;pi-xx + :|(~pi>xx)&~xx>pi1.5;xx-pi + ;xx-pi2]; + :[xx>pi;-sin.tlr(xx);sin.tlr(xx)]} +cos::{sin(pi0.5-x)} +tan::{sin(x)%cos(x)} +:" Taylor series takes forever for big (>0.999) values " +asin::{[a b aa bb v vv xx r];a::1;b::2;aa::3;bb::4;xx::3;v::x^3;vv::x*x; + {r::x+(a%b)*v%xx; + v::v*vv;a::a*aa;b::b*bb;aa::aa+2;bb::bb+2;xx::xx+2;r}:~x} +:" so use regula falsi instead " +asin::{[y];y::x;regf({sin(x)-y};0;1-.e)} +acos::{[y];y::x;regf({cos(x)-y};0;1-.e)} +atan::{[y];y::x;regf({tan(x)-y};0;1-.e)} +azeroq::{[t];t::1.0e-8;((#x)1}{y1::y1+(pi%4);(x-1)%(x+1)}:~x1; + y1+atanh3(x1)} +atanh5::{x-((x*x*x)%3.0)} +atanh6::{(atanh5(x),atanh4(x))@(x>0.1)} +atan21::{[idx;ww];idx::(x<0)+(y<0)*2;ww::atanh6((#y)%(#x)); + (ww,(pi-ww),(-ww),(ww-pi))@idx} +atan2::{[fnl];fnl::{atan21(x;y)},{(((-0.5)*pi),(0.5*pi))@(y>0)}; + (fnl@azeroq(x))@(x,y)} +cadd::{((*x)+*y),(x@1)+y@1} +csub::{((*x)-*y),(x@1)-y@1} +cmul::{(((*x)**y)-(x@1)*y@1),((x@1)**y)+(*x)*y@1} +cdiv::{[d];d::((*y)^2)+(y@1)^2;((((*x)^2)+(^*y)^2)%d),(((x@1)**y)-(*x)*y@1)%d} +cexp::{(exp(*x)*cos(x@1)),exp(*x)*sin(x@1)} +cabs::{sqr(((*x)^2)+(x@1)^2)} +fft::{ff2::{[n e o p t k];n::#x; + f::{p::2:#x;e::ff2(*'p);o::ff2({x@1}'p);k::-1; + t::{k::k+1;cmul(cexp(cdiv(cmul([0 -2];(k*pi),0);n,0));x)}'o; + (e cadd't),e csub't}; + :[n<2;x;f(x)]}; + n::#x;k::{(2^x)*'#''x;d::*(~h)?0; + :[2>#x;x;(,h),0,:\.f({1_x}'{:[d~[];x;x-h*(x@d)%h@d]}'1_m)]} +subst::{[v];v::[];{v::v,((*x)-/:[[]~v;[];v*x@1+!#v])%x@1+#v}'||'x;|v} +gauss::{subst(elim(x))} + +primes::{[n p];n::x;p::[2];{~x>n}{:[&/x!p;p::p,x;0];x+2}:~3;:[x<2;[];p]} +prime::{:[x<2;0:|[2 3 5]?x;1;&/(x!2,3+2*!_sqr(x)%2)]} + +.module(0) + +help.add([ + [".fastpow" "when set, x^y compiles to .pow(x;y)"] + [".md" "number of mantissa digits"] + ["acos(x)" "inverse cosine"] + ["asin(x)" "inverse sine"] + ["atan(x)" "inverse tangent"] + ["beta(a;b)" "Beta function"] + ["bisect(fn)" "find root of function by bisection"] + ["cos(x)" "cosine"] + ["cadd(a,b;c,d)" "sum of complex numbers a+bi and c+di"] + ["csub(a,b;c,d)" "difference of complex numbers a+bi and c+di"] + ["cmul(a,b;c,d)" "product of complex numbers a+bi and c+di"] + ["cdiv(a,b;c,d)" "quotient of complex numbers a+bi and c+di"] + ["cexp(a,b)" "e to the complex number a+bi"] + ["cabs(a,b)" "magnitude of the complex number a+bi"] + ["e" "Euler's number"] + ["elim(x)" "reduce augmented matrix to upper triangular form"] + ["exp(x)" "exponential function"] + ["fft(x)" "fast Fourier transform of the real vector x"] + ["gam(x)" "Gamma function"] + ["gamln(x)" "ln of Gamma function"] + ["gamp(a;x)" "regularized incomplete gamma function P(a,x)"] + ["gamq(a;x)" "regularized incomplete gamma function Q(a,x)"] + ["gauss(x)" "solve system of linear equations; x = augmented matrix"] + ["gcd(x;y)" "greatest common divisor"] + ["ibeta(x;a;b)" "incomplete Beta function Beta(x;a,b)"] + ["lcm(x;y)" "least common multiple"] + ["lgam(a;x)" "lower incomplete gamma function gamma(a,x)"] + ["ln(x)" "natural logarithm"] + ["log(n;x)" "logarithm of x to base n"] + ["matp(A;B)" "matrix product"] + ["nrt(n;x)" "n'th root of x"] + ["pi" "ratio of a circle's circumference to its diamater"] + ["pow(x;y)" "x raised to the y'th power"] + ["prime(x)" "primality test"] + ["primes(x)" "prime numbers up to and including x"] + ["rbeta(x;a;b)" "regularized incomplete Beta function I_x(a,b)"] + ["regf(fn;x0;x1)" "find root of function using regula falsi method"] + ["ri(n)" "random integer in the range [0,n-1]"] + ["rn(0)" "random number (argument ignored)"] + ["rnd(x)" "round half toward zero"] + ["rndn(x;y)" "round half toward zero after y decimal places"] + ["sgn(x)" "sign"] + ["sin(x)" "sine"] + ["sqr(x)" "square root"] + ["subst(x)" "compute values of variables in triangular matrix"] + ["tan(x)" "tangent"] + ["atan2(x;y)" "arctangent (angle of x+yi)"] + ["th(x)" "return string representing x with decimal marks (1000's)"] + ["ugam(a;x)" "upper incomplete gamma function Gamma(a,x)"] +]) diff --git a/klongpy/lib/nstat.kg b/klongpy/lib/nstat.kg new file mode 100644 index 0000000..f79c491 --- /dev/null +++ b/klongpy/lib/nstat.kg @@ -0,0 +1,442 @@ +:"statistics functions" + +.comment("*****") + +A "data set" in this context is a list of data points [x1, ... xN]. +An "x/y set" is a list of x/y pairs [[x1 y1] ... [xN yN]]. + +kp(k;n) k-permutations of set of size n +kc(k;n) k-combinations of set of size n + +rcp(A;B|A;B'|A') reverse conditional probability P(A|B) (Bayes' rule) + +mu(d) mean of data set d = [x1, ...] +Q(x;d) x'th quantile of data set; 0<=x<=1 +M(d) median of data set +mo(d) mode of data set (also mod; deprecated) +rng(d) range of data set +var(d) variance of data set +cov(d;d2) covariance of data sets +cor(d;d2) correlation coefficient of data sets +sd(d) standard deviation of data set (population sd) +svar(d) sample variance of data set +ssd(d) standard deviation of data set (sample sd) + +lreg(s) linear regression coefficients for x/y set s, gives a,b in ax+b +lr(x;l) interpolate "y" at "x" using l=a,b of lreg +hist(k;d) k-histogram of data set d (list of means between k-quantiles) +seg(k;d) divide data set d into k segments, returning k+1 endpoints +rss(s;f) compute RSS for x/y set s and regression function f +rse(s;f) compute RSE (res. sq. error) for x/y set x and reg. fn. f +tss(s) compute total sum of squares (TSS) of set s +r2(s;f) compute R^2 statistic for given x/y set and regression function +mse(s;f) compute mean squared error (MSE) for x/y set and regression fn + +rss.set(x;y), rse.set(x;y), mse.set(x;y), r2.set(x;y) + These function also compute the RSS, RSE, MSE, and r-squared, + but by comparing two data sets instead of an x/y set and a model. + +spl(k;s) compute spline with k degrees of freedom through x/y set s + +cvset(s) create cross validation set from x/y set +cve(f;p;s) cross validation error of set s, fitting fn f, and prediction fn p + +freq(D;n;[x0 xn]) create relative n-point frequency distribution X~D + in [x0,xn]; D is a PDF +nfreq(D;n;[x0 xn]) create normalized n-point frequency distribution X~D + in [x0,xn]; D is a PDF +dist(D;n;[x0 xn]) create n D-distributed data points in [x0,xn]; D is a PDF: + note: "dist" creates absolute frequencies; &dist creates + the actual distribution +fdist(D) create frequency distribution from random data set D +shuffle(x) randomly arrange the elements of data set x + +errdist(x) Create a set of normally distributed error values in the range + [-0.5..0.5]; x is the number of distinct error values; try x=10 +err(x;y;z) Add a normally distributed error to data set z; x is the number + of distinct error values, y the size of the error (1=[-0.5..0.5]) + +ct.xsum(CT) marginal sums of X categories of contingency table (CT) +ct.ysum(CT) marginal sums of Y categories of CT +ct.sum(CT) grand total of CT +ct.exp(CT) expectations of CT +ct.x2(CT) chi-square score of CT + +erf(x) Gauss error function + +Probability Distribution Functions: + +Distrib. Pre PMF/PDF CDF QF Mean Med. Mode Var. Skew. +---------- fix pmf/pdf cdf qf mu M mo var skew +Frequency f. x;f x;f - f f f f - +Uniform r. x;a;b x;a;b - a;b a;b a;b a;b a;b +Discrete +uniform u. x;a;b x;a;b - a;b a;b - a;b a;b +Geometric geo. x;p x;p - p p p p p +Binomial b. x;n;p x;n;p - n;p n;p n;p n;p n;p +Hyper- +geometric hyp. x;n,p,N x;n,p,N - n;p;N - n;P;n n;P;n n;P;n +Poisson poi. x;L x;L - L L L L L +Normal n. x;M;S x;M;S p;M;S M;S M;S M;S M;S M;S +Standard +normal - ndf(x) cdf(x) qf(p) 1 1 1 1 0 +Lognormal ln. x;M;S x;M;S p;M;S M;S M;S M;S M;S M;S +Chi-square x2. N;x N;x N;p N N N N N +Student t t. N;x N;x N;p N N N N N +Fisher- +Snedecor F F. x;N1;N2 x;N1;N2 p;N1;N2 N1;N2 - N1;N2 N1;N2 N1;N2 + +f.add(x;y;z) add amount z to entry y of frequency table (dictionary) x +tscore(x;s2;n) t-score of x given variance s2 and # of observations n +zscore(x;mu;s2) z-score of x given mean mu and variance s2 +fscore([s1 S1];[s2 S2]) + F-score of samples means s1/s2 and population means S1/S2 +X2(E;O) compute X^2 statistic; E=expected, O=observed + +Error function approximation by Abramowitz/Stegun, max error = 1.5x10-7. +T distribution approximation by Zogheib/Elsaheli, max error = 0.0029 + +************************************************************************ + +.l("spline.kg") +.l("math.kg") +.l("help.kg") +.module(:nstat) + +kp::{*/(y-x)+1+!x} +kc::{:[0~x;1;_kp(x;y)%*/1+!x]} + +rcp::{(x*y)%(x*y)+(1-x)*1-z} + +mu::{(+/x)%#x} +S::{:[y<0;0:|~y<#x;*|x;x@y]} +Q::{[s i];s::y@#x;x;(x@k),.f(drop(x;k+1))]} +*** + +shuffle::{x@>(#x){x,.rn()}:*[]} + +cp::{[f b];f::x;b::z;,/{[a];a::x;,f(a;)'b}'y} +ct.xsum::{+/x} +ct.ysum::{+/'x} +ct.sum::{+/+/x} +ct.exp::{[tx ty tt];ty::+/'x;tx::+/x;tt::+/tx;cp({(x*y%tt)};ty;tx)} +ct.x2::{+/,/x{((x-y)^2)%x}'ct.exp(x)} + +efc::[0.0705230784 0.0422820123 0.0092705272 + 0.0001520143 0.0002765672 0.0000430638] +erf::{:[x<0;-erf(-x);1-%(1+/efc**\6:^x)^16]} + +f.pmf::{[p];p::(*'y)?x;:[[]~p;0;((y@*p)@1)%+/{x@1}'y]} +f.cdf::{[f m];f::y;m::&/*'y;+/f.pmf(;f)'m+!1+x-m} +f.mu::{(+/{{x*y}@x}'x)%+/{x@1}'x} +f.M::{[s m c];s::+\{x@1}'x;m::(*|s):%2;:[c::s?m;M(*'x@c,c+1);*x@*&s>m]} +f.mo::{[f];*'x@f?|/f::{x@1}'x} +f.var::{((+/({{y*x^2}@x}'x))%+/{x@1}'x)-f.mu(x)^2} +f.add::{[v];:[:_v::x?y;x,y,z;x,y,v+z]} + +u.pmf::{:[xz;0;%1+z-y]} +u.cdf::{:[xz;1;(1+(_x)-y)%1+z-y]} +u.mu::{(x+y)%2} +u.M::u.mu +u.var::{(((y-x+1)^2)-1)%12} +u.skew::{y;0} + +r.pdf::{:[xz;0;%z-y]} +r.cdf::{:[xz;1;(x-y)%z-y]} +r.mu::{(x+y)%2} +r.M::r.mu +r.mo::{,r.mu(x;y)} +r.var::{((y-x)^2)%12} +r.skew::{y;0} + +geo.pmf::{y*(1-y)^_(x-1)} +geo.cdf::{1-(1-y)^_x} +geo.mu::{%x} +geo.M::{(-1)%log(10;1-x)} +geo.mo::{x;[1]} +geo.var::{(1-x)%x^2} +geo.skew::{(2-x)%(1-x)^0.5} + +b.pmf::{kc(_x;y)*(z^_x)*(1-z)^y-_x} +b.cdf::{[n p];n::y;p::z;+/b.pmf(;n;p)':[0~_x;0;!_1+x]} +b.mu::{x*y} +b.M::{_x*y} +b.mo::{,_y*x+1} +b.var::{x*y*1-y} +b.skew::{((1-y)-y)%(x*y*1-y)^0.5} + +hyp.pmf::{[K n p N];n::y@0;p::y@1;N::y@2;K::rnd(N*p); + :[x<0|n+K-N;0:|x>K&n;0;(kc(_x;K)*kc(n-_x;N-K))%kc(n;N)]} +hyp.cdf::{[n p N];n::y@0;p::y@1;N::y@2;+/hyp.pmf(;n,p,N)':[0~_x;0;!_x]} +hyp.mu::{z;x*y} +hyp.mo::{[K];K::rnd(y*z);,_((x+1)*K+1)%z+2} +hyp.var::{((x*y*y-1)*z-x)%z-1} + +poi.pmf::{((y^_x)*e^-y)%:[0~_x;1;*/1+!_x]} +poi.cdf::{[lam];lam::y;+/poi.pmf(;lam)':[0~_x;0;!_x]} +poi.mu::{x} +poi.M::{(_x+1%3)-0.02%x} +poi.mo::{:[x~_x;(x-1),x;,_x]} +poi.var::{x} +poi.skew::{%x^0.5} + +sqr2pi::(2*pi)^0.5 +sqr2::2^0.5 + +zscore::{(x-y)%sqr(z)} +ndf::{(%sqr2pi)*exp(-(x^2)%2)} +phi::{0.5+0.5*erf(x%sqr2)} +cdf::phi +qf::{[p];p::x;regf({phi(x)-p};0;1)} + +n.pdf::{[s];s::sqr(z);(%s*sqr2pi)*exp(-((x-y)^2)%2*s^2)} +n.cdf::{phi((x-y)%sqr(z))} +n.qf::{y+qf(x)*sqr(z)} +n.mu::{y;x} +n.M::{y;x} +n.mo::{y;,x} +n.vapr::{y} +n.skew::{y;0} + +ln.pdf::{:[0=x;0;(%x*sqr(z)*sqr2pi)*exp(-((ln(x)-y)^2)%2*z)]} +ln.cdf::{:[0=x;0;0.5+0.5*erf((ln(x)-y)%sqr2*sqr(z))]} +ln.qf::{[m s p];p::x;m::y;s::z;bisect({ln.cdf(x;m;s)-p})} +ln.mu::{exp(x+y%2)} +ln.M::{y;exp(x)} +ln.mo::{,exp(x-y)} +ln.var::{(exp(y)-1)*exp(y+2*x)} +ln.skew::{x;(2+exp(y))*sqr(exp(y)-1)} + +X2::{+/x{((y-x)^2)%x}'y} +x2.pdf::{:[0~y;0;(%((2^x%2)*gam(x%2)))*(y^(x%2)-1)*e^-y%2]} +x2.cdf::{gamp(x%2;y%2)} +x2.qf::{[nu p];nu::x;p::y;regf({x2.cdf(nu;x)-p};0;nu)} +x2.qf::{[nu p];nu::x;p::y;bisect({x2.cdf(nu;x)-p})} +x2.mu::{x} +x2.M::{x*(1-2%9*x)^3} +x2.mo::{,0|x-2} +x2.var::{2*x} +x2.skew::{(8%x)^0.5} + +ct.exp::{[r c t];r::{+/x}'x;c::{+/x}'+x;t::+/c;{[a];a::x;{(x*a)%t}'c}'r} +ct.X2::{X2(,/ct.exp(x);,/x)} + +tscore::{sqr(x*(y%sqr(z-1)))} +t.pdf::{(gam((x+1)%2)%(sqr(x*pi))*gam(x%2))*(1+(y^2)%x)^-(x+1)%2} +t.cdf::{z1::y*(1-%4*x)*(1+(%2*x)*y^2)^-0.5; + %(1+e^((0.000345*z1^5)-(0.069647*z1^3))-1.604326*z1)} +t.cdf::{:[y<0;1-.f(x;-y);1-0.5*rbeta(x%x+y^2;x%2;0.5)]} +t.qf::{[nu p];nu::x;p::y; + :[p<0.5;-bisect({t.cdf(nu;x)-1-p});bisect({t.cdf(nu;x)-p})]} +t.mu::{:[x>1;0;%0]} +t.M::{x;0} +t.mo::{x;[0]} +t.var::{:[x>2;x%x-2;%0]} +t.skew::{x;0} + +fscore::{(((*x)^2)*(y@1)^2)%((*y)^2)*(x@1)^2} +F.pdf.n::{gam((y+z)%2)*((y%z)^y%2)*x^((y%2)-1)} +F.pdf.d::{gam(y%2)*gam(z%2)*(1+(y*x)%z)^(y+z)%2} +F.pdf::{:[0=x;0;F.pdf.n(x;y;z)%F.pdf.d(x;y;z)]} +F.cdf::{1-rbeta(z%z+y*x;z%2;y%2)} +F.qf::{[n1 n2 p];n1::y;n2::z;p::x;bisect({F.cdf(x;n1;n2)-p})} +F.mu::{:[y>2;y%y-2;%0]} +F.mo::{:[x>2;,(y*x-2)%x*y+2;%0]} +F.var::{:[y>4;(2*(y^2)*(x+y)-2)%x*((y-2)^2)*y-4;%0]} +F.skew::{:[y>6;(((2*x)+y-2)*sqr(8*y-4))%sqr(x)*(y-6)*sqr(x+y-2);%0]} + +help.add([ + ["F.cdf(nu1;nu2;x)" "CDF of F-distribution F ~ F(nu1,nu2)"] + ["F.pdf(nu1;nu2;x)" "PDF of F-distribution F ~ F(nu1,nu2)"] + ["F.mu(nu1;nu2)" "mean of F-distribution F ~ F(nu1,nu2)"] + ["F.mo(nu1;nu2)" "modes of F-distribution F ~ F(nu1,nu2)"] + ["F.qf(nu1;nu2;p)" "QF of F-distribution F ~ F(nu1,nu2)"] + ["F.skew(nu1;nu2)" "skewness of F-distribution F ~ F(nu1,nu2)"] + ["F.var(nu1;nu2)" "variance of F-distribution F ~ F(nu1,nu2)"] + ["M(D)" "median of data set D"] + ["Q(x;D)" "x'th quantile of data set D; 0<=x<=1"] + ["X2(E;O)" "compute X^2 statistic; E=expected, O=observed values"] + ["b.cdf(x;n;p)" "CDF of binomial distribution X ~ B(n,p)"] + ["b.mu(n;p)" "mean of binomial distribution X ~ B(n,p)"] + ["b.pmf(x;n;p)" "PMF of binomial distribution X ~ B(n,p)"] + ["b.skew(n;p)" "skewness of binomial distribution X ~ B(n,p)"] + ["b.var(n;p)" "variance of binomial distribution X ~ B(n,p)"] + ["cdf(x)" "CDF of standard normal distribution X ~ N(0,1)"] + ["cor(D;S)" "correlation coefficient of data sets"] + ["cov(D;S)" "covariance of data sets D,S"] + ["ct.X2(CT)" "compute X^2 statistic from contingency table"] + ["ct.exp(CT)" "expectations of contingency table"] + ["ct.sum(CT)" "grand total of contingency table"] + ["ct.x2(CT)" "chi-square score of contingency table"] + ["ct.xsum(CT)" "marginal sums of X categories of contingency table"] + ["ct.ysum(CT)" "marginal sums of Y categories of contingency table"] + ["cve(f;p;XY)" + "cross validation error of set XY, fitting fn f, predictor fn p"] + ["cvset(XY)" "create cross validation set from x/y set"] + ["dist(PDF;n;[x1 xn])" "&X = set of n D-distributed data points in x1..xn"] + ["erf(x)" "Gauss error function"] + ["err(n;r;D)" + "Add ND error to data set D; n = err values, r = range of error"] + ["errdist(n)" "n normally distributed error values in the range -0.5..0.5"] + ["f.M(f)" "median of frequency distribution F"] + ["f.cdf(x;F)" "CDF of frequency distribution F"] + ["f.mo(f)" "modes of frequency distribution F"] + ["f.mu(f)" "mean of frequency distribution F"] + ["f.pmf(x;F)" "PMF of frequency distribution F"] + ["f.var(F)" "variance of frequency distribution F"] + ["f.add(x;y;z)" "add amount z to entry y of frequency table (dictionary) x"] + ["fdist(D)" "frequency distribution of data set D"] + ["freq(PDF;n;[x1 xn])" "relative n-point frequency dist X~D in x1..xn"] + ["fscore([s1 S1];[s2 S2])" + "F-score of samples means s1/s2 and population means S1/S2"] + ["geo.cdf(x;p)" "CDF of geometric distribution X ~ Geo(p)"] + ["geo.mu(p)" "mean of geometric distribution X ~ Geo(p)"] + ["geo.pmf(x;p)" "PMF of geometric distribution X ~ Geo(p)"] + ["geo.skew(p)" "skewness of geometric distribution X ~ Geo(p)"] + ["geo.var(p)" "variance of geometric distribution X ~ Geo(p)"] + ["hist(k;D)" "k-histogram of data set D (list of means between k-quantiles)"] + ["hyp.cdf(x;n,p,N)" "CDF of hypergeometric distribution X ~ Hyp(p)"] + ["hyp.mo(n;p;N)" "modes of hypergeometric distribution X ~ Hyp(p)"] + ["hyp.mu(n;p;N)" "mean of hypergeometric distribution X ~ Hyp(p)"] + ["hyp.pmf(x;n,p,N)" "PMF of hypergeometric distribution X ~ Hyp(p)"] + ["hyp.skew(n;p;N)" "skewness of hypergeometric distribution X ~ Hyp(p)"] + ["hyp.var(n;p;N)" "variance of hypergeometric distribution X ~ Hyp(p)"] + ["kc(k;n)" "k-combinations of set of size n"] + ["kp(k;n)" "k-permutations of set of size n"] + ["ln.M(mu;sigma^2)" "median of lognormal distribution ln X ~ N(mu,sigma^2)"] + ["ln.cdf(x;mu;sigma^2)" "CDF of lognormal distribution ln X ~ N(mu,sigma^2)"] + ["ln.mo(mu;sigma^2)" "modes of lognormal distribution ln X ~ N(mu,sigma^2)"] + ["ln.mu(mu;sigma^2)" "mean of lognormal distribution ln X ~ N(mu,sigma^2)"] + ["ln.pdf(x;mu;sigma^2)" "PDF of lognormal distribution ln X ~ N(mu,sigma^2)"] + ["ln.qf(x;mu;sigma^2)" "QF of lognormal distribution ln X ~ N(mu,sigma^2)"] + ["ln.skew(mu;sigma^2)" + "skewness of lognormal distribution ln X ~ N(mu,sigma^2)"] + ["ln.var(mu;sigma^2)" + "variance of lognormal distribution ln X ~ N(mu,sigma^2)"] + ["lr(x;L)" "interpolate y at x using L=a,b of lreg"] + ["lreg(S)" "linear regression coefficients for x/y set S, gives a,b in ax+b"] + ["mod(D)" "mode of data set D"] + ["mse(XY;f)" "mean squared error (MSE) for x/y set and regression fn"] + ["mse.set(XY;E)" "mean squared error (MSE) for x/y set and expectation E"] + ["mu(D)" "mean of data set D"] + ["n.M(mu;sigma^2)" "median of normal distribution X ~ N(mu,sigma^2)"] + ["n.cdf(x;mu;sigma^2)" "CDF of normal distribution X ~ N(mu,sigma^2)"] + ["n.mo(mu;sigma^2)" "modes of normal distribution X ~ N(mu,sigma^2)"] + ["n.mu(mu;sigma^2)" "mean of normal distribution X ~ N(mu,sigma^2)"] + ["n.pdf(x;mu;sigma^2)" "PDF of normal distribution X ~ N(mu,sigma^2)"] + ["n.qf(x;mu;sigma^2)" "QF of normal distribution X ~ N(mu,sigma^2)"] + ["n.skew(mu;sigma^2)" "skewness of normal distribution X ~ N(mu,sigma^2)"] + ["n.var(mu;sigma^2)" "variance of normal distribution X ~ N(mu,sigma^2)"] + ["ndf(x)" "PDF of standard normal distribution X ~ N(0,1)"] + ["nfreq(PDF;n;[x1 xn])" "normalized n-point frequency dist X~D in x1..xn"] + ["phi(x)" "CDF of standard normal distribution X ~ N(0,1)"] + ["poi.M(lambda)" "median of poisson distribution X ~ Poi(lambda)"] + ["poi.cdf(x;lambda)" "CDF of poisson distribution X ~ Poi(lambda)"] + ["poi.mo(lambda)" "modes of poisson distribution X ~ Poi(lambda)"] + ["poi.mu(lambda)" "mean of poisson distribution X ~ Poi(lambda)"] + ["poi.pmf(x;lambda)" "PMF of poisson distribution X ~ Poi(lambda)"] + ["poi.skew(lambda)" "skewness of poisson distributio X ~ Poi(lambda)"] + ["poi.var(lambda)" "variance of poisson distribution X ~ Poi(lambda)"] + ["qf(x)" "QF of standard normal distribution X ~ N(0,1)"] + ["r.M(a;b)" "median of continuous uniform distribution X ~ U(a,b)"] + ["r.cdf(x;a;b)" "CDF of continuous uniform distribution X ~ U(a,b)"] + ["r.mo(a;b)" "modes of continuous uniform distribution X ~ U(a,b)"] + ["r.mu(a;b)" "mean of continuous uniform distribution X ~ U(a,b)"] + ["r.pdf(x;a;b)" "PDF of continuous uniform distribution X ~ U(a,b)"] + ["r.skew(a;b)" "skewness of continuous uniform distribution X ~ U(a,b)"] + ["r.var(a;b)" "variance of continuous uniform distribution X ~ U(a,b)"] + ["r2(XY;f)" "R^2 statistic for x/y set and regression function"] + ["r2.set(XY;E)" "R^2 statistic for x/y set and expectation E"] + ["rcp(A;B|A;B'|A')" "reverse conditional probability P(A|B) (Bayes' rule)"] + ["rng(D)" "range of data set D"] + ["rse(XY;f)" "RSE (res. sq. error) for x/y set and reg. fn. f"] + ["rse.set(XY;E)" "RSE (res. sq. error) for x/y set and expectation E"] + ["rss(XY;f)" "RSS for x/y set and regression function f"] + ["rss.set(XY;E)" "RSS for x/y set and expectation E"] + ["sd(D)" "standard deviation of data set D (population)"] + ["seg(k;D)" "divide data set D into k segments, returning k+1 endpoints"] + ["shuffle(D)" "randomly arrange the elements of data set D"] + ["sp(x;S)" "interpolate y=S(x) using spline S"] + ["sp2(x;S)" + "interpolate y=S(x) using spline S, extrapolate out of range values"] + ["spl(k;SY)" "compute spline with k degrees of freedom through x/y set"] + ["ssd(d)" "standard deviation of data set D (sample)"] + ["svar(d)" "sample variance of data set D"] + ["t.M(nu)" "median of t-distribution T ~ t(nu)"] + ["t.cdf(nu;x)" "CDF of t-distribution T ~ t(nu)"] + ["t.mo(nu)" "modes of t-distribution T ~ t(nu)"] + ["t.mu(nu)" "mean of t-distribution T ~ t(nu)"] + ["t.pdf(nu;x)" "PDF of t-distribution T ~ t(nu)"] + ["t.qf(nu;x)" "QF of t-dist T ~ t(nu)"] + ["t.skew(nu)" "skewness of t-distribution T ~ t(nu)"] + ["t.var(nu)" "variance of t-distribution T ~ t(nu)"] + ["tscore(x;s^2;n)" "t-score of x given variance s^2 and n observations"] + ["tss(XY)" "total sum of squares (TSS) of x/y set"] + ["u.M(a;b)" "median of discrete uniform distribution X ~ U(a,b)"] + ["u.cdf(x;a;b)" "CDF of discrete uniform distribution X ~ U(a,b)"] + ["u.mu(a;b)" "mean of discrete uniform distribution X ~ U(a,b)"] + ["u.pmf(x;a;b)" "PMF of discrete uniform distribution X ~ U(a,b)"] + ["u.skew(a;b)" "skewness of discrete uniform distribution X ~ U(a,b)"] + ["u.var(a;b)" "variance of discrete uniform distribution X ~ U(a,b)"] + ["var(D)" "variance of data set D"] + ["x2.M(nu)" "median of chi-square distribution X ~ chi^2(nu)"] + ["x2.cdf(nu;x)" "CDF of chi-square distribution X ~ chi^2(nu)"] + ["x2.mo(nu)" "modes of chi-square distribution X ~ chi^2(nu)"] + ["x2.mu(nu)" "mean of chi-square distribution X ~ chi^2(nu)"] + ["x2.pdf(nu;x)" "PDF of chi-square distribution X ~ chi^2(nu)"] + ["x2.qf(nu;x)" "QF of chi-square dist X ~ chi^2(nu)"] + ["x2.skew(nu)" "skewness of chi-square distribution X ~ chi^2(nu)"] + ["x2.var(nu)" "variance of chi-square distribution X ~ chi^2(nu)"] + ["zscore(x;mu;sigma^2)" "z-score of x given mean mu and variance sigma^2"] +]) + +.module(0) diff --git a/klongpy/lib/print.kg b/klongpy/lib/print.kg new file mode 100644 index 0000000..9e3aad0 --- /dev/null +++ b/klongpy/lib/print.kg @@ -0,0 +1,3 @@ +.module(:print) +lin2::{.d("+");{.d((2+#x):^"-");.d("+")}'x;.p("")} +.module(0) diff --git a/klongpy/lib/qdict.kg b/klongpy/lib/qdict.kg new file mode 100644 index 0000000..c19192b --- /dev/null +++ b/klongpy/lib/qdict.kg @@ -0,0 +1,12 @@ +:"decode a CGI query string and return a dictionary containing variable" +:"names (strings) as keys and variable values (also strings) as payload" +.module(:qstr) +hex::{[a];a::(#x)-#0c0;:[a>48;a-39:|a>9;a-7;a]} +char::{:#hex(x@2)+16*hex(x@1)} +hexchr::{,/{:[0c%~*x;char(x),3_x;x]}'(x?0c%):_x} +killcr::{,/{:[(:#13)~*x;1_x;x]}'(x?:#13):_x} +decode::{killcr(hexchr(x:=(:#32),x?0c+))} +split::{[a];(*a),{1_x}'1_a::(x?y):_x} +mkdict::{:{},/{[a];a::split(x;0c=);(*a),,decode(a@1)}'split(x;0c&)} +qdict::{:[x?0c=;mkdict(x);[]]} +.module(0) diff --git a/klongpy/lib/qstr.kg b/klongpy/lib/qstr.kg new file mode 100644 index 0000000..e8ffac7 --- /dev/null +++ b/klongpy/lib/qstr.kg @@ -0,0 +1,14 @@ +:"decode a CGI query string into a safe set of Bourne shell assignments" +:"replaces \r\n by \001 for processing in environment variables" +.module(:qdict) +hex::{[a];a::(#x)-#0c0;:[a>48;a-39:|a>9;a-7;a]} +char::{[c];c::hex(x@2)+16*hex(x@1);:[10~c;:#1;:#c]} +hexchr::{,/{:[0c%~*x;char(x),3_x;x]}'(x?0c%):_x} +killcr::{,/{:[(:#13)~*x;1_x;x]}'(x?:#13):_x} +decode::{killcr(hexchr(x:=(:#32),x?0c+))} +safdec::{[a];,/{:[0c'~*x;"'""'""",x;x]}'(a?0c'):_a::decode(x)} +safchr::{:[(~x<0cA)&~x>0cZ;1:|(~x<0ca)&~x>0cz;1;(~x<0c0)&~x>0c9]} +safe::{{:[safchr(x);x;0c_]}'x} +split::{[a];(*a),{1_x}'1_a::(x?y):_x} +qstr::{{[a];a::split(x;0c=);(safe(*a)),,/"='",safdec(a@1),"'"}'split(x;0c&)} +.module(0) diff --git a/klongpy/lib/set.kg b/klongpy/lib/set.kg new file mode 100644 index 0000000..2234683 --- /dev/null +++ b/klongpy/lib/set.kg @@ -0,0 +1,29 @@ +:"set-theoretic operators" + +.comment("*****") +ps(x) power set +cp(x;y) cartesian product +union(x;y) union +isect(x;y) intersection +enum(x;y) enumerate x-vectors from set of y elements +comb(k;S) k-combinations of set S (without replacement) +combr(k;S) k-combinations of set S with replacement +perm(k;S) k-permutation of set S +***** + +.module(:set) +t::{[a];{x_a}'!#a::x} +c::{[n];:[0=n::x;[]:|1=x;,'y;,/{[t];t::x;(*t),:\c(n-1;1_t)}'t(y)]} +ps::{?:[[]~x;[];[[]],,/(1+!#x){c(x;y)}'(#x)#,x]} +cp::{[a b];b::y;,/{a::x;{a,x}'b}'x} +union::{?x,y} +isect::{[a];a::x;x@,/{a?x}'y} +enum::{[n];(x-1){,/{x,:\!n}'x}:*!n::y} +p1::{:[2>#x;x;,/{(*x),:\p1(1_x)}'(-!#x):+'(#x)#,x]} +tls::{[a];{x_a}'!#a::x} +c3::{[n r s t];n::x;r::z;s::y; + :[0=n;[]:|""~s;[]:|1=n;,'s;,/{t::x;(*t),:\c3(n-1;r(t);r)}'tls(s)]} +comb::{c3(x;y;{1_x})} +combr::{c3(x;y;{x})} +perm::{:[1=x;,'y;,/p1'comb(x;y)]} +.module(0) diff --git a/klongpy/lib/spline.kg b/klongpy/lib/spline.kg new file mode 100644 index 0000000..ac55e91 --- /dev/null +++ b/klongpy/lib/spline.kg @@ -0,0 +1,43 @@ +:"spline interpolation" +.comment("*****") + +spline(x) generates a spline that goes through all x/y tuples in x +sp(x;s) interpolates "y" at "x" using the spline "s" +sp2(x;s) like "sp", but returns min/max instead of :undefined for + out-of-range values + +Example: :"save to file foo and then plot with 'kg -l spline foo | gv -'" + .l("nplot") + s::spline([[-10 0.2] [-7.5 -1.9] [-4.5 3.5] [-1.5 -9.0] [0 0] + [1.5 9.0] [4.5 -3.5] [7.5 1.9] [10 -0.2]]) + frame([-10 10 2];[-10 10 2]) + plot({sp(x;s)}) + +***** + +.l("math.kg") +.module(:spline) +f::{:[2=#x;(*x),(((*x)+x@1)%2),(x@1);x]} +spline::{[xx yy m r u zz];xx::f(*'x);yy::f({x@1}'x);r::#xx;m::((r-2),r-1):^0; + u::{y-x}:'xx;zz::{(((yy@x+1)-yy@x)%(xx@x+1)-xx@x)}; + {xy}:'*'y;:[[]~i;%0;spev(x;y@*i;y@*i+1)]} +sp2::{:[x<**y;sp(**y;y):|x>**|y;sp(**|y;y);sp(x;y)]} +.module(0) + +.comment("*test*") +.l("pt.kg") +pt0(s::spline([[1 2.0] [2 1.5] [4 1.25] [5 1.2] [8 1.125] [10 1.1]])) +.p({rndn(1+1%x;4)}'1+!10) +.p({rndn(sp(x;s);4)}'1+!10) +*test* diff --git a/klongpy/lib/time.kg b/klongpy/lib/time.kg new file mode 100644 index 0000000..3aa7921 --- /dev/null +++ b/klongpy/lib/time.kg @@ -0,0 +1,13 @@ +:"measure process time" + +.comment("*****") +time(x) process time of nilad x in seconds + +Note: the resolution of the timer depends on the CLOCKS_PER_SEC +constant, which may be any value between 50Hz and 1MHz. + +The time function depends on the .pc() (process clock) system +function, which is not avaliable on Plan 9. +***** + +time::{[t0];t0::.pc();x@[];.pc()-t0} diff --git a/klongpy/lib/util.kg b/klongpy/lib/util.kg new file mode 100644 index 0000000..5cd666c --- /dev/null +++ b/klongpy/lib/util.kg @@ -0,0 +1,14 @@ +:"utility functions" + +.comment("*****") +all(f;x) apply monadic function f to all atoms in x +all2(f;x;y) apply dyadic function f to all compatible pairs in x and y +dp(x) depth of a list +flr(p;x) filter x satisfying predicate p +***** + +dp::{:[@x;0;1+|/.f'x]} +flr::{[f];f::x;y@(f'y)?1} +all::{[f];f::x;:[@y;f(y);all(f;)'y]} +all2::{[f a b];f::x;a::y;b::z; + :[@a;:[@b;f(a;b);all(f(a;);b)]:|@b;all(f(;b);a);a{all2(f;x;y)}'b]}