Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
trs
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Taddeüs Kroes
trs
Commits
76db9913
Commit
76db9913
authored
13 years ago
by
Taddeus Kroes
Browse files
Options
Downloads
Patches
Plain Diff
Added rules that calculate logarithm exponents to see if a logarithm can be reduced to a number.
parent
a0a12318
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/rules/logarithmic.py
+36
-4
36 additions, 4 deletions
src/rules/logarithmic.py
src/rules/precedences.py
+4
-2
4 additions, 2 deletions
src/rules/precedences.py
tests/test_rules_logarithmic.py
+34
-1
34 additions, 1 deletion
tests/test_rules_logarithmic.py
with
74 additions
and
7 deletions
src/rules/logarithmic.py
+
36
−
4
View file @
76db9913
from
itertools
import
combinations
,
product
,
ifilterfalse
import
math
from
.utils
import
find_variables
,
partition
,
divides
,
is_numeric_node
from
..node
import
ExpressionLeaf
as
L
,
OP_LOG
,
OP_ADD
,
OP_MUL
,
OP_POW
,
\
...
...
@@ -219,18 +220,29 @@ def match_factor_out_exponent(node):
This match simplifies a power with a variable in it to a multiplication:
log(a ^ b) -> blog(a)
log(a ^ -b) -> log((a ^ b) ^ -1) # =>* -log(a ^ b)
log(b, a) and a ** y = b with y in Z -> log(a ^ y, a) # =>* y
"""
assert
node
.
is_op
(
OP_LOG
)
p
=
[]
exp
,
base
=
node
if
node
[
0
]
.
is_power
():
a
,
b
=
node
[
0
]
if
exp
.
is_power
():
a
,
b
=
exp
if
b
.
negated
:
p
.
append
(
P
(
node
,
split_negative_exponent
))
p
.
append
(
P
(
node
,
factor_out_exponent
))
if
a
==
base
:
p
.
append
(
P
(
node
,
factor_out_exponent_important
))
else
:
p
.
append
(
P
(
node
,
factor_out_exponent
))
elif
exp
.
is_numeric
()
and
not
exp
.
negated
:
b
,
a
=
exp
.
value
,
base
.
value
y
=
int
(
round
(
math
.
log
(
b
,
a
)))
if
b
==
a
**
y
:
p
.
append
(
P
(
node
,
make_raised_base
,
(
y
,)))
return
p
...
...
@@ -257,7 +269,27 @@ def factor_out_exponent(root, args):
return
b
*
log
(
a
,
base
=
base
)
MESSAGES
[
factor_out_exponent
]
=
_
(
'
Factor out exponent {0[0][0]} from {0}.
'
)
MESSAGES
[
factor_out_exponent
]
=
_
(
'
Factor out exponent {0[0][1]} from {0}.
'
)
def
factor_out_exponent_important
(
root
,
args
):
return
factor_out_exponent
(
root
,
args
)
MESSAGES
[
factor_out_exponent_important
]
=
MESSAGES
[
factor_out_exponent
]
def
make_raised_base
(
root
,
args
):
"""
log(b, a) and b ** y = a with y in Z -> log(a ^ y, a) # =>* y
"""
exp
,
base
=
root
y
=
L
(
args
[
0
])
return
log
(
base
.
clone
()
**
y
,
base
=
base
).
negate
(
root
.
negated
)
MESSAGES
[
make_raised_base
]
=
_
(
'
Write {0[0]} as a power of {0[1]}.
'
)
def
match_factor_in_multiplicant
(
node
):
...
...
This diff is collapsed.
Click to expand it.
src/rules/precedences.py
+
4
−
2
View file @
76db9913
from
.factors
import
expand_double
,
expand_single
from
.sort
import
move_constant
from
.numerics
import
reduce_fraction_constants
from
.numerics
import
reduce_fraction_constants
,
raise_numerics
from
.logarithmic
import
factor_in_exponent_multiplicant
,
\
factor_out_exponent
,
raised_base
factor_out_exponent
,
raised_base
,
factor_out_exponent_important
from
.derivatives
import
chain_rule
from
.negation
import
double_negation
,
negated_factor
,
negated_nominator
,
\
negated_denominator
...
...
@@ -35,6 +35,8 @@ RELATIVE = [
# Expand 'single' before 'double' to avoid unnessecary complexity
(
expand_single
,
expand_double
),
(
factor_out_exponent_important
,
raise_numerics
),
]
...
...
This diff is collapsed.
Click to expand it.
tests/test_rules_logarithmic.py
+
34
−
1
View file @
76db9913
...
...
@@ -6,7 +6,8 @@ from src.rules.logarithmic import log, match_constant_logarithm, \
factor_out_exponent
,
match_factor_in_multiplicant
,
\
factor_in_multiplicant
,
match_expand_terms
,
\
expand_multiplication_terms
,
expand_division_terms
,
\
factor_in_exponent_multiplicant
factor_in_exponent_multiplicant
,
factor_out_exponent_important
,
\
make_raised_base
from
src.node
import
Scope
from
src.possibilities
import
Possibility
as
P
from
tests.rulestestcase
import
RulesTestCase
,
tree
...
...
@@ -140,6 +141,27 @@ class TestRulesLogarithmic(RulesTestCase):
[
P
(
root
,
split_negative_exponent
),
P
(
root
,
factor_out_exponent
)])
def
test_match_factor_out_exponent_important
(
self
):
root
=
tree
(
'
log(10 ^ 2)
'
)
self
.
assertEqualPos
(
match_factor_out_exponent
(
root
),
[
P
(
root
,
factor_out_exponent_important
)])
def
test_match_factor_out_exponent_make_raised_base
(
self
):
root
=
tree
(
'
log(100)
'
)
self
.
assertEqualPos
(
match_factor_out_exponent
(
root
),
[
P
(
root
,
make_raised_base
,
(
2
,))])
root
=
tree
(
'
log(1000)
'
)
self
.
assertEqualPos
(
match_factor_out_exponent
(
root
),
[
P
(
root
,
make_raised_base
,
(
3
,))])
root
=
tree
(
'
log_2(16)
'
)
self
.
assertEqualPos
(
match_factor_out_exponent
(
root
),
[
P
(
root
,
make_raised_base
,
(
4
,))])
root
=
tree
(
'
log(99)
'
)
self
.
assertEqualPos
(
match_factor_out_exponent
(
root
),
[])
def
test_split_negative_exponent
(
self
):
root
,
expect
=
tree
(
'
log(a ^ -b), log((a ^ b) ^ -1)
'
)
self
.
assertEqual
(
split_negative_exponent
(
root
,
()),
expect
)
...
...
@@ -148,6 +170,17 @@ class TestRulesLogarithmic(RulesTestCase):
((
a
,
l2
),
l10
)
=
root
=
tree
(
'
log(a ^ 2)
'
)
self
.
assertEqual
(
factor_out_exponent
(
root
,
()),
l2
*
log
(
a
))
def
test_make_raised_base
(
self
):
root
,
expect
=
tree
(
'
log(1000), log(10 ^ 3)
'
)
self
.
assertEqual
(
make_raised_base
(
root
,
(
3
,)),
expect
)
root
,
expect
=
tree
(
'
log_2(64), log_2(2 ^ 4)
'
)
self
.
assertEqual
(
make_raised_base
(
root
,
(
4
,)),
expect
)
def
test_factor_out_exponent_important
(
self
):
((
a
,
l2
),
l10
)
=
root
=
tree
(
'
log(10 ^ 2)
'
)
self
.
assertEqual
(
factor_out_exponent_important
(
root
,
()),
l2
*
log
(
a
))
def
test_match_factor_in_multiplicant
(
self
):
(
l2
,
log_3
)
=
root
=
tree
(
'
2log(3)
'
)
self
.
assertEqualPos
(
match_factor_in_multiplicant
(
root
),
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment