DLabMatrix Swift Package


A swift library package with matrix algebraic objects and functions necessary for landscape and population genetic analyses on the iOS and MacOS platforms.

Rodney Dyer https://dyerlab.org (Center for Environmental Studies)https://ces.vcu.edu


Current Version:

This package is the foundation layer for all matrix algebra routines needed in software developed for the iOS and OSX platforms from the DyerLab. The motivation herenotion here is to provide an abstraction layer relying as much upon the Swift Accelerate framework to allow population genetic and spatial analytic routines to be easily added to any deliverable product.

## Installation

Swift Package Manager (XCode 13)

  1. Select File > Swift Packages > Add Package Dependency… from the File menu.
  2. Paste https://github.com/dyerlab/DLabMatrix.git in the dialog box.
  3. Follow the Xcode’s instruction to complete the installation.

Why not CocoaPods, or Carthage, or blank?

Supporting multiple dependency managers makes maintaining a library exponentially more complicated and time consuming. Since, the Swift Package Manager is integrated with Xcode 11 (and greater), it’s the easiest choice to support going further.

This package defines the following two objects:

This packages defines the following general functions:

### Vector Objects

A Vector object is simple [Double] useable for normal algebraic operations. The

Instance Variables:

Static Constructors

Instance Functions

Static Functions - .designMatrix( strata: [String] ) -> Matrix Returns (N x K) design matrix. - .idempotentHatMatrix( strata: [String] ) -> Matrix Returns N x N idempotent Hat matrix, H.

Operator Overloads

The following operators are defined for the Vector object v:


The Vector object defines the protocol VectorConvertable which defined the required function asVector() -> Vector to advertise that they can yield a Vector object.

### Matrix Objects

A matrix object is a class that represents a 2-dimensional representation of type Double’s. A Matrix has the following instance variables:

Constructors - Matrix(r,c,Vector) Creates a new Matrix with r rows and c columns with values from Vector. - Matrix(r,c,ClosedRange) Creates a new Matrix with r rows and c whose falues are equally spaced along a ClosedRange<Double>. - Matrix(r,c,rowNames,colNames) Creates a new Matrix with r rows and c columns with values set to 0.0 but with the row and column names set by the vectors rowNames and colNames of length r and c (respectively).

The following operators are overloaded for an object of type Matrix:

The following functions are available for Matrix objects: - .center() Centers the values of the matrix to 0.0 - .submatrix([rows],[cols]) -> Matrix Returns submatrix defined by the integer arrays [rows] and [cols]. -


The Matrix object defines the protocol MatrixConvertable that defines a required function asMatrix() -> Matrix is to advertise that they can yield a Matrix object.

### RangeEnum

A simple enum defining the following values: - uniform_0_1 A uniform distribution bound to [0.0 ... 1.0]. - uniform_neg1_1 A uniform distribution bound on [-1.0 ... 1.0]. - normal_0_1 A value from the normal probability density function bound on [0 ... 1].

This enum conforms to the following protocols: - Int - CaseIterable - Comoparable Defines < operator.

Algebraic Functions

GeneralizedInverse( X: Matrix ) -> Matrix

This returns a generalized inverse of the original matrix.

PCRotation(X: Matrix) -> (d: Vector, V: Matrix, X: Matrix)?

Performs a principal component rotation on the original data matrix X returning the eigenvalues in d, the loadings in V and the predicted projections of the original data in X. If the original matrix was not factorable, no values are returned.

SingularValueDecomposition(A: Matrix) -> (U: Matrix, d: Vector, V: Matrix)?

Performs a singular value spectral decomposition on the matrix A resulting in left and right eigenvalues (U and V) as well as eigenvalues in d. If the original matrix was not factorable, no values are returned.

Versions Highlights

1.0.3 - Refactoring some stuff.
1.0.2 - added designMatrix, idepotentHatMatrix, asCovariance, asDistance, rowMatrix
1.0.1 - Added Unit tests.
1.0.0 - Intial Import


For attribution, please cite this work as

Dyer (2021, Dec. 24). The Dyer Laboratory: DLabMatrix Swift Package. Retrieved from https://dyerlab.github.io/DLabWebsite/software/DLabMatrix/

BibTeX citation

  author = {Dyer, Rodney},
  title = {The Dyer Laboratory: DLabMatrix Swift Package},
  url = {https://dyerlab.github.io/DLabWebsite/software/DLabMatrix/},
  year = {2021}