SlideShare une entreprise Scribd logo
1  sur  42
Télécharger pour lire hors ligne
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 で数学のススメ 〜 プログラミングと数学は同時に学べ

Contenu connexe

Tendances

パターン認識 第10章 決定木
パターン認識 第10章 決定木 パターン認識 第10章 決定木
パターン認識 第10章 決定木
Miyoshi Yuya
 
Djangoアプリの実践的設計手法
Djangoアプリの実践的設計手法Djangoアプリの実践的設計手法
Djangoアプリの実践的設計手法
Ian Lewis
 

Tendances (20)

Deep Learningを用いたロボット制御
Deep Learningを用いたロボット制御Deep Learningを用いたロボット制御
Deep Learningを用いたロボット制御
 
パターン認識 第10章 決定木
パターン認識 第10章 決定木 パターン認識 第10章 決定木
パターン認識 第10章 決定木
 
何となく勉強した気分になれるパーサ入門
何となく勉強した気分になれるパーサ入門何となく勉強した気分になれるパーサ入門
何となく勉強した気分になれるパーサ入門
 
各言語の k-means 比較
各言語の k-means 比較各言語の k-means 比較
各言語の k-means 比較
 
楽しい研究のために今からできること 〜新しく研究を始める皆さんへ〜
楽しい研究のために今からできること 〜新しく研究を始める皆さんへ〜楽しい研究のために今からできること 〜新しく研究を始める皆さんへ〜
楽しい研究のために今からできること 〜新しく研究を始める皆さんへ〜
 
国際会議運営記
国際会議運営記国際会議運営記
国際会議運営記
 
Djangoアプリの実践的設計手法
Djangoアプリの実践的設計手法Djangoアプリの実践的設計手法
Djangoアプリの実践的設計手法
 
形式手法と AWS のおいしい関係。- モデル検査器 Alloy によるインフラ設計技法 #jawsfesta
形式手法と AWS のおいしい関係。- モデル検査器 Alloy によるインフラ設計技法 #jawsfesta形式手法と AWS のおいしい関係。- モデル検査器 Alloy によるインフラ設計技法 #jawsfesta
形式手法と AWS のおいしい関係。- モデル検査器 Alloy によるインフラ設計技法 #jawsfesta
 
『自由エネルギー原理入門』勉強会1章&2章前半
『自由エネルギー原理入門』勉強会1章&2章前半『自由エネルギー原理入門』勉強会1章&2章前半
『自由エネルギー原理入門』勉強会1章&2章前半
 
データベース設計徹底指南
データベース設計徹底指南データベース設計徹底指南
データベース設計徹底指南
 
Fantastic DSL in Python
Fantastic DSL in PythonFantastic DSL in Python
Fantastic DSL in Python
 
ScrapyとPhantomJSを用いたスクレイピングDSL
ScrapyとPhantomJSを用いたスクレイピングDSLScrapyとPhantomJSを用いたスクレイピングDSL
ScrapyとPhantomJSを用いたスクレイピングDSL
 
機械学習と機械発見:自然科学融合が誘起するデータ科学の新展開
機械学習と機械発見:自然科学融合が誘起するデータ科学の新展開機械学習と機械発見:自然科学融合が誘起するデータ科学の新展開
機械学習と機械発見:自然科学融合が誘起するデータ科学の新展開
 
正規表現と正規言語
正規表現と正規言語正規表現と正規言語
正規表現と正規言語
 
数学プログラムを Haskell で書くべき 6 の理由
数学プログラムを Haskell で書くべき 6 の理由数学プログラムを Haskell で書くべき 6 の理由
数学プログラムを Haskell で書くべき 6 の理由
 
SQLアンチパターン - 開発者を待ち受ける25の落とし穴 (拡大版)
SQLアンチパターン - 開発者を待ち受ける25の落とし穴 (拡大版)SQLアンチパターン - 開発者を待ち受ける25の落とし穴 (拡大版)
SQLアンチパターン - 開発者を待ち受ける25の落とし穴 (拡大版)
 
For Power BI Beginners
For Power BI BeginnersFor Power BI Beginners
For Power BI Beginners
 
Tackling Complexity
Tackling ComplexityTackling Complexity
Tackling Complexity
 
qubitによる波動関数の虚時間発展のシミュレーション: a review
qubitによる波動関数の虚時間発展のシミュレーション: a reviewqubitによる波動関数の虚時間発展のシミュレーション: a review
qubitによる波動関数の虚時間発展のシミュレーション: a review
 
圏論のモナドとHaskellのモナド
圏論のモナドとHaskellのモナド圏論のモナドとHaskellのモナド
圏論のモナドとHaskellのモナド
 

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

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
lunfu zhong
 
Делаем пользовательское 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
Eelco Visser
 

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

Cocoaheads Meetup / Alex Zimin / Swift magic
Cocoaheads Meetup / Alex Zimin / Swift magicCocoaheads Meetup / Alex Zimin / Swift magic
Cocoaheads Meetup / Alex Zimin / Swift magic
 
Александр Зимин (Alexander Zimin) — Магия Swift
Александр Зимин (Alexander Zimin) — Магия SwiftАлександр Зимин (Alexander Zimin) — Магия Swift
Александр Зимин (Alexander Zimin) — Магия Swift
 
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.
 

Plus de 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
 
何もないところから数を作る
何もないところから数を作る何もないところから数を作る
何もないところから数を作る
Taketo Sano
 
「数える」とは何か? 〜 「とは何か?」を問う、AI時代の数学
「数える」とは何か? 〜 「とは何か?」を問う、AI時代の数学「数える」とは何か? 〜 「とは何か?」を問う、AI時代の数学
「数える」とは何か? 〜 「とは何か?」を問う、AI時代の数学
Taketo Sano
 
2015 02-18 xxx-literalconvertible
2015 02-18 xxx-literalconvertible2015 02-18 xxx-literalconvertible
2015 02-18 xxx-literalconvertible
Taketo Sano
 

Plus de 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
 
プログラマのための線形代数再入門
プログラマのための線形代数再入門プログラマのための線形代数再入門
プログラマのための線形代数再入門
 

Dernier

Dernier (20)

Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 

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