LCOV - code coverage report
Current view: top level - variable - math.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 16 16 100.0 %
Date: 2024-11-17 01:47:58 Functions: 1 1 100.0 %

          Line data    Source code
       1             : // SPDX-License-Identifier: BSD-3-Clause
       2             : // Copyright (c) 2023 Scipp contributors (https://github.com/scipp)
       3             : #include "scipp/variable/math.h"
       4             : 
       5             : #include "scipp/core/element/math.h"
       6             : #include "scipp/variable/transform.h"
       7             : 
       8             : namespace scipp::variable {
       9             : 
      10          21 : Variable midpoints(const Variable &var, const std::optional<Dim> dim) {
      11          21 :   if (var.ndim() == 0) {
      12           2 :     throw except::DimensionError(
      13           4 :         "`midpoints` requires at least one input dimension, got a scalar.");
      14             :   }
      15             : 
      16          19 :   if (!dim.has_value() && var.ndim() != 1) {
      17           1 :     throw std::invalid_argument("Cannot deduce dimension to compute "
      18           1 :                                 "midpoints of variable with dimensions " +
      19           3 :                                 to_string(var.dims()) +
      20           2 :                                 ". Select one using the `dim` argument.");
      21             :   }
      22             : 
      23          18 :   const auto d = dim.has_value() ? *dim : var.dim();
      24          18 :   const auto len = var.dims()[d];
      25          18 :   if (len == scipp::index{1}) {
      26           4 :     throw except::DimensionError("Cannot compute midpoints in dimension `" +
      27           6 :                                  to_string(d) + "` of length 1.");
      28             :   }
      29          32 :   return transform(var.slice({d, 0, len - 1}), var.slice({d, 1, len}),
      30          48 :                    core::element::midpoint, "midpoints");
      31             : } // namespace scipp::variable
      32             : } // namespace scipp::variable

Generated by: LCOV version 1.14