Mapytex/pymath/calculus/operator/mul.py

124 lines
2.8 KiB
Python
Raw Normal View History

#!/usr/bin/env python
# encoding: utf-8
from .operator import Operator, save_mainOp
class Mul(Operator):
r""" The operator *
>>> mul = Mul()
>>> mul
*
>>> mul(1, 2)
2
>>> mul.__tex__('1','2')
'1 \\times 2'
>>> mul.__tex__('2','a')
'2 a'
>>> mul.__tex__('1','-2')
'1 \\times ( -2 )'
>>> mul.__txt__('1','2')
'1 * 2'
>>> mul.__txt__('2','a')
'2 a'
>>> mul.__txt__('a','2')
'a * 2'
>>> mul.__txt__('1','-2')
'1 * ( -2 )'
"""
_CARACT = {
"operator" : "*", \
"name" : "mul",\
"priority" : 4, \
"arity" : 2, \
"actions" : ("__mul__","__rmul__"), \
"txt" : "{op1} * {op2}",\
"tex" : "{op1} \\times {op2}",\
}
def __init__(self):
""" Initiate Mul Operator """
super(Mul, self).__init__(**self._CARACT)
# TODO: Add self.visibility |sam. nov. 8 17:00:08 CET 2014
self.visibility = 1
def is_visible(self, op1, op2):
""" Tells whether self has to be visible or not
:param op1: left operande
:param op2: rigth operande
>>> m = Mul()
>>> m.is_visible(2, 3)
True
>>> m.is_visible(2, -3)
True
>>> m.is_visible(-2, 3)
True
>>> m.is_visible('a', 2)
True
>>> m.is_visible('(2a + 1)', 2)
True
>>> m.is_visible(2, '(-2)')
True
>>> m.is_visible(2, '2a')
True
>>> m.is_visible(2, '(-2a)')
True
>>> m.is_visible(2, '(-2abc)')
True
>>> m.is_visible(2, 'a')
False
>>> m.is_visible(2, '(2a + 1)')
False
>>> m.is_visible('(3x - 1)', '(2a + 1)')
False
>>> m.is_visible(2, '(-2x + 1)(3x + 2)')
False
"""
# TODO: À finir!!! |lun. mars 9 00:03:40 CET 2015
if type(op2) == int:
# op2 est maintenant une chaine de caractères
return True
elif op2.isdecimal():
return True
elif op2.isalpha():
return False
elif op2[0].isdecimal():
return True
elif op2[0] == "(" and not ("+" in op2 or "-" in op2[3:]):
return True
# Giga bricolage...
elif "frac" in op2:
return True
else:
return False
def _render(self, link, *args):
op1 = self.l_parenthesis(args[0], True)
op2 = self.r_parenthesis(args[1], True)
if not self.is_visible(op1, op2):
ans = "{op1} {op2}".format(op1 = op1, op2 = op2)
else:
ans = link.format(op1 = op1, op2 = op2)
ans = save_mainOp(ans, self)
return ans
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del