Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
T
trs
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Taddeüs Kroes
trs
Commits
9146b782
Commit
9146b782
authored
Mar 15, 2012
by
Taddeus Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added possibility to pass an operator as an integer to the ExprexxionNode constructor.
parent
5cf274a2
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
44 additions
and
33 deletions
+44
-33
src/node.py
src/node.py
+21
-11
src/parser.py
src/parser.py
+9
-13
src/rules/derivatives.py
src/rules/derivatives.py
+1
-1
src/rules/goniometry.py
src/rules/goniometry.py
+10
-5
src/rules/logarithmic.py
src/rules/logarithmic.py
+1
-1
src/rules/powers.py
src/rules/powers.py
+2
-2
No files found.
src/node.py
View file @
9146b782
...
@@ -78,7 +78,6 @@ OP_MAP = {
...
@@ -78,7 +78,6 @@ OP_MAP = {
'der'
:
OP_DER
,
'der'
:
OP_DER
,
'solve'
:
OP_SOLVE
,
'solve'
:
OP_SOLVE
,
'log'
:
OP_LOG
,
'log'
:
OP_LOG
,
'ln'
:
OP_LOG
,
'='
:
OP_EQ
,
'='
:
OP_EQ
,
'??'
:
OP_POSSIBILITIES
,
'??'
:
OP_POSSIBILITIES
,
'?'
:
OP_HINT
,
'?'
:
OP_HINT
,
...
@@ -86,6 +85,9 @@ OP_MAP = {
...
@@ -86,6 +85,9 @@ OP_MAP = {
'@'
:
OP_REWRITE
,
'@'
:
OP_REWRITE
,
}
}
OP_VALUE_MAP
=
dict
([(
v
,
k
)
for
k
,
v
in
OP_MAP
.
iteritems
()])
OP_MAP
[
'ln'
]
=
OP_LOG
TOKEN_MAP
=
{
TOKEN_MAP
=
{
OP_COMMA
:
'COMMA'
,
OP_COMMA
:
'COMMA'
,
OP_ADD
:
'PLUS'
,
OP_ADD
:
'PLUS'
,
...
@@ -192,19 +194,20 @@ class ExpressionBase(object):
...
@@ -192,19 +194,20 @@ class ExpressionBase(object):
return
self
.
type
&
(
TYPE_FLOAT
|
TYPE_INTEGER
)
return
self
.
type
&
(
TYPE_FLOAT
|
TYPE_INTEGER
)
def
__add__
(
self
,
other
):
def
__add__
(
self
,
other
):
return
ExpressionNode
(
'+'
,
self
,
to_expression
(
other
))
return
ExpressionNode
(
OP_ADD
,
self
,
to_expression
(
other
))
def
__sub__
(
self
,
other
):
def
__sub__
(
self
,
other
):
return
ExpressionNode
(
'-'
,
self
,
to_expression
(
other
))
#FIXME: return ExpressionNode(OP_ADD, self, -to_expression(other))
return
ExpressionNode
(
OP_SUB
,
self
,
to_expression
(
other
))
def
__mul__
(
self
,
other
):
def
__mul__
(
self
,
other
):
return
ExpressionNode
(
'*'
,
self
,
to_expression
(
other
))
return
ExpressionNode
(
OP_MUL
,
self
,
to_expression
(
other
))
def
__div__
(
self
,
other
):
def
__div__
(
self
,
other
):
return
ExpressionNode
(
'/'
,
self
,
to_expression
(
other
))
return
ExpressionNode
(
OP_DIV
,
self
,
to_expression
(
other
))
def
__pow__
(
self
,
other
):
def
__pow__
(
self
,
other
):
return
ExpressionNode
(
'^'
,
self
,
to_expression
(
other
))
return
ExpressionNode
(
OP_POW
,
self
,
to_expression
(
other
))
def
__pos__
(
self
):
def
__pos__
(
self
):
return
self
.
reduce_negation
()
return
self
.
reduce_negation
()
...
@@ -238,7 +241,14 @@ class ExpressionNode(Node, ExpressionBase):
...
@@ -238,7 +241,14 @@ class ExpressionNode(Node, ExpressionBase):
def
__init__
(
self
,
*
args
,
**
kwargs
):
def
__init__
(
self
,
*
args
,
**
kwargs
):
super
(
ExpressionNode
,
self
).
__init__
(
*
args
,
**
kwargs
)
super
(
ExpressionNode
,
self
).
__init__
(
*
args
,
**
kwargs
)
self
.
type
=
TYPE_OPERATOR
self
.
type
=
TYPE_OPERATOR
self
.
op
=
OP_MAP
[
args
[
0
]]
op
=
args
[
0
]
if
isinstance
(
op
,
str
):
self
.
value
=
op
self
.
op
=
OP_MAP
[
op
]
else
:
self
.
value
=
OP_VALUE_MAP
[
op
]
self
.
op
=
op
def
construct_function
(
self
,
children
):
def
construct_function
(
self
,
children
):
if
self
.
op
==
OP_DER
:
if
self
.
op
==
OP_DER
:
...
@@ -296,13 +306,13 @@ class ExpressionNode(Node, ExpressionBase):
...
@@ -296,13 +306,13 @@ class ExpressionNode(Node, ExpressionBase):
>>> from src.node import ExpressionNode as N, ExpressionLeaf as L
>>> from src.node import ExpressionNode as N, ExpressionLeaf as L
>>> c, r, e = L('c'), L('r'), L('e')
>>> c, r, e = L('c'), L('r'), L('e')
>>> n1 = N(
'*'
, c, N('^', r, e))
>>> n1 = N(
OP_MUL)
, c, N('^', r, e))
>>> n1.extract_polynome()
>>> n1.extract_polynome()
(c, r, e)
(c, r, e)
>>> n2 = N(
'*'
, N('^', r, e), c)
>>> n2 = N(
OP_MUL
, N('^', r, e), c)
>>> n2.extract_polynome()
>>> n2.extract_polynome()
(c, r, e)
(c, r, e)
>>> n3 =
N('-', r)
>>> n3 =
-r
>>> n3.extract_polynome()
>>> n3.extract_polynome()
(1, -r, 1)
(1, -r, 1)
"""
"""
...
@@ -512,7 +522,7 @@ class Scope(object):
...
@@ -512,7 +522,7 @@ class Scope(object):
self
.
remove
(
node
,
replacement
=
replacement
)
self
.
remove
(
node
,
replacement
=
replacement
)
def
as_nary_node
(
self
):
def
as_nary_node
(
self
):
return
nary_node
(
self
.
node
.
value
,
self
.
nodes
).
negate
(
self
.
node
.
negated
)
return
nary_node
(
self
.
node
.
op
,
self
.
nodes
).
negate
(
self
.
node
.
negated
)
def
nary_node
(
operator
,
scope
):
def
nary_node
(
operator
,
scope
):
...
...
src/parser.py
View file @
9146b782
...
@@ -16,7 +16,7 @@ from graph_drawing.graph import generate_graph
...
@@ -16,7 +16,7 @@ from graph_drawing.graph import generate_graph
from
node
import
ExpressionNode
as
Node
,
ExpressionLeaf
as
Leaf
,
OP_MAP
,
\
from
node
import
ExpressionNode
as
Node
,
ExpressionLeaf
as
Leaf
,
OP_MAP
,
\
OP_DER
,
TOKEN_MAP
,
TYPE_OPERATOR
,
OP_COMMA
,
OP_NEG
,
OP_MUL
,
OP_DIV
,
\
OP_DER
,
TOKEN_MAP
,
TYPE_OPERATOR
,
OP_COMMA
,
OP_NEG
,
OP_MUL
,
OP_DIV
,
\
OP_LOG
,
Scope
,
PI
,
E
,
DEFAULT_LOGARITHM_BASE
OP_LOG
,
OP_ADD
,
Scope
,
PI
,
E
,
DEFAULT_LOGARITHM_BASE
,
OP_VALUE_MAP
from
rules
import
RULES
from
rules
import
RULES
from
strategy
import
pick_suggestion
from
strategy
import
pick_suggestion
from
possibilities
import
filter_duplicates
,
apply_suggestion
from
possibilities
import
filter_duplicates
,
apply_suggestion
...
@@ -389,26 +389,24 @@ class Parser(BisonParser):
...
@@ -389,26 +389,24 @@ class Parser(BisonParser):
op
=
values
[
0
].
split
(
' '
,
1
)[
0
]
op
=
values
[
0
].
split
(
' '
,
1
)[
0
]
if
op
==
'ln'
:
if
op
==
'ln'
:
return
Node
(
'log'
,
values
[
1
],
Leaf
(
E
))
return
Node
(
OP_LOG
,
values
[
1
],
Leaf
(
E
))
if
values
[
1
].
is_op
(
OP_COMMA
):
if
values
[
1
].
is_op
(
OP_COMMA
):
return
Node
(
op
,
*
values
[
1
])
return
Node
(
op
,
*
values
[
1
])
if
op
==
'log'
:
if
op
==
OP_VALUE_MAP
[
OP_LOG
]
:
return
Node
(
'log'
,
values
[
1
],
Leaf
(
DEFAULT_LOGARITHM_BASE
))
return
Node
(
OP_LOG
,
values
[
1
],
Leaf
(
DEFAULT_LOGARITHM_BASE
))
m
=
re
.
match
(
r'^log_([0-9]+)'
,
op
)
m
=
re
.
match
(
r'^log_([0-9]+)'
,
op
)
if
m
:
if
m
:
return
Node
(
'log'
,
values
[
1
],
Leaf
(
int
(
m
.
group
(
1
))))
return
Node
(
OP_LOG
,
values
[
1
],
Leaf
(
int
(
m
.
group
(
1
))))
return
Node
(
op
,
values
[
1
])
return
Node
(
op
,
values
[
1
])
if
option
==
3
:
# rule: DERIVATIVE exp
if
option
==
3
:
# rule: DERIVATIVE exp
op
=
[
k
for
k
,
v
in
OP_MAP
.
iteritems
()
if
v
==
OP_DER
][
0
]
# DERIVATIVE looks like 'd/d*x*' -> extract the 'x'
# DERIVATIVE looks like 'd/d*x*' -> extract the 'x'
return
Node
(
op
,
values
[
1
],
Leaf
(
values
[
0
][
-
2
]))
return
Node
(
OP_DER
,
values
[
1
],
Leaf
(
values
[
0
][
-
2
]))
if
option
==
4
:
# rule: bracket_derivative
if
option
==
4
:
# rule: bracket_derivative
return
values
[
0
]
return
values
[
0
]
...
@@ -422,13 +420,11 @@ class Parser(BisonParser):
...
@@ -422,13 +420,11 @@ class Parser(BisonParser):
| bracket_derivative APOSTROPH
| bracket_derivative APOSTROPH
"""
"""
op
=
[
k
for
k
,
v
in
OP_MAP
.
iteritems
()
if
v
==
OP_DER
][
0
]
if
option
==
0
:
# rule: LBRACKET exp RBRACKET APOSTROPH
if
option
==
0
:
# rule: LBRACKET exp RBRACKET APOSTROPH
return
Node
(
op
,
values
[
1
])
return
Node
(
OP_DER
,
values
[
1
])
if
option
==
1
:
# rule: bracket_derivative APOSTROPH
if
option
==
1
:
# rule: bracket_derivative APOSTROPH
return
Node
(
op
,
values
[
0
])
return
Node
(
OP_DER
,
values
[
0
])
raise
BisonSyntaxError
(
'Unsupported option %d in target "%s".'
raise
BisonSyntaxError
(
'Unsupported option %d in target "%s".'
%
(
option
,
target
))
# pragma: nocover
%
(
option
,
target
))
# pragma: nocover
...
@@ -459,7 +455,7 @@ class Parser(BisonParser):
...
@@ -459,7 +455,7 @@ class Parser(BisonParser):
# Explicit call the hook handler on the created unary negation.
# Explicit call the hook handler on the created unary negation.
node
=
self
.
hook_handler
(
'binary'
,
4
,
names
,
values
,
node
)
node
=
self
.
hook_handler
(
'binary'
,
4
,
names
,
values
,
node
)
return
Node
(
'+'
,
values
[
0
],
values
[
2
])
return
Node
(
OP_ADD
,
values
[
0
],
values
[
2
])
raise
BisonSyntaxError
(
'Unsupported option %d in target "%s".'
raise
BisonSyntaxError
(
'Unsupported option %d in target "%s".'
%
(
option
,
target
))
# pragma: nocover
%
(
option
,
target
))
# pragma: nocover
...
...
src/rules/derivatives.py
View file @
9146b782
...
@@ -10,7 +10,7 @@ from ..translate import _
...
@@ -10,7 +10,7 @@ from ..translate import _
def
der
(
f
,
x
=
None
):
def
der
(
f
,
x
=
None
):
return
N
(
'der'
,
f
,
x
)
if
x
else
N
(
'der'
,
f
)
return
N
(
OP_DER
,
f
,
x
)
if
x
else
N
(
OP_DER
,
f
)
def
second_arg
(
node
):
def
second_arg
(
node
):
...
...
src/rules/goniometry.py
View file @
9146b782
from
.utils
import
is_fraction
from
.utils
import
is_fraction
from
..node
import
ExpressionNode
as
N
,
ExpressionLeaf
as
L
,
Scope
,
OP_ADD
,
\
from
..node
import
ExpressionNode
as
N
,
ExpressionLeaf
as
L
,
Scope
,
OP_ADD
,
\
OP_POW
,
OP_MUL
,
OP_DIV
,
OP_SIN
,
OP_COS
,
OP_TAN
,
PI
,
TYPE_OPERATOR
OP_POW
,
OP_MUL
,
OP_DIV
,
OP_SIN
,
OP_COS
,
OP_TAN
,
OP_SQRT
,
PI
,
\
TYPE_OPERATOR
from
..possibilities
import
Possibility
as
P
,
MESSAGES
from
..possibilities
import
Possibility
as
P
,
MESSAGES
from
..translate
import
_
from
..translate
import
_
def
sin
(
*
args
):
def
sin
(
*
args
):
return
N
(
'sin'
,
*
args
)
return
N
(
OP_SIN
,
*
args
)
def
cos
(
*
args
):
def
cos
(
*
args
):
return
N
(
'cos'
,
*
args
)
return
N
(
OP_COS
,
*
args
)
def
tan
(
*
args
):
def
tan
(
*
args
):
return
N
(
'tan'
,
*
args
)
return
N
(
OP_TAN
,
*
args
)
def
match_add_quadrants
(
node
):
def
match_add_quadrants
(
node
):
...
@@ -102,6 +103,10 @@ def match_half_pi_subtraction(node):
...
@@ -102,6 +103,10 @@ def match_half_pi_subtraction(node):
return
[]
return
[]
def
half_pi_subtraction_sinus
(
root
,
args
):
pass
def
is_pi_frac
(
node
,
denominator
):
def
is_pi_frac
(
node
,
denominator
):
"""
"""
Check if a node is a fraction of 1 multiplied with PI.
Check if a node is a fraction of 1 multiplied with PI.
...
@@ -124,7 +129,7 @@ def is_pi_frac(node, denominator):
...
@@ -124,7 +129,7 @@ def is_pi_frac(node, denominator):
def
sqrt
(
value
):
def
sqrt
(
value
):
return
N
(
'sqrt'
,
L
(
value
))
return
N
(
OP_SQRT
,
L
(
value
))
l0
,
l1
,
sq2
,
sq3
=
L
(
0
),
L
(
1
),
sqrt
(
2
),
sqrt
(
3
)
l0
,
l1
,
sq2
,
sq3
=
L
(
0
),
L
(
1
),
sqrt
(
2
),
sqrt
(
3
)
...
...
src/rules/logarithmic.py
View file @
9146b782
...
@@ -7,7 +7,7 @@ def log(exponent, base=10):
...
@@ -7,7 +7,7 @@ def log(exponent, base=10):
if
not
isinstance
(
base
,
L
):
if
not
isinstance
(
base
,
L
):
base
=
L
(
base
)
base
=
L
(
base
)
return
N
(
'log'
,
exponent
,
base
)
return
N
(
OP_LOG
,
exponent
,
base
)
def
ln
(
exponent
):
def
ln
(
exponent
):
...
...
src/rules/powers.py
View file @
9146b782
from
itertools
import
combinations
from
itertools
import
combinations
from
..node
import
ExpressionNode
as
N
,
ExpressionLeaf
as
L
,
Scope
,
\
from
..node
import
ExpressionNode
as
N
,
ExpressionLeaf
as
L
,
Scope
,
\
OP_MUL
,
OP_DIV
,
OP_POW
,
OP_ADD
,
negate
OP_MUL
,
OP_DIV
,
OP_POW
,
OP_ADD
,
OP_SQRT
,
negate
from
..possibilities
import
Possibility
as
P
,
MESSAGES
from
..possibilities
import
Possibility
as
P
,
MESSAGES
from
..translate
import
_
from
..translate
import
_
...
@@ -238,7 +238,7 @@ def exponent_to_root(root, args):
...
@@ -238,7 +238,7 @@ def exponent_to_root(root, args):
"""
"""
a
,
n
,
m
=
args
a
,
n
,
m
=
args
return
N
(
'sqrt'
,
a
if
n
==
1
else
a
**
n
,
m
)
return
N
(
OP_SQRT
,
a
if
n
==
1
else
a
**
n
,
m
)
def
match_extend_exponent
(
node
):
def
match_extend_exponent
(
node
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment