tmaeda
February 08, 2014
11k

# それ NArray でできるよ

## tmaeda

February 08, 2014

## Transcript

22. ### 3VCZඪ४ͷ"SSBZͩͱʁ a = [[1,2,3], [4,5,6], [7,8,9]] => [[1, 2, 3],

[4, 5, 6], [7, 8, 9]] a[1].inject(:+) => 15
23. ### /"SSBZͩͱʁ na = NArray.int(3,3).indgen!(1,1) => NArray.int(3,3): [ [ 1, 2,

3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] na[true, 1] => NArray.int(3): [ 4, 5, 6 ] na[true, 1].sum => 15
24. ### /"SSBZͩͱʁ na = NArray.int(3,3).indgen!(1,1) => NArray.int(3,3): [ [ 1, 2,

3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] na[true, 1] => NArray.int(3): [ 4, 5, 6 ] na[true, 1].sum => 15 JOUܕͷ ºͷ࣍ݩ ഑ྻΛ༻ҙͯ͠
25. ### /"SSBZͩͱʁ na = NArray.int(3,3).indgen!(1,1) => NArray.int(3,3): [ [ 1, 2,

3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] na[true, 1] => NArray.int(3): [ 4, 5, 6 ] na[true, 1].sum => 15 ࢝·ΓͰɺ ͣͭ૿Ճ͢Δ਺Ͱ ຒΊΔ
26. ### /"SSBZͩͱʁ na = NArray.int(3,3).indgen!(1,1) => NArray.int(3,3): [ [ 1, 2,

3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] na[true, 1] => NArray.int(3): [ 4, 5, 6 ] na[true, 1].sum => 15 ศ্ٓɺԣ࣠ΛY࣠ɺ ॎ࣠ΛZ࣠ͱݺͼ·͢ Αɻ Y Z
27. ### /"SSBZͩͱʁ na = NArray.int(3,3).indgen!(1,1) => NArray.int(3,3): [ [ 1, 2,

3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] na[true, 1] => NArray.int(3): [ 4, 5, 6 ] na[true, 1].sum => 15 Y࣠͸શ෦ɺ Z࣠͸൪໨ɺ Λऔಘ ศ্ٓɺԣ࣠ΛY࣠ɺ ॎ࣠ΛZ࣠ͱݺͼ·͢ Αɻ Y Z
28. ### /"SSBZͩͱʁ na = NArray.int(3,3).indgen!(1,1) => NArray.int(3,3): [ [ 1, 2,

3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] na[true, 1] => NArray.int(3): [ 4, 5, 6 ] na[true, 1].sum => 15 ͦΕΛ߹ܭ
29. ### ·ͱΊΔͱʁ na = NArray.int(3,3).indgen!(1,1) => NArray.int(3,3): [ [ 1, 2,

3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] na[true, 1].sum => 15
30. ### /"SSBZͷಛ௕ na = NArray.int(3,3).indgen!(1,1) => NArray.int(3,3): [ [ 1, 2,

3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] na[true, 1].sum => 15 ͪΌΜͱºͷܗ Ͱදࣔͯ͘͠ΕΔ
31. ### /"SSBZͷಛ௕ na = NArray.int(3,3).indgen!(1,1) => NArray.int(3,3): [ [ 1, 2,

3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] na[true, 1].sum => 15 ૢ࡞͸શͯNBQɻ ࣗ෼ͰNBQ ΍JOKFDUͳͲΛॻ ͘ඞཁ͸ͳ͍ɻ

·ͩڐͤΔϨϕϧ

37. ### 3VCZඪ४ͷ"SSBZͩͱʁ a = [[1,2,3], [4,5,6], [7,8,9]] => [[1, 2, 3],

[4, 5, 6], [7, 8, 9]] a.map{|row| row[0]}.inject(:+) => 12
38. ### /"SSBZͩͱʁ na = NArray.int(3,3).indgen!(1,1) => NArray.int(3,3): [ [ 1, 2,

3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] na[0, true] => NArray.int(3): [ 1, 4, 7 ] na[0, true].sum => 12
39. ### /"SSBZͩͱʁ na = NArray.int(3,3).indgen!(1,1) => NArray.int(3,3): [ [ 1, 2,

3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] na[0, true] => NArray.int(3): [ 1, 4, 7 ] na[0, true].sum => 12 Y࣠͸൪໨ɺ Z࣠͸શ෦ Λऔಘ Y Z
40. ### /"SSBZͩͱʁ na = NArray.int(3,3).indgen!(1,1) => NArray.int(3,3): [ [ 1, 2,

3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] na[0, true] => NArray.int(3): [ 1, 4, 7 ] na[0, true].sum => 12 ͦΕΛ߹ܭ
41. ### ·ͱΊΔͱʁ na = NArray.int(3,3).indgen!(1,1) => NArray.int(3,3): [ [ 1, 2,

3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] na[0, true].sum => 12
42. ### ൺ΂ͯΈ·͠ΐ͏ x࣠ํ޲΁ͷ߹ܭ y࣠ํ޲΁ͷ߹ܭ Array a[1].inject(:+) a.map{|row| row[0]}. inject(:+) NArray na[true,

1].sum na[0, true].sum
43. ### ൺ΂ͯΈ·͠ΐ͏ x࣠ํ޲΁ͷ߹ܭ y࣠ํ޲΁ͷ߹ܭ Array a[1].inject(:+) a.map{|row| row[0]}. inject(:+) NArray na[true,

