-
Notifications
You must be signed in to change notification settings - Fork 88
/
ch11-maths-repl-interactions.clj
696 lines (521 loc) · 13 KB
/
ch11-maths-repl-interactions.clj
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
;-----
(class (inc (Integer. 5)))
;= java.lang.Long
;-----
(dec 1)
;= 0
(dec 1.0)
;= 0.0
(dec 1N)
;= 0N
(dec 1M)
;= 0M
(dec 5/4)
;= 1/4
;-----
(* 3 0.08 1/4 6N 1.2M)
;= 0.432
(< 1 1.6 7/3 9N 14e9000M)
;= true
;-----
(+ 0.1 0.1 0.1)
;= 0.30000000000000004
;-----
(+ 1/10 1/10 1/10)
;= 3/10
;-----
(+ 7/10 1/10 1/10 1/10)
;= 1
;-----
(double 1/3)
;= 0.3333333333333333
;-----
(rationalize 0.45)
;= 9/20
;-----
(+ 1 1)
;= 2
(+ 1 1.5)
;= 2.5
(+ 1 1N)
;= 2N
(+ 1.1M 1N)
;= 2.1M
;-----
(defn squares-sum
[& vals]
(reduce + (map * vals vals)))
;= #'user/squares-sum
(squares-sum 1 4 10)
;= 117
;-----
(squares-sum 1 4 10 20.5)
;= 537.25
(squares-sum 1 4 10 9N)
;= 198N
(squares-sum 1 4 10 9N 5.6M)
;= 229.36M
(squares-sum 1 4 10 25/2)
;= 1093/4
;-----
(.hashCode (BigInteger. "6948736584"))
;= -1641197977
(.hashCode (Long. 6948736584))
;= -1641198007
;-----
(def k Long/MAX_VALUE)
;= #'user/k
k
;= 9223372036854775807
;-----
(inc k)
;= ArithmeticException integer overflow
;-----
(inc (bigint k))
;= 9223372036854775808N
(* 100 (bigdec Double/MAX_VALUE))
;= 1.797693134862315700E+310M
;-----
(dec 10223372636454715900N)
;= 10223372636454715899N
(* 0.5M 1e403M)
;= 5E+402M
;-----
10223372636454715900
;= 10223372636454715900N
(* 2 10223372636454715900)
;= 20446745272909431800N
;-----
(inc' k)
;= 9223372036854775808N
;-----
(inc' 1)
;= 2
(inc' (dec' Long/MAX_VALUE))
;= 9223372036854775807
;-----
System.out.println(Long.MAX_VALUE);
System.out.println(Long.MAX_VALUE + 1);
;-----
9223372036854775807
-9223372036854775808
;-----
Long/MIN_VALUE
;= -9223372036854775808
(dec Long/MIN_VALUE)
;= #<ArithmeticException java.lang.ArithmeticException: integer overflow>
;-----
(unchecked-dec Long/MIN_VALUE)
;= 9223372036854775807
(unchecked-multiply 92233720368547758 1000)
;= -80
;-----
(inc Long/MAX_VALUE)
;= #<ArithmeticException java.lang.ArithmeticException: integer overflow>
(set! *unchecked-math* true)
;= true
(inc Long/MAX_VALUE)
;= -9223372036854775808
(set! *unchecked-math* false)
;= false
;-----
(binding [*unchecked-math* true]
(inc Long/MAX_VALUE))
;= #<ArithmeticException java.lang.ArithmeticException: integer overflow>
;-----
new BigDecimal(1).divide(new BigDecimal(3));
= java.lang.ArithmeticException:
= Non-terminating decimal expansion; no exact representable decimal result.
;-----
new BigDecimal(1).divide(new BigDecimal(3), new MathContext(10, RoundingMode.HALF_UP));
= 0.3333333333
;-----
(/ 22M 7)
;= #<ArithmeticException java.lang.ArithmeticException:
;= Non-terminating decimal expansion; no exact representable decimal result.>
(with-precision 10 (/ 22M 7))
;= 3.142857143M
(with-precision 10 :rounding FLOOR
(/ 22M 7))
;= 3.142857142M
;-----
(set! *math-context* (java.math.MathContext. 10 java.math.RoundingMode/FLOOR))
;= #<MathContext precision=10 roundingMode=FLOOR>
(/ 22M 7)
;= 3.142857142M
;-----
(identical? "foot" (str "fo" "ot"))
;= false
(let [a (range 10)]
(identical? a a))
;= true
;-----
(identical? 5/4 (+ 3/4 1/2))
;= false
(identical? 5.4321 5.4321)
;= false
(identical? 2600 2600)
;= false
;-----
(identical? 127 (dec 128))
;= true
(identical? 128 (dec 129))
;= false
;-----
(= {:a 1 :b ["hi"]}
(into (sorted-map) [[:b ["hi"]] [:a 1]])
(doto (java.util.HashMap.)
(.put :a 1)
(.put :b ["hi"])))
;= true
;-----
(= 1 1N (Integer. 1) (Short. (short 1)) (Byte. (byte 1)))
;= true
(= 1.25 (Float. 1.25))
;= true
;-----
(= 1 1.0)
;= false
(= 1N 1M)
;= false
(= 1.25 5/4)
;= false
;-----
(== 0.125 0.125M 1/8)
;= true
(== 4 4N 4.0 4.0M)
;= true
;-----
(defn equiv?
"Same as `==`, but doesn't throw an exception if any arguments are not numbers."
[& args]
(and (every? number? args)
(apply == args)))
;= #'user/equiv?
(equiv? "foo" 1)
;= false
(equiv? 4 4N 4.0 4.0M)
;= true
(equiv? 0.125 0.125M 1/8)
;= true
;-----
java.util.Map m = new java.util.HashMap();
m.put(1, "integer");
m.put(1L, "long");
m.put(java.math.BigInteger.valueOf(1), "bigint");
System.out.println(m);
>> {1=bigint, 1=long, 1=integer}
;-----
(into #{} [1 1N (Integer. 1) (Short. (short 1))])
;= #{1}
(into {}
[[1 :long]
[1N :bigint]
[(Integer. 1) :integer]])
;= {1 :integer}
;-----
(+ 0.1 0.2)
;= 0.30000000000000004
;-----
(== 1.1 (float 1.1))
;= false
;-----
(double (float 1.1))
;= 1.100000023841858
;-----
1.1f == 1.1d
;-----
(defn foo [a] 0)
;= #'user/foo
(seq (.getDeclaredMethods (class foo)))
;= (#<Method public java.lang.Object user$foo.invoke(java.lang.Object)>)
;-----
(defn foo [^Double a] 0)
;= #'user/foo
(seq (.getDeclaredMethods (class foo)))
;= (#<Method public java.lang.Object user$foo.invoke(java.lang.Object)>)
;-----
(defn round ^long [^double a] (Math/round a))
;= #'user/round
(seq (.getDeclaredMethods (round foo)))
;= (#<Method public java.lang.Object user$round.invoke(java.lang.Object)>
;= #<Method public final long user$round.invokePrim(double)>)
;-----
(round "string")
;= #<ClassCastException java.lang.ClassCastException:
;= java.lang.String cannot be cast to java.lang.Number>
;-----
(defn idem ^long [^long x] x)
;= #'user/long
(idem 18/5)
;= 3
(idem 3.14M)
;= 3
(idem 1e15)
;= 1000000000000000
(idem 1e150)
;= #<IllegalArgumentException java.lang.IllegalArgumentException:
;= Value out of range for long: 1.0E150>
;-----
(map round [4.5 6.9 8.2])
;= (5 7 8)
(apply round [4.2])
;= 4
;-----
(defn foo ^long [a b c d e] 0)
;= #<CompilerException java.lang.IllegalArgumentException:
;= fns taking primitives support only 4 or fewer args, compiling:(NO_SOURCE_PATH:1)>
;-----
(defn foo ^long [^int a] 0)
;= #<CompilerException java.lang.IllegalArgumentException:
;= Only long and double primitives are supported>
(defn foo ^long [^double a] a)
;= #<CompilerException java.lang.IllegalArgumentException:
;= Mismatched primitive return, expected: long, had: double>
;-----
(set! *warn-on-reflection* true)
;= true
(loop [x 5]
(when-not (zero? x)
(recur (dec x))))
;= nil
;-----
(loop [x 5]
(when-not (zero? x)
(recur (dec' x))))
; NO_SOURCE_FILE:2 recur arg for primitive local:
; x is not matching primitive, had: Object, needed: long
; Auto-boxing loop arg: x
;= nil
;-----
(loop [x 5]
(when-not (zero? x)
(recur 0.0)))
; NO_SOURCE_FILE:2 recur arg for primitive local:
; x is not matching primitive, had: double, needed: long
; Auto-boxing loop arg: x
;= nil
;-----
(defn dfoo ^double [^double a] a)
;= #'user/dfoo
(loop [x 5]
(when-not (zero? x)
(recur (dfoo (dec x)))))
; NO_SOURCE_FILE:2 recur arg for primitive local:
; x is not matching primitive, had: double, needed: long
; Auto-boxing loop arg: x
;= nil
;-----
(loop [x 5]
(when-not (zero? x)
(recur (long (dfoo (dec x))))))
;= nil
;-----
(defn round [v]
(Math/round v))
; Reflection warning, NO_SOURCE_PATH:2 - call to round can't be resolved.
;= #'user/round
(defn round [v]
(Math/round (double v)))
;= #'user/round
;-----
(class (int 5))
;= java.lang.Long
;-----
(defn vector-histogram
[data]
(reduce (fn [hist v]
(update-in hist [v] inc))
(vec (repeat 10 0))
data))
;-----
(def data (doall (repeatedly 1e6 #(rand-int 10))))
;= #'user/data
(time (vector-histogram data))
; "Elapsed time: 505.409 msecs"
;= [100383 100099 99120 100694 100003 99940 100247 99731 99681 100102]
;-----
(defn array-histogram
[data]
(vec
(reduce (fn [^longs hist v]
(aset hist v (inc (aget hist v)))
hist)
(long-array 10)
data)))
;-----
(time (array-histogram data))
; "Elapsed time: 25.925 msecs"
;= [100383 100099 99120 100694 100003 99940 100247 99731 99681 100102]
;-----
(into-array ["a" "b" "c"])
;= #<String[] [Ljava.lang.String;@4413515e>
(into-array CharSequence ["a" "b" "c"])
;= #<CharSequence[] [Ljava.lang.CharSequence;@5acad437>
;-----
(into-array Long/TYPE (range 5))
;= #<long[] [J@21e3cc77>
;-----
(long-array 10)
;= #<long[] [J@12ee6d57>
(long-array (range 10))
;= #<long[] [J@676982f8>
;-----
(seq (long-array 20 (range 10)))
;= (0 1 2 3 4 5 6 7 8 9 0 0 0 0 0 0 0 0 0 0)
;-----
(def arr (make-array String 5 5))
;= #'user/arr
(aget arr 0 0)
;= nil
(def arr (make-array Boolean/TYPE 10))
;= #'user/arr
(aget arr 0)
;= false
;-----
(class (make-array Character/TYPE 0 0 0))
;= [[[C
;-----
(Class/forName "[[Z")
;= [[Z
(.getComponentType *1)
;= [Z
(.getComponentType *1)
;= boolean
;-----
^objects
^booleans
^bytes
^chars
^longs
^ints
^shorts
^doubles
^floats
;-----
(let [arr (long-array 10)]
(aset arr 0 50)
(aget arr 0))
;= 50
;-----
(let [a (int-array (range 10))]
(amap a i res
(inc (aget a i))))
;= #<int[] [I@eaf261a>
(seq *1)
;= (1 2 3 4 5 6 7 8 9 10)
;-----
(let [a (int-array (range 10))]
(areduce a i sum 0
(+ sum (aget a i))))
;= 45
;-----
(def arr (make-array Double/TYPE 1000 1000))
;= #'user/arr
(time (dotimes [i 1000]
(dotimes [j 1000]
(aset arr i j 1.0)
(aget arr i j))))
; "Elapsed time: 50802.798 msecs"
;-----
(time (dotimes [i 1000]
(dotimes [j 1000]
(let [^doubles darr (aget ^objects arr i)]
(aset darr j 1.0)
(aget darr j)))))
; "Elapsed time: 21.543 msecs"
;= nil
;-----
(defmacro deep-aget
"Gets a value from a multidimensional array as if via `aget`,
but with automatic application of appropriate type hints to
each step in the array traversal as guided by the hint added
to the source array.
e.g. (deep-aget ^doubles arr i j)"
([array idx]
`(aget ~array ~idx))
([array idx & idxs]
(let [a-sym (gensym "a")]
`(let [~a-sym (aget ~(vary-meta array assoc :tag 'objects) ~idx)]
(deep-aget ~(with-meta a-sym {:tag (-> array meta :tag)}) ~@idxs)))))<3>
;-----
(defmacro deep-aset
"Sets a value in a multidimensional array as if via `aset`,
but with automatic application of appropriate type hints to
each step in the array traversal as guided by the hint added
to the target array.
e.g. (deep-aset ^doubles arr i j 1.0)"
[array & idxsv]
(let [hints '{booleans boolean, bytes byte
chars char, longs long
ints int, shorts short
doubles double, floats float}
hint (-> array meta :tag)
[v idx & sxdi] (reverse idxsv)
idxs (reverse sxdi)
v (if-let [h (hints hint)] (list h v) v)
nested-array (if (seq idxs)
`(deep-aget ~(vary-meta array assoc :tag 'objects) ~@idxs)
array)
a-sym (gensym "a")]
`(let [~a-sym ~nested-array]
(aset ~(with-meta a-sym {:tag hint}) ~idx ~v))))
;-----
(time (dotimes [i 1000]
(dotimes [j 1000]
(deep-aset ^doubles arr i j 1.0)
(deep-aget ^doubles arr i j))))
; "Elapsed time: 25.033 msecs"
;-----
(render-text (mandlebrot -2.25 0.75 -1.5 1.5 :width 80 :height 40 :depth 100))
***
******
****
** *************
***********************
***********************
**************************
****************************
******* ******************************
*********** ******************************
************* *****************************
************************************************************
************* *****************************
*********** ******************************
******* ******************************
****************************
**************************
***********************
***********************
** *************
****
******
***
;-----
(do (time (mandlebrot -2.25 0.75 -1.5 1.5
:width 1600 :height 1200 :depth 1000))
nil)
; "Elapsed time: 82714.764 msecs"
;-----
(defn- escape
[^double a0 ^double b0 depth]
(loop [a a0
b b0
iteration 0]
(cond
(< 4 (+ (* a a) (* b b))) iteration
(>= iteration depth) -1
:else (recur (+ a0 (- (* a a) (* b b)))
(+ b0 (* 2 (* a b)))
(inc iteration)))))
;-----
(do (time (mandlebrot -2.25 0.75 -1.5 1.5
:width 1600 :height 1200 :depth 1000))
nil)
; "Elapsed time: 8663.841 msecs"
;-----
(render-image (mandlebrot -2.25 0.75 -1.5 1.5 :width 800 :height 800 :depth 500))
;-----
(render-image (mandlebrot -1.5 -1.3 -0.1 0.1 :width 800 :height 800 :depth 500))
;-----
(javax.imageio.ImageIO/write *1 "png" (java.io.File. "mandlebrot.png"))