From f3f0f90a51412d1684b43be6f2b5f93cb4154ee0 Mon Sep 17 00:00:00 2001 From: Arturs Artamonovs Date: Fri, 25 Oct 2024 10:29:54 +0100 Subject: Initial empty project structure --- LA/Lib/Matrix.swift | 73 +++++++++++++++++++++++++++++++++++++++++ LA/Test/MatrixBenchmark.swift | 7 ++++ LA/Test/MatrixOperations.swift | 8 +++++ LA/Test/MatrixXT/MatrixXT.swift | 53 ++++++++++++++++++++++++++++++ 4 files changed, 141 insertions(+) create mode 100644 LA/Lib/Matrix.swift create mode 100644 LA/Test/MatrixBenchmark.swift create mode 100644 LA/Test/MatrixOperations.swift create mode 100644 LA/Test/MatrixXT/MatrixXT.swift (limited to 'LA') diff --git a/LA/Lib/Matrix.swift b/LA/Lib/Matrix.swift new file mode 100644 index 0000000..8b3f538 --- /dev/null +++ b/LA/Lib/Matrix.swift @@ -0,0 +1,73 @@ +// +// Matrix.swift +// PrySDR +// +// Created by Jacky Jack on 20/10/2024. +// + +enum MatrixOperationError:Error { + case columnValue + case rowValue +} + +/// Matrix Data Type +class Matrix { + /// matrix row number + let rows: Int + /// matrix column number + let columns: Int + /// array where matrix is stored + var data: [Float] + + /// Create NxM matrix + /// - Parameters: + /// - rows: number of rows in matrix + /// - columns: number of columns in matrix + /// - val: default value of matrix elements + init(rows: Int, columns: Int, val: Float) throws { + if (rows < 1) { + throw MatrixOperationError.columnValue + } + if (columns < 1) { + throw MatrixOperationError.rowValue + } + self.rows = rows + self.columns = columns + self.data = Array(repeating: 0.0, count: rows*columns) + //print(self.data) + } + + /// Set elements to value + /// - Parameters: + /// - val: value to set + func set(_ val: Float) { + for idx in 0.. Matrix { + return try! Matrix.init(rows: rows, columns: columns, val: 0.0) +} + +/// Return identity matrix +/// - Parameters: +/// - rows: number of rows in matrix +/// - columns: number of columns in matrix +func one(_ rows: Int, _ columns: Int) -> Matrix { + return try! Matrix.init(rows: rows, columns: columns, val: 1.0) +} diff --git a/LA/Test/MatrixBenchmark.swift b/LA/Test/MatrixBenchmark.swift new file mode 100644 index 0000000..7057b9e --- /dev/null +++ b/LA/Test/MatrixBenchmark.swift @@ -0,0 +1,7 @@ +// +// MatrixBenchmark.swift +// PrySDR +// +// Created by Jacky Jack on 21/10/2024. +// + diff --git a/LA/Test/MatrixOperations.swift b/LA/Test/MatrixOperations.swift new file mode 100644 index 0000000..a5ff995 --- /dev/null +++ b/LA/Test/MatrixOperations.swift @@ -0,0 +1,8 @@ +// +// MatrixOperations.swift +// PrySDR +// +// Created by Jacky Jack on 21/10/2024. +// + + diff --git a/LA/Test/MatrixXT/MatrixXT.swift b/LA/Test/MatrixXT/MatrixXT.swift new file mode 100644 index 0000000..55661be --- /dev/null +++ b/LA/Test/MatrixXT/MatrixXT.swift @@ -0,0 +1,53 @@ +// +// MatrixXT.swift +// MatrixXT +// +// Created by Jacky Jack on 21/10/2024. +// + +import Testing + +struct MatrixXT { + + @Test func example() async throws { + // Write your test here and use APIs like `#expect(...)` to check expected conditions. + } + + @Test func matrix_create_with_init() async throws { + do { + let _ = try Matrix(row:-1,column: -1,val:0.0) + Issue.record("Should fail") + } catch { + //should allway get here, + } + do { + let _ = try Matrix(row: 0, column: 0, val: 0.0) + Issue.record("Should fail") + } catch { + //should allways get here + } + do { + let _ = try Matrix(row: 1, column: 1, val: 0.0) + } catch { + Issue.record("Failed") + } + + do { + let _ = try Matrix(row: 2, column: 2, val: 0.0) + } catch { + Issue.record("Failed") + } + do { + let _ = try Matrix(row: 3, column: 3, val: 0.0) + } catch { + Issue.record("Failed") + } + do { + let _ = try Matrix(row:16, column: 16, val: 0.0) + } catch { + Issue.record("Failed") + } + +} + +} -- cgit v1.2.3