Hello
# // Examples
# // turn on implicit printing
# /p
Implicit printing on
# // Basic operations
# 5 + 8 * 11
93
# 5 + 8 * ++11 / 6 + 3
24
# 5 + 8 * 11++ / 6 + 3
24
# (5 + 8) * ++11 / 6 + 3
29
# ((5 + 8) * ++11 / 6) + 3
29
# 6^2
36
# n~10
# ]n,n>>1,n<<2
10 5 40
# ]5 > 10, 5 < 10, 6 <= 6, 7 = 10, 7 = 7, 9 != 6
$0 $1 $1 $0 $1 $1
# ]4 && 16, 63 && 16, 47 && 20 // bitwise
0 16 4
# ]4 & 16, 63 & 16, 0 & 20 // logical
$1 $1 $0
# // Conditionals
# f () n ~ ? {n > 6} {]n,"is greater than 6"}
# f 5; f 7
7 is greater than 6
# f2 () n ~ ?: {n > 6} {]n,"is greater than 6"} {]n,"is less than 6"}
# f3 () n ~ ?: {n > 6} {"is greater than 6"} {"is less than 6"}
# ]9,f3 9
9 is greater than 6
# f4 () n ~ ?: {n > 6} {Str n :: " is greater than 6"} {Str n :: " is less than 6"}
# f4 4
4 is less than 6
# f2 5; f2 7
5 is less than 6
7 is greater than 6
# ]@f2
f2 () n ~ ?: {n > 6} {]n,"is greater than 6"} {]n,"is less than 6"}
# ?+ 10 {]_,"\t",_^2,"\t",_^0.5}
1 1 1
2 4 1.4142135623731
3 9 1.7320508075689
4 16 2
5 25 2.2360679774998
6 36 2.4494897427832
7 49 2.6457513110646
8 64 2.8284271247462
9 81 3
10 100 3.1622776601684
# f () n ~ ?+ n {]_,"\t",_^2,"\t",_^0.5}
# f 10
1 1 1
2 4 1.4142135623731
3 9 1.7320508075689
4 16 2
5 25 2.2360679774998
6 36 2.4494897427832
7 49 2.6457513110646
8 64 2.8284271247462
9 81 3
10 100 3.1622776601684
# f2 () n m ~ ?+ n {k ~ m + _; ]k,"\t",k^2,"\t",k^0.5}
# f2 10 0
1 1 1
2 4 1.4142135623731
3 9 1.7320508075689
4 16 2
5 25 2.2360679774998
6 36 2.4494897427832
7 49 2.6457513110646
8 64 2.8284271247462
9 81 3
10 100 3.1622776601684
# f2 11 9
10 100 3.1622776601684
11 121 3.3166247903554
12 144 3.4641016151378
13 169 3.605551275464
14 196 3.7416573867739
15 225 3.8729833462074
16 256 4
17 289 4.1231056256177
18 324 4.2426406871193
19 361 4.3588989435407
20 400 4.4721359549996
# f3 () n m ~ ?+ {n <= m} {k ~ n + _ - 1; m--; ]k,"\t",k^2,"\t",k^0.5}
# f3 10 20
10 100 3.1622776601684
11 121 3.3166247903554
12 144 3.4641016151378
13 169 3.605551275464
14 196 3.7416573867739
15 225 3.8729833462074
16 256 4
17 289 4.1231056256177
18 324 4.2426406871193
19 361 4.3588989435407
20 400 4.4721359549996
# f ~ .
# ?+ 10 {fs~Factors _;?: {#fs=1} {]fs'1,"is prime"} {]_,"has factors",fs}}
1 is prime
2 is prime
3 is prime
4 has factors [ 2 2 ]
5 is prime
6 has factors [ 2 3 ]
7 is prime
8 has factors [ 2 2 2 ]
9 has factors [ 3 3 ]
10 has factors [ 2 5 ]
# ?+ 10 {fs~Factors _;?: {#fs=1} {]fs'1,"is prime"} {]_,"has factors",Unique fs}}
1 is prime
2 is prime
3 is prime
4 has factors [ 2 ]
5 is prime
6 has factors [ 2 3 ]
7 is prime
8 has factors [ 2 ]
9 has factors [ 3 ]
10 has factors [ 2 5 ]
# f () m n ~ {?+ n {n ~ UIRand m;fs~Factors n;?: {#fs=1} {]fs'1,"is prime"} {]n,"has factors",Unique fs}}}
# f 100000000 10
57085797 has factors [ 3 19028599 ]
49731451 has factors [ 7 11 23 28081 ]
33166431 has factors [ 3 127 29017 ]
88692571 has factors [ 11 193 41777 ]
8553450 has factors [ 2 3 5 127 449 ]
24788497 has factors [ 577 42961 ]
73959897 has factors [ 3 11 2241209 ]
37933240 has factors [ 2 5 948331 ]
10465820 has factors [ 2 5 37 14143 ]
25486411 is prime
# // supports nesting to 10 levels
# ?+ 3 {]_; ?+ 4 {]_*10}}
1
10
20
30
40
2
10
20
30
40
3
10
20
30
40
# // Arrays
# a ~ [[10]]
# Fill a 5
[ 5 5 5 5 5 5 5 5 5 5 ]
# a'1 ~ 10
# a
[ 10 5 5 5 5 5 5 5 5 5 ]
# a ~ a + [1:10]
# a
[ 11 7 8 9 10 11 12 13 14 15 ]
# [1:20:2]
[ 1 3 5 7 9 11 13 15 17 19 ]
# [0.1:2.0:0.2]
[ 0.1 0.3 0.5 0.7 0.9 1.1 1.3 1.5 1.7 1.9 2.1 ]
# [1:12]/4
[ 1/4 1/2 3/4 1 5/4 3/2 7/4 2 9/4 5/2 11/4 3 ]
# Dec $
[ 0.25 0.5 0.75 1 1.25 1.5 1.75 2 2.25 2.5 2.75 3 ]
# Frac $
[ 1/4 1/2 3/4 1 5/4 3/2 7/4 2 9/4 5/2 11/4 3 ]
# [1 4 -3 2 a]^2
[ 1 16 9 4 [ 121 49 64 81 100 121 144 169 196 225 ] ]
# ]a~Shuffle [1:10]
[ 6 7 3 4 8 2 5 1 9 10 ]
# #a // length
10
# `a // tail
[ 7 3 4 8 2 5 1 9 10 ]
# :a // head
6
# a'[3:6] // subrange
[ 3 4 8 2 ]
# 6<-a // index in
1
# a::99 // append
[ 6 7 3 4 8 2 5 1 9 10 99 ]
# // choose the even entries only
# a ## (a % 2 = 0)
[ 6 4 8 2 10 ]
# a'4~. // delete the 4th entry
# a'[5 7] ~. // delete the 5th and 7th entries
# a
[ 6 7 3 8 5 9 10 99 ]
# Part a 4 // partition into a 4 by 2 matrix
[ 6 7 3 8
5 9 10 99 ]
# a'2 // print the second element
7
# a'[2] // print the second row
[ 5 9 10 99 ]
# a'[2] ~ [1 2 3 4] // replace the second row
# a
[ 6 7 3 8
1 2 3 4 ]
# a'[2] ~ . // delete the second row
# a
[ 6 7 3 8 ]
# listprint () x:xs ~ {]%x,"";listprint xs}{]"and",x}
# listprint [1:4]
1 2 3 and 4
# listprint [1]
and 1
# listprint2 () l ~ ?: {#l>1}{listprint l}{]l'1}
# listprint2 [1:4]
1 2 3 and 4
# listprint2 [1]
1
# ?+ 10 {fs~Factors _;?: {#fs=1} {]fs'1,"is prime"} {]%_,"has factors ";listprint2 Unique fs}}
1 is prime
2 is prime
3 is prime
4 has factors 2
5 is prime
6 has factors 2 and 3
7 is prime
8 has factors 2
9 has factors 3
10 has factors 2 and 5
# // Combinatorics
# a~[1 2]; ?+ {!NaN a} {]a;a~NextPerm a 3}
[ 1 2 ]
[ 2 1 ]
[ 1 3 ]
[ 3 1 ]
[ 2 3 ]
[ 3 2 ]
# a~[1 2 3]; ?+ {!NaN a} {]a;a~NextComb a 5}
[ 1 2 3 ]
[ 1 2 4 ]
[ 1 2 5 ]
[ 1 3 4 ]
[ 1 3 5 ]
[ 1 4 5 ]
[ 2 3 4 ]
[ 2 3 5 ]
[ 2 4 5 ]
[ 3 4 5 ]
# a~[]; ?+ {!NaN a} {]a;a~NextSubset a 3}
[ ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 1 2 ]
[ 1 3 ]
[ 2 3 ]
[ 1 2 3 ]
# a~[ [1 2 3] ]; ?+ {!NaN a} {]a;a~NextPart a}
[ [ 1 2 3 ] ]
[ [ 1 2 ] [ 3 ] ]
[ [ 1 3 ] [ 2 ] ]
[ [ 1 ] [ 2 3 ] ]
[ [ 1 ] [ 2 ] [ 3 ] ]
# s~["a" "b" "c"]
# a~[ [1 2 3] ]; ?+ {!NaN a} {]s'a;a~NextPart a}
[ [ a b c ] ]
[ [ a b ] [ c ] ]
[ [ a c ] [ b ] ]
[ [ a ] [ b c ] ]
[ [ a ] [ b ] [ c ] ]
# // Operator arguments
# f ~ .
# f () x:xs @1 ~ {x @1 f xs @1}{x}
# f [1:4] +
10
# f [1:4] *
24
# // validate the Factors function
# Factors 568
[ 2 2 2 71 ]
# f $ *
568
# Factors Complex 568
[ (1+i) (1-i) (1+i) (1-i) (1+i) (1-i) 71 ]
# f $ *
568
# // Functions as arguments
# f3 () @f ~ @f 3
# f3 @Log
0.47712125471966
# s~"@Log"; ]f3 s
0.47712125471966
# // Matrices
# fill () ~ UIRand 10
# m ~ [[16]]
# FnFill m @fill
[ 8 2 3 5 3 8 4 2 7 7 5 10 10 5 6 3 ]
# Part m 4
[ 8 2 3 5
3 8 4 2
7 7 5 10
10 5 6 3 ]
# Diag m
[ 8 8 5 3 ]
# Trace m
24
# Det m
-460
# Poly CharP m
x^4-24x^3+32x^2+547x-460
# Roots CharP m
[ 0.82492428183042 -4.2783376656583 6.105342852804 21.348070531024 ]
# Inv m
[ 21/46 41/230 -49/230 -39/230
33/92 163/460 -77/460 -127/460
-95/92 -249/460 191/460 321/460
-5/92 -47/460 73/460 -17/460 ]
# Dec $
[ 0.45652173913043 0.17826086956522 -0.21304347826087 -0.1695652173913
0.35869565217391 0.35434782608696 -0.16739130434783 -0.27608695652174
-1.0326086956522 -0.541304347 ... ]
# lu~LU m
# lu'1
[ 1 0 0 0
3/8 1 0 0
7/8 21/29 1 0
5/4 10/29 73/17 1 ]
# lu'2
[ 8 2 3 5
0 29/4 23/8 1/8
0 0 17/58 321/58
0 0 0 -460/17 ]
# MSolve m [1 2 3 4]
[ -58/115
-62/115
221/115
8/115 ]
# m ** $
[ 1
2
3
4 ]
# // Array functions
# n ~ 1000
# x ~ [[n]]
# FnFill x @fill
[ 2 1 7 1 5 7 10 10 9 5 3 10 10 2 4 1 1 1 10 2 9 1 1 6 5 10 9 2 2 7 4 6 1 7 7 1 3 6 7 10 10 2 5 9 1 1 5 5 9 2 5 2 5 4 2 8 5 8 2 9 4 9 1 1 1 4 3 10 3 7 9 4 8 2 7 10 4 4 1 6 2 1 10 ... ]
# Unique x
[ 1 2 3 4 5 6 7 8 9 10 ]
# Freq x Unique x]
[ 96 93 101 98 97 92 107 106 93 117 ]
# x'[30:35]
[ 7 4 6 1 7 7 ]
# // explicit function application
# Apply @Log [1:3]
[ 0 0.30102999566398 0.47712125471966 ]
# f () x ~ Log x
# Apply @f [1:3]
[ 0 0.30102999566398 0.47712125471966 ]
# // and in 2D
# f () x y ~ x + y
# Apply2D @f [1:4] [10:30:10]
[ 11 12 13 14
21 22 23 24
31 32 33 34 ]
# ]x ~ Shuffle [1:10]
[ 5 2 8 10 9 4 1 7 3 6 ]
# Sort x
[ 1 2 3 4 5 6 7 8 9 10 ]
# Sample x 6 $0
[ 6 5 4 3 10 9 ]
# Sample x (#x + 1) $1
[ 7 4 10 10 1 5 10 1 6 7 3 ]
# sum () x:xs ~ {x + sum xs}{x}
# a ~ Sample [1:10] 1000000 $1
# Hist a 10
[ 100075 100203 99985 100277 99716 100124 100003 99510 100027 100080 ]
# Hist a [10 1 10]
[ 100075 100203 99985 100277 99716 100124 100003 99510 100027 100080 ]
# sum $
1000000
# data ~ Sample [1:10] 1000 $1
# Hist data 10
[ 98 104 99 96 98 109 78 103 103 112 ]
# Sort data
[ 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... ]
# ones ~ data ## data = 1
# onesandtwos ~ data ## ((data = 1) | (data = 2))
# # ones
98
# Moments ones
[ 1 0 nan nan ]
# # onesandtwos
202
# Moments onesandtwos
[ 1.5148514851485 0.25102211713709 -0.059432163881274 1.0035321821036 ]
# data ~ Sample [1:10] 1000 $1
# ranks ~ Ranks data
# data ## ((ranks >= 100) & (ranks < 200))
[ 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 ... ]
# Moments $
[ 2 0 nan nan ]
# Pi
3.1415926535898
# Frac Pi
3126535/995207
# Frac E
1084483/398959
# /d 7
setting maximum denominator to 7
# Frac Pi
22/7
# Frac E
19/7
# /d 1000000
setting maximum denominator to 1000000
# // Model fitting
# file~FOpen "lm.dat" "w"
# FWrite file "x\ty\tz\tf"
1
# FWrite file "1\t1\t1\t1"
1
# FWrite file "2\t-1\t2\t2"
1
# FWrite file "-1\t4\t3\t-1"
1
# FWrite file "4\t2\t1\t4"
1
# FWrite file "3\t-3\t4\t8"
1
# FWrite file "7\t4\t9\t11"
1
# FClose file
NaN
# /s cat lm.dat
x y z f
1 1 1 1
2 -1 2 2
-1 4 3 -1
4 2 1 4
3 -3 4 8
7 4 9 11
# file~FOpen "lm.dat" "r"
# data ~ FRead file 100
# ]x~`Extract 1 data
[ 1 2 -1 4 3 7 ]
# ]y~`Extract 2 data
[ 1 -1 4 2 -3 4 ]
# ]z~`Extract 3 data
[ 1 2 3 1 4 9 ]
# f ~ .
# ]f~`Extract 4 data
[ 1 2 -1 4 8 11 ]
# data~.
# FClose file
NaN
# "#\n# With intercept\n#"
#
# With intercept
#
# LMFit "f : 1 x y z"
Rsq->0.96199584691297
Residuals->[ 0.11162116453249 -1.6132721269228 0.25382942739792 0.51046146412177 0.95865345896755 -0.22129338809696 ]
F->16.87527579262
PValues->[ 0.73920160257934 0.081764381738368 0.17705316716973 0.14448554994521 ]
Parameters->[ -0.35195296770556 1.032004509946 -0.4948538294274 0.70318112265443 ]
StdErrs->[ 0.92123194497534 0.31473534437828 0.24156221910276 0.30093259777589 ]
Model->f : 1 x y z
AdjRsq->0.90498961728242
PValue->0.056461130794629
SSE->3.9080937424499
# "#\n# Without intercept\n#"
#
# Without intercept
#
# LMFit "f : x y z"
Rsq->0.97974248919862
Residuals->[ -0.16489683903428 -1.8373406846081 0.028098846624558 0.32241355865155 0.82787138565759 0.013486951899427 ]
F->48.364406604778
PValues->[ 0.030441871028519 0.090211965229835 0.068768449636633 ]
Parameters->[ 1.0050684237202 -0.5025156688465 0.66234408416056 ]
StdErrs->[ 0.25942740108434 0.20359974884897 0.23792170338938 ]
Model->f : x y z
AdjRsq->0.95948497839724
PValue->0.0048648664373978
SSE->4.1933047358857
# // easier way
# ..
# file~FOpen "lm.dat" "r"
# data ~ Assign FRead file 100
# ]]
f: [ 1 2 -1 4 8 11 ]
file: 11
x: [ 1 2 -1 4 3 7 ]
y: [ 1 -1 4 2 -3 4 ]
z: [ 1 2 3 1 4 9 ]
data: [ [ x y z f ] [ 1 1 1 1 ] [ 2 -1 2 2 ] [ -1 4 3 -1 ] [ 4 2 1 4 ] [ 3 -3 4 8 ] [ 7 4 9 11 ] ]
# data~.
# FClose file
NaN
# ]LMFit "f : 1 x y z"
Rsq->0.96199584691297
Residuals->[ 0.11162116453249 -1.6132721269228 0.25382942739792 0.51046146412177 0.95865345896755 -0.22129338809696 ]
F->16.87527579262
PValues->[ 0.73920160257934 0.081764381738368 0.17705316716973 0.14448554994521 ]
Parameters->[ -0.35195296770556 1.032004509946 -0.4948538294274 0.70318112265443 ]
StdErrs->[ 0.92123194497534 0.31473534437828 0.24156221910276 0.30093259777589 ]
Model->f : 1 x y z
AdjRsq->0.90498961728242
PValue->0.056461130794629
SSE->3.9080937424499
# // Factors and complex arithmetic
# Factors 24
[ 2 2 2 3 ]
# Factors 11232354
[ 2 3 7 31 8627 ]
# Factors Complex 24
[ (1+i) (1-i) (1+i) (1-i) (1+i) (1-i) 3 ]
# GCD 25 80
5
# GCD (25 + 5 * I) (4 + 2 * I)
(-3+i)
# (4 + 2 * I) / (I - 3)
(-1-i)
# (25 + 5 * I) / (I - 3)
(-7-4i)
# (I-3)*(-1-I)
(4+2i)
# z ~ 3 + 4*I
# Arg z
0.92729521800161
# Abs z
5
# Arg z * 180 / Pi
53.130102354156
# Cos Arg z
0.6
# n~[1:20:2]
# n/15
[ 1/15 1/5 1/3 7/15 3/5 11/15 13/15 1 17/15 19/15 ]
# nn~n/15 + n
# Ln (nn - 5)
[ (1.3694872428035+3.1415926535898i) (0.58778666490212+3.1415926535898i) -1.0986122886681 0.90286771154201 1.526056303495 1.907070315739 2.1822989271195 2.3978952727984 2.57515352 ... ]
# e~Chop Exp $ 10
# ??e
array
# type () a ~ ??a
# Apply @type e
[ complex complex decimal decimal decimal decimal decimal integer decimal decimal ]
# Frac e
Evaluation failed: argument type or range mismatch because of 'Frac e'
# fr ~ Frac Re e
# (fr + 5) * 15 / 16
[ 1 3 5 7 9 11 13 15 17 19 ]
# // Mathematical functions
# a~[1:10]
# Ln a
[ 0 0.69314718055995 1.0986122886681 1.3862943611199 1.6094379124341 1.7917594692281 1.9459101490553 2.0794415416798 2.1972245773362 2.302585092994 ]
# Ln (-a)
[ 3.1415926535898i (0.69314718055995+3.1415926535898i) (1.0986122886681+3.1415926535898i) (1.3862943611199+3.1415926535898i) (1.6094379124341+3.1415926535898i) (1.7917594692281+3. ... ]
# Exp $
[ -1 (-2+2.4492127076448e-16i) (-3+3.6738190614671e-16i) (-4+4.8984254152895e-16i) (-5+6.1230317691119e-16i) (-6+7.3476381229343e-16i) (-7+8.5722444767566e-16i) (-8+9.796850830579 ... ]
# Chop $ 10
[ -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 ]
# Fact 6
720
# Gamma 7
720
# Gamma 7.4
1541.3361918941
# Gamma (3 + 5 * I)
(0.059470074633257+0.005750979187797i)
# Ln $
(-2.8176279605488+0.096403984062884i)
# LnGamma (3 + 5 * I)
(-2.8176279605488+6.3795892912425i)
# Exp $
(0.059470074633257+0.0057509791877971i)
# LnGamma (123 + 225 * I)
(316.54065905151+1154.1764381989i)
# Beta 3 I
(-0.6-0.2i)
# // Date and Time
# Now
1337260504
# Date Now
17-May-2012
# Time Now
23:15:04
# DateTime (Now + 1000000)
29-May-2012 13:01:44
# DateSecs [2000 1 1]
946648800
# Date $
01-Jan-2000
# // Statistical functions
# nfill () ~ GRand 0 1
# n ~ 10000
# d ~ [[n]]
# FnFill d @nfill
[ -0.0038063189694339 -0.38176469772768 -0.71394764448691 -0.14661983733275 0.15296247202399 1.327564724161 -0.38959980530423 1.0066637734222 0.2861807785978 -0.0027190053399673 - ... ]
# Moments d
[ -0.018312367534774 0.99671944855368 -0.014642204019945 3.048718955525 ]
# Stdev d
0.9983583768135
# Range d
[ -4.2096801682634 4.033096495747 ]
# Z d
[ 0.014530627737579 -0.36406816691675 -0.69681396729022 -0.12852487506761 0.17156504910568 1.3481575573029 -0.37191655036632 1.0267128692601 0.30500908191279 0.01561978372052 -1.5 ... ]
# Z d - d
[ 0.018336946707013 0.017696530810926 0.017133677196693 0.018094962265138 0.018602577081692 0.020592833141949 0.017683254937916 0.020049095837863 0.018828303314988 0.0183387890604 ... ]
# Median d
-0.022668552334777
# ZTest d 0 1
0.067065213136243
# TestG d
0.90909910534371
# TestU d
5.9999999968419e-08
# n ~ 100
# d ~ Sample [1:10] n $1
# Moments d
[ 5.12 8.3692929292929 0.15913950990142 1.8301523885405 ]
# ZTest d 0 1
0
# TestG d
0.04093168145652
# Ranks d
[ 95.5 19.5 81 88 60.5 6.5 95.5 49.5 49.5 71 19.5 88 31.5 19.5 19.5 40 95.5 60.5 6.5 31.5 60.5 6.5 40 81 31.5 40 71 71 81 6.5 71 95.5 81 31.5 88 6.5 49.5 71 31.5 49.5 60.5 6.5 49. ... ]
# // Random number generators
# n ~ 1000
# a ~ [[n]]
# ?+ n {a'_ ~ UIRand 10}
# Freq a Unique a
[ 78 89 110 122 94 106 95 110 105 91 ]
# ?+ n {a'_ ~ UDRand 0 1}
# a
[ 0.73120053834282 0.93443385092542 0.60449900804088 0.95718105579726 0.85783026576973 0.26639210362919 0.54631106951274 0.99045055406168 0.75159501237795 0.17468912317418 0.95515 ... ]
# Hist a [10 0 1.0]
[ 89 110 96 90 116 104 93 104 83 115 ]
# Hist a 10
[ 89 110 96 88 116 104 94 102 83 118 ]
# ?+ n {a'_ ~ PRand 2}
# a
[ 1 0 0 1 0 0 2 2 2 4 1 1 3 4 1 6 0 2 4 2 2 2 4 2 1 3 2 1 1 0 0 2 2 1 1 6 1 1 0 2 1 4 1 1 5 2 2 4 1 3 2 3 4 7 4 2 4 1 1 0 2 1 2 3 4 1 3 4 3 5 1 1 2 1 1 1 2 2 1 3 1 3 0 1 4 0 2 3 0 ... ]
# (Unique a)->Freq a Unique a
0->140
1->257
2->254
3->193
4->101
5->31
6->14
7->7
8->3
# ?+ n {a'_ ~ BRand 10 0.2} // 10 trials, 20% probability of success
# a
[ 1 1 1 0 5 3 4 3 5 3 2 0 3 1 1 3 1 0 4 3 0 1 2 1 2 2 2 3 2 1 0 3 2 2 1 0 1 2 2 2 3 2 2 0 1 3 4 2 0 2 2 1 2 3 1 2 3 3 2 2 3 2 0 3 3 2 2 0 2 3 4 1 3 1 3 3 3 3 1 3 2 2 4 1 1 0 2 2 2 ... ]
# (Unique a)->Freq a Unique a
0->114
1->272
2->305
3->191
4->86
5->26
6->6
# fill () ~ UIRand 10
# a ~ [[10]]
# SeedRand
0
# first ~ FnFill a @fill
# second ~ FnFill a @fill
# FnFill a @fill
[ 9 6 8 9 2 1 6 6 7 2 ]
# SeedRand
0
# test ~ FnFill a @fill // back to the first
# test - first
[ 0 0 0 0 0 0 0 0 0 0 ]
# test - second
[ 3 -2 5 5 -6 3 -3 3 -3 -2 ]
# // Signal processing
# a~[1 2 3 4 5];b~[1 10 100 1000]
# Chop LinCorr a b $1 10
[ 4321 5432 ]
# Chop LinCorr a b $0 10
[ 1000 2100 3210 4321 5432 543 54 5 ]
# Chop CycCorr a b $0 10
[ 1543 2154 3215 4321 5432 ]
# Chop CycCorr a b $1 10
[ 4321 5432 ]
# Chop LinConv a b $0 10
[ 1 12 123 1234 2345 3450 4500 5000 ]
# Chop LinConv a b $1 10
[ 1234 2345 ]
# Chop CycConv a b $0 10
[ 3451 4512 5123 1234 2345 ]
# FFT a
[ 3 (-0.5+0.68819096023559i) (-0.5+0.16245984811645i) (-0.5-0.16245984811645i) (-0.5-0.68819096023559i) ]
# Chop IFFT FFT a 10
[ 1 2 3 4 5 ]
# // Numerical methods
# f ~ .
# f () x ~ Sin x
# Integrate @f 0 Pi 10
2
# Solve @f 0.1 4.1 10
3.1415926535898
# f $
0
# f2 () x ~ 3 * Sin x - x * x
# Solve @f2 0.1 4.1 100
1.7221251120536
# f2 $
9.0181195844252e-11
# f2d () x ~ 3 * Cos x - 2 * x
# Newton @f2 @f2d 1 10
1.7221251120767
# f2 $
-1.9095836023553e-14
# // Polynomials
# p ~ [1 3 5 3 4]
# Poly p
x^4+3x^3+5x^2+3x+4
# ]r ~ Roots p
[ -i i (-1.5+1.3228756555323i) (-1.5-1.3228756555323i) ]
# PEval p (r'1)
0
# // loop to check all
# ?+ #r {]"p(":r'_:")="\PEval p (r'_)}
p(-i)=0
p(i)=0
p((-1.5+1.3228756555323i))=0
p((-1.5-1.3228756555323i))=0
# // or write a recursive function
# pp () p r:rs ~ {]"p(":r:")="\PEval p r; pp p rs}{]"p(":r:")="\PEval p r}
# pp p Roots p
p(-i)=0
p(i)=0
p((-1.5+1.3228756555323i))=0
p((-1.5-1.3228756555323i))=0
# // be a bit fancier with the output
# complex () z ~ Re z != 0 & Im z != 0
# ppp1 () p r ~ ?: {complex r} {]"p":r:"="\PEval p r} {]"p(":r:")="\PEval p r}
# ppp2 () p r:rs ~ { ppp1 p r; ppp2 p rs } { ppp1 p r }
# ppp2 p Roots p
p(-i)=0
p(i)=0
p(-1.5+1.3228756555323i)=0
p(-1.5-1.3228756555323i)=0
# p2 ~ [1 -3 2]
# Poly p2
x^2-3x+2
# pm ~ PMul p p2; ]Poly pm
x^6-2x^4-6x^3+5x^2-6x+8
# Poly PDiv pm p2
[ x^4+3x^3+5x^2+3x+4 0 ]
# t () x ~ ?: {??x = "complex"}{"complex"}{"not complex"}
# t 5
not complex
# t (5 + 8 * I)
complex
# t Complex 5
complex
# // Strings
# s ~ "hello"
# s'3
$6C
# Str s'3
$6C
# Str (s'3)
l
# Str (:s)
h
# `s
ello
# // Stacks
# s~<>
# <s> 3 2 1
1
# s
< 1 2 3 >
# #s // depth
3
# <s> . // pop
1
# s
< 2 3 >
# <s> 1 + . // add top two entries and pop
3
# s
< 3 >
# s ~ <> // clear
# <s> 25 10 * 50 + .
300
# f () a b ~ a + 2 * b
# <s> 3 5
5
# <s> f // pull arguments and push result (need to check order...)
11
# ]<s> .
11
# <s> 3 5
5
# <s> : * \ : * + . // sum of squares
34
# answer ~ $
# answer - 3 * 3 - 5 * 5 // should be zero
0
# // Dictionaries
# a ~ [1:4]
# b ~ a^2
# d ~ a->b // associated
# d'4
16
# d'5 // NaN
NaN
# a ~ [1 2 3]
# b ~ ["one" "two" "three"]
# d ~ a->b
# d'2
two
# :d // head is the keys array
[ 1 2 3 ]
# `d // tail is the values array
[ one two three ]
# e ~ b->a
# e'"two"
2
# e'"two"~[1:10]
# e
three->3
two->[ 1 2 3 4 5 6 7 8 9 10 ]
one->1
# e'"two"~.
# e
three->3
one->1
# // Arbitrary precision integer arithmetic
# a~[2139481092348 12394 1928340918230491823 491230 1239410239841238764518723412341234]
# a
[ 2139481092348 12394 1928340918230491823 491230 1239410239841238764518723412341234 ]
# b~a^2
# b^0.5
[ 2139481092348 12394 1928340918230491648 491230 1.2394102398412e+33 ]
# b^(1/2)
[ 2139481092348 12394 1928340918230491823 491230 1239410239841238764518723412341234 ]
# Int Log $ + 1 // num of digits in each entry
[ 29 5 43 6 77 ]
# c~a^6
# b'5++
1536137742623316998084114067858476943770530051300485873657256642757
# b
[ 4577379344514591304153104 153611236 3718498696922016351163753508469863329 241306912900 1536137742623316998084114067858476943770530051300485873657256642757 ]
# b^(1/2) // last entry no longer a perfect square
[ 2139481092348 12394 1928340918230491823 491230 1.2394102398412e+33 ]
# c^(1/3)
[ 4577379344514591304153104 153611236 3718498696922016351163753508469863329 241306912900 1536137742623316998084114067858476943770530051300485873657256642756 ]
# Dec BiCo 300 20
7.5004337589221e+30
# Log BiCo 300 20
71.092513643214
# Log Dec BiCo 300 20
30.87508637988
# /q
Bye