1].sum na[0, true].sum "SSBZͩͱɺྻͷ߹ܭͷͱ͖ͷίʔυͱશ͘ର শੑ͕ͳ্͍ʹɺগ͠௕ͯ͘Կ΍͍ͬͯΔͷ͔ Θ͔Γʹ͍͘Ͱ͢Ͷ

50. ### /"SSBZͰ na = NArray.int(3,3).indgen!(1,1) => NArray.int(3,3): [ [ 1, 2,

3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] na[true, [0,1]] => NArray.int(3,2): [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] na[true, [0,1]].sum(1) => NArray.int(3): [ 5, 7, 9 ]
51. ### /"SSBZͰ na = NArray.int(3,3).indgen!(1,1) => NArray.int(3,3): [ [ 1, 2,

3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] na[true, [0,1]] => NArray.int(3,2): [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] na[true, [0,1]].sum(1) => NArray.int(3): [ 5, 7, 9 ] Y࣠͸શ෦ɺ Z࣠͸൪໨ͱ൪໨ Λऔಘ Y Z
52. ### /"SSBZͰ na = NArray.int(3,3).indgen!(1,1) => NArray.int(3,3): [ [ 1, 2,

3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] na[true, [0,1]] => NArray.int(3,2): [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] na[true, [0,1]].sum(1) => NArray.int(3): [ 5, 7, 9 ] ͦΕΛZ࣠ํ޲ʹ ߹ܭ
53. ### ͪͳΈʹ na[true, [0,1]] => NArray.int(3,2): [ [ 1, 2, 3

], [ 4, 5, 6 ] ] na[true, [0,1]].sum(0) na[true, [0,1]].sum(1) na[true, [0,1]].sum => NArray.int(2): [ 6, 15 ] => NArray.int(3): [ 5, 7, 9 ] => 21
54. ### ͪͳΈʹ na[true, [0,1]] => NArray.int(3,2): [ [ 1, 2, 3

], [ 4, 5, 6 ] ] na[true, [0,1]].sum(0) na[true, [0,1]].sum(1) na[true, [0,1]].sum => NArray.int(2): [ 6, 15 ] => NArray.int(3): [ 5, 7, 9 ] => 21 TVN  ͩͱɺ Y࣠ํ޲ʹ߹ܭ͞Εͯ
55. ### ͪͳΈʹ na[true, [0,1]] => NArray.int(3,2): [ [ 1, 2, 3

], [ 4, 5, 6 ] ] na[true, [0,1]].sum(0) na[true, [0,1]].sum(1) na[true, [0,1]].sum => NArray.int(2): [ 6, 15 ] => NArray.int(3): [ 5, 7, 9 ] => 21 TVN  ͩͱɺ Z࣠ํ޲ʹ߹ܭ͞Εͯ
56. ### ͪͳΈʹ na[true, [0,1]] => NArray.int(3,2): [ [ 1, 2, 3

], [ 4, 5, 6 ] ] na[true, [0,1]].sum(0) na[true, [0,1]].sum(1) na[true, [0,1]].sum => NArray.int(2): [ 6, 15 ] => NArray.int(3): [ 5, 7, 9 ] => 21 TVNͩͱɺ શํ޲ʹ ߹ܭ͞Ε·͢

64. ### ͜Μͳײ͡    "ళ  #ళ  \$ళ 

உ  ঁ  ୅  ୅      ººͷදʢۭؒ
65. ### na = NArray.int(3, 2, 2).indgen!(1, 1) => NArray.int(3,2,2): [ [

[ 1, 2, 3 ], [ 4, 5, 6 ] ], [ [ 7, 8, 9 ], [ 10, 11, 12 ] ] ] /"SSBZͰදݱ͢Δͱʁ
66. ### na[0, 0, 0] => 1 na[0, 1, 0] => 4

na[0, 0, 1] => 7 ஋Λऔಘͯ͠Έ·͠ΐ͏ "ళ உ ୅ "ళ ঁ ୅ "ళ உ ୅
67. ### ୅ͷ஋શ෦͸ʁ na[true, true, 0] => NArray.int(3,2): [ [ 1, 2,

3 ], [ 4, 5, 6 ] ] na[true, true, 0].sum => 21 ళશ෦ ୅ ੑผશ෦
68. ### உੑͷ஋શ෦͸ʁ na[true, 0, true] => NArray.int(3,2): [ [ 1, 2,

3 ], [ 7, 8, 9 ] ] na[true, 0, true].sum => 30 ళશ෦ ੈ୅શ෦ உੑ

70. ### cube = Cube.new([ {key: :mise, values: ["Aళ", "Bళ", "Cళ"]}, {key:

:sedai, values: ["10୅","20୅"]}, {key: :sei, values:["உ","ঁ"]} ]) # ళA, 20୅, ঁʹ20Λઃఆ cube.set( { mise: "Aళ", sedai: "20୅", sei: "ঁ" }, 20) # ళA, 20୅, ঁͷ஋Λऔಘ cube.get( { mise: "Aళ", sedai: "20୅", sei: "ঁ" }) => 20 # 10୅ͷ஋Λऔಘ cube.get( { sedai: "10୅"} ) => NArray.int(3,2): [ [ 1, 2, 3 ], [ 7, 8, 9 ] ]