Skip to main content

ndim

Multidimensional volumes and monomial integrals.

PyPi Version PyPI pyversions GitHub stars Downloads

Discord

ndim computes all kinds of volumes and integrals of monomials over such volumes in a fast, numerically stable way, using recurrence relations.

Installation

Install ndim from PyPI with

pip install ndim

How to get a license

Licenses for personal and academic use can be purchased here. You'll receive a confirmation email with a license key. Install the key with

slim install <your-license-key>

on your machine and you're good to go.

For commercial use, please contact support@mondaytech.com.

Use ndim

import ndim

val = ndim.nball.volume(17)
print(val)

val = ndim.nball.integrate_monomial((4, 10, 6, 0, 2), lmbda=-0.5)
print(val)

# or nsphere, enr, enr2, ncube, nsimplex
0.14098110691713894
1.0339122278806983e-07

All functions have the symbolic argument; if set to True, computations are performed symbolically.

import ndim

vol = ndim.nball.volume(17, symbolic=True)
print(vol)
512*pi**8/34459425

The formulas

A PDF version of the text can be found here.

This note gives closed formulas and recurrence expressions for many $n$-dimensional volumes and monomial integrals. The recurrence expressions are often much simpler, more instructive, and better suited for numerical computation.

n-dimensional unit cube

C_n = \left\{(x_1,\dots,x_n): -1 \le x_i \le 1\right\}
  • Volume.
|C_n| = 2^n = \begin{cases}
  1&\text{if $n=0$}\\
  |C_{n-1}| \times 2&\text{otherwise}
\end{cases}
  • Monomial integration.
\begin{align}
  I_{k_1,\dots,k_n}
  &= \int_{C_n} x_1^{k_1}\cdots x_n^{k_n}\\
    &= \prod_i \frac{1 + (-1)^{k_i}}{k_i+1}
  =\begin{cases}
    0&\text{if any $k_i$ is odd}\\
    |C_n|&\text{if all $k_i=0$}\\
    I_{k_1,\dots,k_{i_0}-2,\dots,k_n} \times \frac{k_{i_0}-1}{k_{i_0}+1}&\text{if $k_{i_0} > 0$}
  \end{cases}
\end{align}

n-dimensional unit simplex

 T_n = \left\{(x_1,\dots,x_n):x_i \geq 0, \sum_{i=1}^n x_i \leq 1\right\}
  • Volume.
|T_n| = \frac{1}{n!} = \begin{cases}
  1&\text{if $n=0$}\\
  |T_{n-1}| \times \frac{1}{n}&\text{otherwise}
\end{cases}
  • Monomial integration.
\begin{align}
  I_{k_1,\dots,k_n}
  &= \int_{T_n} x_1^{k_1}\cdots x_n^{k_n}\\
  &= \frac{\prod_i\Gamma(k_i + 1)}{\Gamma\left(n + 1 + \sum_i k_i\right)}\\
  &=\begin{cases}
    |T_n|&\text{if all $k_i=0$}\\
    I_{k_1,\dots,k_{i_0}-1,\dots,k_n} \times \frac{k_{i_0}}{n + \sum_i k_i}&\text{if $k_{i_0} > 0$}
  \end{cases}
\end{align}

Remark

Note that both numerator and denominator in the closed expression will assume very large values even for polynomials of moderate degree. This can lead to difficulties when evaluating the expression on a computer; the registers will overflow. A common countermeasure is to use the log-gamma function,

\frac{\prod_i\Gamma(k_i)}{\Gamma\left(\sum_i k_i\right)}
= \exp\left(\sum_i \ln\Gamma(k_i) - \ln\Gamma\left(\sum_i k_i\right)\right),

but a simpler and arguably more elegant solution is to use the recurrence. This holds true for all such expressions in this note.

n-dimensional unit sphere (surface)

U_n = \left\{(x_1,\dots,x_n): \sum_{i=1}^n x_i^2 = 1\right\}
  • Volume.
 |U_n|
 = \frac{n \sqrt{\pi}^n}{\Gamma(\frac{n}{2}+1)}
 = \begin{cases}
   2&\text{if $n = 1$}\\
   2\pi&\text{if $n = 2$}\\
   |U_{n-2}| \times \frac{2\pi}{n - 2}&\text{otherwise}
 \end{cases}
  • Monomial integral.
