From cdc982dd54a482ae7f6ac24da48897c43e07b1fd Mon Sep 17 00:00:00 2001 From: Yi Zhang Date: Wed, 8 Jan 2025 10:40:46 +0800 Subject: [PATCH 1/6] tmp update --- lib/geometry/point3s.h | 45 +++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/lib/geometry/point3s.h b/lib/geometry/point3s.h index 9e4e5c8..9b60050 100644 --- a/lib/geometry/point3s.h +++ b/lib/geometry/point3s.h @@ -471,32 +471,45 @@ namespace gctl * @param[in] dlon lon间隔 * @param[in] dlat lat间隔 * @param[in] rad 半径值 + * @param[in] cor 网格起始位置 */ template - void get_grid_point3s(array> &out_ps, T lonmin, T lonmax, T latmin, - T latmax, T dlon, T dlat, T rad) + void grid_points_2d(array> &out_ps, T lonmin, T lonmax, T latmin, + T latmax, T dlon, T dlat, T rad, matrix_corner_e cor = BtmLeft) { - if (lonmin >= lonmax || latmin >= latmax || lonmin+dlon>lonmax || latmin+dlat>latmax) + if (lonmin >= lonmax || latmin >= latmax || + lonmin + dlon > lonmax || latmin + dlat > latmax || + dlon <= 0 || dlat <= 0) { - throw invalid_argument("Invalid range parameters. From get_grid_point3s(...)"); + throw invalid_argument("[gctl::grid_points_2d] Invalid parameters."); } - if (dlon <= 0 || dlat <= 0) - { - throw invalid_argument("Invalid interval parameters. From get_grid_point3s(...)"); - } - - int lonnum = floor((lonmax-lonmin)/dlon) + 1; - int latnum = floor((latmax-latmin)/dlat) + 1; + int lonnum = round((lonmax-lonmin)/dlon) + 1; + int latnum = round((latmax-latmin)/dlat) + 1; out_ps.resize(lonnum*latnum); - for (int j = 0; j < latnum; j++) + if (cor == BtmLeft) { - for (int i = 0; i < lonnum; i++) + for (int j = 0; j < latnum; j++) { - out_ps.at(j*lonnum+i).lon = lonmin + dlon*i; - out_ps.at(j*lonnum+i).lat = latmin + dlat*j; - out_ps.at(j*lonnum+i).rad = rad; + for (int i = 0; i < lonnum; i++) + { + out_ps.at(j*lonnum+i).lon = lonmin + dlon*i; + out_ps.at(j*lonnum+i).lat = latmin + dlat*j; + out_ps.at(j*lonnum+i).rad = rad; + } + } + } + else // cor == TopLeft + { + for (int j = 0; j < latnum; j++) + { + for (int i = 0; i < lonnum; i++) + { + out_ps.at(j*lonnum+i).lon = lonmin + dlon*i; + out_ps.at(j*lonnum+i).lat = latmax - dlat*j; + out_ps.at(j*lonnum+i).rad = rad; + } } } return; From 6b6066b1624e761b7351999e28fad79fd3a37cc8 Mon Sep 17 00:00:00 2001 From: Yi Zhang Date: Wed, 8 Jan 2025 18:45:26 +0800 Subject: [PATCH 2/6] tmp --- lib/io/dsv_io.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/io/dsv_io.cpp b/lib/io/dsv_io.cpp index 1115f62..a8a7701 100644 --- a/lib/io/dsv_io.cpp +++ b/lib/io/dsv_io.cpp @@ -980,7 +980,7 @@ void gctl::geodsv_io::fill_column_point3dc(const array &data, int xid, std::stringstream ss; std::string s; - for (size_t i = 1; i < std::min(row_num_, (int) data.size()); i++) + for (size_t i = 1; i <= std::min(row_num_, (int) data.size()); i++) { ss.clear(); ss << data[i - 1].x; @@ -1016,7 +1016,7 @@ void gctl::geodsv_io::fill_column_point3ds(const array &data, int rid, std::stringstream ss; std::string s; - for (size_t i = 1; i < std::min(row_num_, (int) data.size()); i++) + for (size_t i = 1; i <= std::min(row_num_, (int) data.size()); i++) { ss.clear(); ss << data[i - 1].rad; From 57ead40a0adc2028f32f66440ac0b111f544fc00 Mon Sep 17 00:00:00 2001 From: Yi Zhang Date: Wed, 8 Jan 2025 23:40:02 +0800 Subject: [PATCH 3/6] update dsv_io --- lib/io/dsv_io.cpp | 45 ++++++++------------------------------------- 1 file changed, 8 insertions(+), 37 deletions(-) diff --git a/lib/io/dsv_io.cpp b/lib/io/dsv_io.cpp index a8a7701..44c1403 100644 --- a/lib/io/dsv_io.cpp +++ b/lib/io/dsv_io.cpp @@ -951,15 +951,8 @@ void gctl::geodsv_io::fill_column_point2dc(const array &data, int xid, std::string s; for (size_t i = 1; i <= std::min(row_num_, (int) data.size()); i++) { - ss.clear(); - ss << data[i - 1].x; - ss >> s; - table_[i][xid].str_ = s; - - ss.clear(); - ss << data[i - 1].y; - ss >> s; - table_[i][yid].str_ = s; + table_[i][xid].value(data[i - 1].x, p); + table_[i][yid].value(data[i - 1].y, p); } return; } @@ -982,20 +975,9 @@ void gctl::geodsv_io::fill_column_point3dc(const array &data, int xid, std::string s; for (size_t i = 1; i <= std::min(row_num_, (int) data.size()); i++) { - ss.clear(); - ss << data[i - 1].x; - ss >> s; - table_[i][xid].str_ = s; - - ss.clear(); - ss << data[i - 1].y; - ss >> s; - table_[i][yid].str_ = s; - - ss.clear(); - ss << data[i - 1].z; - ss >> s; - table_[i][zid].str_ = s; + table_[i][xid].value(data[i - 1].x, p); + table_[i][yid].value(data[i - 1].y, p); + table_[i][zid].value(data[i - 1].y, p); } return; } @@ -1018,20 +1000,9 @@ void gctl::geodsv_io::fill_column_point3ds(const array &data, int rid, std::string s; for (size_t i = 1; i <= std::min(row_num_, (int) data.size()); i++) { - ss.clear(); - ss << data[i - 1].rad; - ss >> s; - table_[i][rid].str_ = s; - - ss.clear(); - ss << data[i - 1].lon; - ss >> s; - table_[i][pid].str_ = s; - - ss.clear(); - ss << data[i - 1].lat; - ss >> s; - table_[i][tid].str_ = s; + table_[i][rid].value(data[i - 1].rad, p); + table_[i][pid].value(data[i - 1].lon, p); + table_[i][tid].value(data[i - 1].lat, p); } return; } From 00aadbd1ea3cf6995837dab8592c564c170da570 Mon Sep 17 00:00:00 2001 From: Yi Zhang Date: Thu, 9 Jan 2025 00:08:21 +0800 Subject: [PATCH 4/6] bug fixed --- lib/io/dsv_io.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/io/dsv_io.cpp b/lib/io/dsv_io.cpp index 44c1403..f587243 100644 --- a/lib/io/dsv_io.cpp +++ b/lib/io/dsv_io.cpp @@ -977,7 +977,7 @@ void gctl::geodsv_io::fill_column_point3dc(const array &data, int xid, { table_[i][xid].value(data[i - 1].x, p); table_[i][yid].value(data[i - 1].y, p); - table_[i][zid].value(data[i - 1].y, p); + table_[i][zid].value(data[i - 1].z, p); } return; } From a0f0ceca5202dc91dc65be4c10fea70d821a8a41 Mon Sep 17 00:00:00 2001 From: Yi Zhang Date: Thu, 9 Jan 2025 11:21:42 +0800 Subject: [PATCH 5/6] update --- example/CMakeLists.txt | 2 +- example/array_ex.cpp | 6 +- lib/algorithm/fir_filter.cpp | 2 +- lib/core/array.h | 439 ++++++++++++++++++++++++++++++----- 4 files changed, 390 insertions(+), 59 deletions(-) diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index a32fda5..6b39b6d 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -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) \ No newline at end of file diff --git a/example/array_ex.cpp b/example/array_ex.cpp index 34c84f6..08f39d9 100644 --- a/example/array_ex.cpp +++ b/example/array_ex.cpp @@ -34,11 +34,15 @@ int main(int argc, char const *argv[]) try { // create a new array and give initial values array 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); diff --git a/lib/algorithm/fir_filter.cpp b/lib/algorithm/fir_filter.cpp index 295c9d4..7bf9142 100644 --- a/lib/algorithm/fir_filter.cpp +++ b/lib/algorithm/fir_filter.cpp @@ -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_); diff --git a/lib/core/array.h b/lib/core/array.h index 5c0a5ad..bdfe5bb 100644 --- a/lib/core/array.h +++ b/lib/core/array.h @@ -245,6 +245,103 @@ namespace gctl */ array& operator/= (const array &b); + /** + * @brief Overloaded summation operator for the array template. + * + * @param a Input array + * @param b Scale value + * @return Target array + */ + array operator+ (const ArrValType &b); + + /** + * @brief Overloaded minus operator for the array template. + * + * @param a Input array + * @param b Scale value + * @return Target array + */ + array operator- (const ArrValType &b); + + /** + * @brief Overloaded multiple operator for the array template. + * + * @param a Input array + * @param b Scale value + * @return Target array + */ + array operator* (const ArrValType &b); + + /** + * @brief Overloaded division operator for the array template. + * + * @param a Input array + * @param b Scale value + * @return Target array + */ + array operator/ (const ArrValType &b); + + /** + * @brief Overloaded summation operator for the array template. + * + * @param a Input array + * @param b Scale value + * @return Target array + * + */ + array& operator+= (const ArrValType &b); + + /** + * @brief Overloaded summation operator for the array template. + * + * @param a Input array + * @param b Scale value + * @return Target array + */ + array& operator-= (const ArrValType &b); + + /** + * @brief Overloaded multiple operator for the array template. + * + * @param a Input array + * @param b Scale value + * @return Target array + */ + array& operator*= (const ArrValType &b); + + /** + * @brief Overloaded division operator for the array template. + * + * @param a Input array + * @param b Scale value + * @return Target array + */ + array& operator/= (const ArrValType &b); + + /** + * @brief Compare two arraies. + * + * @return True if the two arraies are equal, false otherwise. + */ + bool operator== (const array &b) const; + + /** + * @brief Compare two arraies. + * + * @return True if the two arraies are not equal, false otherwise. + */ + bool operator!= (const array &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 &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 &b); + void concat(const array &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 &b, size_t st = 0) const; + void slice(array &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 &b, const array &ids) const; + void slice(array &b, const array &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 &b, size_t st = 0); + void insert(array &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 @@ -434,6 +531,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. * @@ -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 &b) const; + void output(std::vector &b) const; /** * @brief Copy the array from a vector. * * @param b Target vector. */ - void import_vector(const std::vector &b); + void input(const std::vector &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 + 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 &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 如果存在等于指定值的元素返回true,否则返回false + */ + bool any_of(const ArrValType& value) const; + + /** + * @brief 检查是否所有元素都等于指定值 + * + * @param value 要比较的值 + * @return 如果所有元素都等于指定值返回true,否则返回false + */ + 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 @@ -898,6 +1047,130 @@ namespace gctl return *this; } + template + array array::operator+ (const ArrValType &b) + { + array out(length_); + for (size_t i = 0; i < length_; i++) + { + out[i] = val_[i] + b; + } + return out; + } + + template + array array::operator- (const ArrValType &b) + { + array out(length_); + for (size_t i = 0; i < length_; i++) + { + out[i] = b - val_[i]; + } + return out; + } + + template + array array::operator* (const ArrValType &b) + { + array out(length_); + for (size_t i = 0; i < length_; i++) + { + out[i] = b*val_[i]; + } + return out; + } + + template + array array::operator/ (const ArrValType &b) + { + array out(length_); + for (size_t i = 0; i < length_; i++) + { + out[i] = b/val_[i]; + } + return out; + } + + template + array& array::operator+= (const ArrValType &b) + { + for (size_t i = 0; i < length_; i++) + { + val_[i] += b; + } + return *this; + } + + template + array& array::operator-= (const ArrValType &b) + { + for (size_t i = 0; i < length_; i++) + { + val_[i] -= b; + } + return *this; + } + + template + array& array::operator*= (const ArrValType &b) + { + for (size_t i = 0; i < length_; i++) + { + val_[i] *= b; + } + return *this; + } + + template + array& array::operator/= (const ArrValType &b) + { + for (size_t i = 0; i < length_; i++) + { + val_[i] /= b; + } + return *this; + } + + template + bool array::operator== (const array &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 + bool array::operator!= (const array &b) const + { + return !(*this == b); + } + + template + bool array::equals(const array &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 array::~array() { @@ -999,7 +1272,7 @@ namespace gctl } template - void array::assign_all(ArrValType in_val) + void array::assign(ArrValType in_val) { for (size_t i = 0; i < length_; i++) { @@ -1025,7 +1298,7 @@ namespace gctl } template - void array::append_array(const array &b) + void array::concat(const array &b) { ArrValType *t = new ArrValType [length_ + b.size()]; @@ -1047,7 +1320,7 @@ namespace gctl } template - void array::extract_array(array &b, size_t st) const + void array::slice(array &b, size_t st) const { if (b.size() + st <= length_) { @@ -1069,7 +1342,7 @@ namespace gctl } template - void array::extract_array(array &b, const array &ids) const + void array::slice(array &b, const array &ids) const { b.resize(ids.size()); for (size_t i = 0; i < ids.size(); i++) @@ -1080,7 +1353,7 @@ namespace gctl } template - void array::insert_array(array &b, size_t st) + void array::insert(array &b, size_t st) { if (b.size() + st <= length_) { @@ -1104,10 +1377,8 @@ namespace gctl template ArrValType *array::get(size_t index) const { -#ifdef GCTL_CHECK_BOUNDER if (index >= length_) throw std::out_of_range("[gctl::array::at] Invalid index."); -#endif // GCTL_CHECK_BOUNDER return &val_[index]; } @@ -1115,10 +1386,8 @@ namespace gctl template ArrValType &array::at(size_t index) { -#ifdef GCTL_CHECK_BOUNDER if (index >= length_) throw std::out_of_range("[gctl::array::at] Invalid index."); -#endif // GCTL_CHECK_BOUNDER return val_[index]; } @@ -1126,22 +1395,20 @@ namespace gctl template ArrValType &array::at(size_t index) const { -#ifdef GCTL_CHECK_BOUNDER if (index >= length_) throw std::out_of_range("[gctl::array::at] Invalid index."); -#endif // GCTL_CHECK_BOUNDER return val_[index]; } template - ArrValType &array::operator[](size_t index) + ArrValType &array::operator[](size_t index) noexcept { return val_[index]; } template - ArrValType &array::operator[](size_t index) const + ArrValType &array::operator[](size_t index) const noexcept { return val_[index]; } @@ -1171,20 +1438,20 @@ namespace gctl } template - bool array::empty() const + bool array::empty() const noexcept { if (length_ == 0) return true; else return false; } template - size_t array::size() const + size_t array::size() const noexcept { return length_; } template - void array::export_vector(std::vector &b) const + void array::output(std::vector &b) const { if (!b.empty()) { @@ -1200,7 +1467,7 @@ namespace gctl } template - void array::import_vector(const std::vector &b) + void array::input(const std::vector &b) { resize(b.size()); for (size_t i = 0; i < length_; i++) @@ -1212,7 +1479,7 @@ namespace gctl #ifdef GCTL_EIGEN template - void array::export_eigen_vector(Eigen::VectorXd &b) const + void array::output(Eigen::VectorXd &b) const { b.resize(length_); for (size_t i = 0; i < length_; i++) @@ -1224,7 +1491,7 @@ namespace gctl template - void array::import_eigen_vector(const Eigen::VectorXd &b) + void array::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 void array::for_each(foreach_a_ptr func) { @@ -1244,6 +1512,7 @@ namespace gctl } return; } +*/ template void array::show(std::ostream &os, char sep) @@ -1433,12 +1702,29 @@ namespace gctl throw std::runtime_error("Incompatible array sizes. gctl::array::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 + ArrValType array::sum() const + { + static_assert(std::is_arithmetic::value, + "gctl::array::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 @@ -1447,14 +1733,8 @@ namespace gctl static_assert(std::is_arithmetic::value, "gctl::array::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(length_); } template @@ -1463,14 +1743,14 @@ namespace gctl static_assert(std::is_arithmetic::value, "gctl::array::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(length_)); } template @@ -1479,14 +1759,14 @@ namespace gctl static_assert(std::is_arithmetic::value, "gctl::array::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(length_)); } template @@ -1495,7 +1775,7 @@ namespace gctl static_assert(std::is_arithmetic::value, "gctl::array::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::value, "gctl::array::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::value, "gctl::array::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(length_); } template @@ -1710,6 +1990,53 @@ namespace gctl } return; } + + template + bool array::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 + bool array::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 + size_t array::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 + void array::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 \ No newline at end of file From a6b57b66396d8acdf0f13c405d0fbf895cfb8d0b Mon Sep 17 00:00:00 2001 From: Yi Zhang Date: Thu, 9 Jan 2025 11:25:25 +0800 Subject: [PATCH 6/6] tmp --- lib/core/array.h | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/lib/core/array.h b/lib/core/array.h index bdfe5bb..9d89361 100644 --- a/lib/core/array.h +++ b/lib/core/array.h @@ -910,12 +910,10 @@ namespace gctl template array array::operator+ (const array &b) { -#ifdef GCTL_CHECK_SIZE if (b.size() != length_) { throw std::runtime_error("[gctl::array::operator+] Incompatible array sizes."); } -#endif // GCTL_CHECK_SIZE array out(length_); for (size_t i = 0; i < length_; i++) @@ -928,12 +926,10 @@ namespace gctl template array array::operator- (const array &b) { -#ifdef GCTL_CHECK_SIZE if (b.size() != length_) { throw std::runtime_error("[gctl::array::operator-] Incompatible array sizes."); } -#endif // GCTL_CHECK_SIZE array out(length_); for (size_t i = 0; i < length_; i++) @@ -946,12 +942,10 @@ namespace gctl template array array::operator* (const array &b) { -#ifdef GCTL_CHECK_SIZE if (b.size() != length_) { throw std::runtime_error("[gctl::array::operator*] Incompatible array sizes."); } -#endif // GCTL_CHECK_SIZE array out(length_); for (size_t i = 0; i < length_; i++) @@ -964,12 +958,10 @@ namespace gctl template array array::operator/ (const array &b) { -#ifdef GCTL_CHECK_SIZE if (b.size() != length_) { throw std::runtime_error("[gctl::array::operator/] Incompatible array sizes."); } -#endif // GCTL_CHECK_SIZE array out(length_); for (size_t i = 0; i < length_; i++) @@ -982,12 +974,10 @@ namespace gctl template array& array::operator+= (const array &b) { -#ifdef GCTL_CHECK_SIZE if (b.size() != length_) { throw std::runtime_error("[gctl::array::operator+=] Incompatible array sizes."); } -#endif // GCTL_CHECK_SIZE for (size_t i = 0; i < length_; i++) { @@ -999,12 +989,10 @@ namespace gctl template array& array::operator-= (const array &b) { -#ifdef GCTL_CHECK_SIZE if (b.size() != length_) { throw std::runtime_error("[gctl::array::operator-=] Incompatible array sizes."); } -#endif // GCTL_CHECK_SIZE for (size_t i = 0; i < length_; i++) { @@ -1016,12 +1004,10 @@ namespace gctl template array& array::operator*= (const array &b) { -#ifdef GCTL_CHECK_SIZE if (b.size() != length_) { throw std::runtime_error("[gctl::array::operator*=] Incompatible array sizes."); } -#endif // GCTL_CHECK_SIZE for (size_t i = 0; i < length_; i++) { @@ -1033,12 +1019,10 @@ namespace gctl template array& array::operator/= (const array &b) { -#ifdef GCTL_CHECK_SIZE if (b.size() != length_) { throw std::runtime_error("[gctl::array::operator/=] Incompatible array sizes."); } -#endif // GCTL_CHECK_SIZE for (size_t i = 0; i < length_; i++) {