diff options
author | Nao Pross <np@0hm.ch> | 2024-03-03 03:15:40 +0100 |
---|---|---|
committer | Nao Pross <np@0hm.ch> | 2024-03-03 03:15:40 +0100 |
commit | 584c527bfb647ec16609bf79864292399cef54e3 (patch) | |
tree | e399a86812445dd115bc7e25d8f6330636c9f48a /docs/tutorials.rst | |
parent | Improve docstrings (diff) | |
download | mdpoly-584c527bfb647ec16609bf79864292399cef54e3.tar.gz mdpoly-584c527bfb647ec16609bf79864292399cef54e3.zip |
Setup Sphinx documentation generator
Diffstat (limited to 'docs/tutorials.rst')
-rw-r--r-- | docs/tutorials.rst | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/docs/tutorials.rst b/docs/tutorials.rst new file mode 100644 index 0000000..a2e1b20 --- /dev/null +++ b/docs/tutorials.rst @@ -0,0 +1,40 @@ +Tutorials +========= + +Quickstart +---------- + +For the impatient, here is a very quick overview. + +.. code:: py + + from mdpoly import State, Variable, Parameter + from mdpoly.representations import SparseRepr + + # Construct an expression + x, y = Variable.from_names("x, y") # or just Variable("x") + k = Parameter("k") + + p = (x + 2 * y) ** 3 + y ** 2 + k + print(f"{p = }") + + # Make a concrete representation + state = State(parameters={k: 3.14}) # try to replace with empty dict + sparse, state = p.to_repr(SparseRepr, state) + + # Look inside the representation + for entry in sparse.entries(): + print(f"at (row, col) = {entry.row, entry.col} there is a polynomial:") + for term in sparse.terms(entry): + monomial_str = "" + for idx in term: + var = state.from_index(idx.var_idx) + monomial_str += f"{var.name}^{idx.power} " + + # Get the coefficient + coeff = sparse.at(entry, term) + print(" - the monomial", monomial_str, "has coefficient", coeff) + + # You can also simply iterate over it + for entry, term, coeff in sparse: + print(entry, term, coeff) |