\begin{align*}
  I_{k_1,\dots,k_n}
  &= \int_{U_n} x_1^{k_1}\cdots x_n^{k_n}\\
  &= \frac{2\prod_i
    \Gamma\left(\frac{k_i+1}{2}\right)}{\Gamma\left(\sum_i \frac{k_i+1}{2}\right)}\\\\
  &=\begin{cases}
    0&\text{if any $k_i$ is odd}\\
    |U_n|&\text{if all $k_i=0$}\\
    I_{k_1,\dots,k_{i_0}-2,\dots,k_n} \times \frac{k_{i_0} - 1}{n - 2 + \sum_i k_i}&\text{if $k_{i_0} > 0$}
  \end{cases}
\end{align*}

n-dimensional unit ball

S_n = \left\{(x_1,\dots,x_n): \sum_{i=1}^n x_i^2 \le 1\right\}
  • Volume.

    |S_n|
    = \frac{\sqrt{\pi}^n}{\Gamma(\frac{n}{2}+1)}
    = \begin{cases}
       1&\text{if $n = 0$}\\
       2&\text{if $n = 1$}\\
       |S_{n-2}| \times \frac{2\pi}{n}&\text{otherwise}
    \end{cases}
    
  • Monomial integral.

\begin{align}
  I_{k_1,\dots,k_n}
  &= \int_{S_n} x_1^{k_1}\cdots x_n^{k_n}\\
  &= \frac{2^{n + p}}{n + p} |S_n|
  =\begin{cases}
    0&\text{if any $k_i$ is odd}\\
    |S_n|&\text{if all $k_i=0$}\\
    I_{k_1,\dots,k_{i_0}-2,\dots,k_n} \times \frac{k_{i_0} - 1}{n + p}&\text{if $k_{i_0} > 0$}
  \end{cases}
\end{align}

with $p=\sum_i k_i$.

n-dimensional unit ball with Gegenbauer weight

$\lambda > -1$.

  • Volume.
    \begin{align}
    |G_n^{\lambda}|
      &= \int_{S^n} \left(1 - \sum_i x_i^2\right)^\lambda\\
      &= \frac{%
        \Gamma(1+\lambda)\sqrt{\pi}^n
      }{%
        \Gamma\left(1+\lambda + \frac{n}{2}\right)
      }
      = \begin{cases}
        1&\text{for $n=0$}\\
        B\left(\lambda + 1, \frac{1}{2}\right)&\text{for $n=1$}\\
        |G_{n-2}^{\lambda}|\times \frac{2\pi}{2\lambda + n}&\text{otherwise}
      \end{cases}
  \end{align}
  • Monomial integration.
\begin{align}
  I_{k_1,\dots,k_n}
    &= \int_{S^n} x_1^{k_1}\cdots x_n^{k_n} \left(1 - \sum_i x_i^2\right)^\lambda\\
    &= \frac{%
      \Gamma(1+\lambda)\prod_i \Gamma\left(\frac{k_i+1}{2}\right)
    }{%
      \Gamma\left(1+\lambda + \sum_i \frac{k_i+1}{2}\right)
    }\\
    &= \begin{cases}
      0&\text{if any $k_i$ is odd}\\
      |G_n^{\lambda}|&\text{if all $k_i=0$}\\
      I_{k_1,\dots,k_{i_0}-2,\dots,k_n} \times \frac{k_{i_0}-1}{2\lambda + n + \sum_i k_i}&\text{if $k_{i_0} > 0$}
    \end{cases}
\end{align}

n-dimensional unit ball with Chebyshev-1 weight

Gegenbauer with $\lambda=-\frac{1}{2}$.

  • Volume.
\begin{align}
|G_n^{-1/2}|
  &= \int_{S^n} \frac{1}{\sqrt{1 - \sum_i x_i^2}}\\
  &= \frac{%
    \sqrt{\pi}^{n+1}
  }{%
    \Gamma\left(\frac{n+1}{2}\right)
  }
  =\begin{cases}
    1&\text{if $n=0$}\\
    \pi&\text{if $n=1$}\\
    |G_{n-2}^{-1/2}| \times \frac{2\pi}{n-1}&\text{otherwise}
  \end{cases}
\end{align}
  • Monomial integration.
