Menu

How to template over different numerics?

thrope
2012-11-12
2013-05-15
  • thrope

    thrope - 2012-11-12

    Hi,

    I would like to template over different numerical types (ie normal real, double precision, 8,16,32,64 bit integers etc.)

    How do I pass these as classes to a template? mymodule<real> works but mymodule<real*8> or mymodule<real(8)> doesn't.

    Thanks

     
  • thrope

    thrope - 2012-11-12

    I guess I will post my follow up question here… what I really want to do is template over numeric types/kinds and array ranks… so I would like to be able to do something like
    template <class data_type, class rank>
    function myfunc_(x)
    data_type :: x(:,:,:)
    end function
    end template

    where the data_type can be real*4, real*8, integer*4, integer*8, integer*2 etc and rank is used to define the arrays of the right rank… ie
    rank=3 =>  data_type :: x(:,:,:)
    rank=5 => data_type :: x(:,:,:,:,:)

    How can this best be achieved with PyF95++?

    Thanks for any help

     
  • thrope

    thrope - 2012-11-12

    Sorry for the noise - of course I can pass an integer kind to the template and use that in the declaration.

    So the only remaining question is how to template expressions like

    data_type(kind=data_kind) :: x(:,:,:)
    over ranks?

     
  • thrope

    thrope - 2012-11-12

    Never mind I think I solved this one as well… at least the following seems to work using a POD:
      template <class data_type, class data_kind, class data_rank>
      function fpGetPr_(mx) result (fp)
        pod colons from mypods
         n = data_rank
         src = "data_type(kind=data_kind), pointer :: fp($cols$)"
        end pod colons

    with mypods.py:

    def colons(blksrc, globalDict):
        n = None
        src = None
        code = compile(blksrc, '', 'exec')
        exec code

        cols = n*':,'
        cols = cols
        block = src.replace('$cols$', cols)

        return block

    This is really a great tool!

     

Log in to post a comment.

MongoDB Logo MongoDB