ivy
Rob Pike
APL

Russ Cox
Rob Pike

demo 使

REPL
ivy
使REPL
quit
2+2 #
4 #
# #
# demo
2*3+4 # (2*3)+4 2*(3+4)
14
2**2+3 # (2**2) + 3 2**5
32
(2**2)+3 #
7
1/3
1/3
1/3 + 4/5
17/15
1.2
6/5
64bits
1e100
10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
3/1e10
3/10000000000
2**64
18446744073709551616
2**100
1267650600228229401496703205376
2**6400 #
1 j 3 # 1+3j
1j3
1j1 # 1+j 1j
1j1
0j-1 # -j
0j-1
# 00j0
0j0
0j0
0
0
#
sqrt -1 # sqrt
0j1
acos 2j1
0.507356303217j-1.46935174437
Unicode
# demo
'x'
x
'xx' # Vector
xx
char 0x61 # char is an operator: character with given value.
a
char 0x1f4a9
💩
code '💩' # char's inverse, the value of given character, here printed in decimal.
128169

1 2 3
1 2 3
1 4/3 5/3 (2+1/3)
1 4/3 5/3 7/3
# (1 4/3 5/3 2)+1/3
1 4/3 5/3 2+1/3
4/3 5/3 2 7/3
quotes
'h' 'e' 'l' 'l' 'o'
hello
#
'h' 'e' 'll' 'o'
h e (ll) o
# 'h' 'e' 'l' 'l' 'o'
'hello'
hello
使
NumpyA+c=A+cEA+c=A+c⋅E
matrix vector
1 2 3 + 4 5 6
5 7 9
# 使
23 + 1 2 3
24 25 26
#
1 2 3 + 23
24 25 26
1 << 1 2 3 4 5
2 4 8 16 32
(1 << 1 2 3 4 5) == (2 ** 1 2 3 4 5)
1 1 1 1 1 # true 1

==
true 1false0
5==5
1
5==6
0
iota
1
iota 10
1 2 3 4 5 6 7 8 9 10
2 ** iota 5
2 4 8 16 32
take, drop
Haskell
3 take iota 10
1 2 3
3 drop iota 10
4 5 6 7 8 9 10
6 drop 'hello world'
world
-3 take iota 10
8 9 10
-3 drop iota 10
1 2 3 4 5 6 7

Reduction
使
iota 15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# sum
(+/iota 15) == (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15)
1
# factorical
1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10
3628800
*/iota 10
3628800


max, min
max , min
3 max 7; 'is max and'; 3 min 7; 'is min'
7
is max and
3
is min
使
2 3 4 max 4 3 2
4 3 4
max/2 34 42 233 2 2 521 14 1 4 1 55 133
521

