start using sphinx for documentation

This commit is contained in:
Lafrite
2015-04-23 11:23:08 +02:00
parent 4e4c1a763f
commit e8854eeef7
10 changed files with 820 additions and 587 deletions

View File

@@ -0,0 +1,264 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# pyMath documentation build configuration file, created by
# sphinx-quickstart on Thu Apr 23 10:44:48 2015.
#
# This file is execfile()d with the current directory set to its
# containing dir.
#
# Note that not all possible configuration values are present in this
# autogenerated file.
#
# All configuration values have a default; values that are commented out
# serve to show the default.
import sys
import os
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#sys.path.insert(0, os.path.abspath('.'))
# -- General configuration ------------------------------------------------
# If your documentation needs a minimal Sphinx version, state it here.
#needs_sphinx = '1.0'
# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.doctest',
'sphinx.ext.mathjax',
'sphinx.ext.viewcode',
]
# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
# The suffix of source filenames.
source_suffix = '.rst'
# The encoding of source files.
#source_encoding = 'utf-8-sig'
# The master toctree document.
master_doc = 'index'
# General information about the project.
project = 'pyMath'
copyright = '2015, Benjamin Bertrand'
# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
#
# The short X.Y version.
version = '1.0'
# The full version, including alpha/beta/rc tags.
release = '1.0'
# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#language = None
# There are two options for replacing |today|: either, you set today to some
# non-false value, then it is used:
#today = ''
# Else, today_fmt is used as the format for a strftime call.
#today_fmt = '%B %d, %Y'
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
exclude_patterns = []
# The reST default role (used for this markup: `text`) to use for all
# documents.
#default_role = None
# If true, '()' will be appended to :func: etc. cross-reference text.
#add_function_parentheses = True
# If true, the current module name will be prepended to all description
# unit titles (such as .. function::).
#add_module_names = True
# If true, sectionauthor and moduleauthor directives will be shown in the
# output. They are ignored by default.
#show_authors = False
# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'sphinx'
# A list of ignored prefixes for module index sorting.
#modindex_common_prefix = []
# If true, keep warnings as "system message" paragraphs in the built documents.
#keep_warnings = False
# -- Options for HTML output ----------------------------------------------
# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
html_theme = 'default'
# Theme options are theme-specific and customize the look and feel of a theme
# further. For a list of options available for each theme, see the
# documentation.
#html_theme_options = {}
# Add any paths that contain custom themes here, relative to this directory.
#html_theme_path = []
# The name for this set of Sphinx documents. If None, it defaults to
# "<project> v<release> documentation".
#html_title = None
# A shorter title for the navigation bar. Default is the same as html_title.
#html_short_title = None
# The name of an image file (relative to this directory) to place at the top
# of the sidebar.
#html_logo = None
# The name of an image file (within the static path) to use as favicon of the
# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32
# pixels large.
#html_favicon = None
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']
# Add any extra paths that contain custom files (such as robots.txt or
# .htaccess) here, relative to this directory. These files are copied
# directly to the root of the documentation.
#html_extra_path = []
# If not '', a 'Last updated on:' timestamp is inserted at every page bottom,
# using the given strftime format.
#html_last_updated_fmt = '%b %d, %Y'
# If true, SmartyPants will be used to convert quotes and dashes to
# typographically correct entities.
#html_use_smartypants = True
# Custom sidebar templates, maps document names to template names.
#html_sidebars = {}
# Additional templates that should be rendered to pages, maps page names to
# template names.
#html_additional_pages = {}
# If false, no module index is generated.
#html_domain_indices = True
# If false, no index is generated.
#html_use_index = True
# If true, the index is split into individual pages for each letter.
#html_split_index = False
# If true, links to the reST sources are added to the pages.
#html_show_sourcelink = True
# If true, "Created using Sphinx" is shown in the HTML footer. Default is True.
#html_show_sphinx = True
# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True.
#html_show_copyright = True
# If true, an OpenSearch description file will be output, and all pages will
# contain a <link> tag referring to it. The value of this option must be the
# base URL from which the finished HTML is served.
#html_use_opensearch = ''
# This is the file name suffix for HTML files (e.g. ".xhtml").
#html_file_suffix = None
# Output file base name for HTML help builder.
htmlhelp_basename = 'pyMathdoc'
# -- Options for LaTeX output ---------------------------------------------
latex_elements = {
# The paper size ('letterpaper' or 'a4paper').
#'papersize': 'letterpaper',
# The font size ('10pt', '11pt' or '12pt').
#'pointsize': '10pt',
# Additional stuff for the LaTeX preamble.
#'preamble': '',
}
# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title,
# author, documentclass [howto, manual, or own class]).
latex_documents = [
('index', 'pyMath.tex', 'pyMath Documentation',
'Benjamin Bertrand', 'manual'),
]
# The name of an image file (relative to this directory) to place at the top of
# the title page.
#latex_logo = None
# For "manual" documents, if this is true, then toplevel headings are parts,
# not chapters.
#latex_use_parts = False
# If true, show page references after internal links.
#latex_show_pagerefs = False
# If true, show URL addresses after external links.
#latex_show_urls = False
# Documents to append as an appendix to all manuals.
#latex_appendices = []
# If false, no module index is generated.
#latex_domain_indices = True
# -- Options for manual page output ---------------------------------------
# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
('index', 'pymath', 'pyMath Documentation',
['Benjamin Bertrand'], 1)
]
# If true, show URL addresses after external links.
#man_show_urls = False
# -- Options for Texinfo output -------------------------------------------
# Grouping the document tree into Texinfo files. List of tuples
# (source start file, target name, title, author,
# dir menu entry, description, category)
texinfo_documents = [
('index', 'pyMath', 'pyMath Documentation',
'Benjamin Bertrand', 'pyMath', 'One line description of project.',
'Miscellaneous'),
]
# Documents to append as an appendix to all manuals.
#texinfo_appendices = []
# If false, no module index is generated.
#texinfo_domain_indices = True
# How to display URL addresses: 'footnote', 'no', or 'inline'.
#texinfo_show_urls = 'footnote'
# If true, do not generate a @detailmenu in the "Top" node's menu.
#texinfo_no_detailmenu = False

