Giter VIP home page Giter VIP logo

fast-matrix's Introduction

Fast-matrix (tensor?)

This small library optimizes the associative order of matrix multiplication (in the future -- tensor contraction) using a dynamic programming algorithm.

Abstract

Consider 3 matrices A_(50 . 5), B_(5 . 100), C_(100 . 10). We can obtain matrix

D = A * B * C;

in two ways:

$$D = (AB)C , D = A(BC)$$

Using the first way gives 50*5*100 + 50*100*10 = 75000 scalar multiplications. Using the second way gives 5*100*10 + 50*5*10 = 7500, which is ten times less. Our goal is to minimize the number of multiplications.

Function %build-tree takes a vector of dimensions, #((4 . 5) (5 . 10) (10 . 2) (2 . 8) (8 . 6) (6 . 5) (5 . 10) ...) (every element corresponds to (height . width) of a matrix) and returns list of (the best tree for the whole range from 0 to n, the resulting matrix size, the total number of scalar multiplications)

Algorithm

Dynamic programming - we solve it for each subrange (from i to j).

Ranges of length 1 and 2 are trivial. For range from i to (+ i k) now -- there are k-1 options, of which we chose the best one. Options are:

  • multiply i matrix by the best of range (+ i 1) to (+ i k)

  • multiply the best of range i, (+ i 1) to the best of range (+ i 2) (+ i k)

  • multiply the best of range i, (+ i 2) to the best of range (+ i 3) (+ i k)

...etc

The best one is the one with the lowest (total) amount of multiplications.

License

MIT. Contributions are welcome.

fast-matrix's People

Contributors

alex-eg avatar commander-trashdin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

alex-eg

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.