Skip to content

Commit

Permalink
Merge pull request #99 from bluescarni/pr/neg
Browse files Browse the repository at this point in the history
Implement unary negation.
  • Loading branch information
bluescarni authored Feb 19, 2021
2 parents ca097cf + a229a0a commit 91c5266
Show file tree
Hide file tree
Showing 10 changed files with 962 additions and 4 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ set(HEYOKA_SRC_FILES
"${CMAKE_CURRENT_SOURCE_DIR}/src/math/acosh.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/math/atanh.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/math/erf.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/math/neg.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/detail/string_conv.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/detail/math_wrappers.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/detail/llvm_helpers.cpp"
Expand Down
14 changes: 14 additions & 0 deletions doc/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ Changelog
New
~~~

- Introduce a dedicated negation operator in the
expression system
(`#99 <https://github.com/bluescarni/heyoka/pull/99>`__).
- Implement various new automatic simplifications
in the expression system, and introduce ``powi()`` as
an alternative exponentiation function for natural exponents
(`#98 <https://github.com/bluescarni/heyoka/pull/98>`__).
- Implement propagation over a time grid
(`#95 <https://github.com/bluescarni/heyoka/pull/95>`__).
- Implement support for dense output
Expand All @@ -16,6 +23,13 @@ New
integrator classes
(`#91 <https://github.com/bluescarni/heyoka/pull/91>`__).

Fix
~~~

- Avoid division by zero in certain corner cases
when using ``pow()`` with small natural exponents
(`#98 <https://github.com/bluescarni/heyoka/pull/98>`__).

0.3.0 (2021-02-11)
------------------

Expand Down
1 change: 1 addition & 0 deletions include/heyoka/math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <heyoka/math/erf.hpp>
#include <heyoka/math/exp.hpp>
#include <heyoka/math/log.hpp>
#include <heyoka/math/neg.hpp>
#include <heyoka/math/pow.hpp>
#include <heyoka/math/sigmoid.hpp>
#include <heyoka/math/sin.hpp>
Expand Down
68 changes: 68 additions & 0 deletions include/heyoka/math/neg.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2020, 2021 Francesco Biscani ([email protected]), Dario Izzo ([email protected])
//
// This file is part of the heyoka library.
//
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.

#ifndef HEYOKA_MATH_NEG_HPP
#define HEYOKA_MATH_NEG_HPP

#include <cstdint>
#include <ostream>
#include <string>
#include <unordered_map>
#include <vector>

#include <heyoka/config.hpp>
#include <heyoka/detail/fwd_decl.hpp>
#include <heyoka/detail/llvm_fwd.hpp>
#include <heyoka/detail/visibility.hpp>
#include <heyoka/func.hpp>

namespace heyoka
{

namespace detail
{

class HEYOKA_DLL_PUBLIC neg_impl : public func_base
{
public:
neg_impl();
explicit neg_impl(expression);

void to_stream(std::ostream &) const;

llvm::Value *codegen_dbl(llvm_state &, const std::vector<llvm::Value *> &) const;
llvm::Value *codegen_ldbl(llvm_state &, const std::vector<llvm::Value *> &) const;
#if defined(HEYOKA_HAVE_REAL128)
llvm::Value *codegen_f128(llvm_state &, const std::vector<llvm::Value *> &) const;
#endif

llvm::Value *taylor_diff_dbl(llvm_state &, const std::vector<std::uint32_t> &, const std::vector<llvm::Value *> &,
llvm::Value *, llvm::Value *, std::uint32_t, std::uint32_t, std::uint32_t,
std::uint32_t) const;
llvm::Value *taylor_diff_ldbl(llvm_state &, const std::vector<std::uint32_t> &, const std::vector<llvm::Value *> &,
llvm::Value *, llvm::Value *, std::uint32_t, std::uint32_t, std::uint32_t,
std::uint32_t) const;
#if defined(HEYOKA_HAVE_REAL128)
llvm::Value *taylor_diff_f128(llvm_state &, const std::vector<std::uint32_t> &, const std::vector<llvm::Value *> &,
llvm::Value *, llvm::Value *, std::uint32_t, std::uint32_t, std::uint32_t,
std::uint32_t) const;
#endif
llvm::Function *taylor_c_diff_func_dbl(llvm_state &, std::uint32_t, std::uint32_t) const;
llvm::Function *taylor_c_diff_func_ldbl(llvm_state &, std::uint32_t, std::uint32_t) const;
#if defined(HEYOKA_HAVE_REAL128)
llvm::Function *taylor_c_diff_func_f128(llvm_state &, std::uint32_t, std::uint32_t) const;
#endif
};

} // namespace detail

HEYOKA_DLL_PUBLIC expression neg(expression);

} // namespace heyoka

#endif
7 changes: 6 additions & 1 deletion src/expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <heyoka/expression.hpp>
#include <heyoka/func.hpp>
#include <heyoka/llvm_state.hpp>
#include <heyoka/math/neg.hpp>
#include <heyoka/math/square.hpp>
#include <heyoka/number.hpp>
#include <heyoka/param.hpp>
Expand Down Expand Up @@ -200,7 +201,11 @@ expression operator+(expression e)

expression operator-(expression e)
{
return expression{number{-1.}} * std::move(e);
if (auto num_ptr = std::get_if<number>(&e.value())) {
return expression{-std::move(*num_ptr)};
} else {
return neg(std::move(e));
}
}

expression operator+(expression e1, expression e2)
Expand Down
Loading

0 comments on commit 91c5266

Please sign in to comment.