それ NArray でできるよ

B9dd885e3ef94ac5e0bcfdf4800ba703?s=47 tmaeda
February 08, 2014

それ NArray でできるよ

B9dd885e3ef94ac5e0bcfdf4800ba703?s=128

tmaeda

February 08, 2014
Tweet

Transcript

  1. ͦΕ/"SSBZͰ Ͱ͖ΔΑ 札幌中央区RRuubbyy会議0011 22001144年22月88日 @@ttmmaaeeddaa

  2. !UNBFEB

  3. !UNBFEB w3VCZྺ͸΋͏೥ʹͳΓ·͢ɻ

  4. !UNBFEB w3VCZྺ͸΋͏೥ʹͳΓ·͢ɻ (C)Ωϟϓςϯཌྷ

  5. !UNBFEB w3VCZྺ͸΋͏೥ʹͳΓ·͢ɻ (C)Ωϟϓςϯཌྷ

  6. !UNBFEB wͦͷੲຊΛॻ͍ͨΓ΋͠·ͨ͠ɻ

  7. !UNBFEB wաڈճ΄Ͳࡳຈ3VCZձٞͰ3BJMTͷ͓࿩Λ͞ ͤͯ௖͖·ͨ͠ɻ

  8. !UNBFEB w ג ͑ʹ͠ςοΫͰͲͬ΀Γ3VCZͰ࢓ࣄ͕Ͱ͖ ͯ޾ͤͰ͢ɻ

  9. ࠓ೔͸ޚࣾͷϏδωε Ͱ͝׆༻͍͚ͨͩΔ /"SSBZ ͷ͝ఏҊͰ͢ɻ

  10. /"SSBZʁ  ॳࣖ  ໊લ͙Β͍͸ฉ͍ͨࣄ͋Δ  ࢖ͬͨ͜ͱ͋Δ  Α͘࢖͏

  11. /"SSBZ wాதণ޺͞Μ࡞ͷ3VCZ༻ଟ࣍ݩ਺஋഑ྻܭࢉ ϥΠϒϥϦɻ wओʹେྔͷ਺஋ܭࢉΛߴ଎ʹॲཧ͢Δ͜ͱΛ໨ తʹ࡞ΒΕͨ΋ͷɻ http://magazine.rubyist.net/?0006-RLR

  12. ͲΕ͙Β͍଎͍͔ʁ ഑ྻཁૉ਺ ඵ਺ ഑ྻʹ͔Β·Ͱͷཚ਺Λ֨ೲ͠ɺͦͷ߹ܭɺฏۉɺ෼ࢄɺ࠷େ஋ɺ࠷খ஋ΛٻΊΔͷʹ͔͔ͬͨ߹ܭඵ਺ 3VCZ /"SSBZ .BD#PPL"JS*OUFM$PSFJ()[

  13. ͲΕ͙Β͍଎͍͔ʁ ഑ྻཁૉ਺ ඵ਺ ഑ྻʹ͔Β·Ͱͷཚ਺Λ֨ೲ͠ɺͦͷ߹ܭɺฏۉɺ෼ࢄɺ࠷େ஋ɺ࠷খ஋ΛٻΊΔͷʹ͔͔ͬͨ߹ܭඵ਺ 3VCZ /"SSBZ .BD#PPL"JS*OUFM$PSFJ()[ ໿ഒ ߴ଎

  14. ͲΕ͙Β͍଎͍͔ʁ ഑ྻཁૉ਺ ඵ਺ ഑ྻʹ͔Β·Ͱͷཚ਺Λ֨ೲ͠ɺͦͷ߹ܭɺฏۉɺ෼ࢄɺ࠷େ஋ɺ࠷খ஋ΛٻΊΔͷʹ͔͔ͬͨ߹ܭඵ਺ 3VCZ /"SSBZ .BD#PPL"JS*OUFM$PSFJ()[ ໿ഒ ߴ଎ Ͱɾ͢ɾ͕ʜ

  15. ඵ ඵ ඵ

  16. ສ݅Ҏ ্ͷܭࢉΛΑ ͘ඞཁͱ͢Δ ਓʙʁ ඵ ඵ ඵ

  17. Ͱ͸ɺͲΜͳ ʹখͯ͘͞΋ ͍͍ͷͰɺද ܭࢉΛΑ͘ඞ ཁͱ͢Δ ਓʙʁ

  18. ͱ͍͏Θ͚Ͱɺࠓ೔͸ /"SSBZͷ଎౓໘ΑΓ ΋ɺදܭࢉػೳʹ ண໨͓ͨ͠࿩Λ͠·͢ɻ

  19. ྫ͑͹ɺ͜Μͳද

  20. ྫ͑͹ɺ͜Μͳද ߦ໨શମͷ߹ܭ͸ʁ

  21. &YDFMͩͱʁ

  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ͳͲΛॻ ͘ඞཁ͸ͳ͍ɻ
  32. ൺ΂ͯΈ·͠ΐ͏ x࣠ํ޲΁ͷ߹ܭ Array a[1].inject(:+) NArray na[true, 1].sum

  33. ൺ΂ͯΈ·͠ΐ͏ x࣠ํ޲΁ͷ߹ܭ Array a[1].inject(:+) NArray na[true, 1].sum /"SSBZͷํ͕௚ײత Ͱ͕͢ɺ "SSBZͰ΋

    ·ͩڐͤΔϨϕϧ
  34. Z࣠ํ޲ͷ߹ܭ΋ ΍ͬͯΈ·͠ΐ͏

  35. ྫ͑͹ɺ͜Μͳද "ྻશମͷ߹ܭ͸ʁ

  36. &YDFMͩͱʁ

  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ͩͱɺྻͷ߹ܭͷͱ͖ͷίʔυͱશ͘ର শੑ͕ͳ্͍ʹɺগ͠௕ͯ͘Կ΍͍ͬͯΔͷ͔ Θ͔Γʹ͍͘Ͱ͢Ͷ
  44. ͜͜·Ͱͷ·ͱΊ w/"SSBZͰ͸දΛදͱͯ͠ѻ͑·͢ɻ wૢ࡞͸શͯNBQ΍JOKFDUʹ૬౰͢ΔͷͰɺࣗ ෼ͰNBQ΍JOKFDUΛॻ͘ඞཁ͕͋Γ·ͤΜɻ

  45. ΋ͬͱෳࡶͳྫ΋ ΍ͬͯΈ·͠ΐ͏

  46. ूܭද࡞͍ͬͯͩ͘͞Ҋ݅

  47. ूܭද࡞͍ͬͯͩ͘͞Ҋ݅ Α͋͘Γ·͢ΑͶʁ

  48. ୅ͷখܭΛٻΊΔʹ͸ʁ

  49. 3VCZඪ४ͷ"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ͩͱɺ શํ޲ʹ ߹ܭ͞Ε·͢
  57. ͭ·Γɺ͜ͷลશ෦ɺTVN ͰٻΊΒΕΔΘ͚Ͱ͢ʂ TVN  TVN  TVN

  58. ͜Μͳߟ͑ํ΋ ͋Γ·͢ɻ

  59. ͍ͭ͘ͷ࣠Ͱ෼ྨ͞Εͨ දͰ͠ΐ͏͔ʁ

  60. ͍ͭ͘ͷ࣠Ͱ෼ྨ͞Εͨ දͰ͠ΐ͏͔ʁ  ੈ୅ͷ࣠

  61. ͍ͭ͘ͷ࣠Ͱ෼ྨ͞Εͨ දͰ͠ΐ͏͔ʁ  ੑผͷ࣠

  62. ͍ͭ͘ͷ࣠Ͱ෼ྨ͞Εͨ දͰ͠ΐ͏͔ʁ  ళͷ࣠

  63. ͱ͍͏͜ͱ͸ɺ࣍ݩ ͷදͰ࣋ͭํ͕ࣗવ ͳͷͰ͸ʁ

  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 ళશ෦ ੈ୅શ෦ உੑ
  69. ΋ͬͱศརʹʂ w/"SSBZ͸Կ࣍ݩͷσʔλͰ΋ѻ͑Δ w͍͍ͪͪ஋ͱ഑ྻͷΠϯσοΫεΛ಄Ͱߟ͑ͯ ରԠ͚ͮΔͷ໘౗ͳͷͰɺͦ͜Λਓʹ༏͍͠ײ ͡ͷϥούʔΛඃͤͯ͋͛Δͱɺ೚ҙͷ਺ͷ࣠ ͰσʔλΛ੔ཧͰ͖Δശ͕Ͱ͖͕͋Γ·͢ɻ

  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 ] ]
  71. ·ͱΊ w/"SSBZ͸೚ҙͷ਺ͷ࣠ͷσʔλΛࣗ༝ʹѻ͑ ·͢ɻ w/"SSBZ͸ͦͷσʔλۭؒͷதͷ޷͖ͳ৔ॴͷ σʔλʹॊೈʹΞΫηεͰ͖·͢ɻ w&YDFM͙Β͍ΧδϡΞϧʹ/"SSBZΛ࢖͍·͠ ΐ͏ʂ

  72. ࢀߟ wϚχϡΞϧ IUUQOBSSBZSVCZGPSHFPSHJOEFYIUNMKB w3VCZٯҾ͖Ϩγϐ Ϩγϐ  wOVNQZؔ܎ͷࢿྉ

  73. ͥͻޚࣾͷϏδωε ΁͝׆༻͍ͩ͘͞ɻ