Skip to content

Commit 2e125f2

Browse files
authored
Merge pull request #9 from chrisburr/add-constants
Add constants, expression maths, better testing and more
2 parents 3c73bc8 + f01028d commit 2e125f2

File tree

15 files changed

+961
-220
lines changed

15 files changed

+961
-220
lines changed

formulate/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@
77

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

1414

1515
__all__ = [
16+
'ExpressionComponent',
17+
'SingleComponent',
1618
'Expression',
19+
'Variable',
20+
'Constant',
1721
'ParsingException',
1822
# numexpr
1923
'from_numexpr',

formulate/backends/ROOT.py

Lines changed: 62 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
from __future__ import division
44
from __future__ import print_function
55

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

99

1010
__all__ = [
@@ -13,54 +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

65+
constants = [
66+
PConstant(ConstantIDs.TRUE, 'true'),
67+
PConstant(ConstantIDs.FALSE, 'false'),
6568

66-
root_parser = Parser('ROOT', config)
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()'),
82+
]
83+
84+
root_parser = Parser('ROOT', config, constants)

formulate/backends/numexpr.py

Lines changed: 66 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
from __future__ import division
44
from __future__ import print_function
55

6-
from ..identifiers import IDs
7-
from ..parser import Operator, Function, Parser
6+
import math
7+
8+
from ..identifiers import IDs, ConstantIDs
9+
from ..parser import POperator, PFunction, Parser, PConstant
810

911

1012
__all__ = [
@@ -13,51 +15,68 @@
1315

1416

1517
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, '%'),
23-
24-
Operator(IDs.EQ, '=='),
25-
Operator(IDs.NEQ, '!='),
26-
Operator(IDs.GT, '>'),
27-
Operator(IDs.GTEQ, '>='),
28-
Operator(IDs.LT, '<'),
29-
Operator(IDs.LTEQ, '<='),
30-
31-
Operator(IDs.AND, '&'),
32-
Operator(IDs.OR, '|'),
33-
Operator(IDs.NOT, '~', rhs_only=True),
34-
35-
Function(IDs.SQRT, 'sqrt'),
36-
Function(IDs.ABS, 'abs'),
37-
Function(IDs.WHERE, 'where', 3),
38-
39-
Function(IDs.LOG, 'log'),
40-
Function(IDs.LOG10, 'log10'),
41-
Function(IDs.LOG1p, 'log1p'),
42-
43-
Function(IDs.EXP, 'exp'),
44-
Function(IDs.EXPM1, 'expm1'),
45-
46-
Function(IDs.SIN, 'sin'),
47-
Function(IDs.ASIN, 'arcsin'),
48-
Function(IDs.COS, 'cos'),
49-
Function(IDs.ACOS, 'arccos'),
50-
Function(IDs.TAN, 'tan'),
51-
Function(IDs.ATAN, 'arctan'),
52-
Function(IDs.ATAN2, 'arctan2', 2),
53-
54-
Function(IDs.SINH, 'sinh'),
55-
Function(IDs.ASINH, 'arcsinh'),
56-
Function(IDs.COSH, 'cosh'),
57-
Function(IDs.ACOSH, 'arccosh'),
58-
Function(IDs.TANH, 'tanh'),
59-
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'),
63+
]
64+
65+
66+
constants = [
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)),
6079
]
6180

6281

63-
numexpr_parser = Parser('numexpr', config)
82+
numexpr_parser = Parser('numexpr', config, constants)

0 commit comments

Comments
 (0)