Commit a9209ec2 authored by Taddeus Kroes's avatar Taddeus Kroes

Added possibility to specify an exponent to ExpressionBase.is_power.

parent 31dc5ca0
...@@ -33,6 +33,11 @@ OP_EXPAND = 9 ...@@ -33,6 +33,11 @@ OP_EXPAND = 9
OP_COMMA = 10 OP_COMMA = 10
OP_SQRT = 11 OP_SQRT = 11
# Goniometry
OP_SIN = 12
OP_COS = 13
OP_TAN = 14
TYPE_MAP = { TYPE_MAP = {
int: TYPE_INTEGER, int: TYPE_INTEGER,
...@@ -112,8 +117,11 @@ class ExpressionBase(object): ...@@ -112,8 +117,11 @@ class ExpressionBase(object):
def is_op(self, op): def is_op(self, op):
return not self.is_leaf and self.op == op return not self.is_leaf and self.op == op
def is_power(self): def is_power(self, exponent=None):
return not self.is_leaf and self.op == OP_POW if self.is_leaf or self.op != OP_POW:
return False
return exponent == None or self[1] == exponent
def is_nary(self): def is_nary(self):
return not self.is_leaf and self.op in [OP_ADD, OP_SUB, OP_MUL] return not self.is_leaf and self.op in [OP_ADD, OP_SUB, OP_MUL]
......
...@@ -35,8 +35,12 @@ class TestNode(RulesTestCase): ...@@ -35,8 +35,12 @@ class TestNode(RulesTestCase):
self.assertFalse(N('+', *self.l[:2]).is_leaf) self.assertFalse(N('+', *self.l[:2]).is_leaf)
def test_is_power(self): def test_is_power(self):
self.assertTrue(N('^', *self.l[:2]).is_power()) self.assertTrue(N('^', *self.l[2:]).is_power())
self.assertFalse(N('+', *self.l[:2]).is_power()) self.assertFalse(N('+', *self.l[2:]).is_power())
def test_is_power_exponent(self):
self.assertTrue(N('^', *self.l[2:]).is_power(5))
self.assertFalse(N('^', *self.l[2:]).is_power(2))
def test_is_nary(self): def test_is_nary(self):
self.assertTrue(N('+', *self.l[:2]).is_nary()) self.assertTrue(N('+', *self.l[:2]).is_nary())
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment