Metadata-Version: 2.3
Name: onsi-mc
Version: 0.1.0
Summary: A Python library for Monte Carlo N-dimensional integration
License: GNU GENERAL PUBLIC LICENSE
         Version 3, 29 June 2007
         
         Copyright (C) 2024  Ali Onsi <aonsi@alexu.edu.eg>
         
         mc - A Python library for Monte Carlo N-dimensional integration
         This program is free software: you can redistribute it and/or modify
         it under the terms of the GNU General Public License as published by
         the Free Software Foundation, either version 3 of the License, or
         (at your option) any later version.
         
         This program 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 General Public License for more details.
         
         You should have received a copy of the GNU General Public License
         along with this program.  If not, see <https://www.gnu.org/licenses/>.
Keywords: monte-carlo,integration,numerical-integration,high-dimensional,scientific-computing,python-library
Author: Ali Onsi
Author-email: aonsi@alexu.edu.eg
Requires-Python: >=3.8
Classifier: License :: OSI Approved :: GNU General Public License v3 (GPLv3)
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Requires-Dist: matplotlib (>=3.0.0)
Requires-Dist: numba (>=0.56.0)
Requires-Dist: numpy (>=1.21.0)
Requires-Dist: tqdm (>=4.0.0)
Description-Content-Type: text/markdown

<!-- SPDX-FileCopyrightText: 2025 Ali Onsi -->
<!-- SPDX-License-Identifier: GPL-3.0-or-later -->

## Evaluating N-Dimensional Integration with Monte Carlo Methods
### Mathematical Basis
Another problem which can be solved using Monte Carlo's method is numerical integration. From the definition of the average of a function we have:

$$\langle f(x) \rangle = \frac{1}{b-a} \int_a^b f(x) dx$$

which can be rearranged as:

$$\int_a^b f(x) dx = (b-a) \langle f(x) \rangle$$

From this, we can conclude that with a large number of random points, we can approximate the value of the integral. If we have a function $f(x)$ and want to evaluate the integral from $a$ to $b$, we:

- Generate a large number of random points in the interval $[a,b]$ 
- Calculate the average value of $f(x)$ at these random points
- = Multiply this average by the length of the interval $(b-a)$

### Extension to Higher Dimensions 

This concept extends to 2D, 3D, and higher dimensional integrals:

For a 2D integral:

$$\langle f(x,y) \rangle = \frac{1}{b_x-a_x} , \frac{1}{b_y-a_y} , \int_{a_y}^{b_y} \int_{a_x}^{b_x} f(x,y) , dx , dy$$

Rearranged as:

$$\int_{a_y}^{b_y} \int_{a_x}^{b_x} f(x,y) , dx , dy = (b_x-a_x)(b_y-a_y) \langle f(x,y) \rangle$$

For a 3D integral:

$$\int_{a_z}^{b_z} \int_{a_y}^{b_y} \int_{a_x}^{b_x} f(x,y,z) , dx , dy , dz = (b_x-a_x)(b_y-a_y)(b_z-a_z) \langle f(x,y,z) \rangle$$

A general pattern emerges for $N$-dimensional integrals:

$$\int_{a_N}^{b_N} \cdots \int_{a_2}^{b_2} \int_{a_1}^{b_1} f(x_1, x_2, \ldots, x_N) , dx_1 , dx_2 , \ldots , dx_N = \left[\prod_{i=1}^N (b_i-a_i) \right] \cdot \langle f(x_1,x_2, \ldots, x_N) \rangle$$

What's impressive is that the difficulty of integrating a higher-order integral doesn't increase significantly with Monte Carlo methods, making it suitable for complex integrals.

## License
This project is licensed under the **GNU GPLv3**.  
- **You must**:  
  - Disclose source code for derivative works.  
  - License derivatives under GPL-3.0.  
- **Commercial use**: Allowed, but derivatives must remain open-source.  
