Skip to content

Commit da99de9

Browse files
committed
Add directory for Pad protocol and extensions
1 parent 490b0ff commit da99de9

File tree

5 files changed

+88
-76
lines changed

5 files changed

+88
-76
lines changed

Sources/Numerix/Core/Pad.swift

Lines changed: 0 additions & 76 deletions
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
Double extension to conform to Pad protocol.
3+
*/
4+
5+
import Accelerate
6+
7+
@_documentation(visibility: private)
8+
extension Double: Pad {
9+
public static func pad(a: Matrix<Double>, value: Double) -> Matrix<Double> {
10+
let m = vDSP_Length(a.columns) // number of columns to copy
11+
let n = vDSP_Length(a.rows) // number of rows to copy
12+
let ta = vDSP_Length(a.columns) // number of columns in a
13+
let tc = vDSP_Length(a.columns + 2) // number of columns in c
14+
15+
let c = Matrix<Double>(rows: a.rows + 2, columns: a.columns + 2, fill: value)
16+
vDSP_mmovD(a.buffer.baseAddress!, c.buffer.baseAddress! + (a.columns + 3), m, n, ta, tc)
17+
18+
return c
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
Float extension to conform to Pad protocol.
3+
*/
4+
5+
import Accelerate
6+
7+
@_documentation(visibility: private)
8+
extension Float: Pad {
9+
public static func pad(a: Matrix<Float>, value: Float) -> Matrix<Float> {
10+
let m = vDSP_Length(a.columns) // number of columns to copy
11+
let n = vDSP_Length(a.rows) // number of rows to copy
12+
let ta = vDSP_Length(a.columns) // number of columns in a
13+
let tc = vDSP_Length(a.columns + 2) // number of columns in c
14+
15+
let c = Matrix<Float>(rows: a.rows + 2, columns: a.columns + 2, fill: value)
16+
vDSP_mmov(a.buffer.baseAddress!, c.buffer.baseAddress! + (a.columns + 3), m, n, ta, tc)
17+
18+
return c
19+
}
20+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
Matrix extension for Pad protocol.
3+
*/
4+
5+
extension Matrix where Scalar: Pad, Scalar: Numeric {
6+
7+
/// Pad a matrix with a constant value.
8+
///
9+
/// Create a padded matrix with zeros.
10+
/// ```swift
11+
/// let a: Matrix<Float> = [[1, 2],
12+
/// [3, 4]]
13+
/// let p = a.pad()
14+
/// // The matrix p has values of
15+
/// // 0 0 0 0
16+
/// // 0 1 2 0
17+
/// // 0 3 4 0
18+
/// // 0 0 0 0
19+
/// ```
20+
/// Create a padded matrix using a value of 9.
21+
/// ```swift
22+
/// let a: Matrix<Float> = [[1, 2],
23+
/// [3, 4]]
24+
/// let p = a.pad(with: 9)
25+
/// // The matrix p has values of
26+
/// // 9 9 9 9
27+
/// // 9 1 2 9
28+
/// // 9 3 4 9
29+
/// // 9 9 9 9
30+
/// ```
31+
///
32+
/// - Parameter value: The scalar value for padding the matrix. Default is 0.
33+
/// - Returns: A padded matrix with zeros or some other value along its border.
34+
public func pad(with value: Scalar = .zero) -> Matrix {
35+
Scalar.pad(a: self, value: value)
36+
}
37+
}

Sources/Numerix/Pad/Pad.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
Pad protocol.
3+
Inspired my the NumPy pad function https://numpy.org/doc/stable/reference/generated/numpy.pad.html
4+
*/
5+
6+
import Accelerate
7+
8+
@_documentation(visibility: private)
9+
public protocol Pad {
10+
static func pad(a: Matrix<Self>, value: Self) -> Matrix<Self>
11+
}

0 commit comments

Comments
 (0)