A Python package for automatic differentiation using dual numbers.
- Supports addition, subtraction, multiplication, division
- Implements common mathematical functions like sin, cos, log, exp, etc.
- Designed for automatic differentiation
dual_autodiff/
├── dual_autodiff/
│ ├── __init__.py
│ ├── dual.py
├── tests/
│ ├── __init__.py
│ ├── test_dual.py
├── pyproject.toml
Description:
dual_autodiff is a Python library for automatic differentiation.
- The
dual.pyfile contains the core logic of operations and functions automatic differentiation. - The
tests/folder includes unit tests. - The
pyproject.tomlfile manages project dependencies and build configurations.
dual_autodiff_x/
├── wheelhouse/
├── __init__.py
├── dual.c
├── dual.cp39-win_amd64.pyd
├── dual.pyx
├── pyproject.toml
├── setup.py
├── extract_whl.py
Description:
dual_autodiff_x is an optimized version of the dual_autodiff library, leveraging Cython.
- The
dual.canddual.pyxfiles implement the core logic using C and Cython for speed improvements. - Precompiled binaries (e.g.,
dual.cp39-win_amd64.pyd) ensure compatibility with specific Python versions and platforms. - The
extract_whl.pyscript is used to extract the contents of wheel.
docs/
├── build/
│ ├── doctrees/
│ ├── html/
├── source/
│ ├── conf.py
│ ├── dual_autodiff.ipynb
│ ├── index.rst
│ ├── modules.rst
├── make.bat
├── Makefile
Description:
The docs folder contains all files and scripts for building project documentation using Sphinx.
- The
source/directory includes the configuration file. - The
dual_autodiff.ipynbfile provides an example notebook showcasing the usage of the library. - The
build/directory stores generated documentation, including HTML outputs.
dual_autodiff_docker/
├── Dockerfile
├── dual_autodiff_x_nb.tar
├── dual_autodiff_x-0.1.0-cp310-cp310-manylinux2_17_x86_64.manylinux2014_x86_64.whl
├── dual_autodiff_x-0.1.0-cp311-cp311-manylinux2_17_x86_64.manylinux2014_x86_64.whl
├── dual_autodiff_x.ipynb
Description:
The dual_autodiff_docker directory contains resources for setting up a Docker environment for the dual_autodiff_x project.
- The Dockerfile specifies instructions to build the Docker image.
- The .tar file (
dual_autodiff_x_nb.tar) is a pre-built Docker image that can be loaded directly usingdocker load. - Wheel files (
.whl) provide platform-specific, precompiled versions ofdual_autodiff_xfor Python 3.10 and 3.11. - The Jupyter Notebook (
dual_autodiff_x.ipynb) demonstrates how to use thedual_autodiff_xlibrary within the Docker container.
To set up the environment and install the required dependencies, run the following commands step by step:
conda create -n my_env python=3.9 -y
conda activate my_env
# Navigate to the directory containing requirements.txt
cd /path/to/requirements_directory
pip install --no-cache-dir -r requirements.txt
# Install Pandoc from conda-forge
conda install -c conda-forge pandocThis part corresponds to Q4.
To install the dual_autodiff package in editable mode, follow these steps:
# Navigate to the directory containing the 'dual_autodiff' package
# Ensure that pyproject.toml is located in this directory
cd /path/to/dual_autodiff
# Install the package in editable mode
pip install -e .This part corresponds to Q6.
To run tests using pytest, navigate to the dual_autodiff directory (where the tests folder is located) and execute the following command:
# Navigate to the dual_autodiff directory
cd /path/to/dual_autodiff
# Run pytest with the -s flag to display print statements
pytest -s testsThis part corresponds to Q7.
To generate the HTML documentation using Sphinx, run the following command in the root directory of the Sphinx project (/docs):
# Navigate to the Sphinx project root directory
cd /path/to/docs
# Build the HTML documentation
make htmlOnce the build is complete, open the following file in your file explorer to view the generated documentation:
docs/build/html/index.htmlThis part corresponds to Q8.
To install the dual_autodiff_x package in editable mode, follow these steps:
# Navigate to the dual_autodiff_x directory(Where dual.pyx is located)
cd /path/to/dual_autodiff_x
# Install the package in editable mode
pip install -e .Note: This step requires Microsoft C++ Build Tools to be installed.
This part corresponds to Q10.
To build the wheels, follow these steps:
-
Ensure Docker is running in the background.
-
Navigate to the
dual_autodiff_xdirectory:cd dual_autodiff_x -
Run the
cibuildwheelcommand to create the wheel files:cibuildwheel --output-dir wheelhouse
Note: Be aware that there may be network issues with Docker during this process.
-
After the build process completes, extract the wheel contents:
python extract_whl.py
-
This will generate two folders containing the unzipped
.whlfiles under thedual_autodiff_x/wheelhouse/wheel_contentsdirectory.
This part corresponds to Q11.
-
Navigate to the directory containing the
Dockerfile:cd <directory_containing_dockerfile>
-
Build the Docker image:
docker build -t <name_of_your_image> .
-
Run the Docker container:
docker run -p 8888:8888 <name_of_your_image>
-
Open your browser and navigate to:
http://localhost:8888 -
Select Python 3 as the kernel in Jupyter Notebook.
If you want to load and use the pre-built Docker image (dual_autodiff_x_nb.tar), follow these steps:
-
Navigate to the directory containing the
.tarfile:cd <directory_containing_dual_autodiff_x_nb.tar>
-
Load the Docker image from the
.tarfile:docker load -i dual_autodiff_x_nb.tar
-
Ensure the image is successfully loaded. You should see:
Loaded image: dual_autodiff_x_nb:latest -
Run the Docker container:
docker run -p 8888:8888 dual_autodiff_x_nb
-
Open your browser and navigate to:
http://localhost:8888 -
Choose Python 3 as the kernel in Jupyter Notebook.
Important Note:
If you use the provided dual_autodiff_x_nb.tar file, please be aware that due to network issues I encountered (I'm located in China with an unstable VPN), the notebook still imports dual_autodiff but the environment only has dual_autodiff_x.
This project leverages AI tools to assist in the development process. Specifically:
- Code: Portions of the code were generated using ChatGPT-4o based on pseudocode and instructions provided by the author.
- Report: The project report and documentation were created using ChatGPT-4o, guided by the author's detailed instructions.
However, all ideas, concepts, and the overall project structure are entirely the author's own.
This project is licensed under the terms specified in the LICENSE file.