Skip to content

Commit

Permalink
Add log10 expression
Browse files Browse the repository at this point in the history
Issue #72
  • Loading branch information
rakhimov committed Apr 25, 2017
1 parent f33f74b commit d47ca13
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 1 deletion.
3 changes: 3 additions & 0 deletions share/input.rng
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,9 @@
<element name="log">
<ref name="expression"/>
</element>
<element name="log10">
<ref name="expression"/>
</element>
<element name="sin">
<ref name="expression"/>
</element>
Expand Down
7 changes: 7 additions & 0 deletions src/expression/numerical.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,12 @@ void ValidateExpression<Functor<&std::log>>(
EnsurePositive<InvalidArgument>(args.front(), "Natural Logarithm");
}

template <>
void ValidateExpression<Functor<&std::log10>>(
const std::vector<Expression*>& args) {
assert(args.size() == 1);
EnsurePositive<InvalidArgument>(args.front(), "Decimal Logarithm");
}

} // namespace mef
} // namespace scram
4 changes: 4 additions & 0 deletions src/expression/numerical.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ void ValidateExpression<Functor<&std::asin>>(
template <>
void ValidateExpression<Functor<&std::log>>(
const std::vector<Expression*>& args);
template <>
void ValidateExpression<Functor<&std::log10>>(
const std::vector<Expression*>& args);
/// @}

/// Interval specialization for math functions.
Expand Down Expand Up @@ -102,6 +105,7 @@ using Sinh = FunctorExpression<&std::sinh>; ///< Hyperbolic sine.
using Tanh = FunctorExpression<&std::tanh>; ///< Hyperbolic tangent.
using Exp = FunctorExpression<&std::exp>; ///< Exponential.
using Log = FunctorExpression<&std::log>; ///< Natural logarithm.
using Log10 = FunctorExpression<&std::log10>; ///< Decimal logarithm.

} // namespace mef
} // namespace scram
Expand Down
3 changes: 2 additions & 1 deletion src/initializer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,8 @@ const Initializer::ExtractorMap Initializer::kExpressionExtractors_ = {
{"sinh", &Extract<Sinh>},
{"tanh", &Extract<Tanh>},
{"exp", &Extract<Exp>},
{"log", &Extract<Log>}};
{"log", &Extract<Log>},
{"log10", &Extract<Log10>}};

Expression* Initializer::GetExpression(const xmlpp::Element* expr_element,
const std::string& base_path) {
Expand Down
23 changes: 23 additions & 0 deletions tests/expression_tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,29 @@ TEST(ExpressionTest, Log) {
EXPECT_NO_THROW(dev->Validate());
}

TEST(ExpressionTest, Log10) {
OpenExpression arg_one(1);
std::unique_ptr<Expression> dev;
ASSERT_NO_THROW(dev = std::make_unique<Log10>(&arg_one));
EXPECT_DOUBLE_EQ(0, dev->value());
arg_one.mean = 10;
EXPECT_DOUBLE_EQ(1, dev->value());

arg_one.mean = -1;
EXPECT_THROW(dev->Validate(), InvalidArgument);
arg_one.mean = 0;
EXPECT_THROW(dev->Validate(), InvalidArgument);
arg_one.mean = 1;
EXPECT_NO_THROW(dev->Validate());

arg_one.sample = arg_one.min = 0;
arg_one.max = 1;
EXPECT_THROW(dev->Validate(), InvalidArgument);
arg_one.min = 0.5;
arg_one.max = 1;
EXPECT_NO_THROW(dev->Validate());
}

} // namespace test
} // namespace mef
} // namespace scram
5 changes: 5 additions & 0 deletions tests/input/fta/correct_expressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -247,5 +247,10 @@ The input tries to utilize all the functionality including optional cases.
<float value="1"/>
</log>
</define-parameter>
<define-parameter name="Log10">
<log10>
<float value="1"/>
</log10>
</define-parameter>
</model-data>
</opsa-mef>

0 comments on commit d47ca13

Please sign in to comment.