Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ repos:
- id: remove-tabs
args: [--whitespaces-count, '4']
- repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
rev: v2.11.0
rev: v2.16.0
hooks:
- id: pretty-format-yaml
args: [--autofix, --indent, '2']
Expand All @@ -39,7 +39,7 @@ repos:
hooks:
- id: conda_envfile_parse
files: environment.yaml
# Externally provided executables (so we can use them with editors as well).
# Externally provided executables (so we can use them with editors as well).
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v17.0.6
hooks:
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ message(STATUS "Building xtensor v${${PROJECT_NAME}_VERSION}")
# Dependencies
# ============

set(xtl_REQUIRED_VERSION 0.8.0)
set(xtl_REQUIRED_VERSION 0.8.2)
if(TARGET xtl)
set(xtl_VERSION ${XTL_VERSION_MAJOR}.${XTL_VERSION_MINOR}.${XTL_VERSION_PATCH})
# Note: This is not SEMVER compatible comparison
Expand Down
2 changes: 1 addition & 1 deletion environment-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ channels:
- conda-forge
dependencies:
- cmake
- xtl=0.8.0
- xtl=0.8.2
- xsimd=13.2.0
- nlohmann_json
- doctest
Expand Down
9 changes: 8 additions & 1 deletion include/xtensor/core/xassign.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,14 @@ namespace xt
size_type size = e2.dimension();
index_type shape = uninitialized_shape<index_type>(size);
bool trivial_broadcast = e2.broadcast_shape(shape, true);
e1.resize(std::move(shape));
// Safety check: limit resize to shape size
if (shape.size() > e1.shape().size()) {
index_type safe_shape;
std::copy_n(shape.begin(), e1.shape().size(), safe_shape.begin());
e1.resize(std::move(safe_shape));
} else {
e1.resize(std::move(shape));
}
return trivial_broadcast;
}
}
Expand Down
25 changes: 13 additions & 12 deletions include/xtensor/core/xmath.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <array>
#include <cmath>
#include <complex>
#include <numbers>
#include <type_traits>

#include <xtl/xcomplex.hpp>
Expand All @@ -38,18 +39,18 @@ namespace xt
template <class T = double>
struct numeric_constants
{
static constexpr T PI = 3.141592653589793238463;
static constexpr T PI_2 = 1.57079632679489661923;
static constexpr T PI_4 = 0.785398163397448309616;
static constexpr T D_1_PI = 0.318309886183790671538;
static constexpr T D_2_PI = 0.636619772367581343076;
static constexpr T D_2_SQRTPI = 1.12837916709551257390;
static constexpr T SQRT2 = 1.41421356237309504880;
static constexpr T SQRT1_2 = 0.707106781186547524401;
static constexpr T E = 2.71828182845904523536;
static constexpr T LOG2E = 1.44269504088896340736;
static constexpr T LOG10E = 0.434294481903251827651;
static constexpr T LN2 = 0.693147180559945309417;
static constexpr T PI = std::numbers::pi_v<T>;
static constexpr T PI_2 = 0.5 * PI;
static constexpr T PI_4 = 0.25 * PI;
static constexpr T D_1_PI = std::numbers::inv_pi_v<T>;
static constexpr T D_2_PI = 2 * std::numbers::inv_pi_v<T>;
static constexpr T D_2_SQRTPI = 2 * std::numbers::inv_sqrtpi_v<T>;
static constexpr T SQRT2 = std::numbers::sqrt2_v<T>;
static constexpr T SQRT1_2 = 0.5 * std::numbers::sqrt2_v<T>;
static constexpr T E = std::numbers::e_v<T>;
static constexpr T LOG2E = std::numbers::log2e_v<T>;
static constexpr T LOG10E = std::numbers::log10e_v<T>;
static constexpr T LN2 = std::numbers::ln2_v<T>;
};

