Numlua Anumerical Packagefo Lua

download Numlua Anumerical Packagefo Lua

of 12

Transcript of Numlua Anumerical Packagefo Lua

  • 8/19/2019 Numlua Anumerical Packagefo Lua

    1/27

    Numlua: a numerical package for Lua 

    Luis Carvalho

    Brown University

    [email protected]

    Lua Workshop

    July, 2008

  • 8/19/2019 Numlua Anumerical Packagefo Lua

    2/27

    Introduction

    Numeric Lua Lua Workshop’08

      A lot of problems in scientific computing need high performancenumerical routines

  • 8/19/2019 Numlua Anumerical Packagefo Lua

    3/27

    Introduction

    Numeric Lua Lua Workshop’08

      A lot of problems in scientific computing need high performancenumerical routines

    •   General approach: heavy computations in low level routines

    (FORTRAN or C) wrapped in a scripting  or OO  language (high level)

  • 8/19/2019 Numlua Anumerical Packagefo Lua

    4/27

    Introduction

    Numeric Lua Lua Workshop’08

      A lot of problems in scientific computing need high performancenumerical routines

    •   General approach: heavy computations in low level routines

    (FORTRAN or C) wrapped in a scripting  or OO  language (high level)

    •   Promising candidate:  Lua !

  • 8/19/2019 Numlua Anumerical Packagefo Lua

    5/27

    Introduction

    Numeric Lua Lua Workshop’08

      A lot of problems in scientific computing need high performancenumerical routines

    •   General approach: heavy computations in low level routines

    (FORTRAN or C) wrapped in a scripting  or OO  language (high level)

    •   Promising candidate:  Lua !

    •   Performance:  fast execution and small footprint

  • 8/19/2019 Numlua Anumerical Packagefo Lua

    6/27

    Introduction

    Numeric Lua Lua Workshop’08

      A lot of problems in scientific computing need high performancenumerical routines

    •   General approach: heavy computations in low level routines

    (FORTRAN or C) wrapped in a scripting  or OO  language (high level)

    •   Promising candidate:  Lua !

    •   Performance:  fast execution and small footprint

    •   Powerful:  GC, functional facilities, coroutines, metamethods

  • 8/19/2019 Numlua Anumerical Packagefo Lua

    7/27

    Introduction

    Numeric Lua Lua Workshop’08

      A lot of problems in scientific computing need high performancenumerical routines

    •   General approach: heavy computations in low level routines

    (FORTRAN or C) wrapped in a scripting  or OO  language (high level)

    •   Promising candidate:  Lua !

    •   Performance:  fast execution and small footprint

    •   Powerful:  GC, functional facilities, coroutines, metamethods

    •   Extensible:  simple and well defined API in ANSI C

  • 8/19/2019 Numlua Anumerical Packagefo Lua

    8/27

    Introduction

    Numeric Lua Lua Workshop’08

      A lot of problems in scientific computing need high performancenumerical routines

    •   General approach: heavy computations in low level routines

    (FORTRAN or C) wrapped in a scripting  or OO  language (high level)

    •   Promising candidate:  Lua !

    •   Performance:  fast execution and small footprint

    •   Powerful:  GC, functional facilities, coroutines, metamethods

    •   Extensible:  simple and well defined API in ANSI C

    •   Easy:  semantically rich but simple

  • 8/19/2019 Numlua Anumerical Packagefo Lua

    9/27

    Introduction

    Numeric Lua Lua Workshop’08

      A lot of problems in scientific computing need high performancenumerical routines

    •   General approach: heavy computations in low level routines

    (FORTRAN or C) wrapped in a scripting  or OO  language (high level)

    •   Promising candidate:  Lua !

    •   Performance:  fast execution and small footprint

    •   Powerful:  GC, functional facilities, coroutines, metamethods

    •   Extensible:  simple and well defined API in ANSI C

    •   Easy:  semantically rich but simple

      Free:  MIT license

  • 8/19/2019 Numlua Anumerical Packagefo Lua

    10/27

    Numeric Lua

    Numeric Lua Lua Workshop’08

      Based on “classical” numerical packages

  • 8/19/2019 Numlua Anumerical Packagefo Lua

    11/27

    Numeric Lua

    Numeric Lua Lua Workshop’08

      Based on “classical” numerical packages•   Motivation: keep minimalist approach from Lua in order to provide a

    basis for more elaborated scientific packages

  • 8/19/2019 Numlua Anumerical Packagefo Lua

    12/27

    Numeric Lua

    Numeric Lua Lua Workshop’08

      Based on “classical” numerical packages•   Motivation: keep minimalist approach from Lua in order to provide a

    basis for more elaborated scientific packages

      Components•   Complex numbers

    •   Special functions (Exps + CDFs + PDFs)

    •   Random number generation (MT + Ranlib)•   Numerical linear algebra (BLAS + LAPACK)

  • 8/19/2019 Numlua Anumerical Packagefo Lua

    13/27

    Numlua – Features

    Numeric Lua Lua Workshop’08

      Some OO and functional flavor, but no paradigm compromise

  • 8/19/2019 Numlua Anumerical Packagefo Lua

    14/27

    Numlua – Features

    Numeric Lua Lua Workshop’08

      Some OO and functional flavor, but no paradigm compromise•   Matrix type system: general, triangular, symmetric, and posdef

  • 8/19/2019 Numlua Anumerical Packagefo Lua

    15/27

    Numlua – Features

    Numeric Lua Lua Workshop’08

      Some OO and functional flavor, but no paradigm compromise•   Matrix type system: general, triangular, symmetric, and posdef

    •   Small: interpreter + libs  ≈ 1.25Mb (static link) or 200Kb (dynamic link)

  • 8/19/2019 Numlua Anumerical Packagefo Lua

    16/27

    Numlua – Features

    Numeric Lua Lua Workshop’08

      Some OO and functional flavor, but no paradigm compromise•   Matrix type system: general, triangular, symmetric, and posdef

    •   Small: interpreter + libs  ≈ 1.25Mb (static link) or 200Kb (dynamic link)

    •   Implemented in ANSI C and thus as portable as Lua

  • 8/19/2019 Numlua Anumerical Packagefo Lua

    17/27

    Numlua – Features

    Numeric Lua Lua Workshop’08

      Some OO and functional flavor, but no paradigm compromise•   Matrix type system: general, triangular, symmetric, and posdef

    •   Small: interpreter + libs  ≈ 1.25Mb (static link) or 200Kb (dynamic link)

    •   Implemented in ANSI C and thus as portable as Lua

    •   Standard interface to BLAS/LAPACK: can be linked to many optimized

    libs

  • 8/19/2019 Numlua Anumerical Packagefo Lua

    18/27

    Numlua – Features

    Numeric Lua Lua Workshop’08

      Some OO and functional flavor, but no paradigm compromise•   Matrix type system: general, triangular, symmetric, and posdef

    •   Small: interpreter + libs  ≈ 1.25Mb (static link) or 200Kb (dynamic link)

    •   Implemented in ANSI C and thus as portable as Lua

    •   Standard interface to BLAS/LAPACK: can be linked to many optimized

    libs

    •   MIT license

  • 8/19/2019 Numlua Anumerical Packagefo Lua

    19/27

    Numlua – Examples

    Numeric Lua Lua Workshop’08

    function   circ1(v)

    local   n =   table.getn(v)

    local   m =   matrix.zeros(n)

    for   i =   1, n   do

    for   j =   1, n   do

    m[i][j] = v[(j - i) % n + 1]

    end

    end

    return   m

    end

  • 8/19/2019 Numlua Anumerical Packagefo Lua

    20/27

    Numlua – Examples

    Numeric Lua Lua Workshop’08

    function   circ1(v)

    local   n =   table.getn(v)

    local   m =   matrix.zeros(n)

    for   i =   1, n   do

    for   j =   1, n   do

    m[i][j] = v[(j - i) % n + 1]

    end

    end

    return   m

    end

    function   circ2(v)

    local   n =   table.getn(v)

    return   matrix.zeros(n):apply(

    function(i, j)   return   v [ ( j - i ) % n + 1 ]   end)end

  • 8/19/2019 Numlua Anumerical Packagefo Lua

    21/27

    Numlua – Examples

    Numeric Lua Lua Workshop’08

    local   r =   rng(os.clock())

    function   mvnorm(mean, var, n)

    local   u, info =   matrix.chol(var)   -- u’   *   u = var

    assert(info ==   0,   "matrix is not positive definite")

    -- y:col(i)   ∼   N(0_m, I_m)

    local   m = mean:size()

    local   y =   matrix(m, n):map(

    function(e)   return   r:norm(0, 1)   end)

    -- u’   *   y + mean   ∼   N(mean, var)

    y = # u   *   yfor   i , j , e   in   y:entries()   do

    y[i][j] = e + mean[i]

    end

    return   y

    end

  • 8/19/2019 Numlua Anumerical Packagefo Lua

    22/27

    Future work

    Numeric Lua Lua Workshop’08

      Documentation!

  • 8/19/2019 Numlua Anumerical Packagefo Lua

    23/27

    Future work

    Numeric Lua Lua Workshop’08

      Documentation!•   Move to Luarocks

  • 8/19/2019 Numlua Anumerical Packagefo Lua

    24/27

    Future work

    Numeric Lua Lua Workshop’08

      Documentation!•   Move to Luarocks

    •   New internal representation for matrices

  • 8/19/2019 Numlua Anumerical Packagefo Lua

    25/27

    Future work

    Numeric Lua Lua Workshop’08

      Documentation!•   Move to Luarocks

    •   New internal representation for matrices

    •   Use token filters to make matrix declaration easier (less painfull)

  • 8/19/2019 Numlua Anumerical Packagefo Lua

    26/27

    Future work

    Numeric Lua Lua Workshop’08

      Documentation!•   Move to Luarocks

    •   New internal representation for matrices

    •   Use token filters to make matrix declaration easier (less painfull)•   Extensions: HDF5, sparse matrices, stat library, plotting library, ...

  • 8/19/2019 Numlua Anumerical Packagefo Lua

    27/27

    Thank you!

    N i L L W k h ’

    Lua

        N    u  m

      e  r  i c