Giter VIP home page Giter VIP logo

dimensional's Introduction

Dimensional

Dimensional defines a generic type Matrix that is parameterized by the type of element it holds.

let myBoolMatrix: Matrix = [[true, false], [false, true]]
let myStringMatrix: Matrix = [["hello", "hi", "howdy"], ["goodbye", "bye", "see ya"]]

print(myBoolMatrix.map{ $0 ? 1 : 0 }) // -> [[1, 0], [0, 1]]
print(myStringMatrix.transpose) // -> [["hello", "goodbye"], ["hi", "bye"], ["howdy", "see ya"]]

Note that matrices can be initialized from 2-dimensional array literals, and that each inner-array represents a row in the constructed Matrix.

While a Matrix can be parameterized by any type, it is most useful if it is parameterized by some numeric type since it gains tons of special abilities!

let a: Matrix = [[1, 2], [3, 4]]
let b: Matrix = [[5, 10], [-5, 0]]

print(a.determinant)      // -> -2
print(a.dot(b) * (a + b)) // -> [[60, 120], [-20, 40]]
print(a * b)              // -> [[-5, 10], [-5, 30]]

Matrices composed of floating point types gain even more amazing powers, such as the ability to take an inverse!

Not only does Matrix conform to MutableCollectionType, but it exposes two views RowView and ColumnView each of which conform to RangeReplaceableCollectionType allowing for complex manipulations.

var x: Matrix = [[1, 2], [3, 4]]
x.rows.append([5, 6])
x.columns.insert([3, 6, 9], atIndex: 2)
print(x) // -> [[1, 2, 3], [3, 4, 6], [5, 6, 9]]

I want to reiterate how cool this is! The properties rows and columns provide collections that not only allow you to inspect a Matrix, but also allow you to modify it in really familiar ways. Its interface is nearly identical to that of Array since both conform to the same protocols.

A Matrix can be initialized in many different novel ways.

let a: Matrix = Matrix([[1, 2], [3, 4]] as RowView)
let b: Matrix = Matrix([[1, 3], [2, 4]] as ColumnView)
print(a == b) // -> true

let c = Matrix(dimensions: (width: 3, height: 2), repeatedValue: 7)
print(c) // -> [[7, 7, 7], [7, 7, 7]]

let d = Matrix.identity(size: 3)
print(d) // [[1, 0, 0], [0, 1, 0], [0, 0, 1]]

dimensional's People

Contributors

jadengeller avatar mattwhiteside avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.