\begin{align}
I_{k_1,\dots,k_n}
  &= \int_{S^n} \frac{x_1^{k_1}\cdots x_n^{k_n}}{\sqrt{1 - \sum_i x_i^2}}\\
  &= \frac{%
    \sqrt{\pi} \prod_i \Gamma\left(\frac{k_i+1}{2}\right)
  }{%
    \Gamma\left(\frac{1}{2} + \sum_i \frac{k_i+1}{2}\right)
  }\\
  &= \begin{cases}
    0&\text{if any $k_i$ is odd}\\
    |G_n^{-1/2}|&\text{if all $k_i=0$}\\
    I_{k_1,\dots,k_{i_0}-2,\dots,k_n} \times \frac{k_{i_0}-1}{n-1 + \sum_i k_i}&\text{if $k_{i_0} > 0$}
  \end{cases}
\end{align}

n-dimensional unit ball with Chebyshev-2 weight

Gegenbauer with $\lambda = +\frac{1}{2}$.

  • Volume.
\begin{align}
|G_n^{+1/2}|
  &= \int_{S^n} \sqrt{1 - \sum_i x_i^2}\\
  &= \frac{%
    \sqrt{\pi}^{n+1}
  }{%
    2\Gamma\left(\frac{n+3}{2}\right)
  }
  = \begin{cases}
    1&\text{if $n=0$}\\
    \frac{\pi}{2}&\text{if $n=1$}\\
    |G_{n-2}^{+1/2}| \times \frac{2\pi}{n+1}&\text{otherwise}
  \end{cases}
\end{align}
  • Monomial integration.
\begin{align}
I_{k_1,\dots,k_n}
  &= \int_{S^n} x_1^{k_1}\cdots x_n^{k_n} \sqrt{1 - \sum_i x_i^2}\\
  &= \frac{%
    \sqrt{\pi}\prod_i \Gamma\left(\frac{k_i+1}{2}\right)
  }{%
    2\Gamma\left(\frac{3}{2} + \sum_i \frac{k_i+1}{2}\right)
  }\\
  &= \begin{cases}
    0&\text{if any $k_i$ is odd}\\
    |G_n^{+1/2}|&\text{if all $k_i=0$}\\
    I_{k_1,\dots,k_{i_0}-2,\dots,k_n} \times \frac{k_{i_0}-1}{n + 1 + \sum_i k_i}&\text{if $k_{i_0} > 0$}
  \end{cases}
\end{align}

n-dimensional generalized Cauchy volume

As appearing in the Cauchy distribution and Student's t-distribution.

  • Volume. $2 \lambda > n$.
    \begin{align}
    |Y_n^{\lambda}|
      &= \int_{\mathbb{R}^n} \left(1 + \sum_i x_i^2\right)^{-\lambda}\\
      &= |U_{n-1}| \frac{1}{2} B(\lambda - \frac{n}{2}, \frac{n}{2})\\
      &= \begin{cases}
        1&\text{for $n=0$}\\
        B\left(\lambda - \frac{1}{2}, \frac{1}{2}\right)&\text{for $n=1$}\\
        |Y_{n-2}^{\lambda}|\times \frac{2\pi}{2\lambda - n}&\text{otherwise}
      \end{cases}
  \end{align}
  • Monomial integration. $2 \lambda > n + \sum_i k_i$.
\begin{align}
  I_{k_1,\dots,k_n}
    &= \int_{\mathbb{R}^n} x_1^{k_1}\cdots x_n^{k_n} \left(1 + \sum_i x_i^2\right)^{-\lambda}\\
    &= \frac{\Gamma(\frac{n+\sum k_i}{2}) \Gamma(\lambda - \frac{n - \sum k_i}{2})}{2 \Gamma(\lambda)}
       \times \frac{2\prod_i \Gamma(\tfrac{k_i+1}{2})}{\Gamma(\sum_i \tfrac{k_i+1}{2})}\\
    &= \begin{cases}
      0&\text{if any $k_i$ is odd}\\
      |Y_n^{\lambda}|&\text{if all $k_i=0$}\\
      I_{k_1,\dots,k_{i_0}-2,\dots,k_n} \times \frac{k_{i_0}-1}{2\lambda - \left(n + \sum_i k_i\right)}&\text{if $k_{i_0} > 0$}
    \end{cases}
\end{align}

n-dimensional generalized Laguerre volume

