From 605240a8e17cbd0e290ce08060a5153d1c3e3c66 Mon Sep 17 00:00:00 2001 From: sonson Date: Wed, 15 Mar 2017 01:02:53 +0900 Subject: [PATCH] =?UTF-8?q?Removed=20=E2=80=98Release=E2=80=99=20folder.?= =?UTF-8?q?=20Let=20=E2=80=98Release=E2=80=99=20folder=20be=20ignored.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + .../Main.playgroundchapter/Manifest.plist | 14 -- .../Pages/numsw.playgroundpage/Contents.swift | 68 ---------- .../Pages/numsw.playgroundpage/Manifest.plist | 14 -- .../Contents/Manifest.plist | 22 --- .../Contents/Resources/numsw.png | Bin 16400 -> 0 bytes .../Contents/Sources/numsw/Matrix.swift | 74 ----------- .../Sources/numsw/MatrixAddition.swift | 95 ------------- .../Contents/Sources/numsw/MatrixDivide.swift | 14 -- .../Contents/Sources/numsw/MatrixInvert.swift | 14 -- .../Sources/numsw/MatrixLogarithm.swift | 39 ------ .../Sources/numsw/MatrixMultiply.swift | 66 --------- .../Contents/Sources/numsw/MatrixNorm.swift | 23 ---- .../Contents/Sources/numsw/MatrixRandom.swift | 25 ---- .../Contents/Sources/numsw/MatrixRange.swift | 26 ---- .../Sources/numsw/MatrixSubscript.swift | 57 -------- .../Sources/numsw/MatrixTranspose.swift | 14 -- .../Sources/numsw/MatrixTrigonometric.swift | 33 ----- .../Sources/playgrounds/AxisRenderer.swift | 105 --------------- .../Contents/Sources/playgrounds/Chart.swift | 31 ----- .../Sources/playgrounds/ChartBuilder.swift | 27 ---- .../Sources/playgrounds/ChartElement.swift | 23 ---- .../Sources/playgrounds/ChartRenderer.swift | 45 ------- .../playgrounds/CompositeRenderer.swift | 25 ---- .../Sources/playgrounds/DummyData.swift | 109 --------------- .../Sources/playgrounds/LineGraph.swift | 21 --- .../playgrounds/LineGraphRenderer.swift | 84 ------------ .../Sources/playgrounds/NumswPlayground.swift | 110 --------------- .../NumswPlayground_Playground.swift | 18 --- .../RenderScrollViewController.swift | 77 ----------- .../RenderTableViewController.swift | 125 ------------------ .../Sources/playgrounds/Renderer.swift | 27 ---- .../RendererDebugViewController.swift | 53 -------- .../Sources/playgrounds/RendererUtil.swift | 119 ----------------- .../Sources/playgrounds/ScatterGraph.swift | 21 --- .../playgrounds/ScatterGraphRenderer.swift | 46 ------- Release/numsw-xcode.playground/Contents.swift | 62 --------- .../Sources/numsw/Matrix.swift | 74 ----------- .../Sources/numsw/MatrixAddition.swift | 95 ------------- .../Sources/numsw/MatrixDivide.swift | 14 -- .../Sources/numsw/MatrixInvert.swift | 14 -- .../Sources/numsw/MatrixLogarithm.swift | 39 ------ .../Sources/numsw/MatrixMultiply.swift | 66 --------- .../Sources/numsw/MatrixNorm.swift | 23 ---- .../Sources/numsw/MatrixRandom.swift | 25 ---- .../Sources/numsw/MatrixRange.swift | 26 ---- .../Sources/numsw/MatrixSubscript.swift | 57 -------- .../Sources/numsw/MatrixTranspose.swift | 14 -- .../Sources/numsw/MatrixTrigonometric.swift | 33 ----- .../Sources/playgrounds/AxisRenderer.swift | 105 --------------- .../Sources/playgrounds/Chart.swift | 31 ----- .../Sources/playgrounds/ChartBuilder.swift | 27 ---- .../Sources/playgrounds/ChartElement.swift | 23 ---- .../Sources/playgrounds/ChartRenderer.swift | 45 ------- .../playgrounds/CompositeRenderer.swift | 25 ---- .../Sources/playgrounds/DummyData.swift | 109 --------------- .../Sources/playgrounds/LineGraph.swift | 21 --- .../playgrounds/LineGraphRenderer.swift | 84 ------------ .../Sources/playgrounds/NumswPlayground.swift | 110 --------------- .../NumswPlayground_Playground.swift | 18 --- .../RenderScrollViewController.swift | 77 ----------- .../RenderTableViewController.swift | 125 ------------------ .../Sources/playgrounds/Renderer.swift | 27 ---- .../RendererDebugViewController.swift | 53 -------- .../Sources/playgrounds/RendererUtil.swift | 119 ----------------- .../Sources/playgrounds/ScatterGraph.swift | 21 --- .../playgrounds/ScatterGraphRenderer.swift | 46 ------- .../contents.xcplayground | 4 - 68 files changed, 1 insertion(+), 3276 deletions(-) delete mode 100755 Release/numsw-ipad.playgroundbook/Contents/Chapters/Main.playgroundchapter/Manifest.plist delete mode 100755 Release/numsw-ipad.playgroundbook/Contents/Chapters/Main.playgroundchapter/Pages/numsw.playgroundpage/Contents.swift delete mode 100755 Release/numsw-ipad.playgroundbook/Contents/Chapters/Main.playgroundchapter/Pages/numsw.playgroundpage/Manifest.plist delete mode 100755 Release/numsw-ipad.playgroundbook/Contents/Manifest.plist delete mode 100755 Release/numsw-ipad.playgroundbook/Contents/Resources/numsw.png delete mode 100644 Release/numsw-ipad.playgroundbook/Contents/Sources/numsw/Matrix.swift delete mode 100644 Release/numsw-ipad.playgroundbook/Contents/Sources/numsw/MatrixAddition.swift delete mode 100644 Release/numsw-ipad.playgroundbook/Contents/Sources/numsw/MatrixDivide.swift delete mode 100644 Release/numsw-ipad.playgroundbook/Contents/Sources/numsw/MatrixInvert.swift delete mode 100644 Release/numsw-ipad.playgroundbook/Contents/Sources/numsw/MatrixLogarithm.swift delete mode 100644 Release/numsw-ipad.playgroundbook/Contents/Sources/numsw/MatrixMultiply.swift delete mode 100644 Release/numsw-ipad.playgroundbook/Contents/Sources/numsw/MatrixNorm.swift delete mode 100644 Release/numsw-ipad.playgroundbook/Contents/Sources/numsw/MatrixRandom.swift delete mode 100644 Release/numsw-ipad.playgroundbook/Contents/Sources/numsw/MatrixRange.swift delete mode 100644 Release/numsw-ipad.playgroundbook/Contents/Sources/numsw/MatrixSubscript.swift delete mode 100644 Release/numsw-ipad.playgroundbook/Contents/Sources/numsw/MatrixTranspose.swift delete mode 100644 Release/numsw-ipad.playgroundbook/Contents/Sources/numsw/MatrixTrigonometric.swift delete mode 100644 Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/AxisRenderer.swift delete mode 100644 Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/Chart.swift delete mode 100644 Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/ChartBuilder.swift delete mode 100644 Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/ChartElement.swift delete mode 100644 Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/ChartRenderer.swift delete mode 100644 Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/CompositeRenderer.swift delete mode 100755 Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/DummyData.swift delete mode 100755 Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/LineGraph.swift delete mode 100644 Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/LineGraphRenderer.swift delete mode 100644 Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/NumswPlayground.swift delete mode 100644 Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/NumswPlayground_Playground.swift delete mode 100644 Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/RenderScrollViewController.swift delete mode 100755 Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/RenderTableViewController.swift delete mode 100755 Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/Renderer.swift delete mode 100644 Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/RendererDebugViewController.swift delete mode 100644 Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/RendererUtil.swift delete mode 100644 Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/ScatterGraph.swift delete mode 100644 Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/ScatterGraphRenderer.swift delete mode 100644 Release/numsw-xcode.playground/Contents.swift delete mode 100644 Release/numsw-xcode.playground/Sources/numsw/Matrix.swift delete mode 100644 Release/numsw-xcode.playground/Sources/numsw/MatrixAddition.swift delete mode 100644 Release/numsw-xcode.playground/Sources/numsw/MatrixDivide.swift delete mode 100644 Release/numsw-xcode.playground/Sources/numsw/MatrixInvert.swift delete mode 100644 Release/numsw-xcode.playground/Sources/numsw/MatrixLogarithm.swift delete mode 100644 Release/numsw-xcode.playground/Sources/numsw/MatrixMultiply.swift delete mode 100644 Release/numsw-xcode.playground/Sources/numsw/MatrixNorm.swift delete mode 100644 Release/numsw-xcode.playground/Sources/numsw/MatrixRandom.swift delete mode 100644 Release/numsw-xcode.playground/Sources/numsw/MatrixRange.swift delete mode 100644 Release/numsw-xcode.playground/Sources/numsw/MatrixSubscript.swift delete mode 100644 Release/numsw-xcode.playground/Sources/numsw/MatrixTranspose.swift delete mode 100644 Release/numsw-xcode.playground/Sources/numsw/MatrixTrigonometric.swift delete mode 100644 Release/numsw-xcode.playground/Sources/playgrounds/AxisRenderer.swift delete mode 100644 Release/numsw-xcode.playground/Sources/playgrounds/Chart.swift delete mode 100644 Release/numsw-xcode.playground/Sources/playgrounds/ChartBuilder.swift delete mode 100644 Release/numsw-xcode.playground/Sources/playgrounds/ChartElement.swift delete mode 100644 Release/numsw-xcode.playground/Sources/playgrounds/ChartRenderer.swift delete mode 100644 Release/numsw-xcode.playground/Sources/playgrounds/CompositeRenderer.swift delete mode 100755 Release/numsw-xcode.playground/Sources/playgrounds/DummyData.swift delete mode 100755 Release/numsw-xcode.playground/Sources/playgrounds/LineGraph.swift delete mode 100644 Release/numsw-xcode.playground/Sources/playgrounds/LineGraphRenderer.swift delete mode 100644 Release/numsw-xcode.playground/Sources/playgrounds/NumswPlayground.swift delete mode 100644 Release/numsw-xcode.playground/Sources/playgrounds/NumswPlayground_Playground.swift delete mode 100644 Release/numsw-xcode.playground/Sources/playgrounds/RenderScrollViewController.swift delete mode 100755 Release/numsw-xcode.playground/Sources/playgrounds/RenderTableViewController.swift delete mode 100755 Release/numsw-xcode.playground/Sources/playgrounds/Renderer.swift delete mode 100644 Release/numsw-xcode.playground/Sources/playgrounds/RendererDebugViewController.swift delete mode 100644 Release/numsw-xcode.playground/Sources/playgrounds/RendererUtil.swift delete mode 100644 Release/numsw-xcode.playground/Sources/playgrounds/ScatterGraph.swift delete mode 100644 Release/numsw-xcode.playground/Sources/playgrounds/ScatterGraphRenderer.swift delete mode 100644 Release/numsw-xcode.playground/contents.xcplayground diff --git a/.gitignore b/.gitignore index 4a509c6..39d906a 100644 --- a/.gitignore +++ b/.gitignore @@ -69,3 +69,4 @@ fastlane/test_output .DS_Store \#* *~ +/Release \ No newline at end of file diff --git a/Release/numsw-ipad.playgroundbook/Contents/Chapters/Main.playgroundchapter/Manifest.plist b/Release/numsw-ipad.playgroundbook/Contents/Chapters/Main.playgroundchapter/Manifest.plist deleted file mode 100755 index 3626278..0000000 --- a/Release/numsw-ipad.playgroundbook/Contents/Chapters/Main.playgroundchapter/Manifest.plist +++ /dev/null @@ -1,14 +0,0 @@ - - - - - Version - 1.0 - Name - Main Chapter - Pages - - numsw.playgroundpage - - - diff --git a/Release/numsw-ipad.playgroundbook/Contents/Chapters/Main.playgroundchapter/Pages/numsw.playgroundpage/Contents.swift b/Release/numsw-ipad.playgroundbook/Contents/Chapters/Main.playgroundchapter/Pages/numsw.playgroundpage/Contents.swift deleted file mode 100755 index bfd745f..0000000 --- a/Release/numsw-ipad.playgroundbook/Contents/Chapters/Main.playgroundchapter/Pages/numsw.playgroundpage/Contents.swift +++ /dev/null @@ -1,68 +0,0 @@ -//#-code-completion(module, hide, Swift) -//#-code-completion(identifier, hide, _setup()) -//#-code-completion(identifier, hide, AbstractPointChartable) -//#-hidden-code -NumswPlayground.initialize() -//#-end-hidden-code -//#-editable-code Tap to enter code. - -do { - let t = Matrix.range(from: 0, to: 10, stride: 0.01) - let siny = sin(t*64) - - addLine(x: t.elements, y: siny.elements) -} -do { - let t = Matrix.range(from: 0.1, to: 10, stride: 0.01) - let siny = log(t*1.5) - addLine(x: t.elements, y: siny.elements) -} -do { - let t = Matrix.range(from: -10, to: 10, stride: 0.01) - let siny = tan(t*0.2) - addLine(x: t.elements, y: siny.elements) -} - -do { - // create dummy data - let x = Matrix(rows: 2, columns: 6, elements: [1, 2, 3, 4, 5, 6, 1, 1, 1, 1, 1, 1]) - let noise = Matrix.normal(rows: 1, columns: 6) - let x1 = Matrix(rows: 1, columns: 6, elements: [1, 2, 3, 4, 5, 6]) - - // data with noise - let yn = 6 * x1 + 10 + noise - - // linear regression - let xx = x * x.transposed() - let a = xx.inverted() - let b = x.transposed() * a - let A = yn * b - - // data for presentation - let x_p = Matrix.range(from: 0, to: 6, stride: 0.1) - let y_p = A[0, 0] * x_p + A[0, 1] - - // rendering - addLine2(x: x_p.elements, y: y_p.elements, x2: x.elements, y2: yn.elements) -} - -do { - let x = Matrix(rows: 2, columns: 6, elements: [1, 2, 5, 9, 13, 15, 1, 1, 1, 1, 1, 1]) - let y = Matrix(rows: 1, columns: 6, elements: [1, 2, 3, 5, 10, 50]) - - let xx = x * x.transposed() - x.transposed().show() - - let a = xx.inverted() - let logy = log(y) - - let b = x.transposed() * a - let A = logy * b - - let x_p = Matrix.range(from: 0, to: 45, stride: 0.1) - let y_p = exp(A[0, 0] * x_p + A[0, 1]) - - addLine2(x: x_p.elements, y: y_p.elements, x2: x.elements, y2: y.elements) -} - -//#-end-editable-code diff --git a/Release/numsw-ipad.playgroundbook/Contents/Chapters/Main.playgroundchapter/Pages/numsw.playgroundpage/Manifest.plist b/Release/numsw-ipad.playgroundbook/Contents/Chapters/Main.playgroundchapter/Pages/numsw.playgroundpage/Manifest.plist deleted file mode 100755 index cfff98a..0000000 --- a/Release/numsw-ipad.playgroundbook/Contents/Chapters/Main.playgroundchapter/Pages/numsw.playgroundpage/Manifest.plist +++ /dev/null @@ -1,14 +0,0 @@ - - - - - Version - 1.0 - Name - numsw - LiveViewMode - VisibleByDefault - PlaygroundLoggingMode - Off - - diff --git a/Release/numsw-ipad.playgroundbook/Contents/Manifest.plist b/Release/numsw-ipad.playgroundbook/Contents/Manifest.plist deleted file mode 100755 index 6fd66b3..0000000 --- a/Release/numsw-ipad.playgroundbook/Contents/Manifest.plist +++ /dev/null @@ -1,22 +0,0 @@ - - - - - Version - 1.0 - Name - numsw - ContentIdentifier - com.sonson.numsw - ContentVersion - 1.0 - ImageReference - numsw.png - DeploymentTarget - ios10.0 - Chapters - - Main.playgroundchapter - - - diff --git a/Release/numsw-ipad.playgroundbook/Contents/Resources/numsw.png b/Release/numsw-ipad.playgroundbook/Contents/Resources/numsw.png deleted file mode 100755 index 8b85c383d9e7fb8b53f749a2d6f5e5dc2bb503e9..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 16400 zcmeHu1y5bi*L9KNQlPk7arcWCcemnR^y2RB6xWLscP;Mj?(TMRcV2$U^C{l^lQ}ub z$;r%~HMZAYd%_jvC6E#D5Wal*f-EH|s{G{(#GlWH9uDU7%UR=o&*uxmQCZ^Wm&yr( z<1b$XN2ElBRox)YK=7FaYM{Zg7V{reaPUJl=VX8UBH(boefuUU6fdON^%WWtS`KoE z6h3s25$`*b=(JtAP?Lz?UT+d9#Md}{R3T_cYPw2B5@_^F0=Bf{%D0dADm_<)!lI(0 z)){v!7eI=IK0& zo;w~`s&MKpcA@<(@`dmaiIp^FJa~!;iO2~I!|E)oGev)=`p85mry1&f59Iqnv?(4E zLR$Ol-H2M|ubjIfKn{3A9^H386boI8*~?yQbvMn-Lk$L+6boQJlWxq-&-EYC_?Kez zDC#Fh0N-ebg^Jb?J8G+)RB`LPA8uX*%Nqjpt#vfB48TVz*HrzEOq>_;e9$erJ4YDvwMWGNnt= zo~U}3MA>6$sQ~Gd^|0DBTaHOP6H;~@S^6ug0sXNopPaVKLlr%mE}@H zWW4r&W4#%jm;fIJQ%wta1>ZcJ~NFP&- zj7TU`%9anH$3&@eG%_qEMsynbV9-g3yYO^cGK&Ap6qGH!X8(Sj zn^FkBTB+)r&`H0!Q1pr%SBiaI=9VWyt<)bZX2sWYCP6OKChLRx@Mm&wzLsKw*3bB5niZ_o;hhvd*+Y!d(SGKH=d=htg*ZJ$w@lvUY(}NS||zI zkhYxL$+QrFKdM)Gs6*@OGx{v7Z@XXLt!T<-i`<+Yya!nS>OrQZQu37VbqYQ|Esix) zFNy}_ROp?{VxvMZ8us`8dsn`T$$m~KN@hhQKSVkTi|kuVCs*va+|c0}Tj%?%`v8|y zEkG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matrix must have elements.") - let columns = elements[0].count - let elements = elements.flatMap { $0 } - precondition(elements.count == rows*columns, "All rows must have same size.") - self.init(rows: rows, columns: columns, elements: elements) - } - - public static func zeros(rows: Int, columns: Int) -> Matrix { - let count = columns * rows - let cElements = UnsafeMutablePointer.allocate(capacity: count) - defer { cElements.deallocate(capacity: count) } - for i in 0.. Matrix { - let count = columns * rows - let cElements = UnsafeMutablePointer.allocate(capacity: count) - defer { cElements.deallocate(capacity: count) } - for i in 0.. Matrix { - var matrix = Matrix.zeros(rows: size, columns: size) - for i in 0.. Matrix { - assert(rows * columns == self.rows * self.columns, "Element count must be unchanged.") - - return Matrix(rows: rows, columns: columns, elements: self.elements) - } -} diff --git a/Release/numsw-ipad.playgroundbook/Contents/Sources/numsw/MatrixAddition.swift b/Release/numsw-ipad.playgroundbook/Contents/Sources/numsw/MatrixAddition.swift deleted file mode 100644 index db8ecf9..0000000 --- a/Release/numsw-ipad.playgroundbook/Contents/Sources/numsw/MatrixAddition.swift +++ /dev/null @@ -1,95 +0,0 @@ -// -// MatrixAddition.swift -// numsw -// -// Created by sonson on 2017/03/04. -// -// - -import Foundation -import Accelerate - -// MARK: - Addition - -public func add(_ lhs: Matrix, rhs: Double) -> Matrix { - let newElements = lhs.elements.map { (value) -> Double in - return value + rhs - } - return Matrix(rows: lhs.rows, columns: lhs.columns, elements: Array(UnsafeBufferPointer(start: newElements, count: lhs.count))) -} - -public func add(_ lhs: Matrix, rhs: Matrix) -> Matrix { - precondition(lhs.rows == rhs.rows && lhs.columns == rhs.columns, "Matrix dimensions not compatible with addition") - - let cElements = UnsafeMutablePointer.allocate(capacity: lhs.count) - defer { cElements.deallocate(capacity: lhs.count) } - - memcpy(cElements, rhs.elements, rhs.count * MemoryLayout.size) - - cblas_daxpy(Int32(lhs.count), 1.0, lhs.elements, 1, cElements, 1) - - return Matrix(rows: lhs.rows, columns: lhs.columns, elements: Array(UnsafeBufferPointer(start: cElements, count: lhs.count))) -} - -public func += (lhs: inout Matrix, rhs: Double) { - lhs = lhs + rhs -} - -public func += (lhs: inout Matrix, rhs: Matrix) { - lhs = lhs + rhs -} - -public func + (lhs: Matrix, rhs: Double) -> Matrix { - return add(lhs, rhs: rhs) -} - -public func + (lhs: Matrix, rhs: Matrix) -> Matrix { - return add(lhs, rhs: rhs) -} - -// MARK: - Subtraction - -public func subtract(_ lhs: Matrix, rhs: Double) -> Matrix { - let newElements = lhs.elements.map { (value) -> Double in - return value - rhs - } - return Matrix(rows: lhs.rows, columns: lhs.columns, elements: Array(UnsafeBufferPointer(start: newElements, count: lhs.count))) -} - -public func subtract(_ lhs: Matrix, rhs: Matrix) -> Matrix { - precondition(lhs.rows == rhs.rows && lhs.columns == rhs.columns, "Matrix dimensions not compatible with addition") - - let cElements = UnsafeMutablePointer.allocate(capacity: lhs.count) - defer { cElements.deallocate(capacity: lhs.count) } - - memcpy(cElements, lhs.elements, lhs.count * MemoryLayout.size) - - cblas_daxpy(Int32(lhs.count), -1.0, rhs.elements, 1, cElements, 1) - - return Matrix(rows: lhs.rows, columns: lhs.columns, elements: Array(UnsafeBufferPointer(start: cElements, count: lhs.count))) -} - -public func -= (lhs: inout Matrix, rhs: Double) { - lhs = lhs - rhs -} - -public func -= (lhs: inout Matrix, rhs: Matrix) { - lhs = lhs - rhs -} - -public func - (lhs: Matrix, rhs: Double) -> Matrix { - return subtract(lhs, rhs: rhs) -} - -public func - (lhs: Matrix, rhs: Matrix) -> Matrix { - return subtract(lhs, rhs: rhs) -} - -// MARK: - Prefix - -public prefix func - (lhs: Matrix) -> Matrix { - let newElements = lhs.elements.map { (value) -> Double in - return -value - } - return Matrix(rows: lhs.rows, columns: lhs.columns, elements: Array(UnsafeBufferPointer(start: newElements, count: lhs.count))) -} diff --git a/Release/numsw-ipad.playgroundbook/Contents/Sources/numsw/MatrixDivide.swift b/Release/numsw-ipad.playgroundbook/Contents/Sources/numsw/MatrixDivide.swift deleted file mode 100644 index b69adf3..0000000 --- a/Release/numsw-ipad.playgroundbook/Contents/Sources/numsw/MatrixDivide.swift +++ /dev/null @@ -1,14 +0,0 @@ -import Accelerate - -// scalar -public func /(lhs: Matrix, rhs: Double) -> Matrix { - return Matrix(rows: lhs.rows, columns: lhs.columns, elements: lhs.elements.map { $0/rhs }) -} - -public func /(lhs: Double, rhs: Matrix) -> Matrix { - return Matrix(rows: rhs.rows, columns: rhs.columns, elements: rhs.elements.map { lhs/$0 }) -} - -public func /=(lhs: inout Matrix, rhs: Double) { - lhs = lhs / rhs -} diff --git a/Release/numsw-ipad.playgroundbook/Contents/Sources/numsw/MatrixInvert.swift b/Release/numsw-ipad.playgroundbook/Contents/Sources/numsw/MatrixInvert.swift deleted file mode 100644 index 57f6c6e..0000000 --- a/Release/numsw-ipad.playgroundbook/Contents/Sources/numsw/MatrixInvert.swift +++ /dev/null @@ -1,14 +0,0 @@ -import Accelerate - -extension Matrix { - public func inverted() -> Matrix { - var inMatrix: [Double] = self.elements - var N: __CLPK_integer = __CLPK_integer( sqrt( Double( self.rows*self.columns ) ) ) - var pivots: [__CLPK_integer] = [__CLPK_integer](repeating: 0, count: Int(N)) - var workspace: [Double] = [Double](repeating: 0.0, count: Int(N)) - var error: __CLPK_integer = 0 - dgetrf_(&N, &N, &inMatrix, &N, &pivots, &error) - dgetri_(&N, &inMatrix, &N, &pivots, &workspace, &N, &error) - return Matrix(rows: self.rows, columns: self.columns, elements: inMatrix) - } -} diff --git a/Release/numsw-ipad.playgroundbook/Contents/Sources/numsw/MatrixLogarithm.swift b/Release/numsw-ipad.playgroundbook/Contents/Sources/numsw/MatrixLogarithm.swift deleted file mode 100644 index 3e1cb69..0000000 --- a/Release/numsw-ipad.playgroundbook/Contents/Sources/numsw/MatrixLogarithm.swift +++ /dev/null @@ -1,39 +0,0 @@ -// -// MatrixLogarithm.swift -// numsw -// -// Created by sonson on 2017/03/04. -// -// - -import Accelerate - -public func exp(_ x: Matrix) -> Matrix { - var result = Matrix.zeros(rows: x.rows, columns: x.columns) - vvexp(&result.elements, x.elements, [Int32(x.count)]) - return result -} - -public func exp2(_ x: Matrix) -> Matrix { - var result = Matrix.zeros(rows: x.rows, columns: x.columns) - vvexp2(&result.elements, x.elements, [Int32(x.count)]) - return result -} - -public func log(_ x: Matrix) -> Matrix { - var result = Matrix.zeros(rows: x.rows, columns: x.columns) - vvlog(&result.elements, x.elements, [Int32(x.count)]) - return result -} - -public func log10(_ x: Matrix) -> Matrix { - var result = Matrix.zeros(rows: x.rows, columns: x.columns) - vvlog10(&result.elements, x.elements, [Int32(x.count)]) - return result -} - -//public func logb(_ x: Matrix) -> Matrix { -// var result = Matrix.zeros(rows: x.rows, columns: x.columns) -// vvlogb(&result.elements, x.elements, [Int32(x.count)]) -// return result -//} diff --git a/Release/numsw-ipad.playgroundbook/Contents/Sources/numsw/MatrixMultiply.swift b/Release/numsw-ipad.playgroundbook/Contents/Sources/numsw/MatrixMultiply.swift deleted file mode 100644 index 4d02707..0000000 --- a/Release/numsw-ipad.playgroundbook/Contents/Sources/numsw/MatrixMultiply.swift +++ /dev/null @@ -1,66 +0,0 @@ -import Accelerate - -// scalar - -public func *(lhs: Matrix, rhs: Double) -> Matrix { - return Matrix(rows: lhs.rows, columns: lhs.columns, elements: lhs.elements.map { $0*rhs }) -} - -public func *(lhs: Double, rhs: Matrix) -> Matrix { - return rhs * lhs -} - -public func *=(lhs: inout Matrix, rhs: Double) { - lhs = lhs * rhs -} - -// matmul - -public func *(lhs: Matrix, rhs: Matrix) -> Matrix { - precondition(lhs.columns == rhs.rows, "Matrices can't multiply.") - let m = lhs.rows - let n = rhs.columns - let k = lhs.columns - let lda = k - let ldb = n - let cElements = UnsafeMutablePointer.allocate(capacity: m*n) - defer { cElements.deallocate(capacity: m*n) } - - cblas_dgemm( - CblasRowMajor, // Order - CblasNoTrans, // TransA - CblasNoTrans, // TransB - Int32(m), // M - Int32(n), // N - Int32(k), // K - 1.0, // alpha - lhs.elements, // A - Int32(lda), // lda - rhs.elements, // B - Int32(ldb), // ldb - 0.0, // beta - cElements, // C - Int32(n) // ldc - ) - return Matrix(rows: m, - columns: n, - elements: Array(UnsafeBufferPointer(start: cElements, count: m*n))) -} - -// element wise multiply - -infix operator .* -public func .*(lhs: Matrix, rhs: Matrix) -> Matrix { - let pointer = UnsafeMutablePointer.allocate(capacity: lhs.count) - defer { pointer.deallocate(capacity: lhs.count) } - vDSP_vmulD(lhs.elements, 1, rhs.elements, 1, pointer, 1, vDSP_Length(lhs.count)) - - return Matrix(rows: lhs.rows, - columns: lhs.columns, - elements: Array(UnsafeBufferPointer(start: pointer, count: lhs.count))) -} - -infix operator .*= -public func .*=(lhs: inout Matrix, rhs: Matrix) { - lhs = lhs .* rhs -} diff --git a/Release/numsw-ipad.playgroundbook/Contents/Sources/numsw/MatrixNorm.swift b/Release/numsw-ipad.playgroundbook/Contents/Sources/numsw/MatrixNorm.swift deleted file mode 100644 index ab0728b..0000000 --- a/Release/numsw-ipad.playgroundbook/Contents/Sources/numsw/MatrixNorm.swift +++ /dev/null @@ -1,23 +0,0 @@ -// -// Matrix_sonson.swift -// numsw -// -// Created by sonson on 2017/03/04. -// -// - -import Foundation - -public func frobeniusNorm(_ hs: Matrix) -> Double { - return hs.elements.reduce(0) { (result, value) -> Double in - return result + value * value - } -} - -extension Matrix { - public var frobeniusNorm: Double { - return self.elements.reduce(0) { (result, value) -> Double in - return result + value * value - } - } -} diff --git a/Release/numsw-ipad.playgroundbook/Contents/Sources/numsw/MatrixRandom.swift b/Release/numsw-ipad.playgroundbook/Contents/Sources/numsw/MatrixRandom.swift deleted file mode 100644 index 2fa5778..0000000 --- a/Release/numsw-ipad.playgroundbook/Contents/Sources/numsw/MatrixRandom.swift +++ /dev/null @@ -1,25 +0,0 @@ -import Foundation - -func _uniform(low: Double = 0, high: Double = 1) -> Double { - return (high-low)*(Double(arc4random_uniform(UInt32.max)) / Double(UInt32.max))+low -} - -func _normal(mu: Double = 0, sigma: Double = 1) -> Double { - let u = (_uniform(), _uniform()) - let x = sqrt(-2*log(u.0)) * cos(2*M_PI*u.1) - return sigma*x + mu -} - -extension Matrix { - public static func uniform(rows: Int, columns: Int, low: Double = 0, high: Double = 1) -> Matrix { - return Matrix(rows: rows, - columns: columns, - elements: (0.. Matrix { - return Matrix(rows: rows, - columns: columns, - elements: (0.. Matrix { - - let elements = Swift.stride(from: from, to: to, by: stride).map(Double.init) - return Matrix(rows: elements.count, - columns: 1, - elements: elements) - } - - public static func range(from: Double, to: Double, stride: Double) -> Matrix { - let elements = Array(Swift.stride(from: from, to: to, by: stride)) - return Matrix(rows: elements.count, - columns: 1, - elements: elements) - } - - public static func linspace(from: Double, to: Double, count: Int) -> Matrix { - precondition(count > 0, "count must be larger than 0") - let elements = (0.. Double in - from + Double(i)*(to-from)/Double(count-1) - } - return Matrix(rows: count, - columns: 1, - elements: elements) - } -} diff --git a/Release/numsw-ipad.playgroundbook/Contents/Sources/numsw/MatrixSubscript.swift b/Release/numsw-ipad.playgroundbook/Contents/Sources/numsw/MatrixSubscript.swift deleted file mode 100644 index a54548d..0000000 --- a/Release/numsw-ipad.playgroundbook/Contents/Sources/numsw/MatrixSubscript.swift +++ /dev/null @@ -1,57 +0,0 @@ -extension Matrix { - public subscript(row: Int, column: Int) -> Double { - get { - return elements[row * columns + column] - } - set { - elements[row * columns + column] = newValue - } - } - - public subscript(row: Int) -> Matrix { - get { - return Matrix(rows: 1, columns: columns, elements: Array(elements[row * columns.. Matrix { - get { - let rs = rs ?? Array(0...allocate(capacity: rs.count * cs.count) - defer { newElements.deallocate(capacity: rs.count * cs.count) } - var pointer = newElements - for r in rs { - for c in cs { - pointer.pointee = self[r, c] - pointer += 1 - } - } - return Matrix(rows: rs.count, - columns: cs.count, - elements: Array(UnsafeBufferPointer(start: newElements, count: rs.count * cs.count))) - } - set { - let rs = rs ?? Array(0.. Matrix { - let newElements = UnsafeMutablePointer.allocate(capacity: rows*columns) - defer { newElements.deallocate(capacity: rows*columns) } - for r in 0.. Matrix { - var result = Matrix.zeros(rows: x.rows, columns: x.columns) - vvsin(&result.elements, x.elements, [Int32(x.count)]) - return result -} - -public func cos(_ x: Matrix) -> Matrix { - var result = Matrix.zeros(rows: x.rows, columns: x.columns) - vvcos(&result.elements, x.elements, [Int32(x.count)]) - return result -} - -public func tan(_ x: Matrix) -> Matrix { - var result = Matrix.zeros(rows: x.rows, columns: x.columns) - vvtan(&result.elements, x.elements, [Int32(x.count)]) - return result -} - -//public func atan2(_ x: Matrix) -> Matrix { -// var result = Matrix.zeros(rows: x.rows, columns: x.columns) -// vvatan2(<#T##UnsafeMutablePointer#>, <#T##UnsafePointer#>, <#T##UnsafePointer#>, <#T##UnsafePointer#>)(&result.elements, x.elements, [Int32(x.count)]) -// return result -//} diff --git a/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/AxisRenderer.swift b/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/AxisRenderer.swift deleted file mode 100644 index 5947a80..0000000 --- a/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/AxisRenderer.swift +++ /dev/null @@ -1,105 +0,0 @@ -// -// ChartRenderer.swift -// sandbox -// -// Created by omochimetaru on 2017/03/06. -// Copyright © 2017年 sonson. All rights reserved. -// - -// xAxis, yAxis, ticks - -import UIKit - -public class AxisRenderer: Renderer { - public init(chart: Chart) { - self.chart = chart - } - - public let chart: Chart - - public func render(context: CGContext, windowSize: CGSize) { - viewportTransform = RendererUtil.computeViewportTransform(viewport: chart.viewport, windowSize: windowSize) - stepSize = computeStepSize() - - drawAxisX(context: context) - drawAxisY(context: context) - } - - private func computeStepSize() -> CGSize { - let size = chart.viewport.size - - let xStep: CGFloat - - if size.width <= 0.0 { - xStep = 0.0 - } else { - let xLog = log10(size.width) - xStep = pow(10.0, round(xLog) - 1) - } - - let yStep: CGFloat - - if size.height <= 0.0 { - yStep = 0.0 - } else { - let yLog = log10(size.height) - yStep = pow(10.0, round(yLog) - 1) - } - - return CGSize(width: xStep, height: yStep) - } - - private func drawAxisX(context ctx: CGContext) { - let p0 = CGPoint(x: chart.viewport.minX, y: 0) - let p1 = CGPoint(x: chart.viewport.maxX, y: 0) - - ctx.setStrokeColor(UIColor.gray.cgColor) - drawLine(context: ctx, points: [p0, p1]) - - // ticks - - let tickXs = RendererUtil.computeTickValues(min: p0.x, max: p1.x, - step: stepSize!.width) - let tickHeight = chart.viewport.height * 0.04 - - for x in tickXs { - drawLine(context: ctx, points: [ - CGPoint(x: x, y: (tickHeight / 2.0)), - CGPoint(x: x, y: -(tickHeight / 2.0)), - ]) - } - } - - private func drawAxisY(context ctx: CGContext) { - let p0 = CGPoint(x: 0, y: chart.viewport.minY) - let p1 = CGPoint(x: 0, y: chart.viewport.maxY) - - ctx.setStrokeColor(UIColor.gray.cgColor) - drawLine(context: ctx, points: [p0, p1]) - - // ticks - - let tickYs = RendererUtil.computeTickValues(min: p0.y, max: p1.y, - step: stepSize!.height) - let tickWidth = chart.viewport.width * 0.04 - - for y in tickYs { - drawLine(context: ctx, points: [ - CGPoint(x: -(tickWidth / 2.0), y: y), - CGPoint(x: (tickWidth / 2.0), y: y), - ]) - } - } - - private func drawLine(context: CGContext, - points: [CGPoint]) { - let t = viewportTransform! - - let points = points.map { $0.applying(t) } - - RendererUtil.drawLine(context: context, points: points) - } - - private var viewportTransform: CGAffineTransform? - private var stepSize: CGSize? -} diff --git a/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/Chart.swift b/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/Chart.swift deleted file mode 100644 index 373644b..0000000 --- a/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/Chart.swift +++ /dev/null @@ -1,31 +0,0 @@ -// -// Chart.swift -// sandbox -// -// Created by omochimetaru on 2017/03/06. -// Copyright © 2017年 sonson. All rights reserved. -// - -import CoreGraphics - -public struct Chart { - public init() { - computeViewport() - } - - public var viewport: CGRect = .zero - public var elements: [ChartElement] = [] - - public mutating func computeViewport() { - guard elements.count != 0 else { - viewport = RendererUtil.adjustViewport(viewport: CGRect.zero) - return - } - - var b = elements.first!.computeBounds() - for e in elements.dropFirst() { - b = b.union(e.computeBounds()) - } - viewport = RendererUtil.adjustViewport(viewport: b) - } -} diff --git a/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/ChartBuilder.swift b/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/ChartBuilder.swift deleted file mode 100644 index 0e468bf..0000000 --- a/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/ChartBuilder.swift +++ /dev/null @@ -1,27 +0,0 @@ -// -// ChartBuilder.swift -// sandbox -// -// Created by omochimetaru on 2017/03/06. -// Copyright © 2017年 sonson. All rights reserved. -// - -import Foundation - -public class ChartBuilder { - public init() { - chart = Chart() - } - - public func addLine(line: LineGraph) { - chart.elements.append(.line(line)) - chart.computeViewport() - } - - public func addScatter(scatter: ScatterGraph) { - chart.elements.append(.scatter(scatter)) - chart.computeViewport() - } - - public var chart: Chart -} diff --git a/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/ChartElement.swift b/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/ChartElement.swift deleted file mode 100644 index 3cbd101..0000000 --- a/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/ChartElement.swift +++ /dev/null @@ -1,23 +0,0 @@ -// -// ChartElement.swift -// sandbox -// -// Created by omochimetaru on 2017/03/06. -// Copyright © 2017年 sonson. All rights reserved. -// - -import CoreGraphics - -public enum ChartElement { - case line(LineGraph) - case scatter(ScatterGraph) - - func computeBounds() -> CGRect { - switch self { - case .line(let line): - return line.computeBounds() - case .scatter(let scatter): - return scatter.computeBounds() - } - } -} diff --git a/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/ChartRenderer.swift b/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/ChartRenderer.swift deleted file mode 100644 index ccf507b..0000000 --- a/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/ChartRenderer.swift +++ /dev/null @@ -1,45 +0,0 @@ -// -// ChartRenderer.swift -// sandbox -// -// Created by omochimetaru on 2017/03/06. -// Copyright © 2017年 sonson. All rights reserved. -// - -import CoreGraphics - -public class ChartRenderer: Renderer { - - public init(chart: Chart) { - self.chart = chart - - update() - } - - public let chart: Chart - - public func render(context: CGContext, windowSize: CGSize) { - compositer?.render(context: context, windowSize: windowSize) - } - - private func update() { - var renderers: [Renderer] = [] - - renderers.append(AxisRenderer(chart: chart)) - - for element in chart.elements { - switch element { - case .line(let line): - renderers.append(LineGraphRenderer(viewport: chart.viewport, line: line)) - case .scatter(let scatter): - renderers.append(ScatterGraphRenderer(viewport: chart.viewport, scatter: scatter)) - } - } - - let r = CompositeRenderer() - self.compositer = r - r.renderers = renderers - } - - private var compositer: CompositeRenderer? -} diff --git a/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/CompositeRenderer.swift b/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/CompositeRenderer.swift deleted file mode 100644 index e478a02..0000000 --- a/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/CompositeRenderer.swift +++ /dev/null @@ -1,25 +0,0 @@ -// -// CompositableRenderer.swift -// sandbox -// -// Created by omochimetaru on 2017/03/06. -// Copyright © 2017年 sonson. All rights reserved. -// - -// its composite multiple renderer as one renderer - -import CoreGraphics - -public class CompositeRenderer: Renderer { - public init() { - - } - - public var renderers: [Renderer] = [] - - public func render(context: CGContext, windowSize: CGSize) { - for renderer in renderers { - renderer.render(context: context, windowSize: windowSize) - } - } -} diff --git a/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/DummyData.swift b/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/DummyData.swift deleted file mode 100755 index 4b092ac..0000000 --- a/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/DummyData.swift +++ /dev/null @@ -1,109 +0,0 @@ -// -// Dummydata.swift -// sandbox -// -// Created by omochimetaru on 2017/03/04. -// Copyright © 2017年 sonson. All rights reserved. -// - -import UIKit - -#if SANDBOX_APP - import numsw -#endif - -public struct DummyData { - public static func points1() -> [CGPoint] { - return [ - CGPoint(x: 0, y: 0), - CGPoint(x: 1, y: 1), - CGPoint(x: 2, y: 4), - CGPoint(x: 3, y: 9), - CGPoint(x: 4, y: 16) - ] - } - - public static func points2() -> [CGPoint] { - return [ - CGPoint(x: 0, y: 6), - CGPoint(x: 2, y: 2), - CGPoint(x: 4, y: 3) - ] - } - - public static func runTestScenario() { - do { - let t = Matrix.range(from: 0, to: 10, stride: 0.01) - let siny = sin(t*64) - - addLine(x: t.elements, y: siny.elements) - } - do { - let t = Matrix.range(from: 0.1, to: 10, stride: 0.01) - let siny = log(t*1.5) - addLine(x: t.elements, y: siny.elements) - } - do { - let t = Matrix.range(from: -10, to: 10, stride: 0.01) - let siny = tan(t*0.2) - addLine(x: t.elements, y: siny.elements) - } - - do { - // create dummy data - let x = Matrix(rows: 2, columns: 6, elements: [1, 2, 3, 4, 5, 6, 1, 1, 1, 1, 1, 1]) - let noise = Matrix.normal(rows: 1, columns: 6) - let x1 = Matrix(rows: 1, columns: 6, elements: [1, 2, 3, 4, 5, 6]) - - // data with noise - let yn = 6 * x1 + 10 + noise - - // linear regression - let xx = x * x.transposed() - let a = xx.inverted() - let b = x.transposed() * a - let A = yn * b - - // data for presentation - let x_p = Matrix.range(from: 0, to: 6, stride: 0.1) - let y_p = A[0, 0] * x_p + A[0, 1] - - // rendering - addLine2(x: x_p.elements, y: y_p.elements, x2: x.elements, y2: yn.elements) - } - - do { - let x = Matrix(rows: 2, columns: 6, elements: [1, 2, 5, 9, 13, 15, 1, 1, 1, 1, 1, 1]) - let y = Matrix(rows: 1, columns: 6, elements: [1, 2, 3, 5, 10, 50]) - - let xx = x * x.transposed() - x.transposed().show() - - let a = xx.inverted() - let logy = log(y) - - let b = x.transposed() * a - let A = logy * b - - let x_p = Matrix.range(from: 0, to: 45, stride: 0.1) - let y_p = exp(A[0, 0] * x_p + A[0, 1]) - - addLine2(x: x_p.elements, y: y_p.elements, x2: x.elements, y2: y.elements) - } - - } - - public static func runHoldExample() { - - let t1 = Matrix.range(from: 0, to: 10, stride: 0.01) - let siny1 = sin(t1 * 2) - - let t2 = Matrix.range(from: 0, to: 10, stride: 0.4) - let siny2 = sin(t2 * 2) - - hold { - plot(t1.elements, siny1.elements) - scatter(t2.elements, siny2.elements) - } - } -} diff --git a/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/LineGraph.swift b/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/LineGraph.swift deleted file mode 100755 index 0c5c29d..0000000 --- a/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/LineGraph.swift +++ /dev/null @@ -1,21 +0,0 @@ -// -// LineData.swift -// sandbox -// -// Created by omochimetaru on 2017/03/04. -// Copyright © 2017年 sonson. All rights reserved. -// - -import CoreGraphics - -public struct LineGraph { - public init(points: [CGPoint]) { - self.points = points - } - - public var points: [CGPoint] - - public func computeBounds() -> CGRect { - return RendererUtil.computeBounds(points: points) - } -} diff --git a/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/LineGraphRenderer.swift b/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/LineGraphRenderer.swift deleted file mode 100644 index 188bd8c..0000000 --- a/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/LineGraphRenderer.swift +++ /dev/null @@ -1,84 +0,0 @@ -// -// LineGraphRenderer.swift -// sandbox -// -// Created by omochimetaru on 2017/03/04. -// Copyright © 2017年 sonson. All rights reserved. -// - -import UIKit -import CoreGraphics - -public class LineGraphRenderer: Renderer { - public init(viewport: CGRect, line: LineGraph) { - self.viewport = viewport - self.line = line - } - - public let viewport: CGRect - public let line: LineGraph - - public func render(context: CGContext, windowSize: CGSize) { - viewportTransform = RendererUtil.computeViewportTransform(viewport: viewport, windowSize: windowSize) - drawLine(context: context, points: line.points) - } - -// public func drawPoints(context ctx: CGContext) { -// ctx.setStrokeColor(UIColor.white.cgColor) -// -// if lines.count >= 1 { -// drawLine(context: ctx, points: lines[0].points) -// } -// if lines.count >= 2 { -// drawSanpuzu(context: ctx, line: lines[1]) -// } -// } - - public func drawSanpuzu(context ctx: CGContext, line: LineGraph) { - ctx.setStrokeColor(UIColor.green.cgColor) - - let t = viewportTransform! - - for point in line.points { - let p = point.applying(t) - - RendererUtil.drawLine(context: ctx, points: [ - CGPoint(x: p.x - 10, y: p.y - 10), - CGPoint(x: p.x + 10, y: p.y + 10) - ]) - RendererUtil.drawLine(context: ctx, points: [ - CGPoint(x: p.x - 10, y: p.y + 10), - CGPoint(x: p.x + 10, y: p.y - 10) - ]) - } - } - - public func drawDebugX(context ctx: CGContext, - point0: CGPoint, - point1: CGPoint) { - ctx.setStrokeColor(UIColor.red.cgColor) - drawLine(context: ctx, points: [ - CGPoint(x: point0.x, y: point0.y), - CGPoint(x: point1.x, y: point1.y) - ]) - - ctx.setStrokeColor(UIColor.green.cgColor) - drawLine(context: ctx, points: [ - CGPoint(x: point1.x, y: point0.y), - CGPoint(x: point0.x, y: point1.y) - ]) - } - - public func drawLine(context: CGContext, - points: [CGPoint]) { - context.setStrokeColor(UIColor.white.cgColor) - - let t = viewportTransform! - - let points = points.map { $0.applying(t) } - - RendererUtil.drawLine(context: context, points: points) - } - - private var viewportTransform: CGAffineTransform? -} diff --git a/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/NumswPlayground.swift b/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/NumswPlayground.swift deleted file mode 100644 index dd468b1..0000000 --- a/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/NumswPlayground.swift +++ /dev/null @@ -1,110 +0,0 @@ -// -// NumswPlayground.swift -// sandbox -// -// Created by omochimetaru on 2017/03/05. -// Copyright © 2017年 sonson. All rights reserved. -// - -import UIKit - -public class NumswPlayground { - internal init() { - viewController = RenderTableViewController() - } - - public let viewController: RenderTableViewController - - public func append(renderer: ChartRenderer) { - renderers.append(renderer) - } - - public func plot(_ x: [Double], _ y: [Double]) { - guard let b = chartBuilder else { - hold { - plot(x, y) - } - return - } - - let points = zip(x, y).map { - CGPoint(x: $0.0, y: $0.1) - } - b.addLine(line: LineGraph(points: points)) - } - - public func scatter(_ x: [Double], _ y: [Double]) { - guard let b = chartBuilder else { - hold { - scatter(x, y) - } - return - } - let points = zip(x, y).map { - CGPoint(x: $0.0, y: $0.1) - } - b.addScatter(scatter: ScatterGraph(points: points)) - } - - public func hold(_ f: () throws -> Void) rethrows { - let b = ChartBuilder() - chartBuilder = b - - try f() - - chartBuilder = nil - - let renderer = ChartRenderer(chart: b.chart) - append(renderer: renderer) - } - - public static var shared: NumswPlayground { - get { - if _shared == nil { - _shared = NumswPlayground() - } - return _shared! - } - } - - private var renderers: [ChartRenderer] = [] { - didSet { - viewController.renderers = renderers.map { $0 as Renderer } - } - } - - private var chartBuilder: ChartBuilder? - - private static var _shared: NumswPlayground? - -} - -public func addLine( - x: [Double], y: [Double]) { - hold { - plot(x, y) - } -} - -public func addLine2( - x: [Double], y: [Double], - x2: [Double], y2: [Double]) { - hold { - plot(x, y) - scatter(x2, y2) - } -} - -public func plot( - _ x: [Double], _ y: [Double]) { - NumswPlayground.shared.plot(x, y) -} - -public func scatter( - _ x: [Double], _ y: [Double]) { - NumswPlayground.shared.scatter(x, y) -} - -public func hold(f: () throws -> Void) rethrows { - try NumswPlayground.shared.hold(f) -} diff --git a/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/NumswPlayground_Playground.swift b/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/NumswPlayground_Playground.swift deleted file mode 100644 index f5540ea..0000000 --- a/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/NumswPlayground_Playground.swift +++ /dev/null @@ -1,18 +0,0 @@ -// -// NumswPlayground_Playground.swift -// sandbox -// -// Created by omochimetaru on 2017/03/05. -// Copyright © 2017年 sonson. All rights reserved. -// - -// This file is not included for iOS app build target - -import PlaygroundSupport - -public extension NumswPlayground { - public static func initialize() { - let s = NumswPlayground.shared - PlaygroundPage.current.liveView = s.viewController - } -} diff --git a/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/RenderScrollViewController.swift b/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/RenderScrollViewController.swift deleted file mode 100644 index 25bb158..0000000 --- a/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/RenderScrollViewController.swift +++ /dev/null @@ -1,77 +0,0 @@ -// Final customized first implementation with UIScrollView - -import UIKit - -public class RenderScrollViewController: UIViewController { - - func makeRenderer() -> ChartRenderer { - let points1 = DummyData.points1() - let points2 = DummyData.points2() - - var chart = Chart() - chart.elements = [ - .line(LineGraph(points: points1)), - .line(LineGraph(points: points2)) - ] - chart.computeViewport() - - return ChartRenderer(chart: chart) - } - - var renderers: [Renderer] = [] - - var scrollView: UIScrollView! - - public func append(renderer: Renderer) { - self.renderers.append(renderer) - } - - public override func viewDidLoad() { - super.viewDidLoad() - - scrollView = UIScrollView() - scrollView.frame = self.view.frame - - self.view.addSubview(scrollView) - } - - func updateViews() { - for view in scrollView.subviews { - view.removeFromSuperview() - } - scrollView.contentSize.height = 0 - self.render() - } - - public override func viewDidLayoutSubviews() { - super.viewDidLayoutSubviews() - scrollView.frame = self.view.frame - updateViews() - } - - public override func viewWillAppear(_ animated: Bool) { - super.viewWillAppear(animated) - self.render() - } - - public override func didReceiveMemoryWarning() { - super.didReceiveMemoryWarning() - } - - func render() { - - var size = self.view.frame.size - size.height *= 0.5 - // renderer 取り出してscrollviewに追加 - for renderer in renderers { - let image = renderer.renderToImage(size: size) - let imageView = UIImageView(image: image) - imageView.frame.size = size - imageView.contentMode = .scaleToFill - imageView.frame.origin = CGPoint(x: 0, y: scrollView.contentSize.height) - scrollView.addSubview(imageView) - scrollView.contentSize.height += size.height - } - } - -} diff --git a/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/RenderTableViewController.swift b/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/RenderTableViewController.swift deleted file mode 100755 index 48547e7..0000000 --- a/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/RenderTableViewController.swift +++ /dev/null @@ -1,125 +0,0 @@ -// -// RenderTableViewController.swift -// sandbox -// -// Created by color_box on 2017/03/04. -// Copyright © 2017年 sonson. All rights reserved. -// - -// Second Implementation with UITableView - -import UIKit - -private class RenderTableViewCell: UITableViewCell { - - var renderer: Renderer? { - willSet { - self.renderImageView.image = nil - self.renderedImageSize = .zero - } - } - - private let renderImageView = UIImageView(frame: .zero) - - override init(style: UITableViewCellStyle, reuseIdentifier: String?) { - super.init(style: .default, reuseIdentifier: reuseIdentifier) - print("RenderTableViewCell init") - self.separatorInset = .zero - self.selectionStyle = .none - renderImageView.autoresizingMask = [.flexibleWidth, .flexibleHeight] - renderImageView.contentMode = .scaleAspectFit - self.contentView.addSubview(renderImageView) - renderImageView.frame = self.contentView.bounds - } - - required init?(coder aDecoder: NSCoder) { - fatalError("init(coder:) has not been implemented") - } - - deinit { - print("RenderTableViewCell deinit") - } - - private var renderedImageSize = CGSize.zero - - func updateImageViewIfNeeded() { - print("rendering bounds: \(self.contentView.bounds)") - if renderedImageSize == self.contentView.bounds.size && - self.renderImageView.image != nil { - // already rendered - return - } - updateImageView() - } - - private func updateImageView() { - guard let renderer = self.renderer else { return } - let image = renderer.renderToImage(size: self.contentView.bounds.size) - self.renderImageView.image = image - renderedImageSize = self.contentView.bounds.size - } -} - -public class RenderTableViewController: UITableViewController { - private let CellIdentifier = "Cell" - - var renderers: [Renderer] = [] { - didSet { - tableView.reloadData() - } - } - - public init() { - super.init(style: .plain) - } - - public required init?(coder aDecoder: NSCoder) { - fatalError("init(coder:) has not been implemented") - } - - public override func viewDidLoad() { - super.viewDidLoad() - - tableView.contentInset = .zero - tableView.separatorStyle = .none - - tableView.register(RenderTableViewCell.self, forCellReuseIdentifier: CellIdentifier) - } - - public func append(renderer: Renderer) { - self.renderers.append(renderer) - self.tableView.reloadData() - } - - public override func viewDidLayoutSubviews() { - super.viewDidLayoutSubviews() - // will ?? - //self.tableView.reloadData() - } - - public override func viewWillLayoutSubviews() { - super.viewWillLayoutSubviews() - for view in tableView.visibleCells { - (view as! RenderTableViewCell).updateImageViewIfNeeded() - } - } - - public override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { - return renderers.count - } - - public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { - let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier, for: indexPath) as! RenderTableViewCell - cell.renderer = renderers[indexPath.row] - return cell - } - - public override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { - return self.view.bounds.height * 0.5 - } - - public override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { - let cell = cell as! RenderTableViewCell - cell.updateImageViewIfNeeded() - } -} diff --git a/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/Renderer.swift b/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/Renderer.swift deleted file mode 100755 index 1dd833c..0000000 --- a/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/Renderer.swift +++ /dev/null @@ -1,27 +0,0 @@ -// -// Renderer.swift -// sandbox -// -// Created by omochimetaru on 2017/03/04. -// Copyright © 2017年 sonson. All rights reserved. -// - -import UIKit - -public protocol Renderer { - func render(context: CGContext, windowSize: CGSize) -} - -public extension Renderer { - func renderToImage(size: CGSize) -> UIImage { - UIGraphicsBeginImageContextWithOptions(size, true, UIScreen.main.scale) - - let context = UIGraphicsGetCurrentContext()! - - render(context: context, windowSize: size) - - let cgImage = context.makeImage()! - - return UIImage(cgImage: cgImage) - } -} diff --git a/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/RendererDebugViewController.swift b/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/RendererDebugViewController.swift deleted file mode 100644 index 2847124..0000000 --- a/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/RendererDebugViewController.swift +++ /dev/null @@ -1,53 +0,0 @@ -// -// RendererDebugViewController.swift -// sandbox -// -// Created by omochimetaru on 2017/03/04. -// Copyright © 2017年 sonson. All rights reserved. -// - -import UIKit - -class RendererDebugViewController: UIViewController { - - required init() { - super.init(nibName: "RendererDebugViewController", bundle: nil) - } - - required init?(coder aDecoder: NSCoder) { - fatalError("init(coder:) has not been implemented") - } - - override func viewDidLoad() { - func makeRenderer() -> ChartRenderer { - let points1 = DummyData.points1() - let points2 = DummyData.points2() - - var chart = Chart() - chart.elements = [ - .line(LineGraph(points: points1)), - .line(LineGraph(points: points2)) - ] - chart.computeViewport() - - return ChartRenderer(chart: chart) - } - - renderer = makeRenderer() - - } - - var renderer: Renderer? - - @IBOutlet var imageView: UIImageView! - - @IBAction func onRenderButton(sender: UIButton) { - let image = renderer!.renderToImage(size: imageView.bounds.size) - imageView.image = image - } - - @IBAction func onClearButton() { - imageView.image = nil - } - -} diff --git a/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/RendererUtil.swift b/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/RendererUtil.swift deleted file mode 100644 index fbc8034..0000000 --- a/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/RendererUtil.swift +++ /dev/null @@ -1,119 +0,0 @@ -// -// RendererUtil.swift -// sandbox -// -// Created by omochimetaru on 2017/03/04. -// Copyright © 2017年 sonson. All rights reserved. -// - -import CoreGraphics - -// Ideally, I can just define top level function. -// But in playgroundbook, we can not use packagename so need `struct` namespacing. - -public struct RendererUtil { - - public static func toRadian(_ x: CGFloat) -> CGFloat { - return x * CGFloat.pi / CGFloat(180.0) - } - - public static func toDegree(_ x: CGFloat) -> CGFloat { - return x * CGFloat(180.0) / CGFloat.pi - } - - public static func computeBounds(points: [CGPoint]) -> CGRect { - let xs = points.map { $0.x } - let ys = points.map { $0.y } - - let x0 = xs.min()! - let x1 = xs.max()! - let y0 = ys.min()! - let y1 = ys.max()! - - return CGRect(x: x0, y: y0, width: x1 - x0, height: y1 - y0) - } - - public static func adjustViewport(viewport: CGRect) -> CGRect { - // min size constraint - - var width = max(viewport.width, 2) - var height = max(viewport.height, 2) - - // 10% margin - - width += width * 0.1 - height += height * 0.1 - - return CGRect(x: viewport.midX - width / 2.0, - y: viewport.midY - height / 2.0, - width: width, - height: height) - } - - public static func computeViewportTransform( - from: CGRect, - to: CGRect, - flipY: Bool) -> CGAffineTransform { - let fw = from.width - let fh = from.height - let tw = to.width - let th = to.height - - var trans = CGAffineTransform.identity - - trans = trans.translatedBy(x: to.origin.x, y: to.origin.y) - trans = trans.translatedBy(x: (tw / 2.0), y: (th / 2.0)) - - let sx = tw / fw - var sy = th / fh - if flipY { - sy *= -1 - } - - trans = trans.scaledBy(x: sx, y: sy) - trans = trans.translatedBy(x: -(fw / 2.0), y: -(fh / 2.0)) - trans = trans.translatedBy(x: -from.origin.x, y: -from.origin.y) - return trans - } - - public static func computeViewportTransform( - viewport: CGRect, windowSize: CGSize) -> CGAffineTransform { - return computeViewportTransform(from: viewport, - to: CGRect(origin: CGPoint.zero, - size: windowSize), - flipY: true) - } - - public static func computeTickValues( - min: CGFloat, - max: CGFloat, - step: CGFloat) -> [CGFloat] { - let x0 = Int(floor(min / step)) - let x1 = Int(ceil(max / step)) - - var ret: [CGFloat] = [] - for xi in x0...x1 { - let x = CGFloat(xi) * step - ret.append(x) - } - - return ret - } - - public static func drawLine(context: CGContext, - points: [CGPoint]) { - context.setLineWidth(2.0) - - if points.count < 2 { - return - } - - context.move(to: points[0]) - for i in 1.. CGRect { - return RendererUtil.computeBounds(points: points) - } -} diff --git a/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/ScatterGraphRenderer.swift b/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/ScatterGraphRenderer.swift deleted file mode 100644 index aa26c67..0000000 --- a/Release/numsw-ipad.playgroundbook/Contents/Sources/playgrounds/ScatterGraphRenderer.swift +++ /dev/null @@ -1,46 +0,0 @@ -// -// ScatterGraphRenderer.swift -// sandbox -// -// Created by omochimetaru on 2017/03/06. -// Copyright © 2017年 sonson. All rights reserved. -// - -import UIKit -import CoreGraphics - -public class ScatterGraphRenderer: Renderer { - public init(viewport: CGRect, scatter: ScatterGraph) { - self.viewport = viewport - self.scatter = scatter - } - - public let viewport: CGRect - public let scatter: ScatterGraph - - public func render(context: CGContext, windowSize: CGSize) { - viewportTransform = RendererUtil.computeViewportTransform(viewport: viewport, windowSize: windowSize) - drawScatter(context: context, scatter: scatter) - } - - public func drawScatter(context ctx: CGContext, scatter: ScatterGraph) { - ctx.setStrokeColor(UIColor.green.cgColor) - - let t = viewportTransform! - - for point in scatter.points { - let p = point.applying(t) - - RendererUtil.drawLine(context: ctx, points: [ - CGPoint(x: p.x - 10, y: p.y - 10), - CGPoint(x: p.x + 10, y: p.y + 10) - ]) - RendererUtil.drawLine(context: ctx, points: [ - CGPoint(x: p.x - 10, y: p.y + 10), - CGPoint(x: p.x + 10, y: p.y - 10) - ]) - } - } - - private var viewportTransform: CGAffineTransform? -} diff --git a/Release/numsw-xcode.playground/Contents.swift b/Release/numsw-xcode.playground/Contents.swift deleted file mode 100644 index f21c180..0000000 --- a/Release/numsw-xcode.playground/Contents.swift +++ /dev/null @@ -1,62 +0,0 @@ -//: Playground - noun: a place where people can play - -NumswPlayground.initialize() - -do { - let t = Matrix.range(from: 0, to: 10, stride: 0.01) - let siny = sin(t*64) - - addLine(x: t.elements, y: siny.elements) -} -do { - let t = Matrix.range(from: 0.1, to: 10, stride: 0.01) - let siny = log(t*1.5) - addLine(x: t.elements, y: siny.elements) -} -do { - let t = Matrix.range(from: -10, to: 10, stride: 0.01) - let siny = tan(t*0.2) - addLine(x: t.elements, y: siny.elements) -} - -do { - // create dummy data - let x = Matrix(rows: 2, columns: 6, elements: [1, 2, 3, 4, 5, 6, 1, 1, 1, 1, 1, 1]) - let noise = Matrix.normal(rows: 1, columns: 6) - let x1 = Matrix(rows: 1, columns: 6, elements: [1, 2, 3, 4, 5, 6]) - - // data with noise - let yn = 6 * x1 + 10 + noise - - // linear regression - let xx = x * x.transposed() - let a = xx.inverted() - let b = x.transposed() * a - let A = yn * b - - // data for presentation - let x_p = Matrix.range(from: 0, to: 6, stride: 0.1) - let y_p = A[0, 0] * x_p + A[0, 1] - - // rendering - addLine2(x: x_p.elements, y: y_p.elements, x2: x.elements, y2: yn.elements) -} - -do { - let x = Matrix(rows: 2, columns: 6, elements: [1, 2, 5, 9, 13, 15, 1, 1, 1, 1, 1, 1]) - let y = Matrix(rows: 1, columns: 6, elements: [1, 2, 3, 5, 10, 50]) - - let xx = x * x.transposed() - x.transposed().show() - - let a = xx.inverted() - let logy = log(y) - - let b = x.transposed() * a - let A = logy * b - - let x_p = Matrix.range(from: 0, to: 45, stride: 0.1) - let y_p = exp(A[0, 0] * x_p + A[0, 1]) - - addLine2(x: x_p.elements, y: y_p.elements, x2: x.elements, y2: y.elements) -} diff --git a/Release/numsw-xcode.playground/Sources/numsw/Matrix.swift b/Release/numsw-xcode.playground/Sources/numsw/Matrix.swift deleted file mode 100644 index 8b30da2..0000000 --- a/Release/numsw-xcode.playground/Sources/numsw/Matrix.swift +++ /dev/null @@ -1,74 +0,0 @@ -public struct Matrix { - - public var rows: Int - public var columns: Int - public var count: Int { - return columns * rows - } - - public var elements: [Double] - - public init(rows: Int, columns: Int, elements: [Double]) { - self.rows = rows - self.columns = columns - self.elements = elements - } - - public init(_ elements: [[Double]]) { - let rows = elements.count - precondition(rows > 0, "Matrix must have elements.") - let columns = elements[0].count - let elements = elements.flatMap { $0 } - precondition(elements.count == rows*columns, "All rows must have same size.") - self.init(rows: rows, columns: columns, elements: elements) - } - - public static func zeros(rows: Int, columns: Int) -> Matrix { - let count = columns * rows - let cElements = UnsafeMutablePointer.allocate(capacity: count) - defer { cElements.deallocate(capacity: count) } - for i in 0.. Matrix { - let count = columns * rows - let cElements = UnsafeMutablePointer.allocate(capacity: count) - defer { cElements.deallocate(capacity: count) } - for i in 0.. Matrix { - var matrix = Matrix.zeros(rows: size, columns: size) - for i in 0.. Matrix { - assert(rows * columns == self.rows * self.columns, "Element count must be unchanged.") - - return Matrix(rows: rows, columns: columns, elements: self.elements) - } -} diff --git a/Release/numsw-xcode.playground/Sources/numsw/MatrixAddition.swift b/Release/numsw-xcode.playground/Sources/numsw/MatrixAddition.swift deleted file mode 100644 index db8ecf9..0000000 --- a/Release/numsw-xcode.playground/Sources/numsw/MatrixAddition.swift +++ /dev/null @@ -1,95 +0,0 @@ -// -// MatrixAddition.swift -// numsw -// -// Created by sonson on 2017/03/04. -// -// - -import Foundation -import Accelerate - -// MARK: - Addition - -public func add(_ lhs: Matrix, rhs: Double) -> Matrix { - let newElements = lhs.elements.map { (value) -> Double in - return value + rhs - } - return Matrix(rows: lhs.rows, columns: lhs.columns, elements: Array(UnsafeBufferPointer(start: newElements, count: lhs.count))) -} - -public func add(_ lhs: Matrix, rhs: Matrix) -> Matrix { - precondition(lhs.rows == rhs.rows && lhs.columns == rhs.columns, "Matrix dimensions not compatible with addition") - - let cElements = UnsafeMutablePointer.allocate(capacity: lhs.count) - defer { cElements.deallocate(capacity: lhs.count) } - - memcpy(cElements, rhs.elements, rhs.count * MemoryLayout.size) - - cblas_daxpy(Int32(lhs.count), 1.0, lhs.elements, 1, cElements, 1) - - return Matrix(rows: lhs.rows, columns: lhs.columns, elements: Array(UnsafeBufferPointer(start: cElements, count: lhs.count))) -} - -public func += (lhs: inout Matrix, rhs: Double) { - lhs = lhs + rhs -} - -public func += (lhs: inout Matrix, rhs: Matrix) { - lhs = lhs + rhs -} - -public func + (lhs: Matrix, rhs: Double) -> Matrix { - return add(lhs, rhs: rhs) -} - -public func + (lhs: Matrix, rhs: Matrix) -> Matrix { - return add(lhs, rhs: rhs) -} - -// MARK: - Subtraction - -public func subtract(_ lhs: Matrix, rhs: Double) -> Matrix { - let newElements = lhs.elements.map { (value) -> Double in - return value - rhs - } - return Matrix(rows: lhs.rows, columns: lhs.columns, elements: Array(UnsafeBufferPointer(start: newElements, count: lhs.count))) -} - -public func subtract(_ lhs: Matrix, rhs: Matrix) -> Matrix { - precondition(lhs.rows == rhs.rows && lhs.columns == rhs.columns, "Matrix dimensions not compatible with addition") - - let cElements = UnsafeMutablePointer.allocate(capacity: lhs.count) - defer { cElements.deallocate(capacity: lhs.count) } - - memcpy(cElements, lhs.elements, lhs.count * MemoryLayout.size) - - cblas_daxpy(Int32(lhs.count), -1.0, rhs.elements, 1, cElements, 1) - - return Matrix(rows: lhs.rows, columns: lhs.columns, elements: Array(UnsafeBufferPointer(start: cElements, count: lhs.count))) -} - -public func -= (lhs: inout Matrix, rhs: Double) { - lhs = lhs - rhs -} - -public func -= (lhs: inout Matrix, rhs: Matrix) { - lhs = lhs - rhs -} - -public func - (lhs: Matrix, rhs: Double) -> Matrix { - return subtract(lhs, rhs: rhs) -} - -public func - (lhs: Matrix, rhs: Matrix) -> Matrix { - return subtract(lhs, rhs: rhs) -} - -// MARK: - Prefix - -public prefix func - (lhs: Matrix) -> Matrix { - let newElements = lhs.elements.map { (value) -> Double in - return -value - } - return Matrix(rows: lhs.rows, columns: lhs.columns, elements: Array(UnsafeBufferPointer(start: newElements, count: lhs.count))) -} diff --git a/Release/numsw-xcode.playground/Sources/numsw/MatrixDivide.swift b/Release/numsw-xcode.playground/Sources/numsw/MatrixDivide.swift deleted file mode 100644 index b69adf3..0000000 --- a/Release/numsw-xcode.playground/Sources/numsw/MatrixDivide.swift +++ /dev/null @@ -1,14 +0,0 @@ -import Accelerate - -// scalar -public func /(lhs: Matrix, rhs: Double) -> Matrix { - return Matrix(rows: lhs.rows, columns: lhs.columns, elements: lhs.elements.map { $0/rhs }) -} - -public func /(lhs: Double, rhs: Matrix) -> Matrix { - return Matrix(rows: rhs.rows, columns: rhs.columns, elements: rhs.elements.map { lhs/$0 }) -} - -public func /=(lhs: inout Matrix, rhs: Double) { - lhs = lhs / rhs -} diff --git a/Release/numsw-xcode.playground/Sources/numsw/MatrixInvert.swift b/Release/numsw-xcode.playground/Sources/numsw/MatrixInvert.swift deleted file mode 100644 index 57f6c6e..0000000 --- a/Release/numsw-xcode.playground/Sources/numsw/MatrixInvert.swift +++ /dev/null @@ -1,14 +0,0 @@ -import Accelerate - -extension Matrix { - public func inverted() -> Matrix { - var inMatrix: [Double] = self.elements - var N: __CLPK_integer = __CLPK_integer( sqrt( Double( self.rows*self.columns ) ) ) - var pivots: [__CLPK_integer] = [__CLPK_integer](repeating: 0, count: Int(N)) - var workspace: [Double] = [Double](repeating: 0.0, count: Int(N)) - var error: __CLPK_integer = 0 - dgetrf_(&N, &N, &inMatrix, &N, &pivots, &error) - dgetri_(&N, &inMatrix, &N, &pivots, &workspace, &N, &error) - return Matrix(rows: self.rows, columns: self.columns, elements: inMatrix) - } -} diff --git a/Release/numsw-xcode.playground/Sources/numsw/MatrixLogarithm.swift b/Release/numsw-xcode.playground/Sources/numsw/MatrixLogarithm.swift deleted file mode 100644 index 3e1cb69..0000000 --- a/Release/numsw-xcode.playground/Sources/numsw/MatrixLogarithm.swift +++ /dev/null @@ -1,39 +0,0 @@ -// -// MatrixLogarithm.swift -// numsw -// -// Created by sonson on 2017/03/04. -// -// - -import Accelerate - -public func exp(_ x: Matrix) -> Matrix { - var result = Matrix.zeros(rows: x.rows, columns: x.columns) - vvexp(&result.elements, x.elements, [Int32(x.count)]) - return result -} - -public func exp2(_ x: Matrix) -> Matrix { - var result = Matrix.zeros(rows: x.rows, columns: x.columns) - vvexp2(&result.elements, x.elements, [Int32(x.count)]) - return result -} - -public func log(_ x: Matrix) -> Matrix { - var result = Matrix.zeros(rows: x.rows, columns: x.columns) - vvlog(&result.elements, x.elements, [Int32(x.count)]) - return result -} - -public func log10(_ x: Matrix) -> Matrix { - var result = Matrix.zeros(rows: x.rows, columns: x.columns) - vvlog10(&result.elements, x.elements, [Int32(x.count)]) - return result -} - -//public func logb(_ x: Matrix) -> Matrix { -// var result = Matrix.zeros(rows: x.rows, columns: x.columns) -// vvlogb(&result.elements, x.elements, [Int32(x.count)]) -// return result -//} diff --git a/Release/numsw-xcode.playground/Sources/numsw/MatrixMultiply.swift b/Release/numsw-xcode.playground/Sources/numsw/MatrixMultiply.swift deleted file mode 100644 index 4d02707..0000000 --- a/Release/numsw-xcode.playground/Sources/numsw/MatrixMultiply.swift +++ /dev/null @@ -1,66 +0,0 @@ -import Accelerate - -// scalar - -public func *(lhs: Matrix, rhs: Double) -> Matrix { - return Matrix(rows: lhs.rows, columns: lhs.columns, elements: lhs.elements.map { $0*rhs }) -} - -public func *(lhs: Double, rhs: Matrix) -> Matrix { - return rhs * lhs -} - -public func *=(lhs: inout Matrix, rhs: Double) { - lhs = lhs * rhs -} - -// matmul - -public func *(lhs: Matrix, rhs: Matrix) -> Matrix { - precondition(lhs.columns == rhs.rows, "Matrices can't multiply.") - let m = lhs.rows - let n = rhs.columns - let k = lhs.columns - let lda = k - let ldb = n - let cElements = UnsafeMutablePointer.allocate(capacity: m*n) - defer { cElements.deallocate(capacity: m*n) } - - cblas_dgemm( - CblasRowMajor, // Order - CblasNoTrans, // TransA - CblasNoTrans, // TransB - Int32(m), // M - Int32(n), // N - Int32(k), // K - 1.0, // alpha - lhs.elements, // A - Int32(lda), // lda - rhs.elements, // B - Int32(ldb), // ldb - 0.0, // beta - cElements, // C - Int32(n) // ldc - ) - return Matrix(rows: m, - columns: n, - elements: Array(UnsafeBufferPointer(start: cElements, count: m*n))) -} - -// element wise multiply - -infix operator .* -public func .*(lhs: Matrix, rhs: Matrix) -> Matrix { - let pointer = UnsafeMutablePointer.allocate(capacity: lhs.count) - defer { pointer.deallocate(capacity: lhs.count) } - vDSP_vmulD(lhs.elements, 1, rhs.elements, 1, pointer, 1, vDSP_Length(lhs.count)) - - return Matrix(rows: lhs.rows, - columns: lhs.columns, - elements: Array(UnsafeBufferPointer(start: pointer, count: lhs.count))) -} - -infix operator .*= -public func .*=(lhs: inout Matrix, rhs: Matrix) { - lhs = lhs .* rhs -} diff --git a/Release/numsw-xcode.playground/Sources/numsw/MatrixNorm.swift b/Release/numsw-xcode.playground/Sources/numsw/MatrixNorm.swift deleted file mode 100644 index ab0728b..0000000 --- a/Release/numsw-xcode.playground/Sources/numsw/MatrixNorm.swift +++ /dev/null @@ -1,23 +0,0 @@ -// -// Matrix_sonson.swift -// numsw -// -// Created by sonson on 2017/03/04. -// -// - -import Foundation - -public func frobeniusNorm(_ hs: Matrix) -> Double { - return hs.elements.reduce(0) { (result, value) -> Double in - return result + value * value - } -} - -extension Matrix { - public var frobeniusNorm: Double { - return self.elements.reduce(0) { (result, value) -> Double in - return result + value * value - } - } -} diff --git a/Release/numsw-xcode.playground/Sources/numsw/MatrixRandom.swift b/Release/numsw-xcode.playground/Sources/numsw/MatrixRandom.swift deleted file mode 100644 index 2fa5778..0000000 --- a/Release/numsw-xcode.playground/Sources/numsw/MatrixRandom.swift +++ /dev/null @@ -1,25 +0,0 @@ -import Foundation - -func _uniform(low: Double = 0, high: Double = 1) -> Double { - return (high-low)*(Double(arc4random_uniform(UInt32.max)) / Double(UInt32.max))+low -} - -func _normal(mu: Double = 0, sigma: Double = 1) -> Double { - let u = (_uniform(), _uniform()) - let x = sqrt(-2*log(u.0)) * cos(2*M_PI*u.1) - return sigma*x + mu -} - -extension Matrix { - public static func uniform(rows: Int, columns: Int, low: Double = 0, high: Double = 1) -> Matrix { - return Matrix(rows: rows, - columns: columns, - elements: (0.. Matrix { - return Matrix(rows: rows, - columns: columns, - elements: (0.. Matrix { - - let elements = Swift.stride(from: from, to: to, by: stride).map(Double.init) - return Matrix(rows: elements.count, - columns: 1, - elements: elements) - } - - public static func range(from: Double, to: Double, stride: Double) -> Matrix { - let elements = Array(Swift.stride(from: from, to: to, by: stride)) - return Matrix(rows: elements.count, - columns: 1, - elements: elements) - } - - public static func linspace(from: Double, to: Double, count: Int) -> Matrix { - precondition(count > 0, "count must be larger than 0") - let elements = (0.. Double in - from + Double(i)*(to-from)/Double(count-1) - } - return Matrix(rows: count, - columns: 1, - elements: elements) - } -} diff --git a/Release/numsw-xcode.playground/Sources/numsw/MatrixSubscript.swift b/Release/numsw-xcode.playground/Sources/numsw/MatrixSubscript.swift deleted file mode 100644 index a54548d..0000000 --- a/Release/numsw-xcode.playground/Sources/numsw/MatrixSubscript.swift +++ /dev/null @@ -1,57 +0,0 @@ -extension Matrix { - public subscript(row: Int, column: Int) -> Double { - get { - return elements[row * columns + column] - } - set { - elements[row * columns + column] = newValue - } - } - - public subscript(row: Int) -> Matrix { - get { - return Matrix(rows: 1, columns: columns, elements: Array(elements[row * columns.. Matrix { - get { - let rs = rs ?? Array(0...allocate(capacity: rs.count * cs.count) - defer { newElements.deallocate(capacity: rs.count * cs.count) } - var pointer = newElements - for r in rs { - for c in cs { - pointer.pointee = self[r, c] - pointer += 1 - } - } - return Matrix(rows: rs.count, - columns: cs.count, - elements: Array(UnsafeBufferPointer(start: newElements, count: rs.count * cs.count))) - } - set { - let rs = rs ?? Array(0.. Matrix { - let newElements = UnsafeMutablePointer.allocate(capacity: rows*columns) - defer { newElements.deallocate(capacity: rows*columns) } - for r in 0.. Matrix { - var result = Matrix.zeros(rows: x.rows, columns: x.columns) - vvsin(&result.elements, x.elements, [Int32(x.count)]) - return result -} - -public func cos(_ x: Matrix) -> Matrix { - var result = Matrix.zeros(rows: x.rows, columns: x.columns) - vvcos(&result.elements, x.elements, [Int32(x.count)]) - return result -} - -public func tan(_ x: Matrix) -> Matrix { - var result = Matrix.zeros(rows: x.rows, columns: x.columns) - vvtan(&result.elements, x.elements, [Int32(x.count)]) - return result -} - -//public func atan2(_ x: Matrix) -> Matrix { -// var result = Matrix.zeros(rows: x.rows, columns: x.columns) -// vvatan2(<#T##UnsafeMutablePointer#>, <#T##UnsafePointer#>, <#T##UnsafePointer#>, <#T##UnsafePointer#>)(&result.elements, x.elements, [Int32(x.count)]) -// return result -//} diff --git a/Release/numsw-xcode.playground/Sources/playgrounds/AxisRenderer.swift b/Release/numsw-xcode.playground/Sources/playgrounds/AxisRenderer.swift deleted file mode 100644 index 5947a80..0000000 --- a/Release/numsw-xcode.playground/Sources/playgrounds/AxisRenderer.swift +++ /dev/null @@ -1,105 +0,0 @@ -// -// ChartRenderer.swift -// sandbox -// -// Created by omochimetaru on 2017/03/06. -// Copyright © 2017年 sonson. All rights reserved. -// - -// xAxis, yAxis, ticks - -import UIKit - -public class AxisRenderer: Renderer { - public init(chart: Chart) { - self.chart = chart - } - - public let chart: Chart - - public func render(context: CGContext, windowSize: CGSize) { - viewportTransform = RendererUtil.computeViewportTransform(viewport: chart.viewport, windowSize: windowSize) - stepSize = computeStepSize() - - drawAxisX(context: context) - drawAxisY(context: context) - } - - private func computeStepSize() -> CGSize { - let size = chart.viewport.size - - let xStep: CGFloat - - if size.width <= 0.0 { - xStep = 0.0 - } else { - let xLog = log10(size.width) - xStep = pow(10.0, round(xLog) - 1) - } - - let yStep: CGFloat - - if size.height <= 0.0 { - yStep = 0.0 - } else { - let yLog = log10(size.height) - yStep = pow(10.0, round(yLog) - 1) - } - - return CGSize(width: xStep, height: yStep) - } - - private func drawAxisX(context ctx: CGContext) { - let p0 = CGPoint(x: chart.viewport.minX, y: 0) - let p1 = CGPoint(x: chart.viewport.maxX, y: 0) - - ctx.setStrokeColor(UIColor.gray.cgColor) - drawLine(context: ctx, points: [p0, p1]) - - // ticks - - let tickXs = RendererUtil.computeTickValues(min: p0.x, max: p1.x, - step: stepSize!.width) - let tickHeight = chart.viewport.height * 0.04 - - for x in tickXs { - drawLine(context: ctx, points: [ - CGPoint(x: x, y: (tickHeight / 2.0)), - CGPoint(x: x, y: -(tickHeight / 2.0)), - ]) - } - } - - private func drawAxisY(context ctx: CGContext) { - let p0 = CGPoint(x: 0, y: chart.viewport.minY) - let p1 = CGPoint(x: 0, y: chart.viewport.maxY) - - ctx.setStrokeColor(UIColor.gray.cgColor) - drawLine(context: ctx, points: [p0, p1]) - - // ticks - - let tickYs = RendererUtil.computeTickValues(min: p0.y, max: p1.y, - step: stepSize!.height) - let tickWidth = chart.viewport.width * 0.04 - - for y in tickYs { - drawLine(context: ctx, points: [ - CGPoint(x: -(tickWidth / 2.0), y: y), - CGPoint(x: (tickWidth / 2.0), y: y), - ]) - } - } - - private func drawLine(context: CGContext, - points: [CGPoint]) { - let t = viewportTransform! - - let points = points.map { $0.applying(t) } - - RendererUtil.drawLine(context: context, points: points) - } - - private var viewportTransform: CGAffineTransform? - private var stepSize: CGSize? -} diff --git a/Release/numsw-xcode.playground/Sources/playgrounds/Chart.swift b/Release/numsw-xcode.playground/Sources/playgrounds/Chart.swift deleted file mode 100644 index 373644b..0000000 --- a/Release/numsw-xcode.playground/Sources/playgrounds/Chart.swift +++ /dev/null @@ -1,31 +0,0 @@ -// -// Chart.swift -// sandbox -// -// Created by omochimetaru on 2017/03/06. -// Copyright © 2017年 sonson. All rights reserved. -// - -import CoreGraphics - -public struct Chart { - public init() { - computeViewport() - } - - public var viewport: CGRect = .zero - public var elements: [ChartElement] = [] - - public mutating func computeViewport() { - guard elements.count != 0 else { - viewport = RendererUtil.adjustViewport(viewport: CGRect.zero) - return - } - - var b = elements.first!.computeBounds() - for e in elements.dropFirst() { - b = b.union(e.computeBounds()) - } - viewport = RendererUtil.adjustViewport(viewport: b) - } -} diff --git a/Release/numsw-xcode.playground/Sources/playgrounds/ChartBuilder.swift b/Release/numsw-xcode.playground/Sources/playgrounds/ChartBuilder.swift deleted file mode 100644 index 0e468bf..0000000 --- a/Release/numsw-xcode.playground/Sources/playgrounds/ChartBuilder.swift +++ /dev/null @@ -1,27 +0,0 @@ -// -// ChartBuilder.swift -// sandbox -// -// Created by omochimetaru on 2017/03/06. -// Copyright © 2017年 sonson. All rights reserved. -// - -import Foundation - -public class ChartBuilder { - public init() { - chart = Chart() - } - - public func addLine(line: LineGraph) { - chart.elements.append(.line(line)) - chart.computeViewport() - } - - public func addScatter(scatter: ScatterGraph) { - chart.elements.append(.scatter(scatter)) - chart.computeViewport() - } - - public var chart: Chart -} diff --git a/Release/numsw-xcode.playground/Sources/playgrounds/ChartElement.swift b/Release/numsw-xcode.playground/Sources/playgrounds/ChartElement.swift deleted file mode 100644 index 3cbd101..0000000 --- a/Release/numsw-xcode.playground/Sources/playgrounds/ChartElement.swift +++ /dev/null @@ -1,23 +0,0 @@ -// -// ChartElement.swift -// sandbox -// -// Created by omochimetaru on 2017/03/06. -// Copyright © 2017年 sonson. All rights reserved. -// - -import CoreGraphics - -public enum ChartElement { - case line(LineGraph) - case scatter(ScatterGraph) - - func computeBounds() -> CGRect { - switch self { - case .line(let line): - return line.computeBounds() - case .scatter(let scatter): - return scatter.computeBounds() - } - } -} diff --git a/Release/numsw-xcode.playground/Sources/playgrounds/ChartRenderer.swift b/Release/numsw-xcode.playground/Sources/playgrounds/ChartRenderer.swift deleted file mode 100644 index ccf507b..0000000 --- a/Release/numsw-xcode.playground/Sources/playgrounds/ChartRenderer.swift +++ /dev/null @@ -1,45 +0,0 @@ -// -// ChartRenderer.swift -// sandbox -// -// Created by omochimetaru on 2017/03/06. -// Copyright © 2017年 sonson. All rights reserved. -// - -import CoreGraphics - -public class ChartRenderer: Renderer { - - public init(chart: Chart) { - self.chart = chart - - update() - } - - public let chart: Chart - - public func render(context: CGContext, windowSize: CGSize) { - compositer?.render(context: context, windowSize: windowSize) - } - - private func update() { - var renderers: [Renderer] = [] - - renderers.append(AxisRenderer(chart: chart)) - - for element in chart.elements { - switch element { - case .line(let line): - renderers.append(LineGraphRenderer(viewport: chart.viewport, line: line)) - case .scatter(let scatter): - renderers.append(ScatterGraphRenderer(viewport: chart.viewport, scatter: scatter)) - } - } - - let r = CompositeRenderer() - self.compositer = r - r.renderers = renderers - } - - private var compositer: CompositeRenderer? -} diff --git a/Release/numsw-xcode.playground/Sources/playgrounds/CompositeRenderer.swift b/Release/numsw-xcode.playground/Sources/playgrounds/CompositeRenderer.swift deleted file mode 100644 index e478a02..0000000 --- a/Release/numsw-xcode.playground/Sources/playgrounds/CompositeRenderer.swift +++ /dev/null @@ -1,25 +0,0 @@ -// -// CompositableRenderer.swift -// sandbox -// -// Created by omochimetaru on 2017/03/06. -// Copyright © 2017年 sonson. All rights reserved. -// - -// its composite multiple renderer as one renderer - -import CoreGraphics - -public class CompositeRenderer: Renderer { - public init() { - - } - - public var renderers: [Renderer] = [] - - public func render(context: CGContext, windowSize: CGSize) { - for renderer in renderers { - renderer.render(context: context, windowSize: windowSize) - } - } -} diff --git a/Release/numsw-xcode.playground/Sources/playgrounds/DummyData.swift b/Release/numsw-xcode.playground/Sources/playgrounds/DummyData.swift deleted file mode 100755 index 4b092ac..0000000 --- a/Release/numsw-xcode.playground/Sources/playgrounds/DummyData.swift +++ /dev/null @@ -1,109 +0,0 @@ -// -// Dummydata.swift -// sandbox -// -// Created by omochimetaru on 2017/03/04. -// Copyright © 2017年 sonson. All rights reserved. -// - -import UIKit - -#if SANDBOX_APP - import numsw -#endif - -public struct DummyData { - public static func points1() -> [CGPoint] { - return [ - CGPoint(x: 0, y: 0), - CGPoint(x: 1, y: 1), - CGPoint(x: 2, y: 4), - CGPoint(x: 3, y: 9), - CGPoint(x: 4, y: 16) - ] - } - - public static func points2() -> [CGPoint] { - return [ - CGPoint(x: 0, y: 6), - CGPoint(x: 2, y: 2), - CGPoint(x: 4, y: 3) - ] - } - - public static func runTestScenario() { - do { - let t = Matrix.range(from: 0, to: 10, stride: 0.01) - let siny = sin(t*64) - - addLine(x: t.elements, y: siny.elements) - } - do { - let t = Matrix.range(from: 0.1, to: 10, stride: 0.01) - let siny = log(t*1.5) - addLine(x: t.elements, y: siny.elements) - } - do { - let t = Matrix.range(from: -10, to: 10, stride: 0.01) - let siny = tan(t*0.2) - addLine(x: t.elements, y: siny.elements) - } - - do { - // create dummy data - let x = Matrix(rows: 2, columns: 6, elements: [1, 2, 3, 4, 5, 6, 1, 1, 1, 1, 1, 1]) - let noise = Matrix.normal(rows: 1, columns: 6) - let x1 = Matrix(rows: 1, columns: 6, elements: [1, 2, 3, 4, 5, 6]) - - // data with noise - let yn = 6 * x1 + 10 + noise - - // linear regression - let xx = x * x.transposed() - let a = xx.inverted() - let b = x.transposed() * a - let A = yn * b - - // data for presentation - let x_p = Matrix.range(from: 0, to: 6, stride: 0.1) - let y_p = A[0, 0] * x_p + A[0, 1] - - // rendering - addLine2(x: x_p.elements, y: y_p.elements, x2: x.elements, y2: yn.elements) - } - - do { - let x = Matrix(rows: 2, columns: 6, elements: [1, 2, 5, 9, 13, 15, 1, 1, 1, 1, 1, 1]) - let y = Matrix(rows: 1, columns: 6, elements: [1, 2, 3, 5, 10, 50]) - - let xx = x * x.transposed() - x.transposed().show() - - let a = xx.inverted() - let logy = log(y) - - let b = x.transposed() * a - let A = logy * b - - let x_p = Matrix.range(from: 0, to: 45, stride: 0.1) - let y_p = exp(A[0, 0] * x_p + A[0, 1]) - - addLine2(x: x_p.elements, y: y_p.elements, x2: x.elements, y2: y.elements) - } - - } - - public static func runHoldExample() { - - let t1 = Matrix.range(from: 0, to: 10, stride: 0.01) - let siny1 = sin(t1 * 2) - - let t2 = Matrix.range(from: 0, to: 10, stride: 0.4) - let siny2 = sin(t2 * 2) - - hold { - plot(t1.elements, siny1.elements) - scatter(t2.elements, siny2.elements) - } - } -} diff --git a/Release/numsw-xcode.playground/Sources/playgrounds/LineGraph.swift b/Release/numsw-xcode.playground/Sources/playgrounds/LineGraph.swift deleted file mode 100755 index 0c5c29d..0000000 --- a/Release/numsw-xcode.playground/Sources/playgrounds/LineGraph.swift +++ /dev/null @@ -1,21 +0,0 @@ -// -// LineData.swift -// sandbox -// -// Created by omochimetaru on 2017/03/04. -// Copyright © 2017年 sonson. All rights reserved. -// - -import CoreGraphics - -public struct LineGraph { - public init(points: [CGPoint]) { - self.points = points - } - - public var points: [CGPoint] - - public func computeBounds() -> CGRect { - return RendererUtil.computeBounds(points: points) - } -} diff --git a/Release/numsw-xcode.playground/Sources/playgrounds/LineGraphRenderer.swift b/Release/numsw-xcode.playground/Sources/playgrounds/LineGraphRenderer.swift deleted file mode 100644 index 188bd8c..0000000 --- a/Release/numsw-xcode.playground/Sources/playgrounds/LineGraphRenderer.swift +++ /dev/null @@ -1,84 +0,0 @@ -// -// LineGraphRenderer.swift -// sandbox -// -// Created by omochimetaru on 2017/03/04. -// Copyright © 2017年 sonson. All rights reserved. -// - -import UIKit -import CoreGraphics - -public class LineGraphRenderer: Renderer { - public init(viewport: CGRect, line: LineGraph) { - self.viewport = viewport - self.line = line - } - - public let viewport: CGRect - public let line: LineGraph - - public func render(context: CGContext, windowSize: CGSize) { - viewportTransform = RendererUtil.computeViewportTransform(viewport: viewport, windowSize: windowSize) - drawLine(context: context, points: line.points) - } - -// public func drawPoints(context ctx: CGContext) { -// ctx.setStrokeColor(UIColor.white.cgColor) -// -// if lines.count >= 1 { -// drawLine(context: ctx, points: lines[0].points) -// } -// if lines.count >= 2 { -// drawSanpuzu(context: ctx, line: lines[1]) -// } -// } - - public func drawSanpuzu(context ctx: CGContext, line: LineGraph) { - ctx.setStrokeColor(UIColor.green.cgColor) - - let t = viewportTransform! - - for point in line.points { - let p = point.applying(t) - - RendererUtil.drawLine(context: ctx, points: [ - CGPoint(x: p.x - 10, y: p.y - 10), - CGPoint(x: p.x + 10, y: p.y + 10) - ]) - RendererUtil.drawLine(context: ctx, points: [ - CGPoint(x: p.x - 10, y: p.y + 10), - CGPoint(x: p.x + 10, y: p.y - 10) - ]) - } - } - - public func drawDebugX(context ctx: CGContext, - point0: CGPoint, - point1: CGPoint) { - ctx.setStrokeColor(UIColor.red.cgColor) - drawLine(context: ctx, points: [ - CGPoint(x: point0.x, y: point0.y), - CGPoint(x: point1.x, y: point1.y) - ]) - - ctx.setStrokeColor(UIColor.green.cgColor) - drawLine(context: ctx, points: [ - CGPoint(x: point1.x, y: point0.y), - CGPoint(x: point0.x, y: point1.y) - ]) - } - - public func drawLine(context: CGContext, - points: [CGPoint]) { - context.setStrokeColor(UIColor.white.cgColor) - - let t = viewportTransform! - - let points = points.map { $0.applying(t) } - - RendererUtil.drawLine(context: context, points: points) - } - - private var viewportTransform: CGAffineTransform? -} diff --git a/Release/numsw-xcode.playground/Sources/playgrounds/NumswPlayground.swift b/Release/numsw-xcode.playground/Sources/playgrounds/NumswPlayground.swift deleted file mode 100644 index dd468b1..0000000 --- a/Release/numsw-xcode.playground/Sources/playgrounds/NumswPlayground.swift +++ /dev/null @@ -1,110 +0,0 @@ -// -// NumswPlayground.swift -// sandbox -// -// Created by omochimetaru on 2017/03/05. -// Copyright © 2017年 sonson. All rights reserved. -// - -import UIKit - -public class NumswPlayground { - internal init() { - viewController = RenderTableViewController() - } - - public let viewController: RenderTableViewController - - public func append(renderer: ChartRenderer) { - renderers.append(renderer) - } - - public func plot(_ x: [Double], _ y: [Double]) { - guard let b = chartBuilder else { - hold { - plot(x, y) - } - return - } - - let points = zip(x, y).map { - CGPoint(x: $0.0, y: $0.1) - } - b.addLine(line: LineGraph(points: points)) - } - - public func scatter(_ x: [Double], _ y: [Double]) { - guard let b = chartBuilder else { - hold { - scatter(x, y) - } - return - } - let points = zip(x, y).map { - CGPoint(x: $0.0, y: $0.1) - } - b.addScatter(scatter: ScatterGraph(points: points)) - } - - public func hold(_ f: () throws -> Void) rethrows { - let b = ChartBuilder() - chartBuilder = b - - try f() - - chartBuilder = nil - - let renderer = ChartRenderer(chart: b.chart) - append(renderer: renderer) - } - - public static var shared: NumswPlayground { - get { - if _shared == nil { - _shared = NumswPlayground() - } - return _shared! - } - } - - private var renderers: [ChartRenderer] = [] { - didSet { - viewController.renderers = renderers.map { $0 as Renderer } - } - } - - private var chartBuilder: ChartBuilder? - - private static var _shared: NumswPlayground? - -} - -public func addLine( - x: [Double], y: [Double]) { - hold { - plot(x, y) - } -} - -public func addLine2( - x: [Double], y: [Double], - x2: [Double], y2: [Double]) { - hold { - plot(x, y) - scatter(x2, y2) - } -} - -public func plot( - _ x: [Double], _ y: [Double]) { - NumswPlayground.shared.plot(x, y) -} - -public func scatter( - _ x: [Double], _ y: [Double]) { - NumswPlayground.shared.scatter(x, y) -} - -public func hold(f: () throws -> Void) rethrows { - try NumswPlayground.shared.hold(f) -} diff --git a/Release/numsw-xcode.playground/Sources/playgrounds/NumswPlayground_Playground.swift b/Release/numsw-xcode.playground/Sources/playgrounds/NumswPlayground_Playground.swift deleted file mode 100644 index f5540ea..0000000 --- a/Release/numsw-xcode.playground/Sources/playgrounds/NumswPlayground_Playground.swift +++ /dev/null @@ -1,18 +0,0 @@ -// -// NumswPlayground_Playground.swift -// sandbox -// -// Created by omochimetaru on 2017/03/05. -// Copyright © 2017年 sonson. All rights reserved. -// - -// This file is not included for iOS app build target - -import PlaygroundSupport - -public extension NumswPlayground { - public static func initialize() { - let s = NumswPlayground.shared - PlaygroundPage.current.liveView = s.viewController - } -} diff --git a/Release/numsw-xcode.playground/Sources/playgrounds/RenderScrollViewController.swift b/Release/numsw-xcode.playground/Sources/playgrounds/RenderScrollViewController.swift deleted file mode 100644 index 25bb158..0000000 --- a/Release/numsw-xcode.playground/Sources/playgrounds/RenderScrollViewController.swift +++ /dev/null @@ -1,77 +0,0 @@ -// Final customized first implementation with UIScrollView - -import UIKit - -public class RenderScrollViewController: UIViewController { - - func makeRenderer() -> ChartRenderer { - let points1 = DummyData.points1() - let points2 = DummyData.points2() - - var chart = Chart() - chart.elements = [ - .line(LineGraph(points: points1)), - .line(LineGraph(points: points2)) - ] - chart.computeViewport() - - return ChartRenderer(chart: chart) - } - - var renderers: [Renderer] = [] - - var scrollView: UIScrollView! - - public func append(renderer: Renderer) { - self.renderers.append(renderer) - } - - public override func viewDidLoad() { - super.viewDidLoad() - - scrollView = UIScrollView() - scrollView.frame = self.view.frame - - self.view.addSubview(scrollView) - } - - func updateViews() { - for view in scrollView.subviews { - view.removeFromSuperview() - } - scrollView.contentSize.height = 0 - self.render() - } - - public override func viewDidLayoutSubviews() { - super.viewDidLayoutSubviews() - scrollView.frame = self.view.frame - updateViews() - } - - public override func viewWillAppear(_ animated: Bool) { - super.viewWillAppear(animated) - self.render() - } - - public override func didReceiveMemoryWarning() { - super.didReceiveMemoryWarning() - } - - func render() { - - var size = self.view.frame.size - size.height *= 0.5 - // renderer 取り出してscrollviewに追加 - for renderer in renderers { - let image = renderer.renderToImage(size: size) - let imageView = UIImageView(image: image) - imageView.frame.size = size - imageView.contentMode = .scaleToFill - imageView.frame.origin = CGPoint(x: 0, y: scrollView.contentSize.height) - scrollView.addSubview(imageView) - scrollView.contentSize.height += size.height - } - } - -} diff --git a/Release/numsw-xcode.playground/Sources/playgrounds/RenderTableViewController.swift b/Release/numsw-xcode.playground/Sources/playgrounds/RenderTableViewController.swift deleted file mode 100755 index 48547e7..0000000 --- a/Release/numsw-xcode.playground/Sources/playgrounds/RenderTableViewController.swift +++ /dev/null @@ -1,125 +0,0 @@ -// -// RenderTableViewController.swift -// sandbox -// -// Created by color_box on 2017/03/04. -// Copyright © 2017年 sonson. All rights reserved. -// - -// Second Implementation with UITableView - -import UIKit - -private class RenderTableViewCell: UITableViewCell { - - var renderer: Renderer? { - willSet { - self.renderImageView.image = nil - self.renderedImageSize = .zero - } - } - - private let renderImageView = UIImageView(frame: .zero) - - override init(style: UITableViewCellStyle, reuseIdentifier: String?) { - super.init(style: .default, reuseIdentifier: reuseIdentifier) - print("RenderTableViewCell init") - self.separatorInset = .zero - self.selectionStyle = .none - renderImageView.autoresizingMask = [.flexibleWidth, .flexibleHeight] - renderImageView.contentMode = .scaleAspectFit - self.contentView.addSubview(renderImageView) - renderImageView.frame = self.contentView.bounds - } - - required init?(coder aDecoder: NSCoder) { - fatalError("init(coder:) has not been implemented") - } - - deinit { - print("RenderTableViewCell deinit") - } - - private var renderedImageSize = CGSize.zero - - func updateImageViewIfNeeded() { - print("rendering bounds: \(self.contentView.bounds)") - if renderedImageSize == self.contentView.bounds.size && - self.renderImageView.image != nil { - // already rendered - return - } - updateImageView() - } - - private func updateImageView() { - guard let renderer = self.renderer else { return } - let image = renderer.renderToImage(size: self.contentView.bounds.size) - self.renderImageView.image = image - renderedImageSize = self.contentView.bounds.size - } -} - -public class RenderTableViewController: UITableViewController { - private let CellIdentifier = "Cell" - - var renderers: [Renderer] = [] { - didSet { - tableView.reloadData() - } - } - - public init() { - super.init(style: .plain) - } - - public required init?(coder aDecoder: NSCoder) { - fatalError("init(coder:) has not been implemented") - } - - public override func viewDidLoad() { - super.viewDidLoad() - - tableView.contentInset = .zero - tableView.separatorStyle = .none - - tableView.register(RenderTableViewCell.self, forCellReuseIdentifier: CellIdentifier) - } - - public func append(renderer: Renderer) { - self.renderers.append(renderer) - self.tableView.reloadData() - } - - public override func viewDidLayoutSubviews() { - super.viewDidLayoutSubviews() - // will ?? - //self.tableView.reloadData() - } - - public override func viewWillLayoutSubviews() { - super.viewWillLayoutSubviews() - for view in tableView.visibleCells { - (view as! RenderTableViewCell).updateImageViewIfNeeded() - } - } - - public override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { - return renderers.count - } - - public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { - let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier, for: indexPath) as! RenderTableViewCell - cell.renderer = renderers[indexPath.row] - return cell - } - - public override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { - return self.view.bounds.height * 0.5 - } - - public override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { - let cell = cell as! RenderTableViewCell - cell.updateImageViewIfNeeded() - } -} diff --git a/Release/numsw-xcode.playground/Sources/playgrounds/Renderer.swift b/Release/numsw-xcode.playground/Sources/playgrounds/Renderer.swift deleted file mode 100755 index 1dd833c..0000000 --- a/Release/numsw-xcode.playground/Sources/playgrounds/Renderer.swift +++ /dev/null @@ -1,27 +0,0 @@ -// -// Renderer.swift -// sandbox -// -// Created by omochimetaru on 2017/03/04. -// Copyright © 2017年 sonson. All rights reserved. -// - -import UIKit - -public protocol Renderer { - func render(context: CGContext, windowSize: CGSize) -} - -public extension Renderer { - func renderToImage(size: CGSize) -> UIImage { - UIGraphicsBeginImageContextWithOptions(size, true, UIScreen.main.scale) - - let context = UIGraphicsGetCurrentContext()! - - render(context: context, windowSize: size) - - let cgImage = context.makeImage()! - - return UIImage(cgImage: cgImage) - } -} diff --git a/Release/numsw-xcode.playground/Sources/playgrounds/RendererDebugViewController.swift b/Release/numsw-xcode.playground/Sources/playgrounds/RendererDebugViewController.swift deleted file mode 100644 index 2847124..0000000 --- a/Release/numsw-xcode.playground/Sources/playgrounds/RendererDebugViewController.swift +++ /dev/null @@ -1,53 +0,0 @@ -// -// RendererDebugViewController.swift -// sandbox -// -// Created by omochimetaru on 2017/03/04. -// Copyright © 2017年 sonson. All rights reserved. -// - -import UIKit - -class RendererDebugViewController: UIViewController { - - required init() { - super.init(nibName: "RendererDebugViewController", bundle: nil) - } - - required init?(coder aDecoder: NSCoder) { - fatalError("init(coder:) has not been implemented") - } - - override func viewDidLoad() { - func makeRenderer() -> ChartRenderer { - let points1 = DummyData.points1() - let points2 = DummyData.points2() - - var chart = Chart() - chart.elements = [ - .line(LineGraph(points: points1)), - .line(LineGraph(points: points2)) - ] - chart.computeViewport() - - return ChartRenderer(chart: chart) - } - - renderer = makeRenderer() - - } - - var renderer: Renderer? - - @IBOutlet var imageView: UIImageView! - - @IBAction func onRenderButton(sender: UIButton) { - let image = renderer!.renderToImage(size: imageView.bounds.size) - imageView.image = image - } - - @IBAction func onClearButton() { - imageView.image = nil - } - -} diff --git a/Release/numsw-xcode.playground/Sources/playgrounds/RendererUtil.swift b/Release/numsw-xcode.playground/Sources/playgrounds/RendererUtil.swift deleted file mode 100644 index fbc8034..0000000 --- a/Release/numsw-xcode.playground/Sources/playgrounds/RendererUtil.swift +++ /dev/null @@ -1,119 +0,0 @@ -// -// RendererUtil.swift -// sandbox -// -// Created by omochimetaru on 2017/03/04. -// Copyright © 2017年 sonson. All rights reserved. -// - -import CoreGraphics - -// Ideally, I can just define top level function. -// But in playgroundbook, we can not use packagename so need `struct` namespacing. - -public struct RendererUtil { - - public static func toRadian(_ x: CGFloat) -> CGFloat { - return x * CGFloat.pi / CGFloat(180.0) - } - - public static func toDegree(_ x: CGFloat) -> CGFloat { - return x * CGFloat(180.0) / CGFloat.pi - } - - public static func computeBounds(points: [CGPoint]) -> CGRect { - let xs = points.map { $0.x } - let ys = points.map { $0.y } - - let x0 = xs.min()! - let x1 = xs.max()! - let y0 = ys.min()! - let y1 = ys.max()! - - return CGRect(x: x0, y: y0, width: x1 - x0, height: y1 - y0) - } - - public static func adjustViewport(viewport: CGRect) -> CGRect { - // min size constraint - - var width = max(viewport.width, 2) - var height = max(viewport.height, 2) - - // 10% margin - - width += width * 0.1 - height += height * 0.1 - - return CGRect(x: viewport.midX - width / 2.0, - y: viewport.midY - height / 2.0, - width: width, - height: height) - } - - public static func computeViewportTransform( - from: CGRect, - to: CGRect, - flipY: Bool) -> CGAffineTransform { - let fw = from.width - let fh = from.height - let tw = to.width - let th = to.height - - var trans = CGAffineTransform.identity - - trans = trans.translatedBy(x: to.origin.x, y: to.origin.y) - trans = trans.translatedBy(x: (tw / 2.0), y: (th / 2.0)) - - let sx = tw / fw - var sy = th / fh - if flipY { - sy *= -1 - } - - trans = trans.scaledBy(x: sx, y: sy) - trans = trans.translatedBy(x: -(fw / 2.0), y: -(fh / 2.0)) - trans = trans.translatedBy(x: -from.origin.x, y: -from.origin.y) - return trans - } - - public static func computeViewportTransform( - viewport: CGRect, windowSize: CGSize) -> CGAffineTransform { - return computeViewportTransform(from: viewport, - to: CGRect(origin: CGPoint.zero, - size: windowSize), - flipY: true) - } - - public static func computeTickValues( - min: CGFloat, - max: CGFloat, - step: CGFloat) -> [CGFloat] { - let x0 = Int(floor(min / step)) - let x1 = Int(ceil(max / step)) - - var ret: [CGFloat] = [] - for xi in x0...x1 { - let x = CGFloat(xi) * step - ret.append(x) - } - - return ret - } - - public static func drawLine(context: CGContext, - points: [CGPoint]) { - context.setLineWidth(2.0) - - if points.count < 2 { - return - } - - context.move(to: points[0]) - for i in 1.. CGRect { - return RendererUtil.computeBounds(points: points) - } -} diff --git a/Release/numsw-xcode.playground/Sources/playgrounds/ScatterGraphRenderer.swift b/Release/numsw-xcode.playground/Sources/playgrounds/ScatterGraphRenderer.swift deleted file mode 100644 index aa26c67..0000000 --- a/Release/numsw-xcode.playground/Sources/playgrounds/ScatterGraphRenderer.swift +++ /dev/null @@ -1,46 +0,0 @@ -// -// ScatterGraphRenderer.swift -// sandbox -// -// Created by omochimetaru on 2017/03/06. -// Copyright © 2017年 sonson. All rights reserved. -// - -import UIKit -import CoreGraphics - -public class ScatterGraphRenderer: Renderer { - public init(viewport: CGRect, scatter: ScatterGraph) { - self.viewport = viewport - self.scatter = scatter - } - - public let viewport: CGRect - public let scatter: ScatterGraph - - public func render(context: CGContext, windowSize: CGSize) { - viewportTransform = RendererUtil.computeViewportTransform(viewport: viewport, windowSize: windowSize) - drawScatter(context: context, scatter: scatter) - } - - public func drawScatter(context ctx: CGContext, scatter: ScatterGraph) { - ctx.setStrokeColor(UIColor.green.cgColor) - - let t = viewportTransform! - - for point in scatter.points { - let p = point.applying(t) - - RendererUtil.drawLine(context: ctx, points: [ - CGPoint(x: p.x - 10, y: p.y - 10), - CGPoint(x: p.x + 10, y: p.y + 10) - ]) - RendererUtil.drawLine(context: ctx, points: [ - CGPoint(x: p.x - 10, y: p.y + 10), - CGPoint(x: p.x + 10, y: p.y - 10) - ]) - } - } - - private var viewportTransform: CGAffineTransform? -} diff --git a/Release/numsw-xcode.playground/contents.xcplayground b/Release/numsw-xcode.playground/contents.xcplayground deleted file mode 100644 index 5da2641..0000000 --- a/Release/numsw-xcode.playground/contents.xcplayground +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file