// SpaFEDTe, a Template based C++ library for creating 
// Discontinuous Finite Element Spaces,
// Copyright (C) 2012 Lorenzo Alessio Botti

/* This library is free software; you can redistribute it and/or */
/* modify it under the terms of the GNU Lesser General Public */ 
/* License as published by the Free Software Foundation either */ 
/* version 3.0 of the License, or (at your option) any later version. */

/* This software is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU */
/* Lesser General Public License for more details. */

/* You should have received a copy of the GNU Lesser General Public */
/* License along with this software; if not, a copy of the full */
/* GNU Lesser General Public License can be found at */
/* http://www.gnu.org/licenses/ */

#ifndef __dimension_based_polynomial_h__
#define __dimension_based_polynomial_h__

#include "EnumElemType.hpp"
#include "Eigen/Dense"

struct DimBasedPoly 
{
  static void computeBasisIndex(Eigen::Matrix<Eigen::Matrix<unsigned int,1,1>,Eigen::Dynamic,1>&  basisIndex, const ElemType itemType, const unsigned int order)
  {
    basisIndex.resize(order+1);
    for (unsigned int i = 0; i < (order + 1); i++)
    {
      basisIndex(i)(0) = i; 
    }
    return;
  }

  static void computeBasisIndex(Eigen::Matrix<Eigen::Matrix<unsigned int,2,1>,Eigen::Dynamic,1>&  basisIndex, const ElemType itemType, const unsigned int order)
  {
    basisIndex.resize((order+1)*(order+2)/2);
    unsigned int v = 0;
    for (unsigned int i = 0; i <= order; i++)
    {
      for (unsigned int j = 0; j <= i; j++)
      {
        basisIndex(v)(0)= i - j;
        basisIndex(v)(1)= j;
        v++;
      }
    }
    return;
  }
  
  static void computeBasisIndex(Eigen::Matrix<Eigen::Matrix<unsigned int,3,1>,Eigen::Dynamic,1>&  basisIndex, const ElemType itemType, const unsigned int order)
  {
    basisIndex.resize((order+1)*(order+2)*(order+3)/6);
    unsigned int v = 0;
    for (unsigned int i = 0; i <= order; i++)
    {
      for (unsigned int j = 0; j <= i; j++)
      {
        for (unsigned int k = 0; k <= j; k++)
        {
          basisIndex(v)(0)= i - j;
          basisIndex(v)(1)= j - k;
          basisIndex(v)(2)= k;
          v++;
        }
      }
    }
    return;
  }

//    to be replaced with (c++11)
//    constexpr unsigned int modesNumber(const unsigned int Dim, const unsigned int order, const ElemType elemType)
//    {
//      return std::tgamma(order+Dim+1)/(std::tgamma(Dim+1)*std::tgamma(order+1));
//    }
  static unsigned int modesNumber(const unsigned int Dim, const unsigned int order, const ElemType elemType)
  {
    switch (Dim)
    {
      case 1:
      {
        return (order+1);
      }
      case 2:
      {
        return ((order+1)*(order+2)/2);
      }
      case 3:
      {
        return ((order+1)*(order+2)*(order+3)/6);
      }
      default:
      {
        std::cerr<< "DimBasedPoly: Unsupported Dim "<<Dim<<std::endl;
        std::logic_error("halt");
	return 0;
      }
    }
  }
};
 
#endif
