SlideShare ist ein Scribd-Unternehmen logo
1 von 42
Downloaden Sie, um offline zu lesen
protocol AdditiveGroup { //
static var zero: Self { get } //
static func + (a: Self, b: Self) -> Self //
prefix static func - (x: Self) -> Self //
}
extension AdditiveGroup {
static func -(a: Self, b: Self) -> Self {
return (a + (-b)) //
}
}
protocol AdditiveGroup { //
…
}
protocol Ring: AdditiveGroup { //
static var identity: Self { get } // 1
static func * (a: Self, b: Self) -> Self //
var inverse: Self? { get } // (optional)
}
protocol AdditiveGroup { //
…
}
protocol Ring: AdditiveGroup { //
…
}
protocol Field: Ring {} //
extension Field {
static func / (a: Self, b: Self) -> Self { //
return a * b.inverse! // 0
}
}
extension Int: Ring { // Int
static var zero: Int {
return 0
}
static var identity: Int {
return 1
}
}
// Int
struct Rational: Field { //
private let p, q: Int
init(_ p: Int, _ q: Int) {
(self.p, self.q) = (p, q)
}
static var zero: Int {
return Rational(0, 1)
}
static var identity: Int {
return Rational(1, 1)
}
var inverse: Rational? { //
return (p != 0) ? Rational(q, p) : nil
}
…
struct Rational: Field {
…
static func + (a: Rational, b: Rational) -> Rational {
return Rational(a.p * b.q + a.q * b.p, a.q * b.q)
}
static prefix func - (a: Rational) -> Rational {
return Rational(-a.p, a.q)
}
static func * (a: Rational, b: Rational) -> Rational {
return Rational(a.p * b.p, a.q * b.q)
}
}
protocol EuclideanRing: Ring { //
static func eucDiv(_ a: Self, _ b: Self)
-> (q: Self, r: Self) //
}
extension EuclideanRing {
static func % (_ a: Self, b: Self) -> Self { //
return Self.eucDiv(a, b).r
}
}
extension Int: EuclideanRing { // Int EuclideanRing
static func eucDiv(_ a: Int, _ b: Int)
-> (q: Int, r: Int) { //
let q = a / b
return (q: q, r: a - q * b)
}
}
struct Polynomial<K: Field>: EuclideanRing {
public let coeffs: [K]
public init(_ coeffs: K...) {
self.coeffs = coeffs
}
public static func + (f: Polynomial<K>, g: Polynomial<K>) -> Polynomial<K> {
return Polynomial<K>(degree: max(f.degree, g.degree)) {
f.coeff($0) + g.coeff($0)
}
}
public static prefix func - (f: Polynomial<K>) -> Polynomial<K> {
return f.map { -$0 }
}
public static func * (f: Polynomial<K>, g: Polynomial<K>) -> Polynomial<K> {
…
}
…
struct Polynomial<K: Field>: EuclideanRing {
…
static func eucDiv<K: Field>(_ f: Polynomial<K>, _ g: Polynomial<K>)
-> (q: Polynomial<K>, r: Polynomial<K>) {
return (0 ... max(0, f.degree - g.degree))
.reversed()
.reduce( (0, f) ) {
(result: (Polynomial<K>, Polynomial<K>), degree: Int) in
let (q, r) = result
let m = eucDivMonomial(r, g)
return (q + m.q, m.r)
}
}
}
public func gcd<R: EuclideanRing>(_ a: R, _ b: R) -> R {
switch b {
case 0:
return a
default:
return gcd(b, a % b)
}
}
let a = sqrt(2.0) // 1.41421356…
a * a == 2.0 // false
Swift で数学のススメ 〜 プログラミングと数学は同時に学べ
Swift で数学のススメ 〜 プログラミングと数学は同時に学べ
Swift で数学のススメ 〜 プログラミングと数学は同時に学べ
Swift で数学のススメ 〜 プログラミングと数学は同時に学べ
Swift で数学のススメ 〜 プログラミングと数学は同時に学べ
Swift で数学のススメ 〜 プログラミングと数学は同時に学べ
Swift で数学のススメ 〜 プログラミングと数学は同時に学べ
Swift で数学のススメ 〜 プログラミングと数学は同時に学べ

Weitere ähnliche Inhalte

Was ist angesagt?

24 double integral over polar coordinate
24 double integral over polar coordinate24 double integral over polar coordinate
24 double integral over polar coordinatemath267
 
射頻電子 - [第六章] 低雜訊放大器設計
射頻電子 - [第六章] 低雜訊放大器設計射頻電子 - [第六章] 低雜訊放大器設計
射頻電子 - [第六章] 低雜訊放大器設計Simen Li
 
Activites du transistaire
Activites du transistaireActivites du transistaire
Activites du transistaireRabah HELAL
 
21 lagrange multipliers
21 lagrange multipliers21 lagrange multipliers
21 lagrange multipliersmath267
 
射頻電子實驗手冊 [實驗6] 阻抗匹配模擬
射頻電子實驗手冊 [實驗6] 阻抗匹配模擬射頻電子實驗手冊 [實驗6] 阻抗匹配模擬
射頻電子實驗手冊 [實驗6] 阻抗匹配模擬Simen Li
 
Formulaire derivees
Formulaire deriveesFormulaire derivees
Formulaire deriveesHASSANSABRA4
 
A real-world example of Functional Programming with fp-ts - no experience req...
A real-world example of Functional Programming with fp-ts - no experience req...A real-world example of Functional Programming with fp-ts - no experience req...
A real-world example of Functional Programming with fp-ts - no experience req...Frederick Fogerty
 
[ZigBee 嵌入式系統] ZigBee Architecture 與 TI Z-Stack Firmware
[ZigBee 嵌入式系統] ZigBee Architecture 與 TI Z-Stack Firmware[ZigBee 嵌入式系統] ZigBee Architecture 與 TI Z-Stack Firmware
[ZigBee 嵌入式系統] ZigBee Architecture 與 TI Z-Stack FirmwareSimen Li
 
Agilent ADS 模擬手冊 [實習2] 放大器設計
Agilent ADS 模擬手冊 [實習2]  放大器設計Agilent ADS 模擬手冊 [實習2]  放大器設計
Agilent ADS 模擬手冊 [實習2] 放大器設計Simen Li
 
RNN avec mécanisme d'attention
RNN avec mécanisme d'attentionRNN avec mécanisme d'attention
RNN avec mécanisme d'attentionJaouad Dabounou
 
射頻電子 - [第四章] 散射參數網路
射頻電子 - [第四章] 散射參數網路射頻電子 - [第四章] 散射參數網路
射頻電子 - [第四章] 散射參數網路Simen Li
 
射頻電子實驗手冊 - [實驗8] 低雜訊放大器模擬
射頻電子實驗手冊 - [實驗8] 低雜訊放大器模擬射頻電子實驗手冊 - [實驗8] 低雜訊放大器模擬
射頻電子實驗手冊 - [實驗8] 低雜訊放大器模擬Simen Li
 
Rで解く最適化問題 線型計画問題編
Rで解く最適化問題   線型計画問題編 Rで解く最適化問題   線型計画問題編
Rで解く最適化問題 線型計画問題編 Hidekazu Tanaka
 
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...Francesco Casalegno
 
專題製作發想與報告撰寫技巧
專題製作發想與報告撰寫技巧專題製作發想與報告撰寫技巧
專題製作發想與報告撰寫技巧Simen Li
 
射頻電子 - [第二章] 傳輸線理論
射頻電子 - [第二章] 傳輸線理論射頻電子 - [第二章] 傳輸線理論
射頻電子 - [第二章] 傳輸線理論Simen Li
 
Profº. Marcelo Santos Chaves - Cálculo I (Limites e Continuidades) - Exercíci...
Profº. Marcelo Santos Chaves - Cálculo I (Limites e Continuidades) - Exercíci...Profº. Marcelo Santos Chaves - Cálculo I (Limites e Continuidades) - Exercíci...
Profº. Marcelo Santos Chaves - Cálculo I (Limites e Continuidades) - Exercíci...MarcelloSantosChaves
 
Graph generation using a graph grammar
Graph generation using a graph grammarGraph generation using a graph grammar
Graph generation using a graph grammarHiroshi Kajino
 

Was ist angesagt? (20)

Etat des lieux et besoins des associations de solidarité
Etat des lieux et besoins des associations de solidaritéEtat des lieux et besoins des associations de solidarité
Etat des lieux et besoins des associations de solidarité
 
24 double integral over polar coordinate
24 double integral over polar coordinate24 double integral over polar coordinate
24 double integral over polar coordinate
 
射頻電子 - [第六章] 低雜訊放大器設計
射頻電子 - [第六章] 低雜訊放大器設計射頻電子 - [第六章] 低雜訊放大器設計
射頻電子 - [第六章] 低雜訊放大器設計
 
Activites du transistaire
Activites du transistaireActivites du transistaire
Activites du transistaire
 
21 lagrange multipliers
21 lagrange multipliers21 lagrange multipliers
21 lagrange multipliers
 
射頻電子實驗手冊 [實驗6] 阻抗匹配模擬
射頻電子實驗手冊 [實驗6] 阻抗匹配模擬射頻電子實驗手冊 [實驗6] 阻抗匹配模擬
射頻電子實驗手冊 [實驗6] 阻抗匹配模擬
 
Formulaire derivees
Formulaire deriveesFormulaire derivees
Formulaire derivees
 
A real-world example of Functional Programming with fp-ts - no experience req...
A real-world example of Functional Programming with fp-ts - no experience req...A real-world example of Functional Programming with fp-ts - no experience req...
A real-world example of Functional Programming with fp-ts - no experience req...
 
[ZigBee 嵌入式系統] ZigBee Architecture 與 TI Z-Stack Firmware
[ZigBee 嵌入式系統] ZigBee Architecture 與 TI Z-Stack Firmware[ZigBee 嵌入式系統] ZigBee Architecture 與 TI Z-Stack Firmware
[ZigBee 嵌入式系統] ZigBee Architecture 與 TI Z-Stack Firmware
 
PA linearity
PA linearityPA linearity
PA linearity
 
Agilent ADS 模擬手冊 [實習2] 放大器設計
Agilent ADS 模擬手冊 [實習2]  放大器設計Agilent ADS 模擬手冊 [實習2]  放大器設計
Agilent ADS 模擬手冊 [實習2] 放大器設計
 
RNN avec mécanisme d'attention
RNN avec mécanisme d'attentionRNN avec mécanisme d'attention
RNN avec mécanisme d'attention
 
射頻電子 - [第四章] 散射參數網路
射頻電子 - [第四章] 散射參數網路射頻電子 - [第四章] 散射參數網路
射頻電子 - [第四章] 散射參數網路
 
射頻電子實驗手冊 - [實驗8] 低雜訊放大器模擬
射頻電子實驗手冊 - [實驗8] 低雜訊放大器模擬射頻電子實驗手冊 - [實驗8] 低雜訊放大器模擬
射頻電子實驗手冊 - [實驗8] 低雜訊放大器模擬
 
Rで解く最適化問題 線型計画問題編
Rで解く最適化問題   線型計画問題編 Rで解く最適化問題   線型計画問題編
Rで解く最適化問題 線型計画問題編
 
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
 
專題製作發想與報告撰寫技巧
專題製作發想與報告撰寫技巧專題製作發想與報告撰寫技巧
專題製作發想與報告撰寫技巧
 
射頻電子 - [第二章] 傳輸線理論
射頻電子 - [第二章] 傳輸線理論射頻電子 - [第二章] 傳輸線理論
射頻電子 - [第二章] 傳輸線理論
 
Profº. Marcelo Santos Chaves - Cálculo I (Limites e Continuidades) - Exercíci...
Profº. Marcelo Santos Chaves - Cálculo I (Limites e Continuidades) - Exercíci...Profº. Marcelo Santos Chaves - Cálculo I (Limites e Continuidades) - Exercíci...
Profº. Marcelo Santos Chaves - Cálculo I (Limites e Continuidades) - Exercíci...
 
Graph generation using a graph grammar
Graph generation using a graph grammarGraph generation using a graph grammar
Graph generation using a graph grammar
 

Ähnlich wie Swift で数学のススメ 〜 プログラミングと数学は同時に学べ

Александр Зимин (Alexander Zimin) — Магия Swift
Александр Зимин (Alexander Zimin) — Магия SwiftАлександр Зимин (Alexander Zimin) — Магия Swift
Александр Зимин (Alexander Zimin) — Магия SwiftCocoaHeads
 
Cocoaheads Meetup / Alex Zimin / Swift magic
Cocoaheads Meetup / Alex Zimin / Swift magicCocoaheads Meetup / Alex Zimin / Swift magic
Cocoaheads Meetup / Alex Zimin / Swift magicBadoo Development
 
Functional Programming In Java
Functional Programming In JavaFunctional Programming In Java
Functional Programming In JavaAndrei Solntsev
 
function in c
function in cfunction in c
function in csubam3
 
Hive Functions Cheat Sheet
Hive Functions Cheat SheetHive Functions Cheat Sheet
Hive Functions Cheat SheetHortonworks
 
Inheritance compiler support
Inheritance compiler supportInheritance compiler support
Inheritance compiler supportSyed Zaid Irshad
 
All Aboard The Scala-to-PureScript Express!
All Aboard The Scala-to-PureScript Express!All Aboard The Scala-to-PureScript Express!
All Aboard The Scala-to-PureScript Express!John De Goes
 
Mastering Kotlin Standard Library
Mastering Kotlin Standard LibraryMastering Kotlin Standard Library
Mastering Kotlin Standard LibraryNelson Glauber Leal
 
Un dsl pour ma base de données
Un dsl pour ma base de donnéesUn dsl pour ma base de données
Un dsl pour ma base de donnéesRomain Lecomte
 
documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...
documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...
documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...Akaks
 
Lego: A brick system build by scala
Lego: A brick system build by scalaLego: A brick system build by scala
Lego: A brick system build by scalalunfu zhong
 
Kotlin For Android - Functions (part 3 of 7)
Kotlin For Android - Functions (part 3 of 7)Kotlin For Android - Functions (part 3 of 7)
Kotlin For Android - Functions (part 3 of 7)Gesh Markov
 
Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function Kamlesh Makvana
 
Делаем пользовательское Api на базе Shapeless
Делаем пользовательское Api на базе ShapelessДелаем пользовательское Api на базе Shapeless
Делаем пользовательское Api на базе ShapelessВадим Челышов
 
TI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class FunctionsTI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class FunctionsEelco Visser
 
Scala-Gopher: CSP-style programming techniques with idiomatic Scala.
Scala-Gopher: CSP-style programming techniques with idiomatic Scala.Scala-Gopher: CSP-style programming techniques with idiomatic Scala.
Scala-Gopher: CSP-style programming techniques with idiomatic Scala.Ruslan Shevchenko
 

Ähnlich wie Swift で数学のススメ 〜 プログラミングと数学は同時に学べ (20)

Александр Зимин (Alexander Zimin) — Магия Swift
Александр Зимин (Alexander Zimin) — Магия SwiftАлександр Зимин (Alexander Zimin) — Магия Swift
Александр Зимин (Alexander Zimin) — Магия Swift
 
Cocoaheads Meetup / Alex Zimin / Swift magic
Cocoaheads Meetup / Alex Zimin / Swift magicCocoaheads Meetup / Alex Zimin / Swift magic
Cocoaheads Meetup / Alex Zimin / Swift magic
 
oop objects_classes
oop objects_classesoop objects_classes
oop objects_classes
 
Functional Programming In Java
Functional Programming In JavaFunctional Programming In Java
Functional Programming In Java
 
function in c
function in cfunction in c
function in c
 
functions
functionsfunctions
functions
 
Hive Functions Cheat Sheet
Hive Functions Cheat SheetHive Functions Cheat Sheet
Hive Functions Cheat Sheet
 
Wien15 java8
Wien15 java8Wien15 java8
Wien15 java8
 
Inheritance compiler support
Inheritance compiler supportInheritance compiler support
Inheritance compiler support
 
All Aboard The Scala-to-PureScript Express!
All Aboard The Scala-to-PureScript Express!All Aboard The Scala-to-PureScript Express!
All Aboard The Scala-to-PureScript Express!
 
Mastering Kotlin Standard Library
Mastering Kotlin Standard LibraryMastering Kotlin Standard Library
Mastering Kotlin Standard Library
 
Un dsl pour ma base de données
Un dsl pour ma base de donnéesUn dsl pour ma base de données
Un dsl pour ma base de données
 
documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...
documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...
documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...
 
Lego: A brick system build by scala
Lego: A brick system build by scalaLego: A brick system build by scala
Lego: A brick system build by scala
 
Oop objects_classes
Oop objects_classesOop objects_classes
Oop objects_classes
 
Kotlin For Android - Functions (part 3 of 7)
Kotlin For Android - Functions (part 3 of 7)Kotlin For Android - Functions (part 3 of 7)
Kotlin For Android - Functions (part 3 of 7)
 
Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function
 
Делаем пользовательское Api на базе Shapeless
Делаем пользовательское Api на базе ShapelessДелаем пользовательское Api на базе Shapeless
Делаем пользовательское Api на базе Shapeless
 
TI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class FunctionsTI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class Functions
 
Scala-Gopher: CSP-style programming techniques with idiomatic Scala.
Scala-Gopher: CSP-style programming techniques with idiomatic Scala.Scala-Gopher: CSP-style programming techniques with idiomatic Scala.
Scala-Gopher: CSP-style programming techniques with idiomatic Scala.
 

Mehr von Taketo Sano

Divisibility of Lee’s class and its relation with Rasmussen’s invariant / 201...
Divisibility of Lee’s class and its relation with Rasmussen’s invariant / 201...Divisibility of Lee’s class and its relation with Rasmussen’s invariant / 201...
Divisibility of Lee’s class and its relation with Rasmussen’s invariant / 201...Taketo Sano
 
トポロジーと圏論の夜明け
トポロジーと圏論の夜明けトポロジーと圏論の夜明け
トポロジーと圏論の夜明けTaketo Sano
 
Swift で数学研究のススメ
Swift で数学研究のススメSwift で数学研究のススメ
Swift で数学研究のススメTaketo Sano
 
(意欲的な中高生のための)トポロジー・圏論・コンピュータ
(意欲的な中高生のための)トポロジー・圏論・コンピュータ(意欲的な中高生のための)トポロジー・圏論・コンピュータ
(意欲的な中高生のための)トポロジー・圏論・コンピュータTaketo Sano
 
特性類の気持ち
特性類の気持ち特性類の気持ち
特性類の気持ちTaketo Sano
 
山手線は丸いのか?プログラマのためのトポロジー入門
山手線は丸いのか?プログラマのためのトポロジー入門山手線は丸いのか?プログラマのためのトポロジー入門
山手線は丸いのか?プログラマのためのトポロジー入門Taketo Sano
 
何もないところから数を作る
何もないところから数を作る何もないところから数を作る
何もないところから数を作るTaketo Sano
 
「数える」とは何か? 〜 「とは何か?」を問う、AI時代の数学
「数える」とは何か? 〜 「とは何か?」を問う、AI時代の数学「数える」とは何か? 〜 「とは何か?」を問う、AI時代の数学
「数える」とは何か? 〜 「とは何か?」を問う、AI時代の数学Taketo Sano
 
情報幾何学 #2.4
情報幾何学 #2.4情報幾何学 #2.4
情報幾何学 #2.4Taketo Sano
 
情報幾何学 #2 #infogeo16
情報幾何学 #2 #infogeo16情報幾何学 #2 #infogeo16
情報幾何学 #2 #infogeo16Taketo Sano
 
objc2swift 〜 Objective-C から Swift への「コード&パラダイム」シフト
objc2swift 〜 Objective-C から Swift への「コード&パラダイム」シフトobjc2swift 〜 Objective-C から Swift への「コード&パラダイム」シフト
objc2swift 〜 Objective-C から Swift への「コード&パラダイム」シフトTaketo Sano
 
何もないところから数を作る
何もないところから数を作る何もないところから数を作る
何もないところから数を作るTaketo Sano
 
objc2swift (続・自動変換の野望)
objc2swift (続・自動変換の野望) objc2swift (続・自動変換の野望)
objc2swift (続・自動変換の野望) Taketo Sano
 
さらに上を目指すための iOS アプリ設計
さらに上を目指すための iOS アプリ設計さらに上を目指すための iOS アプリ設計
さらに上を目指すための iOS アプリ設計Taketo Sano
 
基底変換、固有値・固有ベクトル、そしてその先
基底変換、固有値・固有ベクトル、そしてその先基底変換、固有値・固有ベクトル、そしてその先
基底変換、固有値・固有ベクトル、そしてその先Taketo Sano
 
objc2swift (自動変換の野望)
objc2swift (自動変換の野望)objc2swift (自動変換の野望)
objc2swift (自動変換の野望)Taketo Sano
 
プログラマのための線形代数再入門2 〜 要件定義から学ぶ行列式と逆行列
プログラマのための線形代数再入門2 〜 要件定義から学ぶ行列式と逆行列プログラマのための線形代数再入門2 〜 要件定義から学ぶ行列式と逆行列
プログラマのための線形代数再入門2 〜 要件定義から学ぶ行列式と逆行列Taketo Sano
 
2015 02-18 xxx-literalconvertible
2015 02-18 xxx-literalconvertible2015 02-18 xxx-literalconvertible
2015 02-18 xxx-literalconvertibleTaketo Sano
 
let UIWebView as WKWebView
let UIWebView as WKWebViewlet UIWebView as WKWebView
let UIWebView as WKWebViewTaketo Sano
 
プログラマのための線形代数再入門
プログラマのための線形代数再入門プログラマのための線形代数再入門
プログラマのための線形代数再入門Taketo Sano
 

Mehr von Taketo Sano (20)

Divisibility of Lee’s class and its relation with Rasmussen’s invariant / 201...
Divisibility of Lee’s class and its relation with Rasmussen’s invariant / 201...Divisibility of Lee’s class and its relation with Rasmussen’s invariant / 201...
Divisibility of Lee’s class and its relation with Rasmussen’s invariant / 201...
 
トポロジーと圏論の夜明け
トポロジーと圏論の夜明けトポロジーと圏論の夜明け
トポロジーと圏論の夜明け
 
Swift で数学研究のススメ
Swift で数学研究のススメSwift で数学研究のススメ
Swift で数学研究のススメ
 
(意欲的な中高生のための)トポロジー・圏論・コンピュータ
(意欲的な中高生のための)トポロジー・圏論・コンピュータ(意欲的な中高生のための)トポロジー・圏論・コンピュータ
(意欲的な中高生のための)トポロジー・圏論・コンピュータ
 
特性類の気持ち
特性類の気持ち特性類の気持ち
特性類の気持ち
 
山手線は丸いのか?プログラマのためのトポロジー入門
山手線は丸いのか?プログラマのためのトポロジー入門山手線は丸いのか?プログラマのためのトポロジー入門
山手線は丸いのか?プログラマのためのトポロジー入門
 
何もないところから数を作る
何もないところから数を作る何もないところから数を作る
何もないところから数を作る
 
「数える」とは何か? 〜 「とは何か?」を問う、AI時代の数学
「数える」とは何か? 〜 「とは何か?」を問う、AI時代の数学「数える」とは何か? 〜 「とは何か?」を問う、AI時代の数学
「数える」とは何か? 〜 「とは何か?」を問う、AI時代の数学
 
情報幾何学 #2.4
情報幾何学 #2.4情報幾何学 #2.4
情報幾何学 #2.4
 
情報幾何学 #2 #infogeo16
情報幾何学 #2 #infogeo16情報幾何学 #2 #infogeo16
情報幾何学 #2 #infogeo16
 
objc2swift 〜 Objective-C から Swift への「コード&パラダイム」シフト
objc2swift 〜 Objective-C から Swift への「コード&パラダイム」シフトobjc2swift 〜 Objective-C から Swift への「コード&パラダイム」シフト
objc2swift 〜 Objective-C から Swift への「コード&パラダイム」シフト
 
何もないところから数を作る
何もないところから数を作る何もないところから数を作る
何もないところから数を作る
 
objc2swift (続・自動変換の野望)
objc2swift (続・自動変換の野望) objc2swift (続・自動変換の野望)
objc2swift (続・自動変換の野望)
 
さらに上を目指すための iOS アプリ設計
さらに上を目指すための iOS アプリ設計さらに上を目指すための iOS アプリ設計
さらに上を目指すための iOS アプリ設計
 
基底変換、固有値・固有ベクトル、そしてその先
基底変換、固有値・固有ベクトル、そしてその先基底変換、固有値・固有ベクトル、そしてその先
基底変換、固有値・固有ベクトル、そしてその先
 
objc2swift (自動変換の野望)
objc2swift (自動変換の野望)objc2swift (自動変換の野望)
objc2swift (自動変換の野望)
 
プログラマのための線形代数再入門2 〜 要件定義から学ぶ行列式と逆行列
プログラマのための線形代数再入門2 〜 要件定義から学ぶ行列式と逆行列プログラマのための線形代数再入門2 〜 要件定義から学ぶ行列式と逆行列
プログラマのための線形代数再入門2 〜 要件定義から学ぶ行列式と逆行列
 
2015 02-18 xxx-literalconvertible
2015 02-18 xxx-literalconvertible2015 02-18 xxx-literalconvertible
2015 02-18 xxx-literalconvertible
 
let UIWebView as WKWebView
let UIWebView as WKWebViewlet UIWebView as WKWebView
let UIWebView as WKWebView
 
プログラマのための線形代数再入門
プログラマのための線形代数再入門プログラマのための線形代数再入門
プログラマのための線形代数再入門
 

Kürzlich hochgeladen

PART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFE
PART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFEPART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFE
PART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFEMISSRITIMABIOLOGYEXP
 
CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...Nguyen Thanh Tu Collection
 
Shark introduction Morphology and its behaviour characteristics
Shark introduction Morphology and its behaviour characteristicsShark introduction Morphology and its behaviour characteristics
Shark introduction Morphology and its behaviour characteristicsArubSultan
 
ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6Vanessa Camilleri
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdfMr Bounab Samir
 
DiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdfDiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdfChristalin Nelson
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQuiz Club NITW
 
How to Uninstall a Module in Odoo 17 Using Command Line
How to Uninstall a Module in Odoo 17 Using Command LineHow to Uninstall a Module in Odoo 17 Using Command Line
How to Uninstall a Module in Odoo 17 Using Command LineCeline George
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research DiscourseAnita GoswamiGiri
 
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxMan or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxDhatriParmar
 
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxCLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxAnupam32727
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 
6 ways Samsung’s Interactive Display powered by Android changes the classroom
6 ways Samsung’s Interactive Display powered by Android changes the classroom6 ways Samsung’s Interactive Display powered by Android changes the classroom
6 ways Samsung’s Interactive Display powered by Android changes the classroomSamsung Business USA
 
Objectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptxObjectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptxMadhavi Dharankar
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDhatriParmar
 

Kürzlich hochgeladen (20)

PART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFE
PART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFEPART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFE
PART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFE
 
CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...
 
CARNAVAL COM MAGIA E EUFORIA _
CARNAVAL COM MAGIA E EUFORIA            _CARNAVAL COM MAGIA E EUFORIA            _
CARNAVAL COM MAGIA E EUFORIA _
 
Shark introduction Morphology and its behaviour characteristics
Shark introduction Morphology and its behaviour characteristicsShark introduction Morphology and its behaviour characteristics
Shark introduction Morphology and its behaviour characteristics
 
ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdf
 
DiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdfDiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdf
 
Chi-Square Test Non Parametric Test Categorical Variable
Chi-Square Test Non Parametric Test Categorical VariableChi-Square Test Non Parametric Test Categorical Variable
Chi-Square Test Non Parametric Test Categorical Variable
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
 
How to Uninstall a Module in Odoo 17 Using Command Line
How to Uninstall a Module in Odoo 17 Using Command LineHow to Uninstall a Module in Odoo 17 Using Command Line
How to Uninstall a Module in Odoo 17 Using Command Line
 
prashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Professionprashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Profession
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research Discourse
 
Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
 
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxMan or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
 
Spearman's correlation,Formula,Advantages,
Spearman's correlation,Formula,Advantages,Spearman's correlation,Formula,Advantages,
Spearman's correlation,Formula,Advantages,
 
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxCLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 
6 ways Samsung’s Interactive Display powered by Android changes the classroom
6 ways Samsung’s Interactive Display powered by Android changes the classroom6 ways Samsung’s Interactive Display powered by Android changes the classroom
6 ways Samsung’s Interactive Display powered by Android changes the classroom
 
Objectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptxObjectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptx
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
 

Swift で数学のススメ 〜 プログラミングと数学は同時に学べ

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17. protocol AdditiveGroup { // static var zero: Self { get } // static func + (a: Self, b: Self) -> Self // prefix static func - (x: Self) -> Self // } extension AdditiveGroup { static func -(a: Self, b: Self) -> Self { return (a + (-b)) // } }
  • 18. protocol AdditiveGroup { // … } protocol Ring: AdditiveGroup { // static var identity: Self { get } // 1 static func * (a: Self, b: Self) -> Self // var inverse: Self? { get } // (optional) }
  • 19. protocol AdditiveGroup { // … } protocol Ring: AdditiveGroup { // … } protocol Field: Ring {} // extension Field { static func / (a: Self, b: Self) -> Self { // return a * b.inverse! // 0 } }
  • 20. extension Int: Ring { // Int static var zero: Int { return 0 } static var identity: Int { return 1 } } // Int
  • 21. struct Rational: Field { // private let p, q: Int init(_ p: Int, _ q: Int) { (self.p, self.q) = (p, q) } static var zero: Int { return Rational(0, 1) } static var identity: Int { return Rational(1, 1) } var inverse: Rational? { // return (p != 0) ? Rational(q, p) : nil } …
  • 22. struct Rational: Field { … static func + (a: Rational, b: Rational) -> Rational { return Rational(a.p * b.q + a.q * b.p, a.q * b.q) } static prefix func - (a: Rational) -> Rational { return Rational(-a.p, a.q) } static func * (a: Rational, b: Rational) -> Rational { return Rational(a.p * b.p, a.q * b.q) } }
  • 23.
  • 24.
  • 25.
  • 26.
  • 27. protocol EuclideanRing: Ring { // static func eucDiv(_ a: Self, _ b: Self) -> (q: Self, r: Self) // } extension EuclideanRing { static func % (_ a: Self, b: Self) -> Self { // return Self.eucDiv(a, b).r } }
  • 28. extension Int: EuclideanRing { // Int EuclideanRing static func eucDiv(_ a: Int, _ b: Int) -> (q: Int, r: Int) { // let q = a / b return (q: q, r: a - q * b) } }
  • 29. struct Polynomial<K: Field>: EuclideanRing { public let coeffs: [K] public init(_ coeffs: K...) { self.coeffs = coeffs } public static func + (f: Polynomial<K>, g: Polynomial<K>) -> Polynomial<K> { return Polynomial<K>(degree: max(f.degree, g.degree)) { f.coeff($0) + g.coeff($0) } } public static prefix func - (f: Polynomial<K>) -> Polynomial<K> { return f.map { -$0 } } public static func * (f: Polynomial<K>, g: Polynomial<K>) -> Polynomial<K> { … } …
  • 30. struct Polynomial<K: Field>: EuclideanRing { … static func eucDiv<K: Field>(_ f: Polynomial<K>, _ g: Polynomial<K>) -> (q: Polynomial<K>, r: Polynomial<K>) { return (0 ... max(0, f.degree - g.degree)) .reversed() .reduce( (0, f) ) { (result: (Polynomial<K>, Polynomial<K>), degree: Int) in let (q, r) = result let m = eucDivMonomial(r, g) return (q + m.q, m.r) } } }
  • 31. public func gcd<R: EuclideanRing>(_ a: R, _ b: R) -> R { switch b { case 0: return a default: return gcd(b, a % b) } }
  • 32.
  • 33.
  • 34. let a = sqrt(2.0) // 1.41421356… a * a == 2.0 // false