This commit is contained in:
张壹 2025-01-09 11:21:42 +08:00
parent 00aadbd1ea
commit a0f0ceca52
4 changed files with 390 additions and 59 deletions

View File

@ -26,7 +26,7 @@ add_example(kde_ex OFF)
add_example(meshio_ex OFF)
add_example(autodiff_ex OFF)
add_example(multinary_ex OFF)
add_example(text_io_ex ON)
add_example(text_io_ex OFF)
add_example(getoption_ex OFF)
add_example(process_ex OFF)
add_example(array_ex OFF)

View File

@ -34,11 +34,15 @@ int main(int argc, char const *argv[]) try
{
// create a new array and give initial values
array<double> A(10, 0.0, 1.0);
A.memory_usage();
A.log2linear(2);
A.show();
A.linear2log(2);
A.for_each([](double &a, size_t i){ a += 1;});
A.show();
A.parallel_for_each([](double &a, size_t i){a += 1;});
A.show();
A.sequence(1.0, 0.5, 3, 4, 1);

View File

@ -71,7 +71,7 @@ void gctl::fir_filter::init(filting_type_e fi_type, window_type_e w_type, int ta
else if (fi_type == BandStop) band_stop_coefficient(f1/fs, f2/fs);
else throw std::runtime_error("[GCTL] Unkown filter type for gctl::fir_filter");
if (w_type == None) w_.assign_all(1.0);
if (w_type == None) w_.assign(1.0);
else if (w_type == Hamming) window_hamming(taps_, w_);
else if (w_type == Hanning) window_hanning(taps_, w_);
else if (w_type == Triangle) window_triangle(taps_, w_);

View File

@ -245,6 +245,103 @@ namespace gctl
*/
array<ArrValType>& operator/= (const array<ArrValType> &b);
/**
* @brief Overloaded summation operator for the array template.
*
* @param a Input array
* @param b Scale value
* @return Target array
*/
array<ArrValType> operator+ (const ArrValType &b);
/**
* @brief Overloaded minus operator for the array template.
*
* @param a Input array
* @param b Scale value
* @return Target array
*/
array<ArrValType> operator- (const ArrValType &b);
/**
* @brief Overloaded multiple operator for the array template.
*
* @param a Input array
* @param b Scale value
* @return Target array
*/
array<ArrValType> operator* (const ArrValType &b);
/**
* @brief Overloaded division operator for the array template.
*
* @param a Input array
* @param b Scale value
* @return Target array
*/
array<ArrValType> operator/ (const ArrValType &b);
/**
* @brief Overloaded summation operator for the array template.
*
* @param a Input array
* @param b Scale value
* @return Target array
*
*/
array<ArrValType>& operator+= (const ArrValType &b);
/**
* @brief Overloaded summation operator for the array template.
*
* @param a Input array
* @param b Scale value
* @return Target array
*/
array<ArrValType>& operator-= (const ArrValType &b);
/**
* @brief Overloaded multiple operator for the array template.
*
* @param a Input array
* @param b Scale value
* @return Target array
*/
array<ArrValType>& operator*= (const ArrValType &b);
/**
* @brief Overloaded division operator for the array template.
*
* @param a Input array
* @param b Scale value
* @return Target array
*/
array<ArrValType>& operator/= (const ArrValType &b);
/**
* @brief Compare two arraies.
*
* @return True if the two arraies are equal, false otherwise.
*/
bool operator== (const array<ArrValType> &b) const;
/**
* @brief Compare two arraies.
*
* @return True if the two arraies are not equal, false otherwise.
*/
bool operator!= (const array<ArrValType> &b) const;
/**
* @brief Compare two arraies.
*
* @param b Input array
* @param eps Tolerance for flaoting point number
*
* @return True if the two arraies are not equal, false otherwise.
*/
bool equals(const array<ArrValType> &b, ArrValType eps = ArrValType{}) const;
/**
* @brief Destructor
*/
@ -318,7 +415,7 @@ namespace gctl
*
* @param[in] in_val Input value.
*/
void assign_all(ArrValType in_val);
void assign(ArrValType in_val);
/**
* @brief Set value to segments of an array.
@ -334,7 +431,7 @@ namespace gctl
*
* @param b Input array
*/
void append_array(const array<ArrValType> &b);
void concat(const array<ArrValType> &b);
/**
* @brief Extract an array
@ -342,7 +439,7 @@ namespace gctl
* @param b Output array. Must be initialized before use.
* @param st Start index
*/
void extract_array(array<ArrValType> &b, size_t st = 0) const;
void slice(array<ArrValType> &b, size_t st = 0) const;
/**
* @brief Extract an array
@ -350,7 +447,7 @@ namespace gctl
* @param b Output array
* @param ids Index of the extracting elements. Must be initialized before use.
*/
void extract_array(array<ArrValType> &b, const array<size_t> &ids) const;
void slice(array<ArrValType> &b, const array<size_t> &ids) const;
/**
* @brief Insert data from an input array
@ -358,12 +455,12 @@ namespace gctl
* @param b The input array
* @param st The inserting point. The default is zero
*/
void insert_array(array<ArrValType> &b, size_t st = 0);
void insert(array<ArrValType> &b, size_t st = 0);
/**
* @brief Get element value at the given index.
*
* @note This function could be called by a pointer.
* @note This function will check if the index was out of range.
*
* @param[in] index index value.
*
@ -374,7 +471,7 @@ namespace gctl
/**
* @brief Get element value at the given index (Constant).
*
* @note This function could be called by a pointer.
* @note This function will check if the index was out of range.
*
* @param[in] index index value.
*
@ -391,7 +488,7 @@ namespace gctl
*
* @return element's value.
*/
ArrValType &operator[](size_t index);
ArrValType &operator[](size_t index) noexcept;
/**
* @brief Get element value at the given index .
@ -402,7 +499,7 @@ namespace gctl
*
* @return element's value.
*/
ArrValType &operator[](size_t index) const;
ArrValType &operator[](size_t index) const noexcept;
/**
* @brief Get the first element
@ -435,6 +532,8 @@ namespace gctl
/**
* @brief Get the pointer to a element at the given index.
*
* @note This function will check if the index was out of range.
*
* @param[in] index index value.
*
* @return Pointer to the element
@ -446,28 +545,28 @@ namespace gctl
*
* @return True for empty. False other wise.
*/
bool empty() const;
bool empty() const noexcept;
/**
* @brief Return the length of the class member array.
*
* @return Length.
*/
size_t size() const;
size_t size() const noexcept;
/**
* @brief Copy the array to a vector.
*
* @param b Target vector.
*/
void export_vector(std::vector<ArrValType> &b) const;
void output(std::vector<ArrValType> &b) const;
/**
* @brief Copy the array from a vector.
*
* @param b Target vector.
*/
void import_vector(const std::vector<ArrValType> &b);
void input(const std::vector<ArrValType> &b);
#ifdef GCTL_EIGEN
/**
@ -475,14 +574,14 @@ namespace gctl
*
* @param b Target vector.
*/
void export_eigen_vector(Eigen::VectorXd &b) const;
void output(Eigen::VectorXd &b) const;
/**
* @brief Copy the array from a Eigen3 vector.
*
* @param b Target vector.
*/
void import_eigen_vector(const Eigen::VectorXd &b);
void input(const Eigen::VectorXd &b);
#endif // GCTL_EIGEN
/**
@ -492,11 +591,23 @@ namespace gctl
typedef void (*foreach_a_ptr)(ArrValType &ele_ptr, size_t id);
/**
* @brief Operate on each and every element
* @brief
*
* @param func operation function
*
* [](ArrValType &a, size_t i){do something here...}
*
* @tparam BiaryOp
* @param op
*/
void for_each(foreach_a_ptr func);
template<typename BiaryOp>
void parallel_for_each(BiaryOp op)
{
#pragma omp parallel for
for (size_t i = 0; i < length_; i++)
{
op(val_[i], i);
}
}
/**
* @brief Display the elements.
@ -582,6 +693,13 @@ namespace gctl
*/
ArrValType dot(const array<ArrValType> &a);
/**
* @brief Return the sum of all elements.
*
* @return sum value.
*/
ArrValType sum() const;
/**
* @brief Return the mean value.
*
@ -680,6 +798,37 @@ namespace gctl
*
*/
void set2range(ArrValType min, ArrValType max, range_type_e rt = HardScale);
/**
* @brief
*
* @param value
* @return truefalse
*/
bool any_of(const ArrValType& value) const;
/**
* @brief
*
* @param value
* @return truefalse
*/
bool all_of(const ArrValType& value) const;
/**
* @brief
*
* @param value
* @return
*/
size_t count(const ArrValType& value) const;
/**
* @brief 使
*
* @return MB
*/
void memory_usage(std::ostream &os = std::cout) const noexcept;
};
template <typename ArrValType>
@ -898,6 +1047,130 @@ namespace gctl
return *this;
}
template <typename ArrValType>
array<ArrValType> array<ArrValType>::operator+ (const ArrValType &b)
{
array<ArrValType> out(length_);
for (size_t i = 0; i < length_; i++)
{
out[i] = val_[i] + b;
}
return out;
}
template <typename ArrValType>
array<ArrValType> array<ArrValType>::operator- (const ArrValType &b)
{
array<ArrValType> out(length_);
for (size_t i = 0; i < length_; i++)
{
out[i] = b - val_[i];
}
return out;
}
template <typename ArrValType>
array<ArrValType> array<ArrValType>::operator* (const ArrValType &b)
{
array<ArrValType> out(length_);
for (size_t i = 0; i < length_; i++)
{
out[i] = b*val_[i];
}
return out;
}
template <typename ArrValType>
array<ArrValType> array<ArrValType>::operator/ (const ArrValType &b)
{
array<ArrValType> out(length_);
for (size_t i = 0; i < length_; i++)
{
out[i] = b/val_[i];
}
return out;
}
template <typename ArrValType>
array<ArrValType>& array<ArrValType>::operator+= (const ArrValType &b)
{
for (size_t i = 0; i < length_; i++)
{
val_[i] += b;
}
return *this;
}
template <typename ArrValType>
array<ArrValType>& array<ArrValType>::operator-= (const ArrValType &b)
{
for (size_t i = 0; i < length_; i++)
{
val_[i] -= b;
}
return *this;
}
template <typename ArrValType>
array<ArrValType>& array<ArrValType>::operator*= (const ArrValType &b)
{
for (size_t i = 0; i < length_; i++)
{
val_[i] *= b;
}
return *this;
}
template <typename ArrValType>
array<ArrValType>& array<ArrValType>::operator/= (const ArrValType &b)
{
for (size_t i = 0; i < length_; i++)
{
val_[i] /= b;
}
return *this;
}
template <typename ArrValType>
bool array<ArrValType>::operator== (const array<ArrValType> &b) const
{
if (length_ != b.size()) return false;
for (size_t i = 0; i < length_; i++)
{
if (val_[i] != b[i]) return false;
}
return true;
}
template <typename ArrValType>
bool array<ArrValType>::operator!= (const array<ArrValType> &b) const
{
return !(*this == b);
}
template <typename ArrValType>
bool array<ArrValType>::equals(const array<ArrValType> &b, ArrValType eps) const
{
if (length_ != b.size()) return false;
if (eps == ArrValType{})
{
for (size_t i = 0; i < length_; i++)
{
if (val_[i] != b[i]) return false;
}
}
else
{
for (size_t i = 0; i < length_; i++)
{
if (std::abs(val_[i] - b[i]) > eps) return false;
}
}
return true;
}
template <typename ArrValType>
array<ArrValType>::~array()
{
@ -999,7 +1272,7 @@ namespace gctl
}
template <typename ArrValType>
void array<ArrValType>::assign_all(ArrValType in_val)
void array<ArrValType>::assign(ArrValType in_val)
{
for (size_t i = 0; i < length_; i++)
{
@ -1025,7 +1298,7 @@ namespace gctl
}
template <typename ArrValType>
void array<ArrValType>::append_array(const array<ArrValType> &b)
void array<ArrValType>::concat(const array<ArrValType> &b)
{
ArrValType *t = new ArrValType [length_ + b.size()];
@ -1047,7 +1320,7 @@ namespace gctl
}
template <typename ArrValType>
void array<ArrValType>::extract_array(array<ArrValType> &b, size_t st) const
void array<ArrValType>::slice(array<ArrValType> &b, size_t st) const
{
if (b.size() + st <= length_)
{
@ -1069,7 +1342,7 @@ namespace gctl
}
template <typename ArrValType>
void array<ArrValType>::extract_array(array<ArrValType> &b, const array<size_t> &ids) const
void array<ArrValType>::slice(array<ArrValType> &b, const array<size_t> &ids) const
{
b.resize(ids.size());
for (size_t i = 0; i < ids.size(); i++)
@ -1080,7 +1353,7 @@ namespace gctl
}
template <typename ArrValType>
void array<ArrValType>::insert_array(array<ArrValType> &b, size_t st)
void array<ArrValType>::insert(array<ArrValType> &b, size_t st)
{
if (b.size() + st <= length_)
{
@ -1104,10 +1377,8 @@ namespace gctl
template <typename ArrValType>
ArrValType *array<ArrValType>::get(size_t index) const
{
#ifdef GCTL_CHECK_BOUNDER
if (index >= length_)
throw std::out_of_range("[gctl::array<T>::at] Invalid index.");
#endif // GCTL_CHECK_BOUNDER
return &val_[index];
}
@ -1115,10 +1386,8 @@ namespace gctl
template <typename ArrValType>
ArrValType &array<ArrValType>::at(size_t index)
{
#ifdef GCTL_CHECK_BOUNDER
if (index >= length_)
throw std::out_of_range("[gctl::array<T>::at] Invalid index.");
#endif // GCTL_CHECK_BOUNDER
return val_[index];
}
@ -1126,22 +1395,20 @@ namespace gctl
template <typename ArrValType>
ArrValType &array<ArrValType>::at(size_t index) const
{
#ifdef GCTL_CHECK_BOUNDER
if (index >= length_)
throw std::out_of_range("[gctl::array<T>::at] Invalid index.");
#endif // GCTL_CHECK_BOUNDER
return val_[index];
}
template <typename ArrValType>
ArrValType &array<ArrValType>::operator[](size_t index)
ArrValType &array<ArrValType>::operator[](size_t index) noexcept
{
return val_[index];
}
template <typename ArrValType>
ArrValType &array<ArrValType>::operator[](size_t index) const
ArrValType &array<ArrValType>::operator[](size_t index) const noexcept
{
return val_[index];
}
@ -1171,20 +1438,20 @@ namespace gctl
}
template <typename ArrValType>
bool array<ArrValType>::empty() const
bool array<ArrValType>::empty() const noexcept
{
if (length_ == 0) return true;
else return false;
}
template <typename ArrValType>
size_t array<ArrValType>::size() const
size_t array<ArrValType>::size() const noexcept
{
return length_;
}
template <typename ArrValType>
void array<ArrValType>::export_vector(std::vector<ArrValType> &b) const
void array<ArrValType>::output(std::vector<ArrValType> &b) const
{
if (!b.empty())
{
@ -1200,7 +1467,7 @@ namespace gctl
}
template <typename ArrValType>
void array<ArrValType>::import_vector(const std::vector<ArrValType> &b)
void array<ArrValType>::input(const std::vector<ArrValType> &b)
{
resize(b.size());
for (size_t i = 0; i < length_; i++)
@ -1212,7 +1479,7 @@ namespace gctl
#ifdef GCTL_EIGEN
template <typename ArrValType>
void array<ArrValType>::export_eigen_vector(Eigen::VectorXd &b) const
void array<ArrValType>::output(Eigen::VectorXd &b) const
{
b.resize(length_);
for (size_t i = 0; i < length_; i++)
@ -1224,7 +1491,7 @@ namespace gctl
template <typename ArrValType>
void array<ArrValType>::import_eigen_vector(const Eigen::VectorXd &b)
void array<ArrValType>::input(const Eigen::VectorXd &b)
{
resize(b.size());
for (size_t i = 0; i < length_; i++)
@ -1235,6 +1502,7 @@ namespace gctl
}
#endif // GCTL_EIGEN
/*
template <typename ArrValType>
void array<ArrValType>::for_each(foreach_a_ptr func)
{
@ -1244,6 +1512,7 @@ namespace gctl
}
return;
}
*/
template <typename ArrValType>
void array<ArrValType>::show(std::ostream &os, char sep)
@ -1433,12 +1702,29 @@ namespace gctl
throw std::runtime_error("Incompatible array sizes. gctl::array<T>::dot(...)");
}
ArrValType sum = 0;
ArrValType s = 0;
for (size_t i = 0; i < length_; i++)
{
sum += val_[i]*a[i];
s += val_[i]*a[i];
}
return sum;
return s;
}
template <typename ArrValType>
ArrValType array<ArrValType>::sum() const
{
static_assert(std::is_arithmetic<ArrValType>::value,
"gctl::array<T>::sum(...) could only be used with an arithmetic type.");
if (length_ == 0) return ArrValType{};
ArrValType s = val_[0];
#pragma omp parallel for reduction(+:s)
for (size_t i = 1; i < length_; i++)
{
s += val_[i];
}
return s;
}
template <typename ArrValType>
@ -1447,14 +1733,8 @@ namespace gctl
static_assert(std::is_arithmetic<ArrValType>::value,
"gctl::array<T>::mean(...) could only be used with an arithmetic type.");
if (length_ == 0) return 0;
ArrValType m = 0;
for (size_t i = 0; i < length_; i++)
{
m = m + val_[i];
}
return m/length_;
if (length_ == 0) return ArrValType{};
return sum()/static_cast<ArrValType>(length_);
}
template <typename ArrValType>
@ -1463,14 +1743,14 @@ namespace gctl
static_assert(std::is_arithmetic<ArrValType>::value,
"gctl::array<T>::std(...) could only be used with an arithmetic type.");
if (length_ == 0 || length_ == 1) return 0;
if (length_ == 0 || length_ == 1) return ArrValType{};
ArrValType m = mean(), s = 0;
for (size_t i = 0; i < length_; i++)
{
s += (val_[i] - m)*(val_[i] - m);
}
return sqrt(s/length_);
return sqrt(s/static_cast<ArrValType>(length_));
}
template <typename ArrValType>
@ -1479,14 +1759,14 @@ namespace gctl
static_assert(std::is_arithmetic<ArrValType>::value,
"gctl::array<T>::rms(...) could only be used with an arithmetic type.");
if (length_ == 0) return 0;
if (length_ == 0) return ArrValType{};
ArrValType m = 0;
for (size_t i = 0; i < length_; i++)
{
m += val_[i]*val_[i];
}
return sqrt(m/length_);
return sqrt(m/static_cast<ArrValType>(length_));
}
template <typename ArrValType>
@ -1495,7 +1775,7 @@ namespace gctl
static_assert(std::is_arithmetic<ArrValType>::value,
"gctl::array<T>::max(...) could only be used with an arithmetic type.");
if (length_ == 0) return 0;
if (length_ == 0) return ArrValType{};
ArrValType m = val_[0];
for (size_t i = 1; i < length_; i++)
@ -1511,7 +1791,7 @@ namespace gctl
static_assert(std::is_arithmetic<ArrValType>::value,
"gctl::array<T>::min(...) could only be used with an arithmetic type.");
if (length_ == 0) return 0;
if (length_ == 0) return ArrValType{};
ArrValType m = val_[0];
for (size_t i = 1; i < length_; i++)
@ -1639,7 +1919,7 @@ namespace gctl
static_assert(std::is_arithmetic<ArrValType>::value,
"gctl::array<T>::variance(...) could only be used with an arithmetic type.");
if (length_ == 0) return 0;
if (length_ == 0) return ArrValType{};
ArrValType mn = mean();
ArrValType d = 0;
@ -1647,7 +1927,7 @@ namespace gctl
{
d = d + (val_[i] - mn)*(val_[i] - mn);
}
return d/length_;
return d/static_cast<ArrValType>(length_);
}
template <typename ArrValType>
@ -1710,6 +1990,53 @@ namespace gctl
}
return;
}
template <typename ArrValType>
bool array<ArrValType>::any_of(const ArrValType& value) const
{
bool result = false;
#pragma omp parallel for reduction(|:result)
for (size_t i = 0; i < length_; i++)
{
result = result || (val_[i] == value);
}
return result;
}
template <typename ArrValType>
bool array<ArrValType>::all_of(const ArrValType& value) const
{
bool result = true;
#pragma omp parallel for reduction(&:result)
for (size_t i = 0; i < length_; i++)
{
result = result && (val_[i] == value);
}
return result;
}
template <typename ArrValType>
size_t array<ArrValType>::count(const ArrValType& value) const
{
size_t result = 0;
#pragma omp parallel for reduction(+:result)
for (size_t i = 0; i < length_; i++)
{
if (val_[i] == value) ++result;
}
return result;
}
template <typename ArrValType>
void array<ArrValType>::memory_usage(std::ostream &os) const noexcept
{
size_t byte = sizeof(*this) + length_ * sizeof(ArrValType);
if (byte/1073741824 > 0) os << byte/1073741824 << " GB\n";
else if (byte/1048576 > 0) os << byte/1048576 << " MB\n";
else if (byte/1024 > 0) os << byte/1024 << " KB\n";
else os << byte << " B\n";
return;
}
}
#endif // _GCTL_ARRAY_H