View File

@@ -0,0 +1,96 @@
Explication sur la logique des classes
======================================
Les types
---------
Ce sont les objets que l'on s'autorise à manipuler dans les
expressions. Ces objets doivent pouvoir être afficher en *txt* ou en
*tex* avec les méthodes:
* __txt__ : affichage en mode text
* __tex__ : affichage pour une compilation latex
Operator
~~~~~~~~
Cette classe regroupe les opérateurs. Que l'on s'autorise à utiliser. On
y accède à partir de deux caractéristiques le symbole et l'arité.
Liste des attributs importants:
* arity: nombre d'opérande accepté
* priority: où se place l'opérateur dans la règles des priorités parmi
les autres opérateurs
* isOperator: permet de vérifier que c'est bien
un opérateur
Liste des méthodes importantes:
* __call__: Permet d'effectuer le calcul sur deux opérandes
* __txt__: affichage en mode texte
* __tex__: affichage pour une compilation latex
Number
~~~~~~
Ce sont tous les types de "nombres" que l'on va vouloir manipuler. On essayera
de rester le plus proche de la construction mathématiques de ces objets.
Par défaut, on travaillera avec des anneaux ce qui permettra de
construire ensuite le corps des fractions et l'anneau des polynômes
(quitte à quotienter) associé.
Pour définir ces anneaux, il faudra contre avoir les méthodes suivantes:
* __add__
* __radd__
...
Fractions
^^^^^^^^^
Polynomes
^^^^^^^^^
Quotient de polynomes (racines)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expression
----------
Render
------
Simplify-simplified / compute-child
-----------------------------------
Dans cette partie, on va expliquer le fonctionnement des mécanismes de
simplification des expressions/objets mathématiques.
La simplification des expressions se fait avec les deux méthodes
suivantes:
- *simplify()* pour:
- un polynôme permet d'accéder à la forme développée d'un polynôme.
- une fraction permet d'avoir la fraction irréductible associée.
- une expression permet de faire tous les calculs possibles de cette
expression (à la fin il ne doit y avoir qu'un élément de la liste
de tokens).
- *compute_exp()* pour:
- un polynôme ou une fraction fait la même chose que `simplify`.
- une expression fait tous les calculs élémentaires de cette
expression.
Ces deux méthodes fonctionnent ensuite sur le même principe. Elles vont
faire le calcul qui leurs est attribué en enregistrant les étapes dans
*steps* puis elles retourneront l'objet de fin de calcul à qui sera
assigné les *steps* (ce qui nécessitera par exemple de détourner la
classe *int*).
Pour accéder à ces étapes, on utilisera alors la méthode *explain* qui
expliqueront les étapes intermédiaires.

