]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Add some SD-related convenience functions for scalar operations.
authorJean-Paul Pelteret <jppelteret@gmail.com>
Sun, 14 Apr 2019 08:57:52 +0000 (10:57 +0200)
committerJean-Paul Pelteret <jppelteret@gmail.com>
Mon, 22 Apr 2019 08:09:09 +0000 (10:09 +0200)
Specifically, these can be used to create a scalar symbol and symbolic
function.

include/deal.II/differentiation/sd.h
include/deal.II/differentiation/sd/symengine_scalar_operations.h [new file with mode: 0644]
source/differentiation/sd/CMakeLists.txt
source/differentiation/sd/symengine_scalar_operations.cc [new file with mode: 0644]
tests/symengine/symengine_scalar_operations_01.cc [new file with mode: 0644]
tests/symengine/symengine_scalar_operations_01.output [new file with mode: 0644]

index e618da464616e95fef49545a40dee831948853f1..2050e6a509dfd3eeccf3e04765b4e2906c8d4ad7 100644 (file)
@@ -23,6 +23,7 @@
 #  include <deal.II/differentiation/sd/symengine_math.h>
 #  include <deal.II/differentiation/sd/symengine_number_traits.h>
 #  include <deal.II/differentiation/sd/symengine_number_types.h>
+#  include <deal.II/differentiation/sd/symengine_scalar_operations.h>
 #  include <deal.II/differentiation/sd/symengine_types.h>
 #  include <deal.II/differentiation/sd/symengine_utilities.h>
 
diff --git a/include/deal.II/differentiation/sd/symengine_scalar_operations.h b/include/deal.II/differentiation/sd/symengine_scalar_operations.h
new file mode 100644 (file)
index 0000000..c920cd7
--- /dev/null
@@ -0,0 +1,119 @@
+// ---------------------------------------------------------------------
+//
+// 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.
+//
+// ---------------------------------------------------------------------
+
+#ifndef dealii_differentiation_sd_symengine_scalar_operations_h
+#define dealii_differentiation_sd_symengine_scalar_operations_h
+
+#include <deal.II/base/config.h>
+
+#ifdef DEAL_II_WITH_SYMENGINE
+
+#  include <deal.II/base/numbers.h>
+
+#  include <deal.II/differentiation/sd/symengine_number_types.h>
+#  include <deal.II/differentiation/sd/symengine_types.h>
+
+#  include <algorithm>
+#  include <type_traits>
+#  include <utility>
+#  include <vector>
+
+DEAL_II_NAMESPACE_OPEN
+
+namespace Differentiation
+{
+  namespace SD
+  {
+    /**
+     * @name Symbolic variable creation
+     */
+    //@{
+
+    /**
+     * Return an Expression representing a scalar symbolic variable
+     * with the identifier specified by @p symbol.
+     *
+     * For example, if the @p symbol is the string `"x"` then the scalar
+     * symbolic variable that is returned represents the scalar $x$.
+     *
+     * @param[in] symbol An indentifier (or name) for the returned symbolic
+     *            variable.
+     * @return A scalar symbolic variable with the name @p symbol.
+     *
+     * @warning It is up to the user to ensure that there is no ambiguity in the
+     * symbols used within a section of code.
+     */
+    Expression
+    make_symbol(const std::string &symbol);
+
+    /**
+     * Return an Expression representing a scalar symbolic function
+     * with the identifier specified by @p symbol. The function's symbolic
+     * dependencies are specified by the input @p arguments.
+     *
+     * For example, if the @p symbol is the string `"f"`, and the
+     * arguments to the function that is generated  are the
+     * symbolic variable `x` and the symbolic expression `y+z`, then the
+     * generic symbolic function that is returned represents $f(x, y+z)$.
+     *
+     * @param[in] symbol An indentifier (or name) for the returned symbolic
+     *            function.
+     * @param[in] arguments A vector of input arguments to the returned
+     *            symbolic function.
+     * @return A generic symbolic function with the identifier @p symbolic and
+     *         the number of input arguments equal to the length of @p arguments.
+     *
+     * @warning It is up to the user to ensure that there is no ambiguity in the
+     * symbols used within a section of code.
+     */
+    Expression
+    make_symbolic_function(const std::string &         symbol,
+                           const types::symbol_vector &arguments);
+
+    /**
+     * Return an Expression representing a scalar symbolic function
+     * with the identifier specified by @p symbol. The function's symbolic
+     * dependencies are specified by the keys to the input @p arguments map;
+     * the values stored in the map are ignored.
+     *
+     * For example, if the @p symbol is the string `"f"`, and the
+     * arguments to the function that is generated are the
+     * symbolic variable `x` and the symbolic expression `y+z`, then the
+     * generic symbolic function that is returned represents $f(x, y+z)$.
+     *
+     * @param[in] symbol An indentifier (or name) for the returned symbolic
+     *            function.
+     * @param[in] arguments A map of input arguments to the returned
+     *            symbolic function.
+     * @return A generic symbolic function with the identifier @p symbolic and
+     *         the number of input arguments equal to the length of @p arguments.
+     *
+     * @warning It is up to the user to ensure that there is no ambiguity in the
+     * symbols used within a section of code.
+     */
+    Expression
+    make_symbolic_function(const std::string &            symbol,
+                           const types::substitution_map &arguments);
+
+    //@}
+
+  } // namespace SD
+} // namespace Differentiation
+
+DEAL_II_NAMESPACE_CLOSE
+
+#endif // DEAL_II_WITH_SYMENGINE
+
+#endif
index 26920f47a919f391ad7eb88d409ff5b85a517efd..0cb7b56a5e7e23aa5dc279c7721e4fb1443cee77 100644 (file)
@@ -18,6 +18,7 @@ INCLUDE_DIRECTORIES(BEFORE ${CMAKE_CURRENT_BINARY_DIR})
 SET(_src
   symengine_math.cc
   symengine_number_types.cc
+  symengine_scalar_operations.cc
   symengine_types.cc
   symengine_utilities.cc
   )
