<commit_before>//
//  vector.swift
//  stem
//
//  Created by <NAME> on 12/10/15.
//  Copyright © 2015 none. All rights reserved.
//

import Foundation

/*
The following classes add constraints to the Tensor class. This can
be useful for dispatching to functions based on those constraints
(e.g. if a function needs a RowVector, any type of Vector, or a
Matrix).
*/

// can be either row or column vector
public class Vector<StorageType:Storage>: Tensor<StorageType> {
    public init(_ array:[StorageType.ElementType], transposed:Bool=false) {
        super.init(array: array, shape: Extent(array.count))
        self.transposed = transposed
    }
    
    public init(_ tensor:Tensor<StorageType>) {
        assert(tensor.shape.span == 1)
        super.init(tensor)
    }
    
    public init(_ vector:Vector<StorageType>, dimIndex:[Int]?=nil, view:StorageView<StorageType>?=nil) {
        super.init(vector, dimIndex: dimIndex, view: view)
    }
    
    public init(rows:Int) {
        super.init(shape: Extent(rows))
    }
    
    public init(cols:Int) {
        super.init(shape: Extent(cols))
        transposed = true
    }
    
    public override init(storage:StorageType, shape:Extent, view:StorageView<StorageType>?=nil, offset:Int?=nil) {
        super.init(storage: storage, shape: shape, view: view, offset: offset)
    }
    
    public override func transpose() -> Vector {
        let newDimIndex = Array(dimIndex.reverse())
        let newShape = Extent(view.shape.reverse())
        let newOffset = Array(view.offset.reverse())
        let newView = StorageView<StorageType>(shape: newShape, offset: newOffset)
        return Vector(self, dimIndex: newDimIndex, view: newView)
    }
}

// constrained to be just a column vector
public class ColumnVector<StorageType:Storage>: Vector<StorageType> {
    public init(_ array:[StorageType.ElementType]) {
        super.init(array, transposed: false)
    }
    
    public override init(_ tensor:Tensor<StorageType>, dimIndex:[Int]?=nil, view:StorageView<StorageType>?=nil) {
        // verify we're being pass a vector
        assert(tensor.shape.span == 1)
        super.init(tensor)
    }
    
//    public override init(_ vector:Vector<StorageType>, dimIndex:[Int]?=nil, view:StorageView<StorageType>?=nil) {
//        super.init(vector, dimIndex: dimIndex, view: view)
//    }
    
    public override init(rows:Int) {
        super.init(rows: rows)
    }
    
//    public override init(storage:StorageType, shape:Extent, view:StorageView<StorageType>?=nil, offset:Int?=nil) {
//        super.init(storage: storage, shape: shape, view: view, offset: offset)
//    }
    
    public override func transpose() -> RowVector<StorageType> {
        let newDimIndex = Array(dimIndex.reverse())
        let newShape = Extent(view.shape.reverse())
        let newOffset = Array(view.offset.reverse())
        let newView = StorageView<StorageType>(shape: newShape, offset: newOffset)
        return RowVector<StorageType>(self, dimIndex: newDimIndex, view: newView)
    }
}

// constrained to be just a row vector
public class RowVector<StorageType:Storage>: Vector<StorageType> {
    public init(_ array:[StorageType.ElementType]) {
        super.init(array, transposed: true)
    }
    
    public override init(_ tensor:Tensor<StorageType>) {
        // verify we're being passed a vector
        assert(tensor.shape.span == 1)
        
        // verify the vector lies along the row-dimension<commit_msg>Vectors now can occupy an arbitrary dimension.<commit_after>//
//  vector.swift
//  stem
//
//  Created by <NAME> on 12/10/15.
//  Copyright © 2015 none. All rights reserved.
//

import Foundation

/*
The following classes add constraints to the Tensor class. This can
be useful for dispatching to functions based on those constraints
(e.g. if a function needs a RowVector, any type of Vector, or a
Matrix).
*/

// can be either row or column vector
public class Vector<StorageType:Storage>: Tensor<StorageType> {
    public init(_ array:[StorageType.ElementType], axis:Int=0, transposed:Bool=false) {
        var dims = [Int](count: axis+1, repeatedValue: 1)
        dims[axis] = array.count
        super.init(array: array, shape: Extent(dims))
        self.transposed = transposed
    }
    
    public init(_ tensor:Tensor<StorageType>) {
        assert(tensor.shape.span == 1)
        super.init(tensor)
    }
    
    public init(_ vector:Vector<StorageType>, dimIndex:[Int]?=nil, view:StorageView<StorageType>?=nil) {
        super.init(vector, dimIndex: dimIndex, view: view)
    }
    
    public init(rows:Int) {
        super.init(shape: Extent(rows))
    }
    
    public init(cols:Int) {
        super.init(shape: Extent(cols))
        transposed = true
    }
    
    public override init(storage:StorageType, shape:Extent, view:StorageView<StorageType>?=nil, offset:Int?=nil) {
        super.init(storage: storage, shape: shape, view: view, offset: offset)
    }
    
    public override func transpose() -> Vector {
        let newDimIndex = Array(dimIndex.reverse())
        let newShape = Extent(view.shape.reverse())
        let newOffset = Array(view.offset.reverse())
        let newView = StorageView<StorageType>(shape: newShape, offset: newOffset)
        return Vector(self, dimIndex: newDimIndex, view: newView)
    }
}

// constrained to be just a column vector
public class ColumnVector<StorageType:Storage>: Vector<StorageType> {
    public init(_ array:[StorageType.ElementType]) {
        super.init(array, axis: 1, transposed: false)
    }
    
    public override init(_ tensor:Tensor<StorageType>, dimIndex:[Int]?=nil, view:StorageView<StorageType>?=nil) {
        // verify we're being pass a vector
        assert(tensor.shape.span == 1)
        super.init(tensor)
    }
    
//    public override init(_ vector:Vector<StorageType>, dimIndex:[Int]?=nil, view:StorageView<StorageType>?=nil) {
//        super.init(vector, dimIndex: dimIndex, view: view)
//    }
    
    public override init(rows:Int) {
        super.init(rows: rows)
    }
    
//    public override init(storage:StorageType, shape:Extent, view:StorageView<StorageType>?=nil, offset:Int?=nil) {
//        super.init(storage: storage, shape: shape, view: view, offset: offset)
//    }
    
    public override func transpose() -> RowVector<StorageType> {
        let newDimIndex = Array(dimIndex.reverse())
        let newShape = Extent(view.shape.reverse())
        let newOffset = Array(view.offset.reverse())
        let newView = StorageView<StorageType>(shape: newShape, offset: newOffset)
        return RowVector<StorageType>(self, dimIndex: newDimIndex, view: newView)
    }
}

// constrained to be just a row vector
public class RowVector<StorageType:Storage>: Vector<StorageType> {
    public init(_ array:[StorageType.ElementType]) {
        super.init(array, axis: 0, transposed: true)
    }
    
    public override init(_ tensor:Tensor<StorageType>) {
        // verify we're being passed a vector
        assert(tensor.shape.span == 1)
        
        // verify the vector lies along the row-dimension
{'fraction_non_alphanumeric': 0.11955449034259129, 'fraction_numerical': 0.0043705061328069925, 'mean_word_length': 3.10294968189705, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '576063', 'n_tokens_mistral': 2108, 'n_tokens_neox': 2068, 'n_words': 544}