View File

View File

@@ -0,0 +1,26 @@
.. pyMath documentation master file, created by
sphinx-quickstart on Thu Apr 23 10:44:48 2015.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
Welcome to pyMath's documentation!
==================================
Contents:
.. toctree::
:maxdepth: 2
tutorial
construction
polynom
Indices and tables
==================
* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`

View File

@@ -0,0 +1,165 @@
Les polynômes
=============
Créer des polynômes
-------------------
Générer un polynôme "fixe"
~~~~~~~~~~~~~~~~~~~~~~~~~~
.. code-block:: python
>>> P = Polynom([1,2,3])
>>> print(P)
3 x ^ 2 + 2 x + 1
>>> P = Polynom([1,2,3], letter = 'h')
>>> print(P)
3 h ^ 2 + 2 h + 1
>>> print(P.name)
'P'
>>> Q = Polynom([1,2,3], name = 'Q')
>>> print(Q.name)
'Q'
Générer un polynôme aléatoirement
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. code-block:: python
>>> P = Polynom.random(["{b}", "{a}"]) # Polynom du type ax + b
>>> print(P)
- 8 x - 3
>>> P = Polynom.random(degree = 2)
>>> print(P)
5 x^{ 2 } + 4 x - 7
Manipuler des polynômes
-----------------------
Les représentations des polynômes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. code-block:: python
>>> P = Polynom([1, 2, 3])
>>> print(P)
3 x ^ 2 + 2 x + 1
Évaluer des polynômes
~~~~~~~~~~~~~~~~~~~~~
Les polynômes peuvent se comporter comme des fonctions, on peut les évaluer. Il est possible de les évaluer sur des nombres, des expressions et même des polynômes.
Évaluer un polynôme avec un entier.
.. code-block:: python
>>> type(P(3))
pymath.expression.Fake_int
>>> P(3)
34
>>> for i in P(3).explain():
print(i)
3 \times 3^{ 2 } + 2 \times 3 + 1
3 \times 9 + 6 + 1
27 + 6 + 1
33 + 1
34
>>> hp1 = Expression('h+1')
Évaluer un polynôme avec une expression.
.. code-block:: python
>>> type(P(hp1))
< <class 'pymath.polynomDeg2.Polynom_deg2'> [6, 8, 3]>
>>> print(P(hp1))
3 h ^ 2 + 8 h + 6
>>> for i in P(hp1).explain():
... print(i)
...
3 ( h + 1 )^{ 2 } + 2 ( h + 1 ) + 1
3 ( h + 1 ) ( h + 1 ) + 2 h + 2 + 1
3 ( h^{ 2 } + ( 1 + 1 ) h + 1 ) + 2 h + 2 + 1
3 ( h^{ 2 } + 2 h + 1 ) + 2 h + 2 + 1
3 ( h^{ 2 } + 2 h + 1 ) + 2 ( h + 1 ) + 1
3 h^{ 2 } + 3 \times 2 h + 3 + 2 h + 2 + 1
3 h^{ 2 } + 6 h + 3 + 2 h + 2 + 1
3 h^{ 2 } + ( 6 + 2 ) h + 3 + 2 + 1
3 h^{ 2 } + 8 h + 5 + 1
3 h^{ 2 } + 8 h + 6
Évaluer un polynôme avec un autre polynôme.
.. code-block:: python
>>> type(P(P))
pymath.polynom.Polynom
>>> print(P(P))
27 x ^ 4 + 36 x ^ 3 + 36 x ^ 2 + 16 x + 6
>>> for i in P(P).explain():
... print(i)
...
3 ( 3 x^{ 2 } + 2 x + 1 )^{ 2 } + 2 ( 3 x^{ 2 } + 2 x + 1 ) + 1
3 ( 3 x^{ 2 } + 2 x + 1 ) ( 3 x^{ 2 } + 2 x + 1 ) + 2 \times 3 x^{ 2 } + 2 \times 2 x + 2 + 1
3 ( 3 \times 3 x^{ 4 } + ( 2 \times 3 + 3 \times 2 ) x^{ 3 } + ( 3 + 2 \times 2 + 3 ) x^{ 2 } + ( 2 + 2 ) x + 1 ) + 6 x^{ 2 } + 4 x + 2 + 1
3 ( 9 x^{ 4 } + ( 6 + 6 ) x^{ 3 } + ( 3 + 4 + 3 ) x^{ 2 } + 4 x + 1 ) + 6 x^{ 2 } + 4 x + 2 + 1
3 ( 9 x^{ 4 } + 12 x^{ 3 } + ( 7 + 3 ) x^{ 2 } + 4 x + 1 ) + 6 x^{ 2 } + 4 x + 2 + 1
3 ( 9 x^{ 4 } + 12 x^{ 3 } + 10 x^{ 2 } + 4 x + 1 ) + 6 x^{ 2 } + 4 x + 2 + 1
3 ( 9 x^{ 4 } + 12 x^{ 3 } + 10 x^{ 2 } + 4 x + 1 ) + 2 ( 3 x^{ 2 } + 2 x + 1 ) + 1
3 \times 9 x^{ 4 } + 3 \times 12 x^{ 3 } + 3 \times 10 x^{ 2 } + 3 \times 4 x + 3 + 2 \times 3 x^{ 2 } + 2 \times 2 x + 2 + 1
27 x^{ 4 } + 36 x^{ 3 } + 30 x^{ 2 } + 12 x + 3 + 6 x^{ 2 } + 4 x + 2 + 1
27 x^{ 4 } + 36 x^{ 3 } + ( 30 + 6 ) x^{ 2 } + ( 12 + 4 ) x + 3 + 2 + 1
27 x^{ 4 } + 36 x^{ 3 } + 36 x^{ 2 } + 16 x + 5 + 1
27 x^{ 4 } + 36 x^{ 3 } + 36 x^{ 2 } + 16 x + 6
Opération et polynômes
~~~~~~~~~~~~~~~~~~~~~~
Les opérations +, -, \* et ^ sont accessibles aux polynômes. Elles renvoient *toujours* un polynôme (même si le résultat est une constante)
.. code-block:: python
>>> type(P + 1)
pymath.polynomDeg2.Polynom_deg2
>>> for i in (P+1).explain():
print(i)
3 x^{ 2 } + 2 x + 1 + 1
3 x^{ 2 } + 2 x + 2
>>> Q = Polynom([4, 5, 6])
>>> for i in (P+Q).explain():
print(i)
3 x^{ 2 } + 2 x + 1 + 6 x^{ 2 } + 5 x + 4
( 3 + 6 ) x^{ 2 } + ( 2 + 5 ) x + 1 + 4
9 x^{ 2 } + 7 x + 5
>>> Q = Polynom([0,2,3])
>>> print(Q)
>>> print(P-Q)
1
>>> type(P-Q)
pymath.polynom.Polynom
Dérivation
~~~~~~~~~~
Il est possible de dériver les polynômes à partir de la méthode *derivate*. De la même façon que pour les opérations, le polynôme dérivé pour s'expliquer avec la méthode *explain*.
.. code-block:: python
>>> P1 = P.derivate()
>>> print(P1)
6 x + 2
>>> for i in P1.explain():
... print(i)
...
2 \times 3 x + 1 \times 2
6 x + 2
>>> print(P1.name)
"P'"
Polynomes du second degré
-------------------------
Les polynômes du second degré héritent de toutes les méthodes venant de la classe Polynom. Ils ont cependant accès à d'autres méthodes plus spécifiques aux polynômes de ce degré:
* Accès aux coefficients de façon 'naturelle'
* *delta*: discriminant du polynôme.
* *alpha*: Abscisse de l'extremum.
* *beta*: ordonnée de l'extremum.
* *roots*: les racines du polynôme (/!\ utilise *sympy* et ne peux pas expliquer le calcul pour le moment)
* *tbl_sgn_header*: en-tête du tableau du tableau de signe écrit pour *TkzTab*
* *tbl_sgn*: ligne du tableau de signe pour *TkzTab*
* *tbl_variation*: ligne du tableau de variation pour *TkzTab*

View File

@@ -0,0 +1,352 @@
Utilisation de pyMath
=====================
À quoi sert pyMath?
-------------------
pyMath est un module python qui permet la manipulation d'expressions
mathématiques. Voici ce qu'il est capable de faire:
- *Calculer comme un collégien*: Pour faire de la correction automatisé
d'exercice, un logiciel de calcul formel ne suffit pas. Si les étapes
de calculs ne sont pas présentes, l'élève ne pourra pas analyser ses
erreurs ou s'inspirer de la correction.
.. code-block:: python
>>> from pymath import Expression
>>> ajout_fractions = Expression("2 / 5 + 2 / 3")
>>> resultat = ajout_fractions.simplify()
>>> print(resultat)
\frac{ 16 }{ 15 }
>>> for i in resultat.explain():
... print(i)
...
\frac{ 2 }{ 5 } + \frac{ 2 }{ 3 }
\frac{ 2 \times 3 }{ 5 \times 3 } + \frac{ 2 \times 5 }{ 3 \times 5 }
\frac{ 6 }{ 15 } + \frac{ 10 }{ 15 }
\frac{ 6 + 10 }{ 15 }
\frac{ 16 }{ 15 }
- *Créer des exercices aléatoirement*: Pour faire des devoirs
personnels, des fiches de révisions ou des exercices en classe, un
générateur d'expressions est inclus.
.. code-block:: python
>>> from pymath import Expression
>>> ajout_fraction = Expression.random("{a} + {b} / {c}")
>>> print(ajout_fraction)
2 + \frac{ 3 }{ 5 }
- *Gérer différents type de données*: Pour le moment, pyMath est
capable de gérer les entiers naturels, les rationnels (sous forme de
fractions) et les polynômes. L'utilisation des nombres à virgules et
des racines devraient être ajoutés dans les prochaines versions.
.. code-block:: python
>>> from pymath import Fraction
>>> une_fraction = Fraction(1,2)
>>> print(une_fraction)
1 / 2
>>> from pymath import Polynom
>>> un_polynom = Polynom([1,2,3])
>>> print(un_polynom)
3 x^{ 2 } + 2 x + 1
- *Afficher avec deux types de rendus*: Un en mode texte pour
l'affichage dans une console. Un deuxième spécialement pour écrire
des documents latex.
.. code-block:: python
>>> from pymath import Expression
>>> ajout_fractions = Expression("2 / 5 + 2 / 3")
>>> for i in ajout_fractions.simpliy().explain():
... print(i)
...
\frac{ 2 }{ 5 } + \frac{ 2 }{ 3 }
\frac{ 2 \times 3 }{ 5 \times 3 } + \frac{ 2 \times 5 }{ 3 \times 5 }
\frac{ 6 }{ 15 } + \frac{ 10 }{ 15 }
\frac{ 6 + 10 }{ 15 }
\frac{ 16 }{ 15 }
>>> from pymath import txt
>>> with Expression.tmp_render(txt):
... for i in ajout_fractions.simpliy().explain():
... print(i)
...
2 / 5 + 2 / 3
( 2 * 3 ) / ( 5 * 3 ) + ( 2 * 5 ) / ( 3 * 5 )
6 / 15 + 10 / 15
( 6 + 10 ) / 15
16 / 15
Ce module a pour but d'être un outil pour faciliter la construction
d'exercices et leurs correction. Il a pour but d'être le plus simple
possible d'utilisation afin que tout le monde avec un minimum de
connaissance en programmation puisse créer librement des exercices.
Calculer comme un collégien.
----------------------------
Actuellement le module principal pour faire calculer python comme un
collégien est *pymath.expression*.
.. code-block:: python
>>> from pymath import Expression
Déclarer une expression
~~~~~~~~~~~~~~~~~~~~~~~
Un expression peut être initialisée de deux façons différentes: à partir
d'une chaine de caractères ou à partir de la liste des termes (en
postfix - cette méthode est essentiellement utilisée pour programmer les
modules, elle ne sera pas détaillée ici).
.. code-block:: python
>>> un_calcul = Expression("1 + 2 * 3")
>>> print(un_calcul)
1 + 2 \times 3
>>> ajout_fractions = Expression("2 / 5 + 2 / 3")
>>> print(ajout_fractions)
\frac{ 2 }{ 5 } + \frac{ 2 }{ 3 }
Et si l'on souhaite un rendu plus adapté à la console:
.. code-block:: python
>>> from pymath import txt
>>> Expression.set_render(txt)
>>> print(un_calcul)
1 + 2 * 3
>>> print(ajout_fractions)
2 / 5 + 2 / 3
Simplification des expressions
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Une fois les expressions créées, elles peuvent se réduire en expliquant
les étapes et en respectant les règles de priorités. Ces étapes de
calcul sont stockés dans l'objet résultat du calcul et sont accéssibles
à travers la méthode *explain*. Les exemples suivants seront données
avec un rendu texte.
.. code-block:: python
>>> from pymath import Expression
>>> from pymath import txt
>>> Expression.set_render(txt)
>>> exp = Expression("1 + 2 * 3")
>>> exp_simplifiee = exp.simplify()
>>> print(exp_simplifiee)
7
>>> for i in exp_simplifiee.explain():
... print(i)
...
1 + 2 * 3
1 + 6
7
Les opérations autorisées sont les opérations "classique": + - * / ^.
L'utilisation des parenthèses est aussi gérée.
.. code-block:: python
>>> exp = Expression("1 + 2 / 5")
>>> for i in exp.simplify().explain():
... print(i)
...
1 + 2 / 5
( 1 * 5 ) / ( 1 * 5 ) + ( 2 * 1 ) / ( 5 * 1 )
( 5 + 2 ) / 5
7 / 5
>>> exp = Expression("(2 + 4)(3 - 4 * 2)")
>>> for i in exp.simplify().explain():
... print(i)
...
( 2 + 4 ) ( 3 - ( 4 * 2 ) )
6 * ( 3 - 8 )
6 * ( -5 )
-30
Type de variables et opérations
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
On peut vouloir créer directement des objets (fractions ou polynômes)
sans passer par le module expression (voir `fraction <fraction>`__
et `polynom <polynom>`__ pour plus de details)
.. code-block:: python
>>> from pymath import Fraction
>>> fraction1 = Fraction(1,2)
>>> fraction2 = Fraction(2,3)
>>> print(fraction1)
1 / 2
>>> from pymath import Polynom
>>> p = Polynom([1,2,3])
>>> print(p)
3 x ^ 2 + 2 x + 1
>>> q = Polynom([0,0,1])
x ^ 2
On peut effectuer des opérations entre les Expressions.
.. code-block:: python
>>> fraction_expression = Expression("2 / 3")
>>> autre_fraction_expression = Expression("4 / 9")
>>> print(fraction_expression + autre_fraction_expression)
2 / 3 + 4 / 9
On remarque qu'un opération sur des expressions, ne fait pas de calculs.
Elle ne fait que "concaténer" les listes des tokens.
À l'inverse, les opérations sur les fractions ou les polynômes renvoient
la liste des étapes jusqu'à leur forme simplifiée
.. code-block:: python
>>> addition_fraction = fraction1 + fraction2
>>> print(addition_fraction)
7 / 6
>>> for i in addition_fraction.explain():
... print(i)
...
1 * 3 / 2 * 3 + 2 * 2 / 3 * 2
( 3 + 4 ) / 6
7 / 6
>>> r = p + q
>>> print(r)
4 x ^ 2 + 2 x + 1
>>> for i in r.explain():
... print(i)
...
3 x ^ 2 + x ^ 2 + 2 x + 1
( 3 + 1 ) x ^ 2 + 2 x + 1
4 x ^ 2 + 2 x + 1
Différents rendus
~~~~~~~~~~~~~~~~~
Comme dit dans l'introduction, il y a deux types de rendus: un rendu
texte (utilisé depuis le début) et un rendu latex.
Voici un exemple de l'utilisation du rendu latex (par défaut).
.. code-block:: python
>>> exp = Expression("1 + 2 / 5")
>>> for i in exp.simplify().explain():
... print(i)
...
1 + \frac{ 2 }{ 5 }
\frac{ 1 \times 5 }{ 1 \times 5 } + \frac{ 2 \times 1 }{ 5 \times 1 }
\frac{ 5 + 2 }{ 5 }
\frac{ 7 }{ 5 }
Pour changer le rendu, on importe le rendu depuis *pymath.render* et on
appelle la méthode de classe d'Expression *set_render*.
Voici un exemple d'utilisation du rendu txt
.. code-block:: python
>>> from pymath import txt
>>> Expression.set_render(txt)
>>> exp = Expression("1 + 2 / 5")
>>> for i in exp.simplify().explain():
... print(i)
...
2 / 5 + 2 / 3
2 * 3 / 5 * 3 + 2 * 5 / 3 * 5
( 6 + 10 ) / 15
16 / 15
Générer des expressions aléatoirement.
--------------------------------------
Créer un expression
~~~~~~~~~~~~~~~~~~~
Pour créer une expression il faut au moins une chose: la forme de
l'expression. Toutes les lettres entre accolades seront remplacées par
des valeurs aléatoires (par défaut entre -10 et 10 et non nulles).
.. code-block:: python
>>> form = "2* {a} + 3"
>>> expression_aleatoire = Expression.random(form)
>>> print(expression_aleatoire)
'2 \times 9 + 3'
>>> print(Expression.random(form,val_min = 30, val_max = 40))
'2 \times 31 + 3'
Créer une expression avec conditions
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Parfois il peut être nécessaire d'imposer des conditions sur les
éléments à générer pour créer des exercices spécifiques.
.. code-block:: python
>>> form = "{a} / {b} + {c} / {d}"
>>> conditions = ["abs({b}) != 1", "{d} > 1", "{b} != {d}", "gcd({a},{b}) == 1", "gcd({c},{d}) == 1"]
>>> addition_fraction_alea = Expression.random(form, conditions)
>>> print(addition_fraction_alea)
'\frac{ 4 }{ 5 } + \frac{ 9 }{ 7 }'
La méthode pour créer les valeurs avec des conditions est la méthode par
rejet. Elle n'est pas très efficace et rien n'est encore prévu dans le
cas où aucune valeur n'est possible.
Opérations avec les valeurs générées
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Pour éviter de faire tourner la méthode par rejet trop longtemps, il est
possible de faire des calculs avec les valeurs générées.
.. code-block:: python
>>> form = "{a} / {b} + {c} / {k*b}"
>>> conditions = ["abs({b}) != 1", "{k} > 1", "{b} != {d}", "gcd({a},{b}) == 1", "gcd({c},{k*b}) == 1"]
>>> random_frac_add_generator = RdExpression(form, conditions)
>>> print(random_frac_add_generator())
\frac{ -9 }{ 7 } + \frac{ 1 }{ 28 }
Rendu des expressions
~~~~~~~~~~~~~~~~~~~~~
On peut vouloir ne pas passer par la classe Expression pour obtenir
notre expression (si l'on veut utiliser la racine carré par exemple, ou
pour créer n'importe quoi qui ne fait pas intervenir d'expression).
Ainsi pymath ne gère plus le rendu de l'expression ni son calcul.
La fonction qui permet de faire cela est *random_str*:
.. code-block:: python
>>> from pymath import random_str
>>> form = "{a} / {b} + {c} / {k*b}"
>>> conditions = ["abs({b}) != 1", "{d} > 1", "{b} != {d}", "gcd({a},{b}) == 1", "gcd({c},{k*b}) == 1"]
>>> str_addition_fraction = random_str(form, conditions)
>>> type(str_addition_fraction)
str
>>> print(str_addition_fraction)
-2 / 5 + -8 / 35
>>> form = "A({a},{b}), B({2*a}, {3*b})"
>>> points_alea = random_str(form)
>>> points_alea
'A(7,5), B(14, 15)'
On remarque le défaut d'utiliser cette forme, le rendu est moins bien
fait (dans l'exemple, il n'y a pas de parenthèses autour du -8).