Skip to content

Commit

Permalink
Add Tensor initializer with TensorSlice
Browse files Browse the repository at this point in the history
  • Loading branch information
aidangomez committed Nov 20, 2015
1 parent 5ee2680 commit 0cf2303
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Source/Tensor/Tensor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ public class Tensor<Element: Value> : Equatable {
self.dimensions = tensor.dimensions
self.elements = ValueArray<Element>(tensor.elements)
}

public convenience init(_ tensorSlice: TensorSlice<Element>) {
self.init(dimensions: tensorSlice.dimensions)
for index in Span(zeroTo: dimensions) {
self[index] = tensorSlice[index]
}
}

public init(_ matrix: Matrix<Element>) {
self.dimensions = [matrix.rows, matrix.columns]
Expand Down Expand Up @@ -122,6 +129,11 @@ public class Tensor<Element: Value> : Equatable {
tensorSlice[index] = newValue
}
}

public func reshape(dimensions: Int...) {
precondition(dimensions.reduce(1, combine: *) == count)
self.dimensions = dimensions
}

func extractMatrix(span: Span) -> Matrix<Element> {
assert(spanIsValid(span))
Expand Down

0 comments on commit 0cf2303

Please sign in to comment.