$\alpha > -1$.

  • Volume
\begin{align}
  V_n
    &= \int_{\mathbb{R}^n} \left(\sqrt{x_1^2+\cdots+x_n^2}\right)^\alpha \exp\left(-\sqrt{x_1^2+\dots+x_n^2}\right)\\
    &= \frac{2 \sqrt{\pi}^n \Gamma(n+\alpha)}{\Gamma(\frac{n}{2})}
  = \begin{cases}
    2\Gamma(1+\alpha)&\text{if $n=1$}\\
    2\pi\Gamma(2 + \alpha)&\text{if $n=2$}\\
    V_{n-2} \times \frac{2\pi(n+\alpha-1) (n+\alpha-2)}{n-2}&\text{otherwise}
  \end{cases}
\end{align}
  • Monomial integration.
  \begin{align}
  I_{k_1,\dots,k_n}
  &= \int_{\mathbb{R}^n} x_1^{k_1}\cdots x_n^{k_n}
    \left(\sqrt{x_1^2+\dots+x_n^2}\right)^\alpha \exp\left(-\sqrt{x_1^2+\dots+x_n^2}\right)\\
  &= \frac{%
    2 \Gamma\left(\alpha + n + \sum_i k_i\right)
    \left(\prod_i \Gamma\left(\frac{k_i + 1}{2}\right)\right)
  }{%
    \Gamma\left(\sum_i \frac{k_i + 1}{2}\right)
  }\\
  &=\begin{cases}
    0&\text{if any $k_i$ is odd}\\
    V_n&\text{if all $k_i=0$}\\
    I_{k_1,\dots,k_{i_0}-2,\ldots,k_n} \times \frac{%
      (\alpha + n + p - 1) (\alpha + n + p - 2) (k_{i_0} - 1)
    }{%
        n + p - 2
    }&\text{if $k_{i_0} > 0$}
  \end{cases}
\end{align}

with $p=\sum_i k_i$.

