2016-02-23 10:53:13 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# encoding: utf-8
|
|
|
|
|
|
|
|
|
|
|
|
from .operator import Operator, save_mainOp
|
|
|
|
|
2016-03-06 15:18:01 +00:00
|
|
|
|
2016-02-23 10:53:13 +00:00
|
|
|
class Mul(Operator):
|
2016-02-27 06:07:54 +00:00
|
|
|
r""" The operator *
|
2016-02-23 10:53:13 +00:00
|
|
|
|
2016-02-27 06:07:54 +00:00
|
|
|
>>> mul = Mul()
|
2016-02-23 10:53:13 +00:00
|
|
|
>>> mul
|
2016-02-27 06:07:54 +00:00
|
|
|
*
|
2016-02-23 10:53:13 +00:00
|
|
|
>>> mul(1, 2)
|
|
|
|
2
|
|
|
|
>>> mul.__tex__('1','2')
|
|
|
|
'1 \\times 2'
|
|
|
|
>>> mul.__tex__('2','a')
|
|
|
|
'2 a'
|
2016-02-27 06:07:54 +00:00
|
|
|
>>> mul.__tex__('1','-2')
|
|
|
|
'1 \\times ( -2 )'
|
|
|
|
|
2016-02-23 10:53:13 +00:00
|
|
|
>>> mul.__txt__('1','2')
|
|
|
|
'1 * 2'
|
|
|
|
>>> mul.__txt__('2','a')
|
|
|
|
'2 a'
|
|
|
|
>>> mul.__txt__('a','2')
|
|
|
|
'a * 2'
|
2016-02-27 06:07:54 +00:00
|
|
|
>>> mul.__txt__('1','-2')
|
|
|
|
'1 * ( -2 )'
|
2016-02-23 10:53:13 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
_CARACT = {
|
2016-03-06 15:18:01 +00:00
|
|
|
"operator": "*",
|
|
|
|
"name": "mul",
|
|
|
|
"priority": 4,
|
|
|
|
"arity": 2,
|
|
|
|
"actions": ("__mul__", "__rmul__"),
|
|
|
|
"txt": "{op1} * {op2}",
|
|
|
|
"tex": "{op1} \\times {op2}",
|
2016-02-23 10:53:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2016-02-27 06:07:54 +00:00
|
|
|
|
|
|
|
>>> 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
|
2016-02-23 10:53:13 +00:00
|
|
|
"""
|
|
|
|
# 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):
|
2016-03-06 15:18:01 +00:00
|
|
|
ans = "{op1} {op2}".format(op1=op1, op2=op2)
|
2016-02-23 10:53:13 +00:00
|
|
|
else:
|
2016-03-06 15:18:01 +00:00
|
|
|
ans = link.format(op1=op1, op2=op2)
|
2016-02-23 10:53:13 +00:00
|
|
|
|
|
|
|
ans = save_mainOp(ans, self)
|
|
|
|
return ans
|
|
|
|
|
|
|
|
|
|
|
|
# -----------------------------
|
|
|
|
# Reglages pour 'vim'
|
|
|
|
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
|
|
|
# cursor: 16 del
|