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
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
16 additions
and
4 deletions
+16
-4
libraries/lepton/src/ParsedExpression.cpp
libraries/lepton/src/ParsedExpression.cpp
+16
-4
No files found.
libraries/lepton/src/ParsedExpression.cpp
View file @
4cbb82d4
...
@@ -68,16 +68,24 @@ double ParsedExpression::evaluate(const ExpressionTreeNode& node, const map<stri
...
@@ -68,16 +68,24 @@ double ParsedExpression::evaluate(const ExpressionTreeNode& node, const map<stri
ParsedExpression
ParsedExpression
::
optimize
()
const
{
ParsedExpression
ParsedExpression
::
optimize
()
const
{
ExpressionTreeNode
result
=
precalculateConstantSubexpressions
(
getRootNode
());
ExpressionTreeNode
result
=
precalculateConstantSubexpressions
(
getRootNode
());
result
=
substituteSimplerExpression
(
result
);
while
(
true
)
{
result
=
substituteSimplerExpression
(
result
);
ExpressionTreeNode
simplified
=
substituteSimplerExpression
(
result
);
if
(
simplified
==
result
)
break
;
result
=
simplified
;
}
return
ParsedExpression
(
result
);
return
ParsedExpression
(
result
);
}
}
ParsedExpression
ParsedExpression
::
optimize
(
const
map
<
string
,
double
>&
variables
)
const
{
ParsedExpression
ParsedExpression
::
optimize
(
const
map
<
string
,
double
>&
variables
)
const
{
ExpressionTreeNode
result
=
preevaluateVariables
(
getRootNode
(),
variables
);
ExpressionTreeNode
result
=
preevaluateVariables
(
getRootNode
(),
variables
);
result
=
precalculateConstantSubexpressions
(
result
);
result
=
precalculateConstantSubexpressions
(
result
);
result
=
substituteSimplerExpression
(
result
);
while
(
true
)
{
result
=
substituteSimplerExpression
(
result
);
ExpressionTreeNode
simplified
=
substituteSimplerExpression
(
result
);
if
(
simplified
==
result
)
break
;
result
=
simplified
;
}
return
ParsedExpression
(
result
);
return
ParsedExpression
(
result
);
}
}
...
@@ -206,12 +214,16 @@ ExpressionTreeNode ParsedExpression::substituteSimplerExpression(const Expressio
...
@@ -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
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
]);
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
;
break
;
}
}
case
Operation
::
MULTIPLY_CONSTANT
:
case
Operation
::
MULTIPLY_CONSTANT
:
{
{
if
(
children
[
0
].
getOperation
().
getId
()
==
Operation
::
MULTIPLY_CONSTANT
)
// Combine two multiplies into a single one
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
]);
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
);
return
ExpressionTreeNode
(
node
.
getOperation
().
clone
(),
children
);
...
...
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