Mapytex/pymath/calculus/operator/div.py
2016-03-22 08:39:20 +03:00

62 lines
1.3 KiB
Python

#!/usr/bin/env python
# encoding: utf-8
from .operator import Operator, save_mainOp
class Div(Operator):
r""" The operator /
>>> div = Div()
>>> div
/
>>> div(1, 2)
< Fraction 1 / 2>
>>> div.__tex__('1','2')
'\\frac{ 1 }{ 2 }'
>>> div.__tex__('-1','2')
'\\frac{ -1 }{ 2 }'
>>> div.__tex__('1','-2')
'\\frac{ 1 }{ -2 }'
>>> div.__txt__('1','2')
'1 / 2'
>>> div.__txt__('-1','2')
'-1 / 2'
>>> div.__txt__('1','-2')
'1 / ( -2 )'
"""
_CARACT = {
"operator": "/",
"name": "div",
"priority": 5,
"arity": 2,
"txt": "{op1} / {op2}",
"tex": "\\frac{{ {op1} }}{{ {op2} }}",
}
def __init__(self):
""" Initiate Div Operator """
super(Div, self).__init__(**self._CARACT)
def __call__(self, op1, op2):
if op2 == 1:
return op1
else:
from ..fraction import Fraction
return Fraction(op1, op2)
def __tex__(self, *args):
# Pas besoin de parenthèses en plus pour \frac
replacement = {"op"+str(i+1): op for (i, op) in enumerate(args)}
ans = self.tex.format(**replacement)
ans = save_mainOp(ans, self)
return ans
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del