Multiplying large blocks of data?
what would be a good way of multiplying a block of numbers 1  1,000,000 by 2? test: [1 2 3...1000000] test2: [] foreach num test [append test2 num * 2] that seems to do the trick, but I'm sure there must be another way.
posted by: Andrew 17Jun2017/0:19:387:00
You can do this: t: [1 2 3 4 5 6] repeat x length? t [t/(x): t/(x) * 2]
posted by: Nick 17Jun2017/1:17:467:00
and BTW, to test it with an actually large block: t: copy [] insert/dup t 1 1000000 repeat x length? t [t/(x): t/(x) * 2]
posted by: Nick 17Jun2017/1:21:097:00
and also BTW, to create a list of numbers 110million, the first option below is much faster: t: copy [] repeat i 10000000 [insert tail t i] t: copy [] repeat i 10000000 [append t i]
posted by: Nick 17Jun2017/1:27:217:00
Why not use `mapeach`, which is designed for it: t: [1 2 3 4 5 6] mapeach i t [i * 2] Well, the times are a bit different: 0:00:11 mapeach 0:00:06 foreach Andrew 0:00:04 repeat Nick
posted by: Geekyi 18Jun2017/8:43:487:00
Yes, and for 10 million+ values, for example, the delay makes a dramatic difference (seconds rather than minutes).
posted by: Nick 18Jun2017/9:55:567:00
This worked a bit faster on my PC t: copy [] insert/dup t 1 1000000 i: 1 until [t/:i: t/:i * 2 tail? t: next t]
posted by: Endo 19Jun2017/5:11:047:00
Interesting, running on a little Windows 8 tablet with Z3735G 1.33 GHz CPU and 1 GB RAM (w/ Rebol 2.7.8.3.1), my results are the opposite in every case: >> t: copy [] repeat i 10000000 [insert tail t i] == [] >> deltatime [i: 1 until [t/:i: t/:i * 2 tail? t: next t]] == 0:00:21.236 >> t: copy [] repeat i 10000000 [insert tail t i] == [] >> deltatime [repeat x length? t [t/(x): t/(x) * 2]] == 0:00:16.228 >> t: copy [] insert/dup t 1 10000000 == [] >> deltatime [i: 1 until [t/:i: t/:i * 2 tail? t: next t]] == 0:00:22.194 >> t: copy [] insert/dup t 1 10000000 == [] >> deltatime [repeat x length? t [t/(x): t/(x) * 2]] == 0:00:15.447 What system are you running Endo? I'm really curious about the difference in performance.
posted by: Nick 19Jun2017/8:41:577:00
Here's the cleaned up copy of the tests  curious to hear results on other platforms: t: copy [] repeat i 10000000 [insert tail t i] deltatime [i: 1 until [t/:i: t/:i * 2 tail? t: next t]] t: copy [] repeat i 10000000 [insert tail t i] deltatime [repeat x length? t [t/(x): t/(x) * 2]] t: copy [] insert/dup t 1 10000000 deltatime [i: 1 until [t/:i: t/:i * 2 tail? t: next t]] t: copy [] insert/dup t 1 10000000 deltatime [repeat x length? t [t/(x): t/(x) * 2]]
posted by: Nick 19Jun2017/8:51:207:00
t1 14.433255 14.375567 14.562163 t2 10.988527 10.948129 10.986415 t3 14.513004 14.739353 14.632172 t4 11.097424 10.935371 10.952677 linux 3.14.20 (i686) Inspiron 1501 tahrpup 6.0 (Puppy Linux) Mobile AMD Sempron 3500+ 800 mhz Memory: Total: 14166616 kB Free: 714000 kB Available: 101500 kB
posted by: Howard 19Jun2017/19:16:537:00
t1 18.689 18.720 18.844 t2 13.876 13.813 13.819 t3 18.454 18.532 18.610 t4 13.829 13.844 13.860 Windows 8 64bit Ausus S200E Celeron 847 1.1 Ghz 2 GB memory, 1.89 GB usable hmnnn....
posted by: Howard 19Jun2017/23:13:137:00
>> t: copy [] repeat i 10000000 [insert tail t i] == [] >> deltatime [i: 1 until [t/:i: t/:i * 2 tail? t: next t]] == 0:00:10.66 >> t: copy [] repeat i 10000000 [insert tail t i] == [] >> deltatime [repeat x length? t [t/(x): t/(x) * 2]] == 0:00:08.004 >> t: copy [] insert/dup t 1 10000000 == [] >> deltatime [i: 1 until [t/:i: t/:i * 2 tail? t: next t]] == 0:00:10.386 >> t: copy [] insert/dup t 1 10000000 == [] >> deltatime [repeat x length? t [t/(x): t/(x) * 2]] == 0:00:08.03
posted by: Nick 20Jun2017/23:11:177:00
My friend had an assignment in college for a program called MatLab. He mentioned that MatLab can multiply arrays together, like [2 3 4 5 6] * [3 4 5 6 7] to make one which is [6 12 20 30 42]. What would be an easy way to do this? R E B O L [] lol1: [2 3 4 5 6] lol2: [3 4 5 6 7] lol3: [] foreach lol lol1 [append lol3 (lol * first lol2) remove lol2]
posted by: Andrew 22Jun2017/19:15:217:00
repeat i length? lol1 [insert tail lol3 lol1/:i * lol2/:i]
posted by: Nick 23Jun2017/1:23:577:00
If you'll perform this operation often, create a function and put it in your rebol.r file: multiplyblocks: func [bl1 bl2] [ bl3: copy [] repeat i length? bl1 [insert tail bl3 bl1/:i * bl2/:i] bl3 ] multiplyblocks [2 3 4 5 6] [3 4 5 6 7] lol1: [2 3 4 5 6] lol2: [3 4 5 6 7] multiplyblocks lol1 lol2 You can choose how to handle blocks of different length, how to handle a arbitrary number of blocks, etc.: multiplyall: func [bl] [ result: copy [] lens: copy [] foreach b bl [append lens length? b] len: first minimumof lens repeat i len [ total: 1 foreach b bl [total: total * b/:i] append result total ] result ] multiplyall [ [1 2 3 4 5 6] [2 3 4 5] [3 4 5 6 7] ]
posted by: Nick 23Jun2017/10:36:417:00
Here's the function above using 'mapeach to find the shortest input block: multiplyall: func [bl] [ result: copy [] repeat i first minimumof mapeach b bl [length? b] [ total: 1 foreach b bl [total: total * b/:i] append result total ] ] multiplyall [[1 2 3 4 5 6][2 3 4 5][3 4 5 6 7]]
posted by: Nick 23Jun2017/10:59:037:00
(try that function with any number of blocks, each containing any arbitrary number of values)
posted by: Nick 23Jun2017/11:04:507:00
Could you explain what lol1/:i means, or give me a link to an explanation ?
posted by: Andrew 23Jun2017/16:48:457:00
> lol1/:i It's an equivalent to pick lol1 i. In addition, you can use the notation for assigning values too. Example with different word names: blk: copy [a b c d e f] ind: 4 blk/:ind ;; equivalent to pick blk ind == d blk/:ind: 88 ;; equivalent to poke blk ind 88 blk == [a b c 88 e f]
posted by: Sunanda 23Jun2017/17:00:577:00
Thank you so much !
posted by: Andrew 23Jun2017/17:13:417:00
Why do you do blk: copy [a b c d e f] instead of blk: [a b c d e f]?
posted by: Andrew 23Jun2017/17:32:307:00
'Copy ensures that any previous values in the block are erased.
posted by: Nick 24Jun2017/6:36:117:00
