Skip to content

Commit 4fdd3e2

Browse files
committed
Add first "complete" implimentation of constants
1 parent 04adff5 commit 4fdd3e2

File tree

7 files changed

+230
-135
lines changed

7 files changed

+230
-135
lines changed

formulate/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@
77

88
from .backends import from_numexpr, to_numexpr
99
from .backends import from_root, to_root
10-
from .expression import Component, Expression, Variable
10+
from .expression import ExpressionComponent, SingleComponent, Expression, Variable, Constant
1111
from .parser import ParsingException
1212
from .version import __version__
1313

1414

1515
__all__ = [
16-
'Component',
16+
'ExpressionComponent',
17+
'SingleComponent',
1718
'Expression',
1819
'Variable',
20+
'Constant',
1921
'ParsingException',
2022
# numexpr
2123
'from_numexpr',

formulate/backends/ROOT.py

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from __future__ import print_function
55

66
from ..identifiers import IDs, ConstantIDs
7-
from ..parser import Operator, Function, Parser, Constant
7+
from ..parser import POperator, PFunction, Parser, PConstant
88

99

1010
__all__ = [
@@ -13,72 +13,72 @@
1313

1414

1515
config = [
16-
Operator(IDs.MINUS, '-', rhs_only=True),
17-
Operator(IDs.PLUS, '+', rhs_only=True),
18-
Operator(IDs.ADD, '+'),
19-
Operator(IDs.SUB, '-'),
20-
Operator(IDs.MUL, '*'),
21-
Operator(IDs.DIV, '/'),
22-
Operator(IDs.MOD, '%'),
16+
POperator(IDs.MINUS, '-', rhs_only=True),
17+
POperator(IDs.PLUS, '+', rhs_only=True),
18+
POperator(IDs.ADD, '+'),
19+
POperator(IDs.SUB, '-'),
20+
POperator(IDs.MUL, '*'),
21+
POperator(IDs.DIV, '/'),
22+
POperator(IDs.MOD, '%'),
2323

24-
Operator(IDs.EQ, '=='),
25-
Operator(IDs.NEQ, '!='),
26-
Operator(IDs.GT, '>'),
27-
Operator(IDs.GTEQ, '>='),
28-
Operator(IDs.LT, '<'),
29-
Operator(IDs.LTEQ, '<='),
24+
POperator(IDs.EQ, '=='),
25+
POperator(IDs.NEQ, '!='),
26+
POperator(IDs.GT, '>'),
27+
POperator(IDs.GTEQ, '>='),
28+
POperator(IDs.LT, '<'),
29+
POperator(IDs.LTEQ, '<='),
3030

31-
Operator(IDs.AND, '&&'),
32-
Operator(IDs.OR, '||'),
33-
Operator(IDs.NOT, '!', rhs_only=True),
31+
POperator(IDs.AND, '&&'),
32+
POperator(IDs.OR, '||'),
33+
POperator(IDs.NOT, '!', rhs_only=True),
3434

35-
Function(IDs.SQRT, 'sqrt'),
36-
Function(IDs.SQRT, 'TMath::Sqrt'),
37-
Function(IDs.ABS, 'TMath::Abs'),
35+
PFunction(IDs.SQRT, 'sqrt'),
36+
PFunction(IDs.SQRT, 'TMath::Sqrt'),
37+
PFunction(IDs.ABS, 'TMath::Abs'),
3838

39-
Function(IDs.LOG, 'log'),
40-
Function(IDs.LOG, 'TMath::Log'),
41-
Function(IDs.LOG2, 'log2'),
42-
Function(IDs.LOG2, 'TMath::Log2'),
43-
Function(IDs.LOG10, 'log10'),
44-
Function(IDs.LOG10, 'TMath::Log10'),
39+
PFunction(IDs.LOG, 'log'),
40+
PFunction(IDs.LOG, 'TMath::Log'),
41+
PFunction(IDs.LOG2, 'log2'),
42+
PFunction(IDs.LOG2, 'TMath::Log2'),
43+
PFunction(IDs.LOG10, 'log10'),
44+
PFunction(IDs.LOG10, 'TMath::Log10'),
4545

46-
Function(IDs.EXP, 'exp'),
47-
Function(IDs.EXP, 'TMath::Exp'),
46+
PFunction(IDs.EXP, 'exp'),
47+
PFunction(IDs.EXP, 'TMath::Exp'),
4848

49-
Function(IDs.SIN, 'sin'),
50-
Function(IDs.SIN, 'TMath::Sin'),
51-
Function(IDs.ASIN, 'arcsin'),
52-
Function(IDs.ASIN, 'TMath::ASin'),
53-
Function(IDs.COS, 'cos'),
54-
Function(IDs.COS, 'TMath::Cos'),
55-
Function(IDs.ACOS, 'arccos'),
56-
Function(IDs.ACOS, 'TMath::ACos'),
57-
Function(IDs.TAN, 'tan'),
58-
Function(IDs.TAN, 'TMath::Tan'),
59-
Function(IDs.ATAN, 'arctan'),
60-
Function(IDs.ATAN, 'TMath::ATan'),
61-
Function(IDs.ATAN2, 'arctan2', 2),
62-
Function(IDs.ATAN2, 'TMath::ATan2', 2),
49+
PFunction(IDs.SIN, 'sin'),
50+
PFunction(IDs.SIN, 'TMath::Sin'),
51+
PFunction(IDs.ASIN, 'arcsin'),
52+
PFunction(IDs.ASIN, 'TMath::ASin'),
53+
PFunction(IDs.COS, 'cos'),
54+
PFunction(IDs.COS, 'TMath::Cos'),
55+
PFunction(IDs.ACOS, 'arccos'),
56+
PFunction(IDs.ACOS, 'TMath::ACos'),
57+
PFunction(IDs.TAN, 'tan'),
58+
PFunction(IDs.TAN, 'TMath::Tan'),
59+
PFunction(IDs.ATAN, 'arctan'),
60+
PFunction(IDs.ATAN, 'TMath::ATan'),
61+
PFunction(IDs.ATAN2, 'arctan2', 2),
62+
PFunction(IDs.ATAN2, 'TMath::ATan2', 2),
6363
]
6464

6565
constants = [
66-
Constant(ConstantIDs.TRUE, 'True'),
67-
Constant(ConstantIDs.FALSE, 'False'),
66+
PConstant(ConstantIDs.TRUE, 'true'),
67+
PConstant(ConstantIDs.FALSE, 'false'),
6868

69-
Constant(ConstantIDs.SQRT2, 'sqrt2'),
70-
Constant(ConstantIDs.SQRT2, 'TMath::Sqrt2()'),
71-
Constant(ConstantIDs.E, 'e'),
72-
Constant(ConstantIDs.E, 'TMath::E()'),
73-
Constant(ConstantIDs.PI, 'pi'),
74-
Constant(ConstantIDs.PI, 'TMath::Pi()'),
75-
Constant(ConstantIDs.INVPI, 'TMath::InvPi()'),
76-
Constant(ConstantIDs.PIOVER2, 'TMath::PiOver2()'),
77-
Constant(ConstantIDs.PIOVER4, 'TMath::PiOver4()'),
78-
Constant(ConstantIDs.TAU, 'TMath::TwoPi()'),
79-
Constant(ConstantIDs.LN10, 'ln10'),
80-
Constant(ConstantIDs.LN10, 'TMath::Ln10()'),
81-
Constant(ConstantIDs.LOG10E, 'TMath::LogE()'),
69+
PConstant(ConstantIDs.SQRT2, 'sqrt2'),
70+
PConstant(ConstantIDs.SQRT2, 'TMath::Sqrt2()'),
71+
PConstant(ConstantIDs.E, 'e'),
72+
PConstant(ConstantIDs.E, 'TMath::E()'),
73+
PConstant(ConstantIDs.PI, 'pi'),
74+
PConstant(ConstantIDs.PI, 'TMath::Pi()'),
75+
PConstant(ConstantIDs.INVPI, 'TMath::InvPi()'),
76+
PConstant(ConstantIDs.PIOVER2, 'TMath::PiOver2()'),
77+
PConstant(ConstantIDs.PIOVER4, 'TMath::PiOver4()'),
78+
PConstant(ConstantIDs.TAU, 'TMath::TwoPi()'),
79+
PConstant(ConstantIDs.LN10, 'ln10'),
80+
PConstant(ConstantIDs.LN10, 'TMath::Ln10()'),
81+
PConstant(ConstantIDs.LOG10E, 'TMath::LogE()'),
8282
]
8383

8484
root_parser = Parser('ROOT', config, constants)

formulate/backends/numexpr.py

Lines changed: 58 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import math
77

88
from ..identifiers import IDs, ConstantIDs
9-
from ..parser import Operator, Function, Parser, Constant
9+
from ..parser import POperator, PFunction, Parser, PConstant
1010

1111

1212
__all__ = [
@@ -15,67 +15,67 @@
1515

1616

1717
config = [
18-
Operator(IDs.MINUS, '-', rhs_only=True),
19-
Operator(IDs.PLUS, '+', rhs_only=True),
20-
Operator(IDs.ADD, '+'),
21-
Operator(IDs.SUB, '-'),
22-
Operator(IDs.MUL, '*'),
23-
Operator(IDs.DIV, '/'),
24-
Operator(IDs.MOD, '%'),
25-
26-
Operator(IDs.EQ, '=='),
27-
Operator(IDs.NEQ, '!='),
28-
Operator(IDs.GT, '>'),
29-
Operator(IDs.GTEQ, '>='),
30-
Operator(IDs.LT, '<'),
31-
Operator(IDs.LTEQ, '<='),
32-
33-
Operator(IDs.AND, '&'),
34-
Operator(IDs.OR, '|'),
35-
Operator(IDs.XOR, '^'),
36-
Operator(IDs.NOT, '~', rhs_only=True),
37-
38-
Function(IDs.SQRT, 'sqrt'),
39-
Function(IDs.ABS, 'abs'),
40-
Function(IDs.WHERE, 'where', 3),
41-
42-
Function(IDs.LOG, 'log'),
43-
Function(IDs.LOG10, 'log10'),
44-
Function(IDs.LOG1p, 'log1p'),
45-
46-
Function(IDs.EXP, 'exp'),
47-
Function(IDs.EXPM1, 'expm1'),
48-
49-
Function(IDs.SIN, 'sin'),
50-
Function(IDs.ASIN, 'arcsin'),
51-
Function(IDs.COS, 'cos'),
52-
Function(IDs.ACOS, 'arccos'),
53-
Function(IDs.TAN, 'tan'),
54-
Function(IDs.ATAN, 'arctan'),
55-
Function(IDs.ATAN2, 'arctan2', 2),
56-
57-
Function(IDs.SINH, 'sinh'),
58-
Function(IDs.ASINH, 'arcsinh'),
59-
Function(IDs.COSH, 'cosh'),
60-
Function(IDs.ACOSH, 'arccosh'),
61-
Function(IDs.TANH, 'tanh'),
62-
Function(IDs.ATANH, 'arctanh'),
18+
POperator(IDs.MINUS, '-', rhs_only=True),
19+
POperator(IDs.PLUS, '+', rhs_only=True),
20+
POperator(IDs.ADD, '+'),
21+
POperator(IDs.SUB, '-'),
22+
POperator(IDs.MUL, '*'),
23+
POperator(IDs.DIV, '/'),
24+
POperator(IDs.MOD, '%'),
25+
26+
POperator(IDs.EQ, '=='),
27+
POperator(IDs.NEQ, '!='),
28+
POperator(IDs.GT, '>'),
29+
POperator(IDs.GTEQ, '>='),
30+
POperator(IDs.LT, '<'),
31+
POperator(IDs.LTEQ, '<='),
32+
33+
POperator(IDs.AND, '&'),
34+
POperator(IDs.OR, '|'),
35+
POperator(IDs.XOR, '^'),
36+
POperator(IDs.NOT, '~', rhs_only=True),
37+
38+
PFunction(IDs.SQRT, 'sqrt'),
39+
PFunction(IDs.ABS, 'abs'),
40+
PFunction(IDs.WHERE, 'where', 3),
41+
42+
PFunction(IDs.LOG, 'log'),
43+
PFunction(IDs.LOG10, 'log10'),
44+
PFunction(IDs.LOG1p, 'log1p'),
45+
46+
PFunction(IDs.EXP, 'exp'),
47+
PFunction(IDs.EXPM1, 'expm1'),
48+
49+
PFunction(IDs.SIN, 'sin'),
50+
PFunction(IDs.ASIN, 'arcsin'),
51+
PFunction(IDs.COS, 'cos'),
52+
PFunction(IDs.ACOS, 'arccos'),
53+
PFunction(IDs.TAN, 'tan'),
54+
PFunction(IDs.ATAN, 'arctan'),
55+
PFunction(IDs.ATAN2, 'arctan2', 2),
56+
57+
PFunction(IDs.SINH, 'sinh'),
58+
PFunction(IDs.ASINH, 'arcsinh'),
59+
PFunction(IDs.COSH, 'cosh'),
60+
PFunction(IDs.ACOSH, 'arccosh'),
61+
PFunction(IDs.TANH, 'tanh'),
62+
PFunction(IDs.ATANH, 'arctanh'),
6363
]
6464

6565

6666
constants = [
67-
Constant(ConstantIDs.TRUE, 'true'),
68-
Constant(ConstantIDs.FALSE, 'false'),
69-
70-
Constant(ConstantIDs.SQRT2, math.sqrt(2)),
71-
Constant(ConstantIDs.E, math.e),
72-
Constant(ConstantIDs.PI, math.pi),
73-
Constant(ConstantIDs.INVPI, 1/math.pi),
74-
Constant(ConstantIDs.PIOVER2, math.pi/2),
75-
Constant(ConstantIDs.PIOVER4, math.pi/4),
76-
Constant(ConstantIDs.TAU, 2*math.pi),
77-
Constant(ConstantIDs.LN10, math.log(10)),
78-
Constant(ConstantIDs.LOG10E, math.log10(math.e)),
67+
PConstant(ConstantIDs.TRUE, 'True'),
68+
PConstant(ConstantIDs.FALSE, 'False'),
69+
70+
PConstant(ConstantIDs.SQRT2, math.sqrt(2)),
71+
PConstant(ConstantIDs.E, math.e),
72+
PConstant(ConstantIDs.PI, math.pi),
73+
PConstant(ConstantIDs.INVPI, 1/math.pi),
74+
PConstant(ConstantIDs.PIOVER2, math.pi/2),
75+
PConstant(ConstantIDs.PIOVER4, math.pi/4),
76+
PConstant(ConstantIDs.TAU, 2*math.pi),
77+
PConstant(ConstantIDs.LN10, math.log(10)),
78+
PConstant(ConstantIDs.LOG10E, math.log10(math.e)),
7979
]
8080

8181

formulate/expression.py

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77

88

99
__all__ = [
10-
'Component',
10+
'ExpressionComponent',
11+
'SingleComponent',
12+
'Constant',
1113
'Expression',
1214
'Variable',
1315
]
1416

1517

16-
class Component(object):
18+
class ExpressionComponent(object):
1719
def n_variables(self):
1820
raise NotImplementedError()
1921

@@ -202,12 +204,17 @@ def complex(self):
202204
raise NotImplementedError()
203205

204206

205-
class Expression(Component):
207+
class SingleComponent(ExpressionComponent):
208+
pass
209+
210+
211+
class Expression(ExpressionComponent):
206212
def __init__(self, id, *args):
207213
self._id = id
208214
self._args = args
209215

210216
def __repr__(self):
217+
# TODO Make a utility to get fully qualified names
211218
return '{class_name}<{id_name}>({args})'.format(
212219
class_name=self.__class__.__name__, id_name=self.id.name,
213220
args=", ".join(map(repr, self.args)))
@@ -231,14 +238,15 @@ def args(self):
231238
return self._args
232239

233240
def to_string(self, config, constants):
234-
if self.id == IDs.CONST:
241+
if isinstance(self.args[0], SingleComponent):
235242
assert len(self.args) == 1, self.args
236-
return constants[self.args[0]].to_string()
243+
# return str(constants[self.args[0]])
244+
return self.args[0].to_string(config, constants)
237245
else:
238246
return config[self.id].to_string(self, config, constants)
239247

240248

241-
class Variable(Component):
249+
class Variable(SingleComponent):
242250
def __init__(self, name):
243251
self._name = name
244252

@@ -255,3 +263,22 @@ def name(self):
255263

256264
def to_string(self, config, constants):
257265
return self.name
266+
267+
268+
class Constant(SingleComponent):
269+
def __init__(self, id):
270+
self._id = id
271+
272+
def __repr__(self):
273+
return '{class_name}({name})'.format(
274+
class_name=self.__class__.__name__, id=self.id)
275+
276+
def __str__(self):
277+
return self.id.name
278+
279+
@property
280+
def id(self):
281+
return self._id
282+
283+
def to_string(self, config, constants):
284+
return str(constants[self.id].value)

formulate/identifiers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717

1818
class IDs(Enum):
19-
CONST = auto()
19+
FIXED = auto() # Something which can't change such as: constants, numbers and variables
2020

2121
MINUS = auto()
2222
PLUS = auto()

0 commit comments

Comments
 (0)