node.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. # vim: set fileencoding=utf-8 :
  2. import os.path
  3. import sys
  4. import copy
  5. import re
  6. sys.path.insert(0, os.path.realpath('external'))
  7. from graph_drawing.graph import generate_graph
  8. from graph_drawing.line import generate_line
  9. from graph_drawing.node import Node, Leaf
  10. TYPE_OPERATOR = 1
  11. TYPE_IDENTIFIER = 2
  12. TYPE_INTEGER = 4
  13. TYPE_FLOAT = 8
  14. # Unary
  15. OP_NEG = 1
  16. OP_ABS = 2
  17. # Binary
  18. OP_ADD = 3
  19. OP_SUB = 4
  20. OP_MUL = 5
  21. OP_DIV = 6
  22. OP_POW = 7
  23. OP_SUBSCRIPT = 8
  24. OP_AND = 9
  25. OP_OR = 10
  26. # N-ary (functions)
  27. OP_INT = 11
  28. OP_INT_INDEF = 12
  29. OP_COMMA = 13
  30. OP_SQRT = 14
  31. OP_DER = 15
  32. OP_LOG = 16
  33. # Goniometry
  34. OP_SIN = 17
  35. OP_COS = 18
  36. OP_TAN = 19
  37. OP_SOLVE = 20
  38. OP_EQ = 21
  39. OP_POSSIBILITIES = 22
  40. OP_HINT = 23
  41. OP_REWRITE_ALL = 24
  42. OP_REWRITE_ALL_VERBOSE = 25
  43. OP_REWRITE = 26
  44. # Special identifiers
  45. PI = 'pi'
  46. E = 'e'
  47. INFINITY = 'oo'
  48. SPECIAL_TOKENS = [PI, E, INFINITY]
  49. # Default base to use in parsing 'log(...)'
  50. DEFAULT_LOGARITHM_BASE = 10
  51. TYPE_MAP = {
  52. int: TYPE_INTEGER,
  53. float: TYPE_FLOAT,
  54. str: TYPE_IDENTIFIER,
  55. }
  56. OP_MAP = {
  57. ',': OP_COMMA,
  58. '+': OP_ADD,
  59. '-': OP_SUB,
  60. '*': OP_MUL,
  61. '/': OP_DIV,
  62. '^': OP_POW,
  63. '_': OP_SUBSCRIPT,
  64. '^^': OP_AND,
  65. 'vv': OP_OR,
  66. 'sin': OP_SIN,
  67. 'cos': OP_COS,
  68. 'tan': OP_TAN,
  69. 'sqrt': OP_SQRT,
  70. 'int': OP_INT,
  71. 'der': OP_DER,
  72. 'solve': OP_SOLVE,
  73. 'log': OP_LOG,
  74. '=': OP_EQ,
  75. '??': OP_POSSIBILITIES,
  76. '?': OP_HINT,
  77. '@': OP_REWRITE,
  78. '@@': OP_REWRITE_ALL,
  79. '@@@': OP_REWRITE_ALL_VERBOSE,
  80. }
  81. OP_VALUE_MAP = dict([(v, k) for k, v in OP_MAP.iteritems()])
  82. OP_MAP['ln'] = OP_LOG
  83. OP_VALUE_MAP[OP_INT_INDEF] = 'indef'
  84. OP_VALUE_MAP[OP_ABS] = 'abs'
  85. TOKEN_MAP = {
  86. OP_COMMA: 'COMMA',
  87. OP_ADD: 'PLUS',
  88. OP_SUB: 'MINUS',
  89. OP_MUL: 'TIMES',
  90. OP_DIV: 'DIVIDE',
  91. OP_POW: 'POW',
  92. OP_SUBSCRIPT: 'SUB',
  93. OP_AND: 'AND',
  94. OP_OR: 'OR',
  95. OP_SQRT: 'FUNCTION',
  96. OP_SIN: 'FUNCTION',
  97. OP_COS: 'FUNCTION',
  98. OP_TAN: 'FUNCTION',
  99. OP_INT: 'INTEGRAL',
  100. OP_DER: 'FUNCTION',
  101. OP_SOLVE: 'FUNCTION',
  102. OP_LOG: 'FUNCTION',
  103. OP_EQ: 'EQ',
  104. OP_POSSIBILITIES: 'POSSIBILITIES',
  105. OP_HINT: 'HINT',
  106. OP_REWRITE: 'REWRITE',
  107. OP_REWRITE_ALL: 'REWRITE_ALL',
  108. OP_REWRITE_ALL_VERBOSE: 'REWRITE_ALL_VERBOSE',
  109. }
  110. def to_expression(obj):
  111. if isinstance(obj, ExpressionBase):
  112. return obj.clone()
  113. return ExpressionLeaf(obj)
  114. class ExpressionBase(object):
  115. def __init__(self, *args, **kwargs):
  116. self.negated = 0
  117. def __lt__(self, other):
  118. """
  119. Comparison between this expression{node,leaf} and another
  120. expression{node,leaf}. This comparison will return True if this
  121. instance has less value than the other expression{node,leaf}.
  122. Otherwise, False is returned.
  123. The comparison is based on the following conditions:
  124. 1. Both are leafs. String comparison of the value is used.
  125. 2. This is a leaf and other is a node. This leaf has less value, thus
  126. True is returned.
  127. 3. This is a node and other is a leaf. This leaf has more value, thus
  128. False is returned.
  129. 4. Both are nodes. Compare the polynome properties of the nodes. True
  130. is returned if this node's root property is less than other's root
  131. property, or this node's exponent property is less than other's
  132. exponent property, or this node's coefficient property is less than
  133. other's coefficient property. Otherwise, False is returned.
  134. """
  135. if self.is_leaf:
  136. if other.is_leaf:
  137. # Both are leafs, string compare the value.
  138. self_value = '-' * (self.negated & 1) + str(self.value)
  139. other_value = '-' * (other.negated & 1) + str(other.value)
  140. return self_value < other_value
  141. # Self is a leaf, thus has less value than an expression node.
  142. return True
  143. if other.is_leaf:
  144. # Self is an expression node, and the other is a leaf. Thus, other
  145. # is greater than self.
  146. return False
  147. # Both are nodes, compare the polynome properties.
  148. s_coeff, s_root, s_exp = self.extract_polynome_properties()
  149. o_coeff, o_root, o_exp = other.extract_polynome_properties()
  150. return s_root < o_root or s_exp < o_exp or s_coeff < o_coeff
  151. def __ne__(self, other):
  152. """
  153. Check strict inequivalence, using the strict equivalence operator.
  154. """
  155. return not (self == other)
  156. def clone(self):
  157. return copy.deepcopy(self)
  158. def is_op(self, *ops):
  159. return not self.is_leaf and self.op in ops
  160. def is_power(self, exponent=None):
  161. if self.is_leaf or self.op != OP_POW:
  162. return False
  163. return exponent == None or self[1] == exponent
  164. def is_nary(self):
  165. return not self.is_leaf and self.op in [OP_ADD, OP_SUB, OP_MUL]
  166. def is_identifier(self, identifier=None):
  167. return self.type == TYPE_IDENTIFIER \
  168. and (identifier == None or self.value == identifier)
  169. def is_variable(self):
  170. return self.type == TYPE_IDENTIFIER and self.value not in (PI, E)
  171. def is_int(self):
  172. return self.type == TYPE_INTEGER
  173. def is_float(self):
  174. return self.type == TYPE_FLOAT
  175. def is_numeric(self):
  176. return self.type & (TYPE_FLOAT | TYPE_INTEGER)
  177. def __add__(self, other):
  178. return ExpressionNode(OP_ADD, self, to_expression(other))
  179. def __sub__(self, other):
  180. return ExpressionNode(OP_ADD, self, -to_expression(other))
  181. #FIXME: return ExpressionNode(OP_SUB, self, to_expression(other))
  182. def __mul__(self, other):
  183. return ExpressionNode(OP_MUL, self, to_expression(other))
  184. def __div__(self, other):
  185. return ExpressionNode(OP_DIV, self, to_expression(other))
  186. def __pow__(self, other):
  187. return ExpressionNode(OP_POW, self, to_expression(other))
  188. def __pos__(self):
  189. return self.reduce_negation()
  190. def __and__(self, other):
  191. return ExpressionNode(OP_AND, self, to_expression(other))
  192. def __or__(self, other):
  193. return ExpressionNode(OP_OR, self, to_expression(other))
  194. def reduce_negation(self, n=1):
  195. """Remove n negation flags from the node."""
  196. assert self.negated >= n
  197. return self.negate(-n)
  198. def negate(self, n=1):
  199. """Negate the node n times."""
  200. return negate(self, self.negated + n)
  201. def contains(self, node, include_self=True):
  202. """
  203. Check if a node equal to the specified one exists within this node.
  204. """
  205. if include_self and negate(self, 0) == node:
  206. return True
  207. if not self.is_leaf:
  208. for child in self:
  209. if child.contains(node, include_self=True):
  210. return True
  211. return False
  212. class ExpressionNode(Node, ExpressionBase):
  213. def __init__(self, *args, **kwargs):
  214. super(ExpressionNode, self).__init__(*args, **kwargs)
  215. self.type = TYPE_OPERATOR
  216. op = args[0]
  217. if isinstance(op, str):
  218. self.value = op
  219. self.op = OP_MAP[op]
  220. else:
  221. self.value = OP_VALUE_MAP[op]
  222. self.op = op
  223. def construct_derivative(self, children):
  224. f = children[0]
  225. if len(children) < 2:
  226. # der(der(x ^ 2)) -> [x ^ 2]''
  227. if self[0].is_op(OP_DER) and len(self[0]) < 2:
  228. return f + '\''
  229. # der(x ^ 2) -> [x ^ 2]'
  230. return '[' + f + ']\''
  231. # der(x ^ 2, x) -> d/dx (x ^ 2)
  232. return 'd/d%s (%s)' % (children[1], f)
  233. def construct_logarithm(self, children):
  234. if self[0].is_op(OP_ABS):
  235. content = children[0]
  236. else:
  237. content = '(' + children[0] + ')'
  238. # log(a, e) -> ln(a)
  239. if self[1].is_identifier(E):
  240. return 'ln%s' % content
  241. # log(a, 10) -> log(a)
  242. if self[1] == 10:
  243. return 'log%s' % content
  244. # log(a, 2) -> log_2(a)
  245. if children[1].isdigit():
  246. return 'log_%s%s' % (children[1], content)
  247. def construct_integral(self, children):
  248. # Make sure that any needed parentheses around f(x) are generated,
  249. # and append ' dx' to it (result 'f(x) dx')
  250. fx, x = self[:2]
  251. operand = re.sub(r'(\s*\*)?\s*d$', ' d' + x.value, str(fx * 'd'))
  252. op = 'int'
  253. # Add bounds
  254. if len(self) > 2:
  255. lbnd, ubnd = self[2:]
  256. lbnd = str(ExpressionNode(OP_SUBSCRIPT, lbnd))
  257. ubnd = str(ExpressionNode(OP_POW, ubnd))
  258. op += lbnd + ubnd
  259. # int x ^ 2 -> int x ^ 2 dx
  260. # int x + 1 -> int (x + 1) dx
  261. # int_a^b x ^ 2 -> int_a^b x ^ 2 dx
  262. return op + ' ' + operand
  263. def construct_indef_integral(self, children):
  264. # [x ^ 2]_a^b
  265. F, lbnd, ubnd = self
  266. lbnd = str(ExpressionNode(OP_SUBSCRIPT, lbnd))
  267. ubnd = str(ExpressionNode(OP_POW, ubnd))
  268. return '[%s]%s%s' % (F, lbnd, ubnd)
  269. def construct_function(self, children):
  270. if self.op == OP_ABS:
  271. return '|%s|' % children[0]
  272. constructors = {
  273. OP_DER: self.construct_derivative,
  274. OP_LOG: self.construct_logarithm,
  275. OP_INT: self.construct_integral,
  276. OP_INT_INDEF: self.construct_indef_integral
  277. }
  278. if self.op in constructors:
  279. result = constructors[self.op](children)
  280. if result != None:
  281. return result
  282. # Function with absolute value as only parameter does not need
  283. # parentheses
  284. if self.op in TOKEN_MAP and TOKEN_MAP[self.op] == 'FUNCTION' \
  285. and len(self) == 1 and self[0].is_op(OP_ABS):
  286. return self.title() + children[0]
  287. def __str__(self): # pragma: nocover
  288. return generate_line(self)
  289. def __eq__(self, other):
  290. """
  291. Check strict equivalence.
  292. """
  293. return isinstance(other, ExpressionNode) and self.op == other.op \
  294. and self.negated == other.negated and self.nodes == other.nodes
  295. def substitute(self, old_child, new_child):
  296. self.nodes[self.nodes.index(old_child)] = new_child
  297. def graph(self): # pragma: nocover
  298. return generate_graph(negation_to_node(self))
  299. def extract_polynome_properties(self):
  300. """
  301. Extract polynome properties into tuple format: (coefficient, root,
  302. exponent). Thus: c * r ^ e will be extracted into the tuple (c, r, e).
  303. This function will normalize the expression before extracting the
  304. properties. Therefore, the expression r ^ e * c results the same tuple
  305. (c, r, e) as the expression c * r ^ e.
  306. >>> from src.node import ExpressionNode as N, ExpressionLeaf as L
  307. >>> c, r, e = L('c'), L('r'), L('e')
  308. >>> n1 = N(OP_MUL), c, N('^', r, e))
  309. >>> n1.extract_polynome()
  310. (c, r, e)
  311. >>> n2 = N(OP_MUL, N('^', r, e), c)
  312. >>> n2.extract_polynome()
  313. (c, r, e)
  314. >>> n3 = -r
  315. >>> n3.extract_polynome()
  316. (1, -r, 1)
  317. """
  318. # TODO: change "get_polynome" -> "extract_polynome".
  319. # TODO: change retval of c * r ^ e to (c, r, e).
  320. # was: (root, exponent, coefficient, literal_exponent)
  321. # rule: r ^ e -> (1, r, e)
  322. if self.is_power():
  323. return (ExpressionLeaf(1), self[0], self[1])
  324. # rule: -r -> (1, r, 1)
  325. # rule: --r -> (1, r, 1)
  326. # rule: ---r -> (1, r, 1)
  327. if self.negated:
  328. return (ExpressionLeaf(1), self, ExpressionLeaf(1))
  329. if self.op != OP_MUL:
  330. return
  331. # rule: 3 * 7 ^ e | 'a' * 'b' ^ e
  332. # expression: c * r ^ e ; tree:
  333. #
  334. # *
  335. # ╭┴───╮
  336. # c ^
  337. # ╭─┴╮
  338. # r e
  339. #
  340. # rule: c * r ^ e | (r ^ e) * c
  341. for i, j in ((0, 1), (1, 0)):
  342. if self[j].is_power():
  343. return (self[i], self[j][0], self[j][1])
  344. # Normalize c * r and r * c -> c * r. Otherwise, the tuple will not
  345. # match if the order of the expression is different. Example:
  346. # r ^ e * c == c * r ^ e
  347. # without normalization, those expressions will not match.
  348. #
  349. # rule: c * r | r * c
  350. if self[0] < self[1]:
  351. return (self[0], self[1], ExpressionLeaf(1))
  352. return (self[1], self[0], ExpressionLeaf(1))
  353. def equals(self, other, ignore_negation=False):
  354. """
  355. Perform a non-strict equivalence check between two nodes:
  356. - If the other node is a leaf, it cannot be equal to this node.
  357. - If their operators differ, the nodes are not equal.
  358. - If both nodes are additions or both are multiplications, match each
  359. node in one scope to one in the other (an injective relationship).
  360. Any difference in order of the scopes is irrelevant.
  361. - If both nodes are divisions, the nominator and denominator have to be
  362. non-strictly equal.
  363. """
  364. if not isinstance(other, ExpressionNode) or other.op != self.op:
  365. return False
  366. if self.op in (OP_ADD, OP_MUL):
  367. s0 = Scope(self)
  368. s1 = set(Scope(other))
  369. # Scopes should be of equal size
  370. if len(s0) != len(s1):
  371. return False
  372. # Each node in one scope should have an image node in the other
  373. matched = set()
  374. for n0 in s0:
  375. found = False
  376. for n1 in s1 - matched:
  377. if n0.equals(n1):
  378. found = True
  379. matched.add(n1)
  380. break
  381. if not found:
  382. return False
  383. else:
  384. # Check if all children are non-strictly equal, preserving order
  385. for i, child in enumerate(self):
  386. if not child.equals(other[i]):
  387. return False
  388. if ignore_negation:
  389. return True
  390. return self.negated == other.negated
  391. class ExpressionLeaf(Leaf, ExpressionBase):
  392. def __init__(self, *args, **kwargs):
  393. super(ExpressionLeaf, self).__init__(*args, **kwargs)
  394. self.type = TYPE_MAP[type(args[0])]
  395. def __eq__(self, other):
  396. """
  397. Check strict equivalence.
  398. """
  399. other_type = type(other)
  400. if other_type in TYPE_MAP:
  401. return self.type == TYPE_MAP[other_type] \
  402. and self.actual_value() == other
  403. return self.negated == other.negated and self.type == other.type \
  404. and self.value == other.value
  405. def __repr__(self):
  406. return str(self)
  407. def equals(self, other, ignore_negation=False):
  408. """
  409. Check non-strict equivalence.
  410. Between leaves, this is the same as strict equivalence, except when
  411. negations must be ignored.
  412. """
  413. if ignore_negation:
  414. other_type = type(other)
  415. if other_type in (int, float):
  416. return TYPE_MAP[other_type] == self.type \
  417. and self.value == abs(other)
  418. elif other_type == str:
  419. return self.type == TYPE_IDENTIFIER and self.value == other
  420. return self.type == other.type and self.value == other.value
  421. else:
  422. return self == other
  423. def extract_polynome_properties(self):
  424. """
  425. An expression leaf will return the polynome tuple (1, r, 1), where r is
  426. the leaf itself. See also the method extract_polynome_properties in
  427. ExpressionBase.
  428. """
  429. # rule: 1 * r ^ 1 -> (1, r, 1)
  430. return (ExpressionLeaf(1), self, ExpressionLeaf(1))
  431. def actual_value(self):
  432. if self.type == TYPE_IDENTIFIER:
  433. return self.value
  434. return (1 - 2 * (self.negated & 1)) * self.value
  435. class Scope(object):
  436. def __init__(self, node):
  437. self.node = node
  438. self.nodes = get_scope(node)
  439. def __getitem__(self, key):
  440. return self.nodes[key]
  441. def __setitem__(self, key, value):
  442. self.nodes[key] = value
  443. def __len__(self):
  444. return len(self.nodes)
  445. def __iter__(self):
  446. return iter(self.nodes)
  447. def __eq__(self, other):
  448. return isinstance(other, Scope) and self.node == other.node \
  449. and self.nodes == other.nodes
  450. def __repr__(self):
  451. return '<Scope of "%s">' % repr(self.node)
  452. def index(self, node):
  453. return self.nodes.index(node)
  454. def remove(self, node, **kwargs):
  455. try:
  456. i = self.nodes.index(node)
  457. if 'replacement' in kwargs:
  458. self[i] = kwargs['replacement']
  459. else:
  460. del self.nodes[i]
  461. except ValueError:
  462. raise ValueError('Node "%s" is not in the scope of "%s".'
  463. % (node, self.node))
  464. def replace(self, node, replacement):
  465. self.remove(node, replacement=replacement)
  466. def as_nary_node(self):
  467. return nary_node(self.node.op, self.nodes).negate(self.node.negated)
  468. def nary_node(operator, scope):
  469. """
  470. Create a binary expression tree for an n-ary operator. Takes the operator
  471. and a list of expression nodes as arguments.
  472. """
  473. if len(scope) == 1:
  474. return scope[0]
  475. return ExpressionNode(operator, nary_node(operator, scope[:-1]), scope[-1])
  476. def get_scope(node):
  477. """
  478. Find all n nodes within the n-ary scope of an operator node.
  479. """
  480. scope = []
  481. for child in node:
  482. if child.is_op(node.op) and not child.negated:
  483. scope += get_scope(child)
  484. else:
  485. scope.append(child)
  486. return scope
  487. def negate(node, n=1):
  488. """Negate the given node n times."""
  489. assert n >= 0
  490. new_node = node.clone()
  491. new_node.negated = n
  492. return new_node
  493. def infinity():
  494. """
  495. Return an infinity leaf node.
  496. """
  497. return ExpressionLeaf(INFINITY)
  498. def absolute(exp):
  499. """
  500. Put an 'absolute value' operator on top of the given expression.
  501. """
  502. return ExpressionNode(OP_ABS, exp)
  503. def sin(*args):
  504. """
  505. Create a sinus function node.
  506. """
  507. return ExpressionNode(OP_SIN, *args)
  508. def cos(*args):
  509. """
  510. Create a cosinus function node.
  511. """
  512. return ExpressionNode(OP_COS, *args)
  513. def tan(*args):
  514. """
  515. Create a tangens function node.
  516. """
  517. return ExpressionNode(OP_TAN, *args)
  518. def log(exponent, base=10):
  519. """
  520. Create a logarithm function node (default base is 10).
  521. """
  522. if not isinstance(base, ExpressionLeaf):
  523. base = ExpressionLeaf(base)
  524. return ExpressionNode(OP_LOG, exponent, base)
  525. def ln(exponent):
  526. """
  527. Create a natural logarithm node.
  528. """
  529. return log(exponent, base=E)
  530. def der(f, x=None):
  531. """
  532. Create a derivative node.
  533. """
  534. return ExpressionNode(OP_DER, f, x) if x else ExpressionNode(OP_DER, f)
  535. def integral(*args):
  536. """
  537. Create an integral node.
  538. """
  539. return ExpressionNode(OP_INT, *args)
  540. def indef(*args):
  541. """
  542. Create an indefinite integral node.
  543. """
  544. return ExpressionNode(OP_INT_INDEF, *args)
  545. def eq(left, right):
  546. """
  547. Create an equality operator node.
  548. """
  549. return ExpressionNode(OP_EQ, left, right)
  550. def sqrt(exp):
  551. """
  552. Create a square root node.
  553. """
  554. return ExpressionNode(OP_SQRT, exp)
  555. def negation_to_node(node):
  556. """
  557. Recursively replace negation flags inside a node by explicit unary negation
  558. nodes.
  559. """
  560. if node.negated:
  561. negations = node.negated
  562. node = negate(node, 0)
  563. for i in range(negations):
  564. node = ExpressionNode('-', node)
  565. if node.is_leaf:
  566. return node
  567. return ExpressionNode(node.op, *map(negation_to_node, node))