rho
<> rho <>
5 rho 1
1 1 1 1 1
5 5 rho 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
5 5 rho 25
25 25 25 25 25
25 25 25 25 25
25 25 25 25 25
25 25 25 25 25
25 25 25 25 25
#
5 5 rho iota 25
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
#
3 3 rho iota 4
1 2 3
4 1 2
3 4 1
使
x = 3 5 rho iota 15; x
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
rho x
3 5
rabel
join
1 2 3 , 4 5 6
1 2 3 4 5 6
rho 1 2 3 , 4 5 6
6
flatten
, 3 5 rho iota 15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
rho , 3 5 rho iota 15
15
x = 3 5 rho iota 15;x
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
x ** 2
1 4 9 16 25
36 49 64 81 100
121 144 169 196 225
<1>.<2>
+.*
使
1 2 3 4 +.* 2 3 4 5
40
#
(1 2 3) +.== (1 3 3)
2
#
(1 2 3) +.!= (1 3 3)
1
o.<>
(iota 3) o.* -2
-2 # 1 * -2
-4 # 2 * -2
-6 # 3 * -2
(iota 5) o.* -1 + iota 5 # (1 2 3 4 5) o.* (0 1 2 3 4)
0 1 2 3 4 # 1 * (0 1 2 3 4)
0 2 4 6 8 # 2 * (0 1 2 3 4)
0 3 6 9 12 # 3 * (0 1 2 3 4)
0 4 8 12 16 # 4 * (0 1 2 3 4)
0 5 10 15 20 # 5 * (0 1 2 3 4)
x = iota 5; x o.== x
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
使
x = iota 5; x o.== x
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
5 5 rho 1 0 0 0 0 0
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
?
?n 1 < x <= n x
?100
6
?100
53
?100
28
?20 rho 6 # 20 rolls of a 6-sided die.
4 1 1 1 4 1 3 4 4 4 2 5 3 5 2 5 5 3 5 4
?(20 rho 6) #
5 6 3 5 5 4 2 5 4 2 2 5 3 3 1 5 1 3 6 5
#
20 rho ?6
6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6
20 rho (?6)
4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
x = ?20 rho 6
x
6 5 1 2 5 1 5 1 5 4 4 2 3 4 3 1 3 5 1 4
x[1] #
6
x[1 19 3] #
6 1 1
#
(5 5 rho iota 25)[rot iota 5; iota 5] # rot reverse
21 22 23 24 25
16 17 18 19 20
11 12 13 14 15
6 7 8 9 10
1 2 3 4 5
sort
up , down
up x
3 6 8 16 19 4 12 13 15 17 10 11 14 20 2 5 7 9 18 1
x[up x]
1 1 1 1 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6
x[down x]
6 5 5 5 5 5 4 4 4 4 3 3 3 2 2 1 1 1 1 1
'hello world'[up 'hello world']
dehllloorw
'hello world'[down 'hello world']
wroolllhed
_
command
)help
)base
)base #
ibase 10
obase 10
)base 16 # 16
)base
ibase 16
obase 16
)obase 10 # obase ibase
)base 16
1<<iota 10 # 16 powers of two, base 16.
2 4 8 10 20 40 80 100 200 400 800 1000 2000 4000 8000 10000
(2**40)-1 # The largest 64-bit number base 16.
ffffffffffffffff
)format '%.12g'
ivy -g 使
256 bits
pi, e
non-integral
2**1/2 # Note: Non-integral exponent generates irrational result.
1.41421356237
e**1e6
3.0332153968e+434294
logarithms
log e**1e6
1000000
log e**1e8
100000000
log 1e1000000 # Yes, that is 10 to the millionth power.
2302585.09299
sin pi/2
1
cos .25*pi * -1 + iota 9
1 0.707106781187 7.70893799599e-78 -0.707106781187 -1 -0.707106781187 2.38604460223e-76 0.707106781187 1
log iota 6
0 0.69314718056 1.09861228867 1.38629436112 1.60943791243 1.79175946923
# Successive approximations to e. (We force the calculation to use float using the "float" unary operator. Why?)
(float 1+10**-iota 9) ** 10**iota 9
2.5937424601 2.70481382942 2.71692393224 2.71814592683 2.71826823717 2.71828046932 2.71828169254 2.71828181487 2.7182818271
# Default precision is 256 bits of mantissa. We can go up to 10000.
)prec 3350 # Units are bits, not digits. 2 log 10 == 3.321. Add a few more bits for floating point errors.
e
2.71828182846
#
op avg x = (+/x)/rho x
avg iota 100
50.5
# xn
op n largest x = n take x[down x]
3 largest ? 100 rho 1000
999 994 982
# Population count. Use encode to turn the value into a string of bits. Use log to decide how many.
# 1
op a base b = ((floor 1 + b log a) rho b) encode a
7 base 2
1 1 1
op popcount n = +/n base 2
popcount 7
3
popcount 1e6
7
popcount 1e100
105
# another version: op popcount n = +/'1' == '%b' text n
#
# Here is one to sum the digits. The unary operator text turns its argument into text, like sprintf.
op sumdigits x = t = text x; +/(code (t in '0123456789') sel t) - code '0'
# Break it down: The sel operator selects from the right based on the non-zero elements in the left.
# The in operator generates a selector by choosing only the bytes that are ASCII digits.
sumdigits 99
18
sumdigits iota 10
46
text
'%x' text 1234
4d2
# A classic (expensive!) algorithm to count primes.
op primes N = (not T in T o.* T) sel T = 1 drop iota N
# The assignment to T gives 2..N. We use outer product to build an array of all products.
# Then we find all elements of T that appear in the product matrix, invert that, and select from the original.
primes 100
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
# A final trick.
# The binary ? operator "deals": x?y selects at random x distinct integers from 1..y inclusive.
5?10
8 5 6 10 2
# We can use this to shuffle a deck of cards. The suits are , the values
# A234567890JQK (using 0 for 10, for simplicity).
# Create the deck using outer product with the ravel operator:
"A234567890JQK" o., ""
(A) (A) (A) (A)
(2) (2) (2) (2)
(3) (3) (3) (3)
(4) (4) (4) (4)
(5) (5) (5) (5)
(6) (6) (6) (6)
(7) (7) (7) (7)
(8) (8) (8) (8)
(9) (9) (9) (9)
(0) (0) (0) (0)
(J) (J) (J) (J)
(Q) (Q) (Q) (Q)
(K) (K) (K) (K)
# To shuffle it, ravel into into a vector and index that by 1 through 52, shuffled.
(, "A234567890JQK" o., "")[52?52]
(5) (2) (A) (4) (0) (0) (0) (2) (J) (9) (2) (9) (6) (6) (Q) (8) (Q) (4) (9) (4) (3) (8) (5) (3) (J) (2) (7) (6) (7) (A) (9) (Q) (K) (A) (3) (6) (5) (K) (K) (J) (7) (3) (Q) (7) (5) (4) (0) (J) (8) (8) (A) (K)
# There is no looping construct in ivy, but there is a conditional evaluator.
# Within a user-defined operator, one can write a condition expression
# using a binary operator, ":". If the left-hand operand is true (integer non-zero),
# the user-defined operator will return the right-hand operand as its
# result; otherwise execution continues.
op a gcd b = a == b: a; a > b: b gcd a-b; a gcd b-a
1562 gcd !11
22

prelude
help
使
arccos?
> That printed using floating-point notation for manageability but it is still an integer inside.
acosacos
ivy#32f43d