Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
tsoc
openmm
Commits
4cbb82d4
Commit
4cbb82d4
authored
Nov 23, 2009
by
Peter Eastman
Browse files
Added some optimizations
parent
ab09b522
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
18 additions
and
6 deletions
+18
-6
libraries/lepton/src/ParsedExpression.cpp
libraries/lepton/src/ParsedExpression.cpp
+16
-4
libraries/lepton/src/Parser.cpp
libraries/lepton/src/Parser.cpp
+2
-2
No files found.
libraries/lepton/src/ParsedExpression.cpp
View file @
4cbb82d4
...
...
@@ -68,16 +68,24 @@ double ParsedExpression::evaluate(const ExpressionTreeNode& node, const map<stri
ParsedExpression
ParsedExpression
::
optimize
()
const
{
ExpressionTreeNode
result
=
precalculateConstantSubexpressions
(
getRootNode
());
result
=
substituteSimplerExpression
(
result
);
result
=
substituteSimplerExpression
(
result
);
while
(
true
)
{
ExpressionTreeNode
simplified
=
substituteSimplerExpression
(
result
);
if
(
simplified
==
result
)
break
;
result
=
simplified
;
}
return
ParsedExpression
(
result
);
}
ParsedExpression
ParsedExpression
::
optimize
(
const
map
<
string
,
double
>&
variables
)
const
{
ExpressionTreeNode
result
=
preevaluateVariables
(
getRootNode
(),
variables
);
result
=
precalculateConstantSubexpressions
(
result
);
result
=
substituteSimplerExpression
(
result
);
result
=
substituteSimplerExpression
(
result
);
while
(
true
)
{
ExpressionTreeNode
simplified
=
substituteSimplerExpression
(
result
);
if
(
simplified
==
result
)
break
;
result
=
simplified
;
}
return
ParsedExpression
(
result
);
}
...
...
@@ -206,12 +214,16 @@ ExpressionTreeNode ParsedExpression::substituteSimplerExpression(const Expressio
{
if
(
children
[
0
].
getOperation
().
getId
()
==
Operation
::
MULTIPLY_CONSTANT
)
// Combine a multiply and a negate into a single multiply
return
ExpressionTreeNode
(
new
Operation
::
MultiplyConstant
(
-
dynamic_cast
<
const
Operation
::
MultiplyConstant
*>
(
&
children
[
0
].
getOperation
())
->
getValue
()),
children
[
0
].
getChildren
()[
0
]);
if
(
children
[
0
].
getOperation
().
getId
()
==
Operation
::
CONSTANT
)
// Negate a constant
return
ExpressionTreeNode
(
new
Operation
::
Constant
(
-
getConstantValue
(
children
[
0
])));
break
;
}
case
Operation
::
MULTIPLY_CONSTANT
:
{
if
(
children
[
0
].
getOperation
().
getId
()
==
Operation
::
MULTIPLY_CONSTANT
)
// Combine two multiplies into a single one
return
ExpressionTreeNode
(
new
Operation
::
MultiplyConstant
(
dynamic_cast
<
const
Operation
::
MultiplyConstant
*>
(
&
node
.
getOperation
())
->
getValue
()
*
dynamic_cast
<
const
Operation
::
MultiplyConstant
*>
(
&
children
[
0
].
getOperation
())
->
getValue
()),
children
[
0
].
getChildren
()[
0
]);
if
(
children
[
0
].
getOperation
().
getId
()
==
Operation
::
CONSTANT
)
// Multiply two constants
return
ExpressionTreeNode
(
new
Operation
::
Constant
(
dynamic_cast
<
const
Operation
::
MultiplyConstant
*>
(
&
node
.
getOperation
())
->
getValue
()
*
getConstantValue
(
children
[
0
])));
}
}
return
ExpressionTreeNode
(
node
.
getOperation
().
clone
(),
children
);
...
...
libraries/lepton/src/Parser.cpp
View file @
4cbb82d4
...
...
@@ -219,7 +219,7 @@ ExpressionTreeNode Parser::parsePrecedence(const vector<ParseToken>& tokens, int
pos
++
;
result
=
parsePrecedence
(
tokens
,
pos
,
customFunctions
,
subexpressionDefs
,
0
);
if
(
pos
==
tokens
.
size
()
||
tokens
[
pos
].
getType
()
!=
ParseToken
::
RightParen
)
throw
Exception
(
"Parse error: unbalanced parentheses"
);
throw
Exception
(
"Parse error: unbalanced parentheses"
);
pos
++
;
}
else
if
(
token
.
getType
()
==
ParseToken
::
Function
)
{
...
...
@@ -233,7 +233,7 @@ ExpressionTreeNode Parser::parsePrecedence(const vector<ParseToken>& tokens, int
pos
++
;
}
while
(
moreArgs
);
if
(
pos
==
tokens
.
size
()
||
tokens
[
pos
].
getType
()
!=
ParseToken
::
RightParen
)
throw
Exception
(
"Parse error: unbalanced parentheses"
);
throw
Exception
(
"Parse error: unbalanced parentheses"
);
pos
++
;
Operation
*
op
=
getFunctionOperation
(
token
.
getText
(),
customFunctions
);
try
{
...
...
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