{ "cells": [ { "attachments": {}, "cell_type": "markdown", "id": "ffabb623", "metadata": { "tags": [] }, "source": [ "# Getting Started - Matrix Multiplication\n", "\n", "In this tutorial, you will learn how to:\n", "\n", "1. Install `egglog` Python\n", "2. Create a representation for matrices and some simplification rules for them. This will be based off of the [matrix multiplication example](https://github.com/egraphs-good/egglog/blob/08a6e8f/tests/matrix.egg) in the egglog repository. By using our high level wrapper, we can rely on Python's built in static type checker to check the correctness of your representation.\n", "3. Try out using our library in an interactive notebook.\n", "\n", "## Install egglog Python\n", "\n", "First, you will need to have a working Python interpreter. In this tutorial, we will [use `miniconda`](https://docs.conda.io/en/latest/miniconda.html) to create a new Python environment and activate it:\n", "\n", "```bash\n", "$ brew install miniconda\n", "$ conda create -n egglog-python python=3.11\n", "$ conda activate egglog-python\n", "```\n", "\n", "Then we want to install `egglog` Python. `egglog` Python can run on any recent Python version, and is tested on 3.8 - 3.11. To install it, run:\n", "\n", "```bash\n", "$ pip install egglog\n", "```\n", "\n", "To test you have installed it correctly, run:\n", "\n", "```bash\n", "$ python -m 'import egglog'\n", "```\n", "\n", "We also want to install `mypy` for static type checking. This is not required, but it will help us write correct representations. To install it, run:\n", "\n", "```bash\n", "$ pip install mypy\n", "```\n", "\n", "## Creating an E-Graph\n", "\n", "In this tutorial, we will use [VS Code](https://code.visualstudio.com/) to create file, `matrix.py`, to include our egraph\n", "and the simplification rules:\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "7369b71b", "metadata": {}, "outputs": [], "source": [ "from __future__ import annotations\n", "\n", "from egglog import *\n", "\n", "egraph = EGraph()" ] }, { "attachments": {}, "cell_type": "markdown", "id": "814a51c5", "metadata": {}, "source": [ "## Defining Dimensions\n", "\n", "We will start by defining a representation for integers, which we will use to represent\n", "the dimensions of the matrix:\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "04fa991a", "metadata": {}, "outputs": [], "source": [ "@egraph.class_\n", "class Dim(Expr):\n", " \"\"\"\n", " A dimension of a matix.\n", "\n", " >>> Dim(3) * Dim.named(\"n\")\n", " Dim(3) * Dim.named(\"n\")\n", " \"\"\"\n", "\n", " def __init__(self, value: i64Like) -> None:\n", " ...\n", "\n", " @classmethod\n", " def named(cls, name: StringLike) -> Dim:\n", " ...\n", "\n", " def __mul__(self, other: Dim) -> Dim:\n", " ..." ] }, { "attachments": {}, "cell_type": "markdown", "id": "f5098a2b", "metadata": { "tags": [] }, "source": [ "As you can see, you must wrap any class with the `egraph.class_` to register\n", "it with the egraph and be able to use it like a Python class.\n", "\n", "### Testing in a notebook\n", "\n", "We can try out this by [creating a new notebook](https://code.visualstudio.com/docs/datascience/jupyter-notebooks#_create-or-open-a-jupyter-notebook) which imports this file:\n", "\n", "```python\n", "from matrix import *\n", "```\n" ] }, { "attachments": {}, "cell_type": "markdown", "id": "fd43c7ef", "metadata": {}, "source": [ "We can then create a new `Dim` object:\n" ] }, { "cell_type": "code", "execution_count": 3, "id": "b6424530", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
(Dim.named("x") * Dim(10)) * Dim(10)\n",
"
Dim.named("x") * Dim(100)\n",
"
kron(Matrix.named("A"), Matrix.named("B"))\n",
"
kron(Matrix.identity(Dim.named("p")), Matrix.named("C")) @ kron(Matrix.named("A"), Matrix.identity(Dim.named("m")))\n",
"