Code Monkey home page Code Monkey logo

ctla's Introduction

Compile-time linear algebra in C++

This library provides the class template ctla::matrix and its associated operations for compile-time (constexpr) linear algebra.

Key features

• Header-only library

• All functions constexpr, all computation done at compile time

• Intuitive syntax for initialisation, indexing, augmenting

• Matrix arithmetic including inverses and linear systems supported

• Block matrices supported

• Runtime printing in MATLAB-compatible syntax if required

• Documentation…not written. Available matrix operations can be found in matrix.h, and see the examples start_here.cpp, arithmetic.cpp, regression.cpp, blocks.cpp.

Installation

Nothing to install. Just #include "matrix.h" and off you go.

Requires

C++17 conforming compiler. Tested on Clang 5.0 and GCC 7.2.

Example

// ********************************
// Simple linear regression example
// ********************************

#include "matrix.h"

using namespace ctla;

template<auto Val>
struct Print{
    // force a compile error, hopefully with a message that outputs Val
    char dummy[0*Val-1];
};

void regression() {

    // The input values
    constexpr auto x = seq<1,10>().T();

    // The response values
    constexpr matrix<double, 10, 1> y(
        {3.8180, 5.0613, 5.2806, 4.0659, 4.1211, 2.2983, 0.2743, -0.9785, -6.8954, -10.4222}
    );

    // Try a linear model y = c0 + c1*x
    constexpr auto A_linear = augc(ones<double,10,1>(), x);
    constexpr auto c_linear = A_linear % y;  // c = A % y means solve the linear system A*c = y
                                             // (in the least squares sense if necessary)
    constexpr auto y_linear = A_linear * c_linear;
    
    // Compute the R^2 value
    constexpr auto SStot           = normsq(y - mean(y));
    constexpr auto SSres_linear    = normsq(y - y_linear);
    constexpr auto Rsquared_linear = 1 - SSres_linear / SStot;
    Print<int(Rsquared_linear*100)>();
    // compiler prints error about Print<76>  i.e.  R^2 == 76%
    
    // Try a quadratic model y = c0 + c1*x + c2*x^2
    constexpr auto A_quadratic = augc(A_linear, mul(x,x));
    constexpr auto c_quadratic = A_quadratic % y;
    constexpr auto y_quadratic = A_quadratic * c_quadratic;
    
    // See if we get a better R^2 value
    constexpr auto SSres_quadratic    = normsq(y - y_quadratic);
    constexpr auto Rsquared_quadratic = 1 - SSres_quadratic / SStot;
    Print<int(Rsquared_quadratic*100)>();
    // compiler prints error about Print<98>  i.e.  R^2 == 98%
    
}

ctla's People

Contributors

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