Skip to content

Commit

Permalink
init for setting up start value witha function, dump for Matrix conte…
Browse files Browse the repository at this point in the history
…nts, apply for runnign function on each Matrix element
  • Loading branch information
dyashkir committed Dec 13, 2016
1 parent 88bc48b commit 3e45f02
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Source/Matrix.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ public struct Matrix<T> where T: FloatingPoint, T: ExpressibleByFloatLiteral {
self.grid = [Element](repeating: repeatedValue, count: rows * columns)
}

public init(rows: Int, columns: Int, valueFunc: ()->Element){
self.rows = rows
self.columns = columns

self.grid = [Element](repeating: valueFunc(), count: rows * columns)
for i in 0..<grid.count{
self.grid[i] = valueFunc()
}
}




public init(_ contents: [[Element]]) {
let m: Int = contents.count
let n: Int = contents[0].count
Expand Down Expand Up @@ -333,6 +346,19 @@ public func transpose(_ x: Matrix<Double>) -> Matrix<Double> {
return results
}


public func apply<T>(_ x: Matrix<T>, function : (T)->T)->Matrix<T>{
var m = Matrix<T>(rows: x.rows, columns: x.columns, repeatedValue: 0.0)
for i in 0..<x.grid.count{
m.grid[i] = function(x.grid[i])
}
return m
}

public func dump<T>(matrix:Matrix<T>) -> [T]{
return matrix.grid
}

// MARK: - Operators

public func + (lhs: Matrix<Float>, rhs: Matrix<Float>) -> Matrix<Float> {
Expand Down Expand Up @@ -387,3 +413,4 @@ public postfix func ′ (value: Matrix<Float>) -> Matrix<Float> {
public postfix func (value: Matrix<Double>) -> Matrix<Double> {
return transpose(value)
}

0 comments on commit 3e45f02

Please sign in to comment.