{kxsystems}
Milano Milano's Page
Simon Simon's Page
why apl?
k: highest integer in float
k: matrix operations (xls sheet)
k: jdbc
k: ksql over sql92
k: re:fifo
k: fifo
k: cross correlation over the freq domain
k: Interpreted... _lsq example
k: rand num generator is good http://www.kx.com/listbox/kdb/msg00138.html

used Andale Mono, ping me if u think i should use another monospaced font.

/ still learning the language, i wish i had a more extensive knowledge of math and statistics 
/ to use k to its full power... for now only simple stuff to wrap my head around
/ stat examples @ http://www.kx.com/a/k/examples/stat.k


/ ALPHABET A
  j: _ic "aA"
  j
97 65
  j+\:!9
(97 98 99 100 101 102 103 104 105
 65 66 67 68 69 70 71 72 73)
  _ci j+\:!30
("abcdefghijklmnopqrstuvwxyz{|}~"
 "ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^")

  3 + 125 * 6 % 100
10.5
  2 5 # !10
(0 1 2 3 4
 5 6 7 8 9)

  ???
/ SPELLING B
  sentence:"index =. a.i.'aA'"
  "."=sentence
0 0 0 0 0 0 0 1 0 0 1 0 1 0 0 0 0
  sentence
"index =. a.i.'aA'"
  s:&"."=sentence
  s
7 10 12
  (0,s) _ sentence
("index ="
 ". a"
 ".i"
 ".'aA'") 
/ GRAMMAR C
  fahrenheit: 50
  (fahrenheit - 32) * 5 % 9
10.0
  prices: 3 1 4 2
  orders: 2 0 2 1
  orders * prices
6 0 8 2
  +/ orders * prices
16
  +\ 1 2 3 4 5
1 3 6 10 15
  2 3 * \ 1 2 3 4 5
1 2 3 4 5
  2 3 *\: 1 2 3 4 5
(2 4 6 8 10
 3 6 9 12 15)
  decr:{x-1}
  decr -1 0 1 2 3
-2 -1 0 1 2
         PARTS OF SPEECH
  50 fahrenheit  Nouns/Pronouns
  + - * % decr   Verbs/Proverbs
  / \            Adverbs
  &              Conjunction ???
  :              Verb-to-be
  ( )            Punctuation
/ TABLES Da
  prices: 3 1 4 2
  orders: 2 0 2 1
  prices * orders
6 0 8 2
  prices *\: orders
(6 0 6 3
 2 0 2 1
 8 0 8 4
 4 0 4 2)

    TO READ A TABLE,
    BORDER IT BY ITS
    ARGUMENTS:

      * | 2 0 2 1
      -----------
      3 | 6 0 6 3
      1 | 2 0 2 1
      4 | 8 0 8 4
      2 | 4 0 4 2 
/ TABLES Db
  n: 0 1 2 3
  n +/: n
(0 1 2 3
 1 2 3 4
 2 3 4 5
 3 4 5 6)

  n */: n
(0 0 0 0
 0 1 2 3
 0 2 4 6
 0 3 6 9)

  n ^/: n
(1 1 1 1.0
 0 1 2 3.0
 0 1 4 9.0
 0 1 8 27.0)

  ???
/ TABLES Dc
  ???

  +

  +

  n: !4
  n =\: n
(1 0 0 0
 0 1 0 0
 0 0 1 0
 0 0 0 1)

  n <\: n
(0 1 1 1
 0 0 1 1
 0 0 0 1
 0 0 0 0)
  ~n >\: n
(1 1 1 1
 0 1 1 1
 0 0 1 1
 0 0 0 1)
/ TABLES Dd
  text: " i sing of olaf "
  text: text,"glad and big"

  alph: " abcdefghijklmno"
  alph: alph,"pqrstuvwxyz"

  alph[!10]=\:text
(1 0 1 0 0 0 0 1 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 1 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 1 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0)

  2 13 #(#:'&:'alph=\:text)
(7 3 1 0 2 0 2 3 0 3 0 0 2
 0 2 2 0 0 0 1 0 0 0 0 0 0)
  2 13 # #:'&:'alph=\:text    / freq of alph in text
(7 3 1 0 2 0 2 3 0 3 0 0 2
 0 2 2 0 0 0 1 0 0 0 0 0 0)

  / 7 spaces alph[0]
  / 3 a    alph[1]
  / 1 b    alph[1]
