]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Add some SD-related convenience functions for scalar operations. 7920/head
authorJean-Paul Pelteret <jppelteret@gmail.com>
Sun, 14 Apr 2019 11:09:07 +0000 (13:09 +0200)
committerJean-Paul Pelteret <jppelteret@gmail.com>
Mon, 22 Apr 2019 08:09:09 +0000 (10:09 +0200)
Specifically, a non-member function for scalar differentiation of an
Expression has been added.

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

index c920cd7de1b7e102488e124bd68a8ffaa10f0c1f..15b9dc7529226faf1f4f2d4ab18732213ef2e3fd 100644 (file)
@@ -109,6 +109,27 @@ namespace Differentiation
 
     //@}
 
+    /**
+     * @name Symbolic differentiation
+     */
+    //@{
+
+    /**
+     * Return the symbolic result of computing the partial derivative of the
+     * scalar @p f with respect to the scalar @p x.
+     * In most use cases the function or variable @f would be called the
+     * dependent variable, and @p x the independent variable.
+     *
+     * @param[in] f A scalar symbolic function or (dependent) variable.
+     * @param[in] x A scalar symbolic (independent) variable.
+     * @return The symbolic function or variable representing the result
+     *         $\frac{\partial f}{\partial x}$.
+     */
+    Expression
+    differentiate(const Expression &f, const Expression &x);
+
+    //@}
+
   } // namespace SD
 } // namespace Differentiation
 
index 1757f0dca15adf4bdacd3784ed2405ea370ad28e..7df4e22cc07aae519aeed5d7afb5d6dec7a7a3ae 100644 (file)
@@ -56,6 +56,15 @@ namespace Differentiation
     }
 
 
+    /* --------------------------- Differentiation ------------------------- */
+
+
+    Expression
+    differentiate(const Expression &func, const Expression &op)
+    {
+      return func.differentiate(op);
+    }
+
   } // namespace SD
 } // namespace Differentiation
 
diff --git a/tests/symengine/symengine_scalar_operations_02.cc b/tests/symengine/symengine_scalar_operations_02.cc
new file mode 100644 (file)
index 0000000..db2e61a
--- /dev/null
@@ -0,0 +1,44 @@
+// ---------------------------------------------------------------------
+//
+// 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 scalar differentiation can be performed 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_number_t x     = SD::make_symbol("x");
+  const SD_number_t f     = 2 * x * x;
+  const SD_number_t df_dx = SD::differentiate(f, x);
+
+  deallog << "x: " << x << std::endl;
+  deallog << "f: " << f << std::endl;
+  deallog << "df_dx: " << df_dx << std::endl;
+
+  deallog << "OK" << std::endl;
+}
diff --git a/tests/symengine/symengine_scalar_operations_02.output b/tests/symengine/symengine_scalar_operations_02.output
new file mode 100644 (file)
index 0000000..91cad5c
--- /dev/null
@@ -0,0 +1,5 @@
+
+DEAL::x: x
+DEAL::f: 2*x**2
+DEAL::df_dx: 4*x
+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.