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
e9dd48f8
Commit
e9dd48f8
authored
Nov 17, 2012
by
Taddeüs Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implemented correct parentheses parsing for logarithms
parent
bbdcbe9c
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
52 additions
and
29 deletions
+52
-29
src/node.py
src/node.py
+21
-5
tests/test_node.py
tests/test_node.py
+15
-8
tests/test_rules_derivatives.py
tests/test_rules_derivatives.py
+13
-13
tests/test_rules_integrals.py
tests/test_rules_integrals.py
+3
-3
No files found.
src/node.py
View file @
e9dd48f8
...
...
@@ -16,6 +16,7 @@
import
os.path
import
sys
import
copy
import
re
sys
.
path
.
insert
(
0
,
os
.
path
.
realpath
(
'external'
))
...
...
@@ -324,22 +325,33 @@ class ExpressionNode(Node, ExpressionBase):
if
self
.
op
in
UNARY_FUNCTIONS
:
return
1
if
self
.
op
==
OP_LOG
and
self
[
1
].
value
in
(
E
,
DEFAULT_LOGARITHM_BASE
):
return
1
#if self.op == OP_LOG and self[1].value in (E, DEFAULT_LOGARITHM_BASE):
# return 1
# Functions always have parentheses
if
self
.
op
in
TOKEN_MAP
and
TOKEN_MAP
[
self
.
op
]
==
'FUNCTION'
:
return
2
return
len
(
self
)
def
operator
(
self
):
# Append an opening parenthesis manually, the closing parentheses is
# appended by postprocess_str
if
self
.
op
==
OP_LOG
:
base
=
self
[
1
].
value
if
base
==
DEFAULT_LOGARITHM_BASE
:
return
self
.
value
return
self
.
value
+
'('
if
base
==
E
:
return
'ln'
return
'ln('
base
=
str
(
self
[
1
])
return
'%s_%s'
%
(
self
.
value
,
str
(
self
[
1
]))
if
not
re
.
match
(
'^[0-9]+|[a-zA-Z]$'
,
base
):
base
=
'('
+
base
+
')'
return
'%s_%s('
%
(
self
.
value
,
base
)
if
self
.
op
==
OP_DXDER
:
return
self
.
value
+
str
(
self
[
1
])
...
...
@@ -365,6 +377,10 @@ class ExpressionNode(Node, ExpressionBase):
self
[
0
]
=
ExpressionNode
(
OP_BRACKETS
,
self
[
0
])
def
postprocess_str
(
self
,
s
):
# A bit hacky, but forced because of operator() method
if
self
.
op
==
OP_LOG
:
return
s
.
replace
(
'( '
,
'('
)
+
')'
if
self
.
op
==
OP_INT
:
return
'%s d%s'
%
(
s
,
self
[
1
])
...
...
tests/test_node.py
View file @
e9dd48f8
...
...
@@ -248,12 +248,6 @@ class TestNode(RulesTestCase):
self
.
assertEqual
(
str
(
tree
(
"(x ^ 2)''"
)),
"[x ^ 2]''"
)
self
.
assertEqual
(
str
(
tree
(
'd/dx x ^ 2'
)),
'd/dx x ^ 2'
)
def
test_construct_function_logarithm
(
self
):
self
.
assertEqual
(
str
(
tree
(
'log(x, e)'
)),
'ln x'
)
self
.
assertEqual
(
str
(
tree
(
'log(x, 10)'
)),
'log x'
)
self
.
assertEqual
(
str
(
tree
(
'log(x, 2)'
)),
'log_2 x'
)
self
.
assertEqual
(
str
(
tree
(
'log(x, g)'
)),
'log_g x'
)
def
test_construct_function_integral
(
self
):
self
.
assertEqual
(
str
(
tree
(
'int x ^ 2'
)),
'int x ^ 2 dx'
)
self
.
assertEqual
(
str
(
tree
(
'int x ^ 2 dx'
)),
'int x ^ 2 dx'
)
...
...
@@ -271,8 +265,21 @@ class TestNode(RulesTestCase):
'[x ^ 2]_(a - b)^(a + b)'
)
def
test_construct_function_absolute_child
(
self
):
self
.
assertEqual
(
str
(
tree
(
'ln(|x|)'
)),
'ln|x|'
)
self
.
assertEqual
(
str
(
tree
(
'sin(|x|)'
)),
'sin|x|'
)
self
.
assertEqual
(
str
(
tree
(
'ln(|x|)'
)),
'ln(|x|)'
)
self
.
assertEqual
(
str
(
tree
(
'sin(|x|)'
)),
'sin(|x|)'
)
def
test_construct_logarithm
(
self
):
self
.
assertEqual
(
str
(
tree
(
'log n'
)),
'log(n)'
)
self
.
assertEqual
(
str
(
tree
(
'log(n)'
)),
'log(n)'
)
self
.
assertEqual
(
str
(
tree
(
'ln n'
)),
'ln(n)'
)
self
.
assertEqual
(
str
(
tree
(
'ln(n)'
)),
'ln(n)'
)
self
.
assertEqual
(
str
(
tree
(
'log_2 n'
)),
'log_2(n)'
)
self
.
assertEqual
(
str
(
tree
(
'log_2(n)'
)),
'log_2(n)'
)
self
.
assertEqual
(
str
(
tree
(
'log_g n'
)),
'log_g(n)'
)
self
.
assertEqual
(
str
(
tree
(
'log_(g + h) n'
)),
'log_(g + h)(n)'
)
def
test_infinity
(
self
):
self
.
assertEqual
(
infinity
(),
tree
(
'oo'
))
...
...
tests/test_rules_derivatives.py
View file @
e9dd48f8
...
...
@@ -118,19 +118,19 @@ class TestRulesDerivatives(RulesTestCase):
def
test_power_rule_chain
(
self
):
self
.
assertRewrite
([
"[x ^ x]'"
,
"[e ^ (ln
x ^ x
)]'"
,
"e ^ (ln
x ^ x)[ln x ^ x
]'"
,
"x ^ x * [ln
x ^ x
]'"
,
"x ^ x * [x ln
x
]'"
,
"x ^ x * ([x]' * ln
x + x[ln x
]')"
,
"x ^ x * (1ln
x + x[ln x
]')"
,
"x ^ x * (ln
x + x[ln x
]')"
,
"x ^ x * (ln
x
+ x * 1 / x)"
,
"x ^ x * (ln
x
+ (x * 1) / x)"
,
"x ^ x * (ln
x
+ x / x)"
,
"x ^ x * (ln
x
+ 1)"
,
"x ^ x * ln
x
+ x ^ x * 1"
,
"x ^ x * ln
x
+ x ^ x"
,
"[e ^ (ln
(x ^ x)
)]'"
,
"e ^ (ln
(x ^ x))[ln(x ^ x)
]'"
,
"x ^ x * [ln
(x ^ x)
]'"
,
"x ^ x * [x ln
(x)
]'"
,
"x ^ x * ([x]' * ln
(x) + x[ln(x)
]')"
,
"x ^ x * (1ln
(x) + x[ln(x)
]')"
,
"x ^ x * (ln
(x) + x[ln(x)
]')"
,
"x ^ x * (ln
(x)
+ x * 1 / x)"
,
"x ^ x * (ln
(x)
+ (x * 1) / x)"
,
"x ^ x * (ln
(x)
+ x / x)"
,
"x ^ x * (ln
(x)
+ 1)"
,
"x ^ x * ln
(x)
+ x ^ x * 1"
,
"x ^ x * ln
(x)
+ x ^ x"
,
])
def
test_variable_root
(
self
):
...
...
tests/test_rules_integrals.py
View file @
e9dd48f8
...
...
@@ -142,9 +142,9 @@ class TestRulesIntegrals(RulesTestCase):
'int a / x'
,
'int a * 1 / x dx'
,
'a(int 1 / x dx)'
,
'a(ln
|x|
+ C)'
,
'a ln
|x|
+ aC'
,
# FIXME: 'a
ln|x|
+ C', # ac -> C
'a(ln
(|x|)
+ C)'
,
'a ln
(|x|)
+ aC'
,
# FIXME: 'a
ln(|x|)
+ C', # ac -> C
])
def
test_match_function_integral
(
self
):
...
...
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