Skip to content
Extraits de code Groupes Projets
Sélectionner une révision Git
  • 8afa20cba5861fb56f41161096c3f13f4d0490ad
  • master par défaut protégée
2 résultats

matlab.h

Blame
  • matlab.h 6,30 Kio
    #pragma once
    
    #include <random>
    #include <iostream>
    #include <fstream>
    #include "../types.h"
    #include <complex>
    /*
     * Code translated from Matlab base functions
     * matlab translation guide : https://eigen.tuxfamily.org/dox/AsciiQuickReference.txt
     */
    
    //-------------------------------------------------------------------------------------------------
    // VARIOUS
    
    const number pi = M_PI;
    
    // squares a number
    template<typename T> inline T squared(const T& x)
    {
        return x*x;
    }
    
    //-------------------------------------------------------------------------------------------------
    // ALGEBRA
    
    // build a vector with values in the given interval
    Vector make_vector(const int fromValue, const int toValue, const int by = 1)
    {
        int size = 1 + (toValue - fromValue) / by;
        Vector result(size);
    
        int currentValue = fromValue;
        for(int i = 0; i < size; i++)
        {
            result(i) = currentValue;
            currentValue += by;
        }
    
        return result;
    }
    
    // repeats a column vector n times in order to build a matrix
    Matrix repmat(const Vector& v, const int colNumber)
    {
        Matrix result(v.size(), colNumber);
    
        for(int col = 0; col < colNumber; col++)
        {
            result.col(col) = v;
        }
    
        return result;
    }
    
    // solves m = m1 \ m2
    inline ComplexMatrix mldivide(const ComplexMatrix& m1, const ComplexMatrix& m2)
    {
        //return m1.colPivHouseholderQr().solve(m2);
        return m1.bdcSvd(Eigen::ComputeThinU | Eigen::ComputeThinV).solve(m2); // you can use JacobiSVD for improved precision
    }
    
    // returns an hermitian matrix where v is the first row and conj(v) the first column
    Matrix toeplitz(const Vector& v)
    {
        auto size = v.size();
        Matrix result(size, size);
    
        for(int row=0; row<size; row++)
        {