{"id":15010958,"url":"https://github.com/stla/scubature","last_synced_at":"2025-10-30T19:36:41.542Z","repository":{"id":48349019,"uuid":"516850521","full_name":"stla/scubature","owner":"stla","description":"Integration over simplices.","archived":false,"fork":false,"pushed_at":"2022-12-12T09:54:26.000Z","size":49,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-23T20:37:17.658Z","etag":null,"topics":["haskell","integration"],"latest_commit_sha":null,"homepage":"","language":"Haskell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/stla.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-07-22T18:27:20.000Z","updated_at":"2025-03-02T23:08:43.000Z","dependencies_parsed_at":"2023-01-27T17:31:40.880Z","dependency_job_id":null,"html_url":"https://github.com/stla/scubature","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stla%2Fscubature","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stla%2Fscubature/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stla%2Fscubature/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stla%2Fscubature/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stla","download_url":"https://codeload.github.com/stla/scubature/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248088400,"owners_count":21045706,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["haskell","integration"],"created_at":"2024-09-24T19:37:50.145Z","updated_at":"2025-10-30T19:36:36.476Z","avatar_url":"https://github.com/stla.png","language":"Haskell","funding_links":[],"categories":[],"sub_categories":[],"readme":"# scubature\n\nPure Haskell implementation of simplicial cubature (integration over a simplex).\n\nThis library is a port of a part of the R package **SimplicalCubature**, \nwritten by John P. Nolan, and which contains R translations of \nsome Matlab and Fortran code written by Alan Genz. \nIt is also a port of a part of the R package **SphericalCubature**, also \nwritten by John P. Nolan. \nIn addition it provides a function for the exact computation of the integral \nof a polynomial over a simplex.\n\n___\n\n## Integral of a function on a simplex\n\n```haskell\nintegrateOnSimplex\n    :: (VectorD -\u003e VectorD)   -- integrand\n    -\u003e Simplices              -- domain of integration (union of the simplices)\n    -\u003e Int                    -- number of components of the integrand\n    -\u003e Int                    -- maximum number of evaluations\n    -\u003e Double                 -- desired absolute error\n    -\u003e Double                 -- desired relative error\n    -\u003e Int                    -- integration rule: 1, 2, 3 or 4\n    -\u003e IO Results             -- values, error estimates, evaluations, success\n```\n\n### Example\n\n![equation](http://latex.codecogs.com/gif.latex?%5Cint_0%5E1%5Cint_0%5Ex%5Cint_0%5Ey%5Cexp%28x+y+z%29%5C,%5Cmathrm%7Bd%7Dz%5C,%5Cmathrm%7Bd%7Dy%5C,%5Cmathrm%7Bd%7Dx=%5Cfrac%7B1%7D%7B6%7D%28e-1%29%5E3%5Capprox%20.8455356853)\n\nDefine the integrand:\n\n```haskell\nimport Data.Vector.Unboxed as V\n:{\nf :: Vector Double -\u003e Vector Double\nf v = singleton $ exp (V.sum v)\n:}\n```\n\nDefine the simplex (tetrahedron in dimension 3) by the list of its vertices:\n\n```haskell\nsimplex = [[0, 0, 0], [1, 1, 1], [0, 1, 1], [0, 0, 1]]\n```\n\nIntegrate:\n\n```haskell\nimport Numeric.Integration.SimplexCubature\nintegrateOnSimplex f [simplex] 1 100000 0 1e-10 3\n-- Results { values = [0.8455356852954488]\n--         , errorEstimates = [8.082378899762402e-11]\n--         , evaluations = 8700\n--         , success = True }\n```\n\nFor a scalar-valued integrand, it's more convenient to define... a scalar-valued\nintegrand! That is:\n\n```haskell\n:{\nf :: Vector Double -\u003e Double\nf v = exp (V.sum v)\n:}\n```\n\nand then to use `integrateOnSimplex'`:\n\n```haskell\nintegrateOnSimplex' f [simplex] 100000 0 1e-10 3\n-- Result { value         = 0.8455356852954488\n--        , errorEstimate = 8.082378899762402e-11\n--        , evaluations   = 8700\n--        , success       = True }\n```\n\n\n## Exact integral of a polynomial on a simplex\n\n```haskell\nintegratePolynomialOnSimplex\n  :: (C a, Fractional a, Ord a) -- `C a` means that `a` must be a ring\n  =\u003e Spray a -- ^ polynomial to be integrated\n  -\u003e [[a]]   -- ^ simplex to integrate over\n  -\u003e a\n```\n\n### Example\n\nWe take as an example the rational numbers for `a`. Thus we must take a \npolynomial with rational coefficients and a simplex whose vertices \nhave rational coordinates. Then the integral will be a rational number.\n\nOur polynomial is \n\n![equation](https://latex.codecogs.com/gif.image?\\dpi{110}P(x,\u0026space;y,\u0026space;z)\u0026space;=\u0026space;x^4\u0026space;\u0026plus;\u0026space;y\u0026space;\u0026plus;\u0026space;2xy^2\u0026space;-\u0026space;3z.)\n\nIt must be defined in Haskell with the \n[**hspray**](https://github.com/stla/hspray) library.\n\n```haskell\nimport Numeric.Integration.IntegratePolynomialOnSimplex\nimport Data.Ratio\nimport Math.Algebra.Hspray \n\n:{\nsimplex :: [[Rational]]\nsimplex = [[1, 1, 1], [2, 2, 3], [3, 4, 5], [3, 2, 1]]\n:}\n\nx = lone 1 :: Spray Rational\ny = lone 2 :: Spray Rational\nz = lone 3 :: Spray Rational\n\n:{\npoly :: Spray Rational\npoly = x^**^4 ^+^ y ^+^ 2.^(x ^*^ y^**^2) ^-^ 3.^z\n:}\n\nintegratePolynomialOnSimplex poly simplex\n-- 1387 % 42\n```\n\n\n## Integration on a spherical triangle\n\nThe library also allows to evaluate an integral on a spherical simplex on the\nunit sphere (in dimension 3, a spherical triangle).\n\n### Example\n\nFor example take the first orthant in dimension 3:\n\n```haskell\nimport Numeric.Integration.SphericalSimplexCubature\no1 = orthants 3 !! 0\no1\n-- [ [1.0, 0.0, 0.0]\n-- , [0.0, 1.0, 0.0]\n-- , [0.0, 0.0, 1.0] ]\n```\n\nAnd this integrand:\n\n```haskell\n:{\nintegrand :: [Double] -\u003e Double\nintegrand x = (x!!0 * x!!0 * x!!2) + (x!!1 * x!!1 * x!!2) + (x!!2 * x!!2 * x!!2)\n:}\n```\n\nCompute the integral (the exact result is `pi/4 ≈ 0.7853981634`):\n\n```haskell\nintegrateOnSphericalSimplex integrand o1 20000 0 1e-7 3\n-- Result { value         = 0.7853981641913279\n--        , errorEstimate = 7.71579524444753e-8\n--        , evaluations   = 17065\n--        , success       = True }\n```\n\n\n## References\n\n- A. Genz and R. Cools. \n  *An adaptive numerical cubature algorithm for simplices.* \n  ACM Trans. Math. Software 29, 297-308 (2003).\n\n- Jean B. Lasserre.\n  *Simple formula for the integration of polynomials on a simplex.* \n  BIT Numerical Mathematics 61, 523-533 (2021).","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstla%2Fscubature","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstla%2Fscubature","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstla%2Fscubature/lists"}