diff --git a/source/differentiation/sd/symengine_scalar_operations.cc b/source/differentiation/sd/symengine_scalar_operations.cc
new file mode 100644 (file)
index 0000000..1757f0d
--- /dev/null
@@ -0,0 +1,64 @@
+// ---------------------------------------------------------------------
+//
+// 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.
+//
+// ---------------------------------------------------------------------
+
+
+#include <deal.II/base/config.h>
+
+#ifdef DEAL_II_WITH_SYMENGINE
+
+#  include <deal.II/differentiation/sd/symengine_number_types.h>
+#  include <deal.II/differentiation/sd/symengine_scalar_operations.h>
+#  include <deal.II/differentiation/sd/symengine_types.h>
+#  include <deal.II/differentiation/sd/symengine_utilities.h>
+
+DEAL_II_NAMESPACE_OPEN
+
+namespace Differentiation
+{
+  namespace SD
+  {
+    /* ------------------------- Symbol creation -----------------------*/
+
+
+    Expression
+    make_symbol(const std::string &symbol)
+    {
+      return Expression(symbol);
+    }
+
+
+    Expression
+    make_symbolic_function(const std::string &             symbol,
+                           const SD::types::symbol_vector &arguments)
+    {
+      return Expression(symbol, arguments);
+    }
+
+
+    Expression
+    make_symbolic_function(const std::string &                symbol,
+                           const SD::types::substitution_map &arguments)
+    {
+      return make_symbolic_function(symbol,
+                                    SD::Utilities::extract_symbols(arguments));
+    }
+
+
+  } // namespace SD
+} // namespace Differentiation
+
+DEAL_II_NAMESPACE_CLOSE
+
+#endif // DEAL_II_WITH_SYMENGINE
diff --git a/tests/symengine/symengine_scalar_operations_01.cc b/tests/symengine/symengine_scalar_operations_01.cc
new file mode 100644 (file)
index 0000000..666be30
--- /dev/null
@@ -0,0 +1,55 @@
+// ---------------------------------------------------------------------
+//
+// 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 symbols and symbolic functions can be created using the
+// utility functions
+
+#include <deal.II/differentiation/sd.h>
+
+#include "../tests.h"
+
+using namespace dealii;
+namespace SD = Differentiation::SD;
+
+
+int
+main()
+{
+  initlog();
+
+  using SD_number_t = SD::Expression;
+
+  const SD::types::substitution_map sub_map{
+    {SD_number_t("c"), SD_number_t(1.0)},
+    {SD_number_t("b"), SD_number_t(2)},
+    {SD_number_t("a"), SD_number_t(3.0f)}};
+
+  const SD::types::symbol_vector symbols{SD_number_t("c"),
+                                         SD_number_t("b"),
+                                         SD_number_t("a")};
+
+  const SD_number_t symb        = SD::make_symbol("d");
+  const SD_number_t symb_func_1 = SD::make_symbolic_function("f", symbols);
+  const SD_number_t symb_func_2 = SD::make_symbolic_function("g", sub_map);
+
+  deallog << "Symbol: " << symb << std::endl;
+  deallog << "Symbolic function (vector of arguments): " << symb_func_1
+          << std::endl;
+  deallog << "Symbolic function (map of arguments): " << symb_func_2
+          << std::endl;
+
+  deallog << "OK" << std::endl;
+}
diff --git a/tests/symengine/symengine_scalar_operations_01.output b/tests/symengine/symengine_scalar_operations_01.output
new file mode 100644 (file)
index 0000000..b81239f
--- /dev/null
@@ -0,0 +1,5 @@
+
+DEAL::Symbol: d
+DEAL::Symbolic function (vector of arguments): f(c, b, a)
+DEAL::Symbolic function (map of arguments): g(a, b, c)
+DEAL::OK

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.