Skip to content

Commit

Permalink
add missing lib
Browse files Browse the repository at this point in the history
  • Loading branch information
briangu committed Dec 14, 2023
1 parent 06ae777 commit f03028a
Show file tree
Hide file tree
Showing 15 changed files with 1,055 additions and 0 deletions.
8 changes: 8 additions & 0 deletions klongpy/lib/README
Original file line number Diff line number Diff line change
@@ -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.

40 changes: 40 additions & 0 deletions klongpy/lib/csv.kg
Original file line number Diff line number Diff line change
@@ -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;
{x<k}{i::1;:[0c\~c::s@x;i::2
:|0c"~c;q::~q
:|(~q)&0c,~c;f::f,x;0];x+i}:~0;
unesc'unquote'(*f),{1_x}'1_f::f:_x}
csv.read::{[r];csv.split(:[ctlm~*|r::.rl();(-1)_r;r])}
csv.make::{{x,",",y}/{"""",($x),""""}'escape'$'x}
csv.write::{.p(csv.make(x))}
csv.load::{|1_.mi{(,csv.read()),x}:~,csv.read()}
.module(0)
24 changes: 24 additions & 0 deletions klongpy/lib/edt.kg
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
:"Interactive line editor by Dave Long; in the public domain"
:"edt(string) will display the string and then accept the"
:"following commands:"

:" this is the string "
:" //// "
:" this is string "
:" ^ some "
:" this is some string "

:"when entering nothing (just pressing ENTER), the edited"
:"string is returned."

edt::{[pad,dlt,ins,upd];
pad::{(#y)#(#y){x,0c }:*x};
dlt::{x@&{~x~0c/}'pad(y;x)};
ins::{:[0c^=*y;(1_y),x;(*x),.f(1_x;1_y)]};
upd::{:[#y?0c^;ins(x;y);dlt(x;y)]};
{.p(x);upd(x;.rl())}:~x}

:"alternative version"
edt::{[upd];
upd::{:[(~#y)|0c^~*y;(1_y),x;((~0c/=*y)#x),.f(1_x;1_y)]};
{.p(x);upd(x;.rl())}:~x}
57 changes: 57 additions & 0 deletions klongpy/lib/eigenv.kg
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
:"Eigenvalue and Eigenvector Function (using the Jacobi method)"
:"by Alexander Shendi"

:"To the extent possible under law, the author"
:"has waived all copyright and related"
:"or neighboring rights to this program."
:"This work is published from: Germany"

.comment("*****")

eigen(x;y) computes the eigenvalues and eigenvectors of the matrix M
with a precision of y fractional digits. It returns a vector [e E]
where e is a vector of eigenvalues and E is a vector of corresponding
eigenvectors. For example,

eigenv([[3.0 2.0 4.0] [2.0 0.0 2.0] [4.0 2.0 3.0]];2)

gives

[[ 8.0 -1.0 -1.0]
[[0.67 0.11 -0.74]
[0.33 0.84 0.42]
[0.67 -0.53 0.53]]]

************************************************************************

.module(:eigenv)
eye::{(x,x):^(1,x:^0)}
diag::{[n];n::*(^x);{+/x}'(x*eye(n))}
msum::{{#(x)+y}/(,/x)}
msum1::{[a1;neq];neq::*(^x);a1::#'(,/(x-(x*eye(neq))));+/((a1>y)*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;
{x<neq}{j::x;{x<j}{[aa;co;si;xx;yy];m::x;xx::(w:@(m,m))-(w:@(j,j));
yy::2.0*(w:@(m,j));aa::atan2(xx;yy)%2.0;co::cos(aa);si::sin(aa);
{x<neq}{[tt];k::x;tt::w:@(k,m);
w::w:-(((co*tt)+(si*(w:@(k,j)))),(k,m));
w::w:-(((co*(w:@(k,j)))-(si*tt)),(k,j));tt::v:@(k,m);
v::v:-((co*tt)+(si*(v:@(k,j)))),(k,m);
v::v:-((co*(v:@(k,j)))-(si*tt)),(k,j);k+1}:~0;
w::w:-(((co*(w:@(m,m)))+si*(w:@(j,m))),(m,m));
w::w:-(((co*(w:@(j,j)))-si*(w:@(m,j))),(j,j));w::w:-(0.0,(m,j));
{x<neq}{k::x;w::w:-((w:@(k,m)),(m,k));
w::w:-((w:@(k,j)),(j,k));k+1}:~0;
m+1}:~0;j+1}:~1;mkvals(w;v)}
eigenv::{[tol;neq;v1;w1;sum;count;maxcnt;z1;nd];maxcnt::50;count::0;
neq::*(^x);v1::eye(neq);tol::0.1^y;w1::{{x}'x}'x;
sum::msum(x);
z1::{count::count+1;termq((*x);sum;tol)}{next(x)}:~mkvals(w1;v1);
nd::y;all(rndn(;nd);mkvals(diag(*z1);(z1@1)))}
.module(0)

help.add([
["eigenv(x;y)"
"eigenvalues and eigenvectors of matrix x with y digits of precision"]
])
88 changes: 88 additions & 0 deletions klongpy/lib/help.kg
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
:"help utility"

.l("util.kg")
.module(:help)

op.db::[
[" a:=b" "Amend" "a=vector, b=[x n0 ...]; amend x in a@n0, ..."]
[" a:=b" "Amend" "a=string, b=[s n0 ...]; amend string s at a@n0, ..."]
[" a:-b" "Amend-in-Depth" "a=vector, b=[x n0 ...]; amend x in (a@n0)@..."]
[" @a" "Atom" "1, if 'a' is atomic"]
[" :#a" "Char" "a=integer; character with code point 'a'"]
[" a:_b" "Cut" "a=integer|list, b=vector; cut 'b' at 'a' or at a0, ..."]
[" a::b" "Define" "a=symbol; assign the value of 'b' to 'a'"]
[" a%b" "Divide" "a,b=real; quotient of 'a' and 'b'"]
[" a_b" "Drop"
"a=integer, b=vector; drop 'a' elements from 'b'; -b = drop from end"]
[" !a" "Enumerate" "a=integer; list of integers 0..'a'-1"]
[" a=b" "Equal" "a,b=real|char|string; 1, if 'a' equals 'b'"]
[" &a" "Expand/Where"
"a=list; list of integers 0.. where each xi is included ai times"]
[" &a" "Expand/Where" "a=integer; list of 'a' zeroes"]
[" a?b" "Find" "a=vector; vector of indices of 'b' in 'a'"]
[" a?b" "Find" "a=string, b=char; vector of indices of 'b' in 'a'"]
[" a?b" "Find"
"a=string, b=string; vector of indices of substring 'b' in 'a'"]
[" a?b" "Find" "a=dict; tuple with key 'b' or :undefined"]
[" *a" "First" "a=vector; first element or []"]
[" *a" "First" "a=string; first element or """""]
[" _a" "Floor" "a=real; 'a' rounded to -infinity"]
[" a:$b" "Form"
"b=string; convert 'b' to an object of the same form (type) as 'a'"]
[" $a" "Format" "convert 'a' to a string representing the value of 'a'"]
[" a$b" "Format2"
"a=real; Format 'b', pad with 'a' blanks or to align to x.y digits"]
[" >a" "Grade-Down"
"a=vector; vector of indices of elements of 'a' in ascending order"]
[" <a" "Grade-Up"
"a=vector; vector of indices of elements of 'a' in descending order"]
[" =a" "Group" "a=vector; vector of groups (matching elements) of 'a'"]
[" a@b" "Index/Apply" "a=vector, b=integer; extract b'th element of 'a'"]
[" a@b" "Index/Apply"
"a=vector, b=list(integer); extract bi'th elements from 'a'"]
[" a:@b" "Index-in-Depth" "a=vector, b=integer; extract b'th element of 'a'"]
[" a:@b" "Index-in-Depth" "a=vector, b=list(integer); extract (a@b0)@..."]
[" a:%b" "Integer-Divide" "truncated quotient of 'a' and 'b'"]
[" a,b" "Join" "join 'a' and 'b' in a list/vector"]
[" a<b" "Less" "a,b=real|char|vector; 1, if 'a' is less than 'b'"]
[" ,a" "List" "single-element list containing 'a'"]
[" a~b" "Match" "1, if a=b or 'a' and 'b' are equal vectors"]
[" a|b" "Max/Or" "a,b=real; greater value of 'a' and 'b'; also logical OR"]
[" a&b" "Min/And" "a,b=real; lesser value of 'a' and 'b'; also logical AND"]
[" a-b" "Minus" "a,b=real; difference between 'a' and 'b'"]
[" a>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)
16 changes: 16 additions & 0 deletions klongpy/lib/huffman.kg
Original file line number Diff line number Diff line change
@@ -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}
Loading

0 comments on commit f03028a

Please sign in to comment.