/ CLASSIFICATION Ea
  x: 1 2 3 4 5 6 7
  y: (x-3) * (x-5)
  y
8 3 0 -1 0 3 8

  range: |n+!(1+|/y-n:&/y)
  range
8 7 6 5 4 3 2 1 0 -1

  bc: range <\: y
  bc
(0 0 0 0 0 0 0
 1 0 0 0 0 0 1
 1 0 0 0 0 0 1
 1 0 0 0 0 0 1
 1 0 0 0 0 0 1
 1 0 0 0 0 0 1
 1 1 0 0 0 1 1
 1 1 0 0 0 1 1
 1 1 0 0 0 1 1
 1 1 1 0 1 1 1)
/ CLASSIFICATION Eb
  x: 1 2 3 4 5 6 7
  y: (x-3) * (x-5)
  y
8 3 0 -1 0 3 8

  range: |n+!(1+|/y-n:&/y)
  range
8 7 6 5 4 3 2 1 0 -1


/
/
/ CLASSIFICATION +,* on subsets Ee
  a: 0 0 0 0 1 1 1 1
  b: 0 0 1 1 0 0 1 1
  c: 0 1 0 1 0 1 0 1
  cct:(a;b;c)
  cct
(0 0 0 0 1 1 1 1
 0 0 1 1 0 0 1 1
 0 1 0 1 0 1 0 1)

  +/cct * 2 3 5
0 5 3 8 2 7 5 10
  +/+2 3 5 */:+cct
0 5 3 8 2 7 5 10

  */cct * 2 3 5