n-dimensional Hermite (physicists')

  • Volume.
\begin{align}
  V_n
  &= \int_{\mathbb{R}^n} \exp\left(-(x_1^2+\cdots+x_n^2)\right)\\
  &= \sqrt{\pi}^n
   = \begin{cases}
     1&\text{if $n=0$}\\
     \sqrt{\pi}&\text{if $n=1$}\\
     V_{n-2} \times \pi&\text{otherwise}
   \end{cases}
\end{align}
  • Monomial integration.
\begin{align}
    I_{k_1,\dots,k_n}
    &= \int_{\mathbb{R}^n} x_1^{k_1}\cdots x_n^{k_n} \exp(-(x_1^2+\cdots+x_n^2))\\
    &= \prod_i \frac{(-1)^{k_i} + 1}{2} \times \Gamma\left(\frac{k_i+1}{2}\right)\\
    &=\begin{cases}
      0&\text{if any $k_i$ is odd}\\
      V_n&\text{if all $k_i=0$}\\
      I_{k_1,\dots,k_{i_0}-2,\dots,k_n} \times \frac{k_{i_0} - 1}{2}&\text{if $k_{i_0} > 0$}
    \end{cases}
\end{align}

n-dimensional Hermite (probabilists')

  • Volume.
V_n = \frac{1}{\sqrt{2\pi}^n} \int_{\mathbb{R}^n}
\exp\left(-\frac{1}{2}(x_1^2+\cdots+x_n^2)\right) = 1
  • Monomial integration.
\begin{align}
  I_{k_1,\dots,k_n}
    &= \frac{1}{\sqrt{2\pi}^n} \int_{\mathbb{R}^n} x_1^{k_1}\cdots x_n^{k_n}
    \exp\left(-\frac{1}{2}(x_1^2+\cdots+x_n^2)\right)\\
  &= \prod_i \frac{(-1)^{k_i} + 1}{2} \times
    \frac{2^{\frac{k_i+1}{2}}}{\sqrt{2\pi}} \Gamma\left(\frac{k_i+1}{2}\right)\\
  &=\begin{cases}
    0&\text{if any $k_i$ is odd}\\
    V_n&\text{if all $k_i=0$}\\
    I_{k_1,\dots,k_{i_0}-2,\dots,k_n} \times (k_{i_0} - 1)&\text{if $k_{i_0} > 0$}
  \end{cases}
\end{align}

Metadata

Release files for ndim 0.1.42

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for ndim 0.1.42
File Actions
ndim-0.1.42-cp315-none-any.whl CPython 3.15 none any Details for ndim-0.1.42-cp315-none-any.whl
ndim-0.1.42-cp314-none-any.whl CPython 3.14 none any Details for ndim-0.1.42-cp314-none-any.whl
ndim-0.1.42-cp313-none-any.whl CPython 3.13 none any Details for ndim-0.1.42-cp313-none-any.whl
ndim-0.1.42-cp312-none-any.whl CPython 3.12 none any Details for ndim-0.1.42-cp312-none-any.whl
ndim-0.1.42-cp311-none-any.whl CPython 3.11 none any Details for ndim-0.1.42-cp311-none-any.whl
ndim-0.1.42-cp310-none-any.whl CPython 3.10 none any Details for ndim-0.1.42-cp310-none-any.whl

Total release size: 312.3 kB

Release files / ndim-0.1.42-cp315-none-any.whl

Download URL ndim-0.1.42-cp315-none-any.whl
Size 58.7 kB
Tags CPython 3.15
SHA-256 checksum
How to use checksums
955d22f061b69f6a949defe03dcfc3a89fea5950c4229556df0d2a8698353ad5
BLAKE2b-256 checksum
How to use checksums
45ac7a8ec30e642207553f48e41dcb46181d1a1b217b3dc54e938fe82982c136
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 9, 2026.

Transparency log

Release files / ndim-0.1.42-cp314-none-any.whl

Download URL ndim-0.1.42-cp314-none-any.whl
Size 57.8 kB
Tags CPython 3.14
SHA-256 checksum
How to use checksums
e0aea0467881a606bbf958f0b80c37162dd26f2ffa96837ea74d0eb870c640db
BLAKE2b-256 checksum
How to use checksums
600bc53cebfac49173c9da7d8b5f54bd40fd314742c2da69586db1f8b7407e09
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 9, 2026.

Transparency log

Release files / ndim-0.1.42-cp313-none-any.whl

Download URL ndim-0.1.42-cp313-none-any.whl
Size 50.9 kB
Tags CPython 3.13
SHA-256 checksum
How to use checksums
58c930fdcb3c269aa14dfa708da908d9084ffec23fca1313d73c949b38867607
BLAKE2b-256 checksum
How to use checksums
6ea7ab358472f11c949c0125800144d645b494b8c3ad6d05821720bdbaa886f5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 9, 2026.

Transparency log

Release files / ndim-0.1.42-cp312-none-any.whl

Download URL ndim-0.1.42-cp312-none-any.whl
Size 50.8 kB
Tags CPython 3.12
SHA-256 checksum
How to use checksums
cb8d798733de278fe7e0bd991c04a090a41f5047dc6ceb45e5930e1e38c041b0
BLAKE2b-256 checksum
How to use checksums
05a646627d50003f5704c0f2ac6eb721023e399ae79ed719e402b4959e75f4bf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 9, 2026.

Transparency log

Release files / ndim-0.1.42-cp311-none-any.whl

Download URL ndim-0.1.42-cp311-none-any.whl
Size 55.4 kB
Tags CPython 3.11
SHA-256 checksum
How to use checksums
3dd4ad4b83a629bc1d3f1b129e9b3e9f40440865c804abe7c948f606a31bea9e
BLAKE2b-256 checksum
How to use checksums
10f4b3b77e7ce7c37c4aa38ef2cd3c6575aa12eca1c293e53e6cf3d240ca7fb3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 9, 2026.

Transparency log

Release files / ndim-0.1.42-cp310-none-any.whl

Download URL ndim-0.1.42-cp310-none-any.whl
Size 38.8 kB
Tags CPython 3.10
SHA-256 checksum
How to use checksums
75c8d536b40d8ba6ebcb2f2f0ef79faa1000e64570fd5cd54dc3b9b31bfdfadb
BLAKE2b-256 checksum
How to use checksums
9a18b6dfff34f9746860b9d7753c7f090d68f96727880f8c85ce85906c6e65c7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 9, 2026.

Transparency log

Security for ndim 0.1.42

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 6 files in this release have been uploaded with provenance metadata.

Select from 6 files

Attestation for ndim-0.1.42-cp315-none-any.whl

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 9, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page