From 5d033e2d55c66c4ecc2629ad9df239502630e875 Mon Sep 17 00:00:00 2001 From: Jean-Paul Pelteret Date: Tue, 7 May 2019 22:00:42 +0200 Subject: [PATCH] Add test for SD scalar substitution and evaluation --- .../symengine_scalar_operations_03.cc | 97 +++++++++++++++++++ .../symengine_scalar_operations_03.output | 7 ++ 2 files changed, 104 insertions(+) create mode 100644 tests/symengine/symengine_scalar_operations_03.cc create mode 100644 tests/symengine/symengine_scalar_operations_03.output diff --git a/tests/symengine/symengine_scalar_operations_03.cc b/tests/symengine/symengine_scalar_operations_03.cc new file mode 100644 index 0000000000..51bf24c042 --- /dev/null +++ b/tests/symengine/symengine_scalar_operations_03.cc @@ -0,0 +1,97 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2019 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE at +// the top level of the deal.II distribution. +// +// --------------------------------------------------------------------- + + +// Check that symbol substitution works for scalars + +#include + +#include "../tests.h" + +using namespace dealii; +namespace SD = Differentiation::SD; + + +int +main() +{ + initlog(); + + using SD_number_t = SD::Expression; + + const double a = 1.0; + const double b = 2.0; + + const SD_number_t symb_a = SD::make_symbol("a"); + const SD_number_t symb_b = SD::make_symbol("b"); + + deallog.push("Substitution: Individual"); + { + const SD_number_t symb_a_plus_b = symb_a + symb_b; + deallog << "Symbolic a+b: " << symb_a_plus_b << std::endl; + + const SD_number_t symb_a_plus_b_1 = + SD::substitute(symb_a_plus_b, symb_a, a); + deallog << "Symbolic a+b (a=1): " << symb_a_plus_b_1 << std::endl; + + const SD_number_t symb_a_plus_b_subs = + SD::substitute(symb_a_plus_b_1, symb_b, b); + deallog << "Symbolic a+b (a=1, b=2): " << symb_a_plus_b_subs << std::endl; + } + deallog.pop(); + + deallog.push("Substitution: Vector"); + { + const SD_number_t symb_a_plus_b = symb_a + symb_b; + + const std::vector> symbol_values{ + std::make_pair(symb_a, a), std::make_pair(symb_b, b)}; + + const SD_number_t symb_a_plus_b_subs = + SD::substitute(symb_a_plus_b, symbol_values); + deallog << "Symbolic a+b (a=1, b=2): " << symb_a_plus_b_subs << std::endl; + } + deallog.pop(); + + deallog.push("Substitution: Map"); + { + const SD_number_t symb_a_plus_b = symb_a + symb_b; + + const SD::types::substitution_map substitution_map = + SD::make_substitution_map(std::make_pair(symb_a, a), + std::make_pair(symb_b, b)); + + const SD_number_t symb_a_plus_b_subs = + SD::substitute(symb_a_plus_b, substitution_map); + deallog << "Symbolic a+b (a=1, b=2): " << symb_a_plus_b_subs << std::endl; + } + deallog.pop(); + + deallog.push("Substitution with evaluation"); + { + const SD_number_t symb_a_plus_b = symb_a + symb_b; + + const SD::types::substitution_map substitution_map = + SD::make_substitution_map(std::make_pair(symb_a, a), + std::make_pair(symb_b, b)); + + const double symb_a_plus_b_subs = + SD::substitute_and_evaluate(symb_a_plus_b, substitution_map); + Assert(symb_a_plus_b_subs == (a + b), ExcInternalError()); + } + deallog.pop(); + + deallog << "OK" << std::endl; +} diff --git a/tests/symengine/symengine_scalar_operations_03.output b/tests/symengine/symengine_scalar_operations_03.output new file mode 100644 index 0000000000..f6540135b0 --- /dev/null +++ b/tests/symengine/symengine_scalar_operations_03.output @@ -0,0 +1,7 @@ + +DEAL:Substitution: Individual::Symbolic a+b: a + b +DEAL:Substitution: Individual::Symbolic a+b (a=1): 1.0 + b +DEAL:Substitution: Individual::Symbolic a+b (a=1, b=2): 3.0 +DEAL:Substitution: Vector::Symbolic a+b (a=1, b=2): 3.0 +DEAL:Substitution: Map::Symbolic a+b (a=1, b=2): 3.0 +DEAL::OK -- 2.39.5