0 0 0 0 0 0 0 30
  g:"23456789"
  (!(-2)+#g) !\:g
("23456789"
"34567892"
"45678923"
"56789234"
"67892345"
"78923456")
  / last element
  / fast , slow
  v: 1000000 _draw 56
  #v
1000000

/ get last element
  \t do[100;v -1+#v] 
0
  \t do[100;*|v]
5618
  n&/:n
(0 0 0 0
 0 1 1 1
 0 1 2 2
 0 1 2 3)
/ alternate takes of 1s and 0s
  4#1 0
1 0 1 0
  1#1 0
,1
  5#1 0
1 0 1 0 1
  5#0 1
0 1 0 1 0
/ split string by spaces
  str:"this is a test."
  " "=str
0 0 0 0 1 0 0 1 0 1 0 0 0 0 0
  g:&" "=str
  g
4 7 9
  g:0,g
  g
0 4 7 9
  g _ str
("this"
 " is"
 " a"
 " test.")
  / reverse
  r:"reverse"
  s:`$r
  s
`reverse
  t:2#s 
  t
`reverse `reverse
  |:'$t
("esrever"
 "esrever")
  `$|:'$t
`esrever `esrever
  / ascii of number
  ,/$2 _vs 15
"1111"
  ,/$2 _vs 15345
"11101111110001"
  / freq of values
  l: 10000 _draw 5
  l[*:'p],'#:'p:=l
(4 1972
 0 1945
 3 2029
 1 2013
 2 2041)
  g[<g:l[*:'p],'#:'p:=l]
(0 1945
 1 2013
 2 2041
 3 2029
 4 1972)
/ wrong terminology(?)
/ flip matrix
  n:!4
  m:n|/:n
  m
(0 1 2 3
 1 1 2 3
 2 2 2 3
 3 3 3 3)
  ||:'m
(3 3 3 3
 3 2 2 2
 3 2 1 1
 3 2 1 0)
  |:'|m
(3 3 3 3
 3 2 2 2
 3 2 1 1
 3 2 1 0)
  / alpha
  _ci 97+!26
"abcdefghijklmnopqrstuvwxyz"
  2 # !3
0 1
  2 #/: !3
(0 0
 1 1
 2 2)
  2 #/ !3
2
  2 #\ !3
(2
 0 0
 ()
 2)
  2 #:\ !3
(0 1 2
 3
 1) 
  / matrix indexing
  g:(4 _draw -4)+/:!4
  g
(2 0 3 1
 3 1 4 2
 4 2 5 3
 5 3 6 4)
  g[0;]
2 0 3 1
  g[;1]
0 1 2 3
  g[;1 3]
(0 1
 1 2
 2 3
 3 4)
  g[1 3;]
(3 1 4 2
 5 3 6 4)
  / identity matrix
  n:!4
  n~'/:n
(1 0 0 0
 0 1 0 0
 0 0 1 0
 0 0 0 1)
  / searching vectors
  v:10000 _draw -10000
  \t do[10000;999 _in v]
630
/ using _bin is faster 
/ even for small vectors
  \t do[10000;v _bin 999]
10
 / string to symbol
  s:"applegate"
  `$($:'s)
`a `p `p `l `e `g `a `t `e
/ try take
  4 2#s
("ap"
 "pl"
 "eg"
 "at")
  `$(4 2#s)
`ap `pl `eg `at
  `$2 4#s
`appl `egat
  / _dot
  / x _dot y  ~  +/x*y

  / arthur says ~2001
  / +/x*y has 3 loads and 1 store per element
  / x _dot y has 2 loads

  / NB: for +/x*y , +/ is not sum
  g:2 2 _draw 5
  g
(1 4
 3 4)
  +g
(1 3
 4 4)
  / sum > 2gb
  s1
{+/0.0$$x}
  s1[50#*/(30#2)]
53687091200.0
 / MFTL 13C
/ k 
  s:"do you love me"
  |/s=/:"aeiou"
0 1 0 0 1 1 0 0 1 0 1 0 0 1
  s[&|/s=/:"aeiou"]
"oouoee"
  s[&~|/s=/:"aeiou"]
"d y lv m"
/ j
  or/'aeiou' =/ s
0 1 0 0 1 1 0 0 1 0 1 0 0 1
  / rotate until
/ last 2 numbers
  g:"23456789"
  (!(-2)+#g) !\:g 
("23456789"
"34567892"
"45678923"
"56789234"
"67892345"
"78923456") 
/ 4 take chars of each string
  4#'(!(-2)+#g)
("2345"
"3456"
"4567"
"5678"
"6789"
"7892")
/ form string to float
/ see last value in vector
  0.0$4#'(!(-2)+#g) !\:g   
2345 3456 4567 5678 6789 7892.0
/ experiment
  4#/:g
("2222"
 "3333"
 "4444"
 "5555"
 "6666"
 "7777"
 "8888"
 "9999")
  2 3 _ g
(,"3"
 "456789")
  / forward reduction
  (!#g) _\: g
("123456789"
 "23456789"
 "3456789"
 "456789"
 "56789"
 "6789"
 "789"
 "89"
 ,"9")
  /
  /
  /
/ Thu Aug 26 01:42:08 UTC 2004
/ what are you trying to do??
select from(update recno:$ from Quotes)where Bid=0
 
/ k cannot be used from ksql directly. wrap in function/proc i.e. find:{x?y}
/ select find[col,0] by Symbol from Quotes.

ksql>n rand 200
158 43 181 161 177 83 115 29 32 49
ksql>n rand -20
1 14 17 2 12 16 13 10 5 3
ksql>n rand -10
7 6 2 3 8 4 0 1 9 5
ksql>n rand -11
9 4 8 1 3 6 2 0 10 5
ksql>n rand -12
10 9 0 6 3 1 11 8 4 7
/----------------------------
/ Getting frequency of items
/----------------------------
  l:`a `a `b
  #:'=l
2 1
  =l
(0 1
 ,2)
  (*:'=l)
0 2

  (#:'=l),'(*:'=l)
(2 0
 1 2)
  l[*:'=l],'#:'=l
((`a;2)
 (`b;1))
  a:(!5;!10)
  a
(0 1 2 3 4
 0 1 2 3 4 5 6 7 8 9)
  fn/(!5;!10)
0 1 2 3 4,0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 0 1 2 3 4 5 6 7 8 9
  g:fn/(!5;!10)
0 1 2 3 4,0 1 2 3 4 5 6 7 8 9
  g
0 1 2 3 4 0 1 2 3 4 5 6 7 8 9
  fn
{`0:(5:x),",",(5:y),"\n";:x,y}   /   :x,y    is the return part,    `0... is the print part


/ fn takes a list of count 2 (2 ~ #a)
/ g returns one thing, but prints another... pretty neat!

Last Updated: $Wed Sep 28 18:13:40 UTC 2005$