Debian Codegraph @

example/ivy/android/app/src/main/assets/demo.ivy

raw
1 # This is a demo of ivy. Type a newline to advance to each new step. Type one now.
2 # At any time, type the word "quit" or EOF to end the demo and return to ivy.
3 # Each step in the demo is one line of input followed by some output from ivy. Type a newline now to see.
4 2+2
5 # The first line you see above (2+2) is input; the next (4) is output from a running ivy.
6 # Comments start with # and produce no output.
7 # Whenever you like, you can type an expression yourself. Try typing 2*3 now, followed by two newlines:
8 # Keep typing newlines; the ivy demo is about to start.
9 # Arithmetic has the obvious operations: + - * etc. ** is exponentiation. mod is modulo.
10 23
11 23 + 45
12 23 * 45
13 23 - 45
14 7 ** 3
15 7 mod 3
16 # Operator precedence is unusual.
17 # Unary operators operate on everything to the right.
18 # Binary operators operate on the item immediately to the left, and everything to the right.
19 2*3+4 # Parsed as 2*(3+4), not the usual (2*3)+4.
20 2**2+3 # 2**5, not (2**2) + 3
21 (2**2)+3 # Use parentheses if you need to group differently.
22 # Ivy can do rational arithmetic, so 1/3 is really 1/3, not 0.333....
23 1/3
24 1/3 + 4/5
25 1/3 ** 2 # We'll see non-integral exponents later.
26 # Even when a number is input in floating notation, it is still an exact rational number inside.
27 1.2
28 # In fact, ivy is a "bignum" calculator that can handle huge numbers and rationals made of huge numbers.
29 1e10 # Still an integer.
30 1e100 # Still an integer.
31 1e10/3 # Not an integer, but an exact rational.
32 3/1e10 # Not an integer, but an exact rational.
33 2**64 # They can get big.
34 2**640 # They can get really big.
35 # They can get really really big. Type a newline to see 2**6400 scroll by.
36 2**6400
37 # Ivy also has characters, which represent a Unicode code point.
38 'x'
39 char 0x61 # char is an operator: character with given value.
40 char 0x1f4a9
41 code '💩' # char's inverse, the value of given character, here printed in decimal.
42 # Everything in ivy can be placed into a vector.
43 # Vectors are written and displayed with spaces between the elements.
44 1 2 3
45 1 4/3 5/3 (2+1/3)
46 # Note that without the parens this becomes (1 4/3 5/3 2)+1/3
47 1 4/3 5/3 2+1/3
48 # Vectors of characters print without quotes or spaces.
49 'h' 'e' 'l' 'l' 'o'
50 # This is a nicer way to write 'h' 'e' 'l' 'l' 'o'. It means the same.
51 'hello'
52 # Arithmetic works elementwise on vectors.
53 1 2 3 + 4 5 6
54 # Arithmetic between scalar and vector also works, either way.
55 23 + 1 2 3
56 1 2 3 + 23 # Note the grouping: vector is a single value.
57 # More fun with scalar and vector.
58 1 << 1 2 3 4 5
59 (1 << 1 2 3 4 5) == (2 ** 1 2 3 4 5) # Note: true is 1, false is 0.
60 # iota is an "index generator": It counts from 1.
61 iota 10
62 2 ** iota 5
63 (1 << iota 100) == 2 ** iota 100
64 2 ** -1 + iota 32 # Again, see how the precedence rules work.
65 # The take operator removes n items from the beginning of the vector.
66 3 take iota 10
67 -3 take iota 10 # Negative n takes from the end.
68 # Drop is the other half: it drops n from the vector.
69 3 drop iota 10
70 -3 drop iota 10 # Negative n drops from the end.
71 6 drop 'hello world'
72 # Reduction
73 iota 15
74 # Add them up:
75 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15
76 # Automate this by reducing + over the vector, like this:
77 +/iota 15
78 # We can reduce using any binary operator. This is factorial:
79 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10
80 */iota 10
81 */iota 100
82 # Type this: */iota 10000
83 # That printed using floating-point notation for manageability but it is still an integer inside.
84 # max and min are binary operators that do the obvious. (Use semicolons to separate expressions.)
85 3 max 7; 'is max and'; 3 min 7; 'is min'
86 # Like all binary arithmetic operators, max applies elementwise.
87 2 3 4 max 4 3 2
88 # Reduce using max to find maximum element in vector.
89 max/2 34 42 233 2 2 521 14 1 4 1 55 133
90 # Ivy allows multidimensional arrays. The binary shape operator, rho, builds them.
91 # Dimension (which may be a vector) on the left, data on the right.
92 5 rho 1
93 5 5 rho 1
94 5 5 rho 25
95 5 5 rho iota 25
96 3 5 5 rho iota 125
97 # Unary rho tells us the shape of an item.
98 x = 3 5 rho iota 15; x
99 rho x
100 x = 3 5 5 rho iota 75; x
101 rho x
102 # Arithmetic on matrices works as you would expect by now.
103 x/2
104 x**2
105 x**3
106 x**10
107 # Inner product is written with a . between the operators.
108 # This gives dot product: multiply corresponding elements and add the result.
109 1 2 3 4 +.* 2 3 4 5
110 # Any operator works. How many items are the same?
111 (1 2 3) +.== (1 3 3)
112 # How many differ?
113 (1 2 3) +.!= (1 3 3)
114 # Outer product generates a matrix of all combinations applying the binary operator.
115 (iota 5) o.* -1 + iota 5
116 # That's a letter 'o', dot, star.
117 # Any operator works; here is how to make an identity matrix.
118 x = iota 5; x o.== x
119 # Assignment is an operator, so you can save an intermediate expression.
120 x o.== x = iota 5
121 # Random numbers: Use a unary ? to roll an n-sided die from 1 to n.
122 ?100
123 ?100
124 ?20 rho 6 # 20 rolls of a 6-sided die.
125 x = ?20 rho 6 # Remember one set of rolls.
126 x
127 # Indexing is easy.
128 x[1]
129 x[1 19 3] # You can index with a vector.
130 # The up and down operators generate index vectors that would sort the input.
131 up x
132 x[up x]
133 x[down x]
134 'hello world'[up 'hello world']
135 'hello world'[down 'hello world']
136 # More rolls of a die.
137 ?10 rho 6
138 # Remember a set of rolls.
139 x = ?10 rho 6; x
140 # The outer product of == and the integers puts 1 in each row where that value appeared.
141 # Compare the last row of the next result to the 6s in x.
142 (iota 6) o.== x
143 # Count the number of times each value appears by reducing the matrix horizontally.
144 +/(iota 6) o.== x
145 # Do it for a much larger set of rolls: is the die fair?
146 +/(iota 6) o.== ?60000 rho 6
147 # Remember that ivy is a big number calculator.
148 */iota 100
149 2**64
150 2**iota 64
151 -1+2**63
152 # Settings are made and queried with a leading right paren. )help helps with settings and other commands.
153 )help
154 # Use )base to switch input and output to base 16.
155 )base 16
156 )base # The input and output for settings is always base 10.
157 # _ is a variable that holds the most recently evaluated expression. It remembers our 63-bit number.
158 _
159 1<<iota 10 # 16 powers of two, base 16.
160 (2**40)-1 # The largest 64-bit number base 16.
161 )obase 10 # Output base 10, input base still 16.
162 )base
163 # The largest 63-bit number base 10.
164 -1+2**40 # The largest 64-bit number base 10.
165 -1+2**3F # The largest 63-bit number base 10.
166 # Go back to base 10 input and output.
167 )base 10
168 # Rationals can be very big too.
169 (2**1e3)/(3**1e2)
170 # Such output can be unwieldy. Change the output format using a Printf string.
171 )format '%.12g'
172 _
173 # We need more precision.
174 )format "%.100g" # Double quotes work too; there's no difference.
175 _
176 )format '%#x'
177 _
178 )format '%.12g' # A nice format, easily available by running ivy -g.
179 _
180 (3 4 rho iota 12)/4
181 # Irrational functions cannot be represented precisely by rational numbers.
182 # Ivy stores irrational results in high-precision (default 256-bit) floating point numbers.
183 sqrt 2
184 # pi and e are built-in, high-precision constants.
185 pi
186 e
187 )format "%.100g"
188 pi
189 )format '%.12g'
190 pi
191 # Exponentials and logarithms.
192 2**1/2 # Note: Non-integral exponent generates irrational result.
193 e**1e6
194 log e**1e6
195 log e**1e8
196 log 1e1000000 # Yes, that is 10 to the millionth power.
197 # Transcendentals. (The low bit isn't always right...)
198 sin pi/2
199 cos .25*pi * -1 + iota 9
200 log iota 6
201 # Successive approximations to e. (We force the calculation to use float using the "float" unary operator. Why?)
202 (float 1+10**-iota 9) ** 10**iota 9
203 # Default precision is 256 bits of mantissa. We can go up to 10000.
204 )prec 3350 # Units are bits, not digits. 2 log 10 == 3.321. Add a few more bits for floating point errors.
205 e
206 )format '%.1000g' # Units are digits. (Sorry for the inconsistency.)
207 e
208 pi
209 sqrt 2
210 e**1e6
211 log e**1e6
212 (2**1e3)/(3**1e2)
213 # User-defined operators are declared as unary or binary (or both). This one computes the (unary) average.
214 op avg x = (+/x)/rho x
215 avg iota 100
216 # Here is a binary operator.
217 op n largest x = n take x[down x]
218 3 largest ? 100 rho 1000
219 4 largest 'hello world'
220 # Population count. Use encode to turn the value into a string of bits. Use log to decide how many.
221 op a base b = ((ceil b log a) rho b) encode a
222 7 base 2
223 op popcount n = +/n base 2
224 popcount 7
225 popcount 1e6
226 popcount 1e100
227 # Here is one to sum the digits. The unary operator text turns its argument into text, like sprintf.
228 op sumdigits x = t = text x; +/(code (t in '0123456789') sel t) - code '0'
229 # Break it down: The sel operator selects from the right based on the non-zero elements in the left.
230 # The in operator generates a selector by choosing only the bytes that are ASCII digits.
231 sumdigits 99
232 sumdigits iota 10
233 sumdigits '23 skidoo' # Note: It counts only the digits.
234 # The binary text operator takes a format string (% optional) on the left and formats the value.
235 '%x' text 1234
236 # We can use this for another version of popcount: %b is binary.
237 op popcount n = +/'1' == '%b' text n
238 popcount 7
239 popcount 1e6
240 popcount 1e100
241 # A classic (expensive!) algorithm to count primes.
242 op primes N = (not T in T o.* T) sel T = 1 drop iota N
243 # The assignment to T gives 2..N. We use outer product to build an array of all products.
244 # Then we find all elements of T that appear in the product matrix, invert that, and select from the original.
245 primes 100
246 # A final trick.
247 # The binary ? operator "deals": x?y selects at random x distinct integers from 1..y inclusive.
248 5?10
249 # We can use this to shuffle a deck of cards. The suits are ♠♡♣♢, the values
250 # A234567890JQK (using 0 for 10, for simplicity).
251 # Create the deck using outer product with the ravel operator:
252 "A234567890JQK" o., "♠♡♣♢"
253 # To shuffle it, ravel into into a vector and index that by 1 through 52, shuffled.
254 (, "A234567890JQK" o., "♠♡♣♢")[52?52]
255 # There is no looping construct in ivy, but there is a conditional evaluator.
256 # Within a user-defined operator, one can write a condition expression
257 # using a binary operator, ":". If the left-hand operand is true (integer non-zero),
258 # the user-defined operator will return the right-hand operand as its
259 # result; otherwise execution continues.
260 op a gcd b = a == b: a; a > b: b gcd a-b; a gcd b-a
261 1562 gcd !11
262 # That's it! Have fun.
263 # For more information visit https://pkg.go.dev/robpike.io/ivy