Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Play SwiftyMath

USAMI Kosuke
September 20, 2019

Play SwiftyMath

USAMI Kosuke

September 20, 2019
Tweet

More Decks by USAMI Kosuke

Other Decks in Programming

Transcript

  1. SwiftyMath ͱ͸ • SwiftyMath : Pure Math in Pure Swift.

    • ϓϩάϥϚ͕਺ֶΛֶͿͳΒɺίʔυΛ࢖ֶͬͯͿͷ͕ྑ͍
  2. Φεεϝͷߨԋ • SwiftyMath ͰֶͿ਺ֶʢந৅୅਺ֶʣ (iOSDC 2019) • SwiftyMath Λ࡞ΒΕͨɺ͞ͷ͚ͨͱ͞Μͷߨԋ •

    ෼͔Γ΍͍͢ͷͰɺ਺ֶΛ͋·Γ஌Βͳ͍ਓʹ΋Φεεϝ • ݟͳ͕Β࣮ࡍʹίʔυΛॻ͘ͱྑ͍
  3. SwiftyMath ͷ۩ମྫ (1) import SwiftyMath let a = RationalNumber(4, 5)

    // 4/5 let b = RationalNumber(3, 2) // 3/2 a + b // 23/10 a * b // 6/5 b / a // 15/8
  4. SwiftyMath ͷ۩ମྫ (2) typealias P = Polynomial< > let f

    = P(0, 2, -3, 1) // x^3 − 3x^2 + 2x let g = P(6, -5, 1) // x^2 − 5x + 6 f + g // x^3 - 2x^2 - 3x + 6 f * g // x^5 - 8x^4 + 23x^3 - 28x^2 + 12x f % g // 6x - 12 gcd(f, g) // ࠷େެ໿਺ 6x - 12
  5. SwiftyMath ͷ۩ମྫ (3) typealias M = SquareMatrix<_2, > // 2×2

    ߦྻ let a = M(1, 2, 3, 4) // [1, 2; 3, 4] let b = M(2, 1, 1, 2) // [2, 1; 1, 2] a + b // [3, 3; 4, 6] a * b // [4, 5; 10, 11] a + b == b + a // true: ଍͠ࢉ͸ॱ൪Λม͑ͯ΋౴͑͸ಉ͡ a * b == b * a // false: ͔͚ࢉ͸ॱ൪Λม͑Δͱ౴͕͑ҟͳΔ
  6. Կ͕ෆຬͳͷ͔ • ࣮ߦ࣌ʹαΠζΛνΣοΫͯ͠Τϥʔʹ͢Δ͜ͱʹͳΔ • Ϗϧυ࣌ʹνΣοΫ͍ͨ͠ΑͶʁ ܕͰ൑ఆ͍ͨ͠ΑͶʁ static func + (lhs:

    Matrix, rhs: Matrix) -> Matrix { assert(lhs.rows == rhs.rows && lhs.columns == rhs.columns, "αΠζ͕Ұக͍ͯ͠ͳ͍ͱ଍͠ࢉͰ͖ͳ͍") ... }
  7. SwiftyMath ͷղܾࡦ • ੔਺஋ʹରԠ͢ΔܕΛ࡞Δ protocol SizeType { static var intValue:

    Int { get } } struct _2 : SizeType { static let intValue = 2 } struct Matrix2D : Matrix<_2, > { ... }
  8. Xcode 11 Ͱ SwiftPM Λ։͘ • Xcode ͔Β SwiftPM ϑΥϧμΛ௚઀։͘͜ͱ͕Ͱ͖Δ

    • Package.swift ϑΝΠϧ·ͨ͸ϑΥϧμΛબͿ • ࣗಈతʹɺઐ༻ͷ Xcode ϫʔΫεϖʔε͕ੜ੒͞ΕΔ • .swiftpm ϑΥϧμҎԼʹੜ੒͞ΕΔ
  9. Playground • ΋ͬͱ΋͓खܰͳํ๏͸ɺSources ϑΥϧμʹϥΠϒϥϦͷ ιʔεΛؙ͝ͱೖΕΔ • ͜Ε͸ Xcode 11 Ͱͳ͘ͱ΋ैདྷ͔Β࢖͑Δ

    • ϥΠϒϥϦͷόʔδϣϯ؅ཧͳͲ͕ෆཁͳΒ͜ΕͰे෼ • iPad ͷ Swift Playground Ͱ΋͜ͷํ๏Ͱ OK
  10. Xcode 11 + Swift Package • Xcode ϓϩδΣΫτʹରͯ͠ Swift Package

    Λ௥ՃͰ͖Δ • Xcode 11 Ͱ File - Swift Packages - Add Package Dependency • ΋ͪΖΜ SwiftyMath ΋ Xcode ϓϩδΣΫτʹ૊ΈࠐΊΔ