Giter Club home page Giter Club logo

silversapphire / eigen-malloc-free-pseudoinverse Goto Github PK

View Code? Open in Web Editor NEW
11.0 11.0 2.0 955 KB

This extended Eigen C++ template library and wrapper provide a malloc-free Moore-Penrose pseudoinverse solver.

CMake 0.01% C++ 98.62% C 1.36% Makefile 0.01%
bidiagonalization cpp eigen eigen-library embedded embedded-systems heap-memory linear-algebra malloc malloc-free mathematics memory-allocation memory-management safety-critical safety-critical-systems singular-value-decomposition

eigen-malloc-free-pseudoinverse's Introduction

Malloc-Free Pseudoinverse Solver with Eigen C++ Template Library

This extended Eigen C++ template library and wrapper provides a malloc-free Moore-Penrose pseudoinverse solver.

Standard Eigen pseudoinverse functions, even those with memory preallocation variants, perform heap allocation during execution. This runtime allocation is undesirable in certain cases, particularly in embedded systems. The BDCSVD class has been extended to allow for up-front allocation of all used memory, with an ensuing malloc-free execution of the decomposition.

The development of this Eigen extension was presented at the Flight Software Workshop, and the talk can be viewed here.

Installation

  • The modified Eigen directory can be used or installed exactly as the original Eigen: simply include the headers in your project, and you are good to go! No make required. See here for the original Eigen documentation.

  • Example pseudoinverse-solving code around the new Eigen functionality is located in PINV. This can be used as-is, requiring compilation and linking of PINV, or can be copied and pasted into your project as appropriate.

Usage

#include "PINV.h"

int main() {
    PINV p; //Initial heap allocation

    Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> input =
        Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>::Zero(3, 2);
    Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> output =
        Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>::Zero(input.cols(), input.rows());

    //fill in input...

    p.reset();
    p.bd.input.block(0, 0, input.rows(), input.cols()) = input;
    p.calculate();
    output = p->bd.output.block(0, 0, output.rows(), output.cols());
}

Constraints

The malloc-free pseudoinverse solver is currently limited to problem sizes of 2500x17 or less. It is not currently templated and is restricted to doubles. Row major is used.

Tests

Tests and more example usages can be found in demo.cpp.

Eigen developer-specific preprocessor directives are used to ensure that Eigen performs no heap allocation during execution, specifically EIGEN_RUNTIME_NO_MALLOC.

Technical Details

Simply, the Eigen modifications allow for BDCSVD to calculate the relevant pieces of a pseudoinverse computation without heap allocation. PINV provides code that takes these pieces and uses them to find the actual pseudoinverse, also without heap allocation. Mathematically, the Moore-Penrose pseudoinverse is calculated via the singular value decomposition through bidiagonalization: BDCSVD inside Eigen performs the decomposition, and PINV uses the decomposition to calculate the pseudoinverse.

Extensions

Modified Eigen files include:

  • Core/MatrixBase
  • Householder/Householder
  • QR/ColPivHouseholderQR
  • SVD/BDCSVD
  • SVD/JacobiSVD
  • SVD/UpperBidiagonalization

and more. See this PR for the full Eigen diff.

Notes

There are numbers hardcoded into the PINV memory initializations for all of the temporary variables. These were determined by stepping through normal malloc'ing execution of BDCSVD with the original 2500x17 problem size and noting down the allocated sizes. These numbers would likely change if the temporaries were initialized with sizes other than 2500x17. This is also why problem sizes exceeding 2500x17 would likely fail to execute.

Problem sizes smaller than 2500x17 can still be computed accurately since the pseudoinverse is forgiving of 0's padding the matrices-- ie, these do not affect the correctness of the output. It is thus important to zero out the input before filling in the problem set.

Licensing

This open-source code is forked from Eigen 3.3.7. For the Eigen MPL2 and other licensing information, see here.

eigen-malloc-free-pseudoinverse's People

Contributors

silversapphire avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  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.