diff options
author | Arturs Artamonovs <arturs.artamonovs@protonmail.com> | 2024-10-25 10:29:54 +0100 |
---|---|---|
committer | Arturs Artamonovs <arturs.artamonovs@protonmail.com> | 2024-10-25 10:29:54 +0100 |
commit | f3f0f90a51412d1684b43be6f2b5f93cb4154ee0 (patch) | |
tree | ac8c4df68d9d1046bd438abe3d6a258e48e95cf2 /LA/Lib/Matrix.swift | |
parent | 8f8f78cd00d3b5e5ce5076d316f2499df35d532f (diff) | |
download | PrySDR-f3f0f90a51412d1684b43be6f2b5f93cb4154ee0.tar.gz PrySDR-f3f0f90a51412d1684b43be6f2b5f93cb4154ee0.zip |
Initial empty project structure
Diffstat (limited to 'LA/Lib/Matrix.swift')
-rw-r--r-- | LA/Lib/Matrix.swift | 73 |
1 files changed, 73 insertions, 0 deletions
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..<self.data.count { + self.data[idx] = val + } + } + /// Set all elements to 0 + func zero() { + self.set(0.0) + } + + /// Set all elements to 1 + func one() { + self.set(1.0) + } +} + +/// Return 0 matrix +/// - Parameters: +/// - rows: number of rows in matrix +/// - columns: number of columns in matrix +func zero(_ rows: Int, _ columns: Int) -> 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) +} |