/***********
Expand Down
21 changes: 11 additions & 10 deletions include/xtensor/generators/xbuilder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "../core/xfunction.hpp"
#include "../core/xoperation.hpp"
#include "../generators/xgenerator.hpp"
#include "../utils/xutils.hpp"
#include "../views/xbroadcast.hpp"

namespace xt
Expand Down Expand Up @@ -498,8 +499,8 @@ namespace xt
inline value_type access(const tuple_type& t, size_type axis, It first, It last) const
{
// trim off extra indices if provided to match behavior of containers
auto dim_offset = std::distance(first, last) - std::get<0>(t).dimension();
size_t axis_dim = *(first + axis + dim_offset);
auto dim_offset = std::distance(first, last) - as_signed(std::get<0>(t).dimension());
size_t axis_dim = as_unsigned(*(first + as_signed(axis) + dim_offset));
auto match = [&](auto& arr)
{
if (axis_dim >= arr.shape()[axis])
Expand All @@ -520,7 +521,7 @@ namespace xt
const size_t stride = std::accumulate(
shape.begin() + i + 1,
shape.end(),
1,
1ul,
std::multiplies<size_t>()
);
if (i == axis)
Expand All @@ -529,11 +530,11 @@ namespace xt
}
else
{
const auto len = (*(first + i + dim_offset));
const auto len = as_unsigned(*(first + as_signed(i) + dim_offset));
offset += len * stride;
}
}
const auto element = arr.begin() + offset;
const auto element = arr.begin() + as_signed(offset);
return *element;
};

Expand Down Expand Up @@ -563,7 +564,7 @@ namespace xt
{
auto get_item = [&](auto& arr)
{
size_t offset = 0;
std::ptrdiff_t offset = 0;
const size_t end = arr.dimension();
size_t after_axis = 0;
for (size_t i = 0; i < end; i++)
Expand All @@ -576,16 +577,16 @@ namespace xt
const size_t stride = std::accumulate(
shape.begin() + i + 1,
shape.end(),
1,
1ul,
std::multiplies<size_t>()
);
const auto len = (*(first + i + after_axis));
offset += len * stride;
const auto len = as_unsigned(*(first + as_signed(i + after_axis)));
offset += as_signed(len * stride);
}
const auto element = arr.begin() + offset;
return *element;
};
size_type i = *(first + axis);
auto i = as_unsigned((*(first + as_signed(axis))));
return apply<value_type>(i, get_item, t);
}
};
Expand Down
26 changes: 16 additions & 10 deletions include/xtensor/misc/xfft.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "../core/xnoalias.hpp"
#include "../generators/xbuilder.hpp"
#include "../misc/xcomplex.hpp"
#include "../utils/xutils.hpp"
#include "../views/xaxis_slice_iterator.hpp"
#include "../views/xview.hpp"
#include "./xtl_concepts.hpp"
Expand Down Expand Up @@ -61,15 +62,15 @@ namespace xt
auto odd = radix2(xt::view(ev, xt::range(1, _, 2)));
#endif

auto range = xt::arange<double>(N / 2);
auto exp = xt::exp(static_cast<value_type>(-2i) * pi * range / N);
auto range = xt::arange<double>(0.5 * static_cast<double>(N));
auto exp = xt::exp(static_cast<value_type>(-2i) * pi * range / static_cast<precision>(N));
auto t = exp * odd;
auto first_half = even + t;
auto second_half = even - t;
// TODO: should be a call to stack if performance was improved
auto spectrum = xt::xtensor<value_type, 1>::from_shape({N});
xt::view(spectrum, xt::range(0, N / 2)) = first_half;
xt::view(spectrum, xt::range(N / 2, N)) = second_half;
xt::view(spectrum, xt::range(0, static_cast<size_t>(N / 2))) = first_half;
xt::view(spectrum, xt::range(static_cast<size_t>(N / 2), N)) = second_half;
return spectrum;
}
}
Expand All @@ -82,15 +83,18 @@ namespace xt

// Find a power-of-2 convolution length m such that m >= n * 2 + 1
const std::size_t n = data.size();
size_t m = std::ceil(std::log2(n * 2 + 1));
m = std::pow(2, m);
auto m = static_cast<size_t>(std::ceil(std::log2(n * 2 + 1)));
m = 1 << m;

// Trignometric table
auto exp_table = xt::xtensor<std::complex<precision>, 1>::from_shape({n});
xt::xtensor<std::size_t, 1> i = xt::pow(xt::linspace<std::size_t>(0, n - 1, n), 2);
i %= (n * 2);

auto angles = xt::eval(precision{3.141592653589793238463} * i / n);
auto angles = xt::eval(
static_cast<precision>(3.141592653589793238463) * xt::cast<precision>(i)
/ static_cast<precision>(n)
);
auto j = std::complex<precision>(0, 1);
exp_table = xt::exp(-angles * j);

Expand All @@ -112,7 +116,7 @@ namespace xt
auto spectrum_k = xv * yv;
auto complex_args = xt::conj(spectrum_k);
auto fft_res = radix2(complex_args);
auto cv = xt::conj(fft_res) / m;
auto cv = xt::conj(fft_res) / static_cast<precision>(m);

return xt::eval(xt::view(cv, xt::range(0, n)) * exp_table);
}
Expand Down Expand Up @@ -162,7 +166,7 @@ namespace xt
if constexpr (xtl::is_complex<typename std::decay<E>::type::value_type>::value)
{
// check the length of the data on that axis
const std::size_t n = e.shape(axis);
const std::size_t n = e.shape(as_unsigned(axis));
if (n == 0)
{
XTENSOR_THROW(std::runtime_error, "Cannot take the iFFT along an empty dimention");
Expand Down Expand Up @@ -218,7 +222,9 @@ namespace xt
auto outvec = ifft(xv, axis);

// Scaling (because this FFT implementation omits it)
outvec = outvec / n;
using outvec_type = typename decltype(outvec)::value_type::value_type;
outvec_type scale = static_cast<outvec_type>(1.0) / static_cast<outvec_type>(n);
outvec *= scale;

return outvec;
}
Expand Down
14 changes: 7 additions & 7 deletions include/xtensor/misc/xmanipulation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,8 @@ namespace xt
auto perm = xtl::make_sequence<S>(dim, 0);
using id_t = typename S::value_type;
std::iota(perm.begin(), perm.end(), id_t(0));
perm[ax1] = ax2;
perm[ax2] = ax1;
perm[ax1] = static_cast<id_t>(ax2);
perm[ax2] = static_cast<id_t>(ax1);
return perm;
}
}
Expand Down Expand Up @@ -339,18 +339,18 @@ namespace xt

// Initializing to src_norm handles case where `dest == -1` and the loop
// does not go check `perm_idx == dest_norm` a `dim+1`th time.
auto perm = xtl::make_sequence<S>(dim, src_norm);
auto perm = xtl::make_sequence<S>(dim, static_cast<id_t>(src_norm));
id_t perm_idx = 0;
for (id_t i = 0; xtl::cmp_less(i, dim); ++i)
{
if (xtl::cmp_equal(perm_idx, dest_norm))
if (xtl::cmp_equal(static_cast<std::size_t>(perm_idx), dest_norm))
{
perm[perm_idx] = src_norm;
perm[static_cast<std::size_t>(perm_idx)] = static_cast<id_t>(src_norm);
++perm_idx;
}
if (xtl::cmp_not_equal(i, src_norm))
if (xtl::cmp_not_equal(static_cast<std::size_t>(i), src_norm))
{
perm[perm_idx] = i;
perm[static_cast<std::size_t>(perm_idx)] = i;
++perm_idx;
}
}
Expand Down
6 changes: 3 additions & 3 deletions include/xtensor/misc/xset_operation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ namespace xt
* @return a boolean array
*/
template <class E, class T>
inline auto in1d(E&& element, std::initializer_list<T> test_elements) noexcept
inline auto in1d(E&& element, std::initializer_list<T> test_elements)
{
XTENSOR_ASSERT(element.dimension() == 1ul);
return isin(std::forward<E>(element), std::forward<std::initializer_list<T>>(test_elements));
Expand All @@ -145,7 +145,7 @@ namespace xt
* @return a boolean array
*/
template <class E, class F>
inline auto in1d(E&& element, F&& test_elements) noexcept
inline auto in1d(E&& element, F&& test_elements)
requires(has_iterator_interface_concept<F>)
{
XTENSOR_ASSERT(element.dimension() == 1ul);
Expand All @@ -165,7 +165,7 @@ namespace xt
* @return a boolean array
*/
template <class E, iterator_concept I>
inline auto in1d(E&& element, I&& test_elements_begin, I&& test_elements_end) noexcept
inline auto in1d(E&& element, I&& test_elements_begin, I&& test_elements_end)
{
XTENSOR_ASSERT(element.dimension() == 1ul);
return isin(
Expand Down
Loading
Loading