xo-unit: refactor: bpu2 -> bpu

This commit is contained in:
Roland Conybeare 2024-04-22 20:32:46 -04:00
commit 6045f5a053
6 changed files with 64 additions and 64 deletions

View file

@ -58,26 +58,26 @@ namespace xo {
* native_bpu<universal::time, ratio<1>, ratio<-1,2>> represents unit of 1/sqrt(t)
**/
template<typename Int>
struct bpu2 : basis_unit {
struct bpu : basis_unit {
public:
using ratio_int_type = Int;
public:
constexpr bpu2() = default;
constexpr bpu2(const basis_unit & bu,
const power_ratio_type & power)
constexpr bpu() = default;
constexpr bpu(const basis_unit & bu,
const power_ratio_type & power)
: basis_unit{bu},
power_{power}
{}
constexpr bpu2(dim native_dim,
constexpr bpu(dim native_dim,
const scalefactor_ratio_type & scalefactor,
const power_ratio_type & power)
: basis_unit(native_dim, scalefactor),
power_{power}
{}
static constexpr bpu2<Int> unit_power(const basis_unit & bu) {
return bpu2<Int>(bu, power_ratio_type(1,1));
static constexpr bpu<Int> unit_power(const basis_unit & bu) {
return bpu<Int>(bu, power_ratio_type(1,1));
}
constexpr const power_ratio_type & power() const { return power_; }
@ -98,8 +98,8 @@ namespace xo {
}
/* for bpu x, x.reciprocal() represents dimension of 1/x */
constexpr bpu2<Int> reciprocal() const {
return bpu2<Int>(native_dim(), scalefactor(), power_.negate());
constexpr bpu<Int> reciprocal() const {
return bpu<Int>(native_dim(), scalefactor(), power_.negate());
}
/** @brief this unit represents native dimension taken to this power **/
@ -108,7 +108,7 @@ namespace xo {
template <typename Int>
constexpr auto make_unit_power(const basis_unit & bu) {
return bpu2<Int>::unit_power(bu);
return bpu<Int>::unit_power(bu);
}
#ifdef NOT_USING

View file

@ -6,14 +6,14 @@
#pragma once
#include "xo/ratio/ratio_iostream.hpp"
#include "bpu2.hpp"
#include "bpu.hpp"
#include <iostream>
namespace xo {
namespace qty {
template <typename Int>
inline std::ostream &
operator<<(std::ostream & os, const bpu2<Int> & x) {
operator<<(std::ostream & os, const bpu<Int> & x) {
os << "<bpu"
<< xtag("dim", x.native_dim())
<< xtag("mult", x.scalefactor())

View file

@ -2,7 +2,7 @@
#pragma once
#include "bpu2.hpp"
#include "bpu.hpp"
namespace xo {
namespace qty {

View file

@ -5,7 +5,7 @@
#pragma once
#include "bpu2.hpp"
#include "bpu.hpp"
#include <cmath>
#include <cassert>
@ -30,22 +30,22 @@ namespace xo {
constexpr natural_unit() : n_bpu_{0} {}
constexpr std::size_t n_bpu() const { return n_bpu_; }
constexpr bpu2<Int> * bpu_v() const { return bpu_v_; }
constexpr bpu<Int> * bpu_v() const { return bpu_v_; }
constexpr void push_back(const bpu2<Int> & bpu) {
constexpr void push_back(const bpu<Int> & bpu) {
if (n_bpu_ < n_dim)
bpu_v_[n_bpu_++] = bpu;
}
constexpr bpu2<Int> & operator[](std::size_t i) { return bpu_v_[i]; }
constexpr const bpu2<Int> & operator[](std::size_t i) const { return bpu_v_[i]; }
constexpr bpu<Int> & operator[](std::size_t i) { return bpu_v_[i]; }
constexpr const bpu<Int> & operator[](std::size_t i) const { return bpu_v_[i]; }
private:
/** @brief the number of occupied slots in @c bpu_v_ **/
std::size_t n_bpu_;
/** @brief storage for basis power units **/
bpu2<Int> bpu_v_[n_dim];
bpu<Int> bpu_v_[n_dim];
};
namespace detail {
@ -116,7 +116,7 @@ namespace xo {
template <typename Int>
struct bpu2_rescale_result {
constexpr bpu2_rescale_result(const bpu2<Int> & bpu_rescaled,
constexpr bpu2_rescale_result(const bpu<Int> & bpu_rescaled,
const ratio::ratio<Int> & outer_scale_exact,
double outer_scale_sq)
: bpu_rescaled_{bpu_rescaled},
@ -125,7 +125,7 @@ namespace xo {
{}
/* (b'.u)^p */
bpu2<Int> bpu_rescaled_;
bpu<Int> bpu_rescaled_;
/* (b/b')^p0 */
ratio::ratio<Int> outer_scale_exact_;
/* [(b/b')^q]^2 -- until c++26 only allow q=0 or q=1/2 */
@ -135,7 +135,7 @@ namespace xo {
template <typename Int>
constexpr
bpu2_rescale_result<Int>
bpu2_rescale(const bpu2<Int> & orig,
bpu2_rescale(const bpu<Int> & orig,
const scalefactor_ratio_type & new_scalefactor)
{
ratio::ratio<Int> mult = (orig.scalefactor() / new_scalefactor);
@ -154,7 +154,7 @@ namespace xo {
// remaining possibilities not supported until c++26
}
return bpu2_rescale_result<Int>(bpu2<Int>(orig.native_dim(),
return bpu2_rescale_result<Int>(bpu<Int>(orig.native_dim(),
new_scalefactor,
orig.power()),
mult.power(orig.power().floor()),
@ -164,15 +164,15 @@ namespace xo {
template <typename Int>
constexpr
outer_scalefactor_result<Int>
bpu_product_inplace(bpu2<Int> * p_target_bpu,
const bpu2<Int> & rhs_bpu_orig)
bpu_product_inplace(bpu<Int> * p_target_bpu,
const bpu<Int> & rhs_bpu_orig)
{
assert(rhs_bpu_orig.native_dim() == p_target_bpu->native_dim());
bpu2_rescale_result<Int> rhs_bpu_rr = bpu2_rescale(rhs_bpu_orig,
p_target_bpu->scalefactor());
*p_target_bpu = bpu2<Int>(p_target_bpu->native_dim(),
*p_target_bpu = bpu<Int>(p_target_bpu->native_dim(),
p_target_bpu->scalefactor(),
p_target_bpu->power() + rhs_bpu_orig.power());
@ -184,7 +184,7 @@ namespace xo {
constexpr
outer_scalefactor_result<Int>
bpu_array_product_inplace(natural_unit<Int> * p_target,
const bpu2<Int> & bpu)
const bpu<Int> & bpu)
{
std::size_t i = 0;
for (; i < p_target->n_bpu(); ++i) {

View file

@ -52,12 +52,12 @@ namespace xo {
template <typename Int>
constexpr
detail::bpu2_rescale_result<Int>
bpu2_product(const bpu2<Int> & lhs_bpu,
const bpu2<Int> & rhs_bpu)
bpu2_product(const bpu<Int> & lhs_bpu,
const bpu<Int> & rhs_bpu)
{
assert(lhs_bpu.native_dim() == rhs_bpu.native_dim());
bpu2<Int> prod_bpu = lhs_bpu;
bpu<Int> prod_bpu = lhs_bpu;
auto rr = bpu_product_inplace(&prod_bpu, rhs_bpu);
return bpu2_rescale_result<Int>(prod_bpu, rr.outer_scale_exact_, rr.outer_scale_sq_);
@ -67,7 +67,7 @@ namespace xo {
constexpr
scaled_unit2<Int>
nu_product(const natural_unit<Int> & lhs_bpu_array,
const bpu2<Int> & rhs_bpu)
const bpu<Int> & rhs_bpu)
{
natural_unit<Int> prod = lhs_bpu_array;
auto rr = bpu_array_product_inplace(&prod, rhs_bpu);

View file

@ -7,7 +7,7 @@
#include "xo/unit/natural_unit.hpp"
#include "xo/unit/natural_unit_iostream.hpp"
#include "xo/unit/bpu_store.hpp"
#include "xo/unit/bpu2.hpp"
#include "xo/unit/bpu.hpp"
#include "xo/unit/bpu2_iostream.hpp"
#include "xo/unit/basis_unit.hpp"
#include "xo/unit/native_unit.hpp"
@ -40,7 +40,7 @@ namespace xo {
using xo::qty::basis_unit2_store;
using xo::qty::power_ratio_type;
using xo::qty::abbrev::flatstring_from_exponent;
using xo::qty::bpu2;
using xo::qty::bpu;
using xo::qty::detail::bpu2_rescale;
using xo::qty::detail::bpu2_product;
using xo::qty::natural_unit;
@ -311,24 +311,24 @@ namespace xo {
log && log(xtag("kg", bpu2_abbrev(dim::mass, scalefactor_ratio_type(1000, 1), power_ratio_type(1, 1))));
log && log(xtag("kg*kg", bpu2_abbrev(dim::mass, scalefactor_ratio_type(1000, 1), power_ratio_type(2, 1))));
static_assert(bpu2<int64_t>(dim::mass, scalefactor_ratio_type(1, 1), power_ratio_type(1, 1)).abbrev()
static_assert(bpu<int64_t>(dim::mass, scalefactor_ratio_type(1, 1), power_ratio_type(1, 1)).abbrev()
== bpu2_abbrev_type::from_chars("g"));
static_assert(bpu2<int64_t>(dim::mass, scalefactor_ratio_type(1000, 1), power_ratio_type(1, 1)).abbrev()
static_assert(bpu<int64_t>(dim::mass, scalefactor_ratio_type(1000, 1), power_ratio_type(1, 1)).abbrev()
== bpu2_abbrev_type::from_chars("kg"));
static_assert(bpu2<int64_t>(dim::mass, scalefactor_ratio_type(1000, 1), power_ratio_type(-1, 1)).abbrev()
static_assert(bpu<int64_t>(dim::mass, scalefactor_ratio_type(1000, 1), power_ratio_type(-1, 1)).abbrev()
== bpu2_abbrev_type::from_chars("kg^-1"));
static_assert(bpu2<int64_t>(dim::mass, scalefactor_ratio_type(1000, 1), power_ratio_type(-2, 1)).abbrev()
static_assert(bpu<int64_t>(dim::mass, scalefactor_ratio_type(1000, 1), power_ratio_type(-2, 1)).abbrev()
== bpu2_abbrev_type::from_chars("kg^-2"));
static_assert(bpu2<int64_t>(dim::time, scalefactor_ratio_type(60, 1), power_ratio_type(-2, 1)).abbrev()
static_assert(bpu<int64_t>(dim::time, scalefactor_ratio_type(60, 1), power_ratio_type(-2, 1)).abbrev()
== bpu2_abbrev_type::from_chars("min^-2"));
static_assert(bpu2<int64_t>(dim::time, scalefactor_ratio_type(3600, 1), power_ratio_type(-1, 1)).abbrev()
static_assert(bpu<int64_t>(dim::time, scalefactor_ratio_type(3600, 1), power_ratio_type(-1, 1)).abbrev()
== bpu2_abbrev_type::from_chars("hr^-1"));
static_assert(bpu2<int64_t>(dim::time, scalefactor_ratio_type(24*3600, 1), power_ratio_type(-1, 1)).abbrev()
static_assert(bpu<int64_t>(dim::time, scalefactor_ratio_type(24*3600, 1), power_ratio_type(-1, 1)).abbrev()
== bpu2_abbrev_type::from_chars("dy^-1"));
static_assert(bpu2<int64_t>(dim::time, scalefactor_ratio_type(360*24*3600, 1), power_ratio_type(-1, 1)).abbrev()
static_assert(bpu<int64_t>(dim::time, scalefactor_ratio_type(360*24*3600, 1), power_ratio_type(-1, 1)).abbrev()
== bpu2_abbrev_type::from_chars("yr360^-1"));
static_assert(bpu2<int64_t>(dim::time, scalefactor_ratio_type(360*24*3600, 1), power_ratio_type(-1, 2)).abbrev()
static_assert(bpu<int64_t>(dim::time, scalefactor_ratio_type(360*24*3600, 1), power_ratio_type(-1, 2)).abbrev()
== bpu2_abbrev_type::from_chars("yr360^(-1/2)"));
} /*TEST_CASE(bpu2_abbrev)*/
@ -349,7 +349,7 @@ namespace xo {
{
constexpr auto p = power_ratio_type(1, 1);
constexpr auto orig_bpu = bpu2<int64_t>(dim::mass,
constexpr auto orig_bpu = bpu<int64_t>(dim::mass,
scalefactor_ratio_type(1000, 1),
power_ratio_type(1, 1));
static_assert(orig_bpu.native_dim() == dim::mass);
@ -384,7 +384,7 @@ namespace xo {
{
constexpr auto p = power_ratio_type(-1, 2);
constexpr auto orig_bpu = bpu2<int64_t>(dim::time,
constexpr auto orig_bpu = bpu<int64_t>(dim::time,
scalefactor_ratio_type(360*24*3600, 1),
power_ratio_type(-1, 2));
static_assert(orig_bpu.native_dim() == dim::time);
@ -423,7 +423,7 @@ namespace xo {
{
constexpr auto p = power_ratio_type(-3, 2);
constexpr auto orig_bpu = bpu2<int64_t>(dim::time,
constexpr auto orig_bpu = bpu<int64_t>(dim::time,
scalefactor_ratio_type(360*24*3600, 1),
power_ratio_type(-3, 2));
static_assert(orig_bpu.native_dim() == dim::time);
@ -472,12 +472,12 @@ namespace xo {
//log && log("(A)", xtag("foo", foo));
{
constexpr auto bpu_x = bpu2<int64_t>(dim::time,
constexpr auto bpu_x = bpu<int64_t>(dim::time,
scalefactor_ratio_type(360*24*3600, 1),
power_ratio_type(-3,2));
static_assert(bpu_x.native_dim() == dim::time);
constexpr auto bpu_y = bpu2<int64_t>(dim::time,
constexpr auto bpu_y = bpu<int64_t>(dim::time,
scalefactor_ratio_type(360*24*3600, 1),
power_ratio_type(1,2));
static_assert(bpu_y.native_dim() == dim::time);
@ -509,12 +509,12 @@ namespace xo {
//log && log("(A)", xtag("foo", foo));
{
constexpr auto bpu_x = bpu2<int64_t>(dim::time,
constexpr auto bpu_x = bpu<int64_t>(dim::time,
scalefactor_ratio_type(360*24*3600, 1),
power_ratio_type(-3,2));
static_assert(bpu_x.native_dim() == dim::time);
constexpr auto bpu_y = bpu2<int64_t>(dim::time,
constexpr auto bpu_y = bpu<int64_t>(dim::time,
scalefactor_ratio_type(30*24*3600, 1),
power_ratio_type(1,2));
static_assert(bpu_y.native_dim() == dim::time);
@ -554,7 +554,7 @@ namespace xo {
{
constexpr natural_unit<int64_t> v
= (bpu_array_maker<int64_t>::make_bpu_array
(bpu2<int64_t>(dim::mass, scalefactor_ratio_type(1000, 1), power_ratio_type(1, 1))));
(bpu<int64_t>(dim::mass, scalefactor_ratio_type(1000, 1), power_ratio_type(1, 1))));
static_assert(v.n_bpu() == 1);
}
@ -562,8 +562,8 @@ namespace xo {
{
constexpr natural_unit<int64_t> v
= (bpu_array_maker<int64_t>::make_bpu_array
(bpu2<int64_t>(dim::distance, scalefactor_ratio_type(1, 1000), power_ratio_type(2, 1)),
bpu2<int64_t>(dim::mass, scalefactor_ratio_type(1, 1000), power_ratio_type(-1, 1))));
(bpu<int64_t>(dim::distance, scalefactor_ratio_type(1, 1000), power_ratio_type(2, 1)),
bpu<int64_t>(dim::mass, scalefactor_ratio_type(1, 1000), power_ratio_type(-1, 1))));
static_assert(v.n_bpu() == 2);
}
@ -584,12 +584,12 @@ namespace xo {
{
constexpr natural_unit<int64_t> v
= (bpu_array_maker<int64_t>::make_bpu_array
(bpu2<int64_t>(dim::distance, scalefactor_ratio_type(1, 1000), power_ratio_type(2, 1)),
bpu2<int64_t>(dim::mass, scalefactor_ratio_type(1, 1000), power_ratio_type(-1, 1))));
(bpu<int64_t>(dim::distance, scalefactor_ratio_type(1, 1000), power_ratio_type(2, 1)),
bpu<int64_t>(dim::mass, scalefactor_ratio_type(1, 1000), power_ratio_type(-1, 1))));
static_assert(v.n_bpu() == 2);
constexpr bpu2<int64_t> bpu(dim::time,
constexpr bpu<int64_t> bpu(dim::time,
scalefactor_ratio_type(250*24*3600, 1),
power_ratio_type(-1, 2));
@ -631,12 +631,12 @@ namespace xo {
{
constexpr natural_unit<int64_t> v
= (bpu_array_maker<int64_t>::make_bpu_array
(bpu2<int64_t>(dim::distance, scalefactor_ratio_type(1, 1000), power_ratio_type(2, 1)),
bpu2<int64_t>(dim::time, scalefactor_ratio_type(30*24*3600, 1), power_ratio_type(-1, 2))));
(bpu<int64_t>(dim::distance, scalefactor_ratio_type(1, 1000), power_ratio_type(2, 1)),
bpu<int64_t>(dim::time, scalefactor_ratio_type(30*24*3600, 1), power_ratio_type(-1, 2))));
static_assert(v.n_bpu() == 2);
constexpr bpu2<int64_t> bpu(dim::time,
constexpr bpu<int64_t> bpu(dim::time,
scalefactor_ratio_type(360*24*3600, 1),
power_ratio_type(-1, 2));
@ -675,12 +675,12 @@ namespace xo {
{
constexpr natural_unit<int64_t> v
= (bpu_array_maker<int64_t>::make_bpu_array
(bpu2<int64_t>(dim::distance, scalefactor_ratio_type(1, 1000), power_ratio_type(2, 1)),
bpu2<int64_t>(dim::mass, scalefactor_ratio_type(1, 1000), power_ratio_type(-1, 1))));
(bpu<int64_t>(dim::distance, scalefactor_ratio_type(1, 1000), power_ratio_type(2, 1)),
bpu<int64_t>(dim::mass, scalefactor_ratio_type(1, 1000), power_ratio_type(-1, 1))));
static_assert(v.n_bpu() == 2);
constexpr bpu2<int64_t> bpu(dim::distance,
constexpr bpu<int64_t> bpu(dim::distance,
scalefactor_ratio_type(1, 1000),
power_ratio_type(-1, 1));
@ -719,14 +719,14 @@ namespace xo {
{
constexpr natural_unit<int64_t> v
= (bpu_array_maker<int64_t>::make_bpu_array
(bpu2<int64_t>(dim::distance, scalefactor_ratio_type(1, 1000), power_ratio_type(2, 1)),
bpu2<int64_t>(dim::mass, scalefactor_ratio_type(1, 1000), power_ratio_type(-1, 1))));
(bpu<int64_t>(dim::distance, scalefactor_ratio_type(1, 1000), power_ratio_type(2, 1)),
bpu<int64_t>(dim::mass, scalefactor_ratio_type(1, 1000), power_ratio_type(-1, 1))));
static_assert(v.n_bpu() == 2);
constexpr natural_unit<int64_t> w
= (bpu_array_maker<int64_t>::make_bpu_array
(bpu2<int64_t>(dim::time, scalefactor_ratio_type(30*24*3600, 1), power_ratio_type(-1, 2))));
(bpu<int64_t>(dim::time, scalefactor_ratio_type(30*24*3600, 1), power_ratio_type(-1, 2))));
static_assert(w.n_bpu() == 1);