tmp update

This commit is contained in:
张壹 2024-09-16 09:05:12 +08:00
parent c760250dbe
commit 8c713c576a
3 changed files with 181 additions and 53 deletions

View File

@ -27,4 +27,5 @@ add_example(autodiff_ex OFF)
add_example(multinary_ex OFF)
add_example(text_io_ex OFF)
add_example(getoption_ex OFF)
add_example(process_ex ON)
add_example(process_ex OFF)
add_example(array_ex OFF)

95
example/array_ex.cpp Normal file
View File

@ -0,0 +1,95 @@
/********************************************************
*
*
*
*
*
*
* Geophysical Computational Tools & Library (GCTL)
*
* Copyright (c) 2022 Yi Zhang (yizhang-geo@zju.edu.cn)
*
* GCTL is distributed under a dual licensing scheme. You can redistribute
* it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 2
* of the License, or (at your option) any later version. You should have
* received a copy of the GNU Lesser General Public License along with this
* program. If not, see <http://www.gnu.org/licenses/>.
*
* If the terms and conditions of the LGPL v.2. would prevent you from using
* the GCTL, please consider the option to obtain a commercial license for a
* fee. These licenses are offered by the GCTL's original author. As a rule,
* licenses are provided "as-is", unlimited in time for a one time fee. Please
* send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget
* to include some description of your company and the realm of its activities.
* Also add information on how to contact you by electronic and paper mail.
******************************************************/
#include "../lib/core.h"
#include "../lib/io.h"
using namespace gctl;
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.log2linear(2);
A.show();
A.linear2log(2);
A.show();
// copy A to a new array
array<double> B = A;
B.normalize();
B.show();
array<double> S = A + B;
std::cout << "B + A = ";
S.show();
S.normalize();
S.show();
// create a new 2D array
matrix<int> C(5, 5, 1);
std::cout << "C = " << std::endl;
for (int i = 0; i < C.row_size(); i++)
{
for (int j = 0; j < C.col_size(); j++)
{
C[i][j] += i*10 + j;
std::cout << C.at(i,j) << " ";
}
std::cout << std::endl;
}
// access row elements
std::cout << "C[3][:] = " << std::endl;
for (int i = 0; i < C.col_size(); i++)
{
std::cout << C.get(3)[i] << " ";
}
std::cout << std::endl;
// save array to a binary file
save_matrix2binary("tmp/array_ex_out", C, "Int");
// import 2D array to a new object
matrix<int> D;
read_binary2matrix("tmp/array_ex_out", D);
std::cout << "D = " << std::endl;
for (int i = 0; i < D.row_size(); i++)
{
for (int j = 0; j < D.col_size(); j++)
{
std::cout << D[i][j] << " ";
}
std::cout << std::endl;
}
return 0;
}
catch(std::exception &e)
{
GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0);
}

View File

@ -146,6 +146,15 @@ namespace gctl
*/
array(std::initializer_list<ArrValType> init_val);
/**
* @brief Construct a new array object as a sequence
*
* @param s Size of the sequence.
* @param st Start value
* @param inc Invrease interval
*/
array(size_t s, ArrValType st, ArrValType inc);
/**
* @brief Overloaded assignment operator for the array template.
*
@ -289,6 +298,15 @@ namespace gctl
*/
void resize(std::initializer_list<ArrValType> init_val);
/**
* @brief Resize a new array object as a sequence
*
* @param s Size of the sequence.
* @param st Start value
* @param inc Invrease interval
*/
void resize(size_t s, ArrValType st, ArrValType inc);
/**
* @brief Clear memory space and reset variables.
*/
@ -514,30 +532,30 @@ namespace gctl
void sequence(ArrValType st_val, ArrValType inc);
/**
* @brief
* @brief Assign values as a 2D sequence.
*
* @param rs
* @param rinc
* @param cs
* @param cinc
* @param rn
* @param cn
* @param rs Start value of the row sequence
* @param rinc Increase value of the row sequence
* @param cs Start value of the column sequence
* @param cinc Increase value of the column sequence
* @param rn Row nnumber
* @param cn Column number
*/
void sequence2d(ArrValType rs, ArrValType rinc, ArrValType cs, ArrValType cinc,
size_t rn, size_t cn);
/**
* @brief
* @brief Assign values as a 3D sequence.
*
* @param ls
* @param linc
* @param rs
* @param rinc
* @param cs
* @param cinc
* @param ln
* @param rn
* @param cn
* @param ls Start value of the layer sequence
* @param linc Increase value of the layer sequence
* @param rs Start value of the row sequence
* @param rinc Increase value of the row sequence
* @param cs Start value of the column sequence
* @param cinc Increase value of the column sequence
* @param ln Layer number
* @param rn Row nnumber
* @param cn Column number
*/
void sequence3d(ArrValType ls, ArrValType linc,
ArrValType rs, ArrValType rinc,
@ -567,9 +585,9 @@ namespace gctl
ArrValType mean() const;
/**
* @brief
* @brief Return the variance value.
*
* @return ArrValType
* @return Variance value.
*/
ArrValType variance() const;
@ -581,9 +599,9 @@ namespace gctl
ArrValType std() const;
/**
* @brief
* @brief Return the unbiased standard deviation value.
*
* @return ArrValType
* @return std value.
*/
ArrValType std_unbiased() const;
@ -635,24 +653,25 @@ namespace gctl
void orth(array<ArrValType> &b);
/**
* @brief
* @brief Assign values as a power sequence.
*
* @param base
* @param start
* @param inc
* @param base Power base
*/
void logspace(const ArrValType &base, const ArrValType &start,
const ArrValType &inc);
void log2linear(const ArrValType &base);
/**
* @brief
* @brief Convert values to a log root.
*
* @param base
* @param base Log base
*/
void linear2log(ArrValType base);
/**
* @brief
* @brief Set values to a range.
*
* @param min Lower bound
* @param max Higher bound
* @param rt Bounder type
*
*/
void set2range(ArrValType min, ArrValType max, range_type_e rt = HardScale);
@ -705,6 +724,14 @@ namespace gctl
resize(init_val);
}
template <typename ArrValType>
array<ArrValType>::array(size_t s, ArrValType st, ArrValType inc)
{
length_ = 0;
val_ = nullptr;
resize(s, st, inc);
}
template <typename ArrValType>
array<ArrValType> &array<ArrValType>::operator= (const array<ArrValType> &b)
{
@ -732,7 +759,7 @@ namespace gctl
#ifdef GCTL_CHECK_SIZE
if (b.size() != length_)
{
throw std::runtime_error("Incompatible array sizes. gctl::array<T>::operator+(...)");
throw std::runtime_error("[gctl::array<T>::operator+] Incompatible array sizes.");
}
#endif // GCTL_CHECK_SIZE
@ -750,7 +777,7 @@ namespace gctl
#ifdef GCTL_CHECK_SIZE
if (b.size() != length_)
{
throw std::runtime_error("Incompatible array sizes. gctl::array<T>::operator-(...)");
throw std::runtime_error("[gctl::array<T>::operator-] Incompatible array sizes.");
}
#endif // GCTL_CHECK_SIZE
@ -768,7 +795,7 @@ namespace gctl
#ifdef GCTL_CHECK_SIZE
if (b.size() != length_)
{
throw std::runtime_error("Incompatible array sizes. gctl::array<T>::operator*(...)");
throw std::runtime_error("[gctl::array<T>::operator*] Incompatible array sizes.");
}
#endif // GCTL_CHECK_SIZE
@ -786,7 +813,7 @@ namespace gctl
#ifdef GCTL_CHECK_SIZE
if (b.size() != length_)
{
throw std::runtime_error("Incompatible array sizes. gctl::array<T>::operator/(...)");
throw std::runtime_error("[gctl::array<T>::operator/] Incompatible array sizes.");
}
#endif // GCTL_CHECK_SIZE
@ -804,7 +831,7 @@ namespace gctl
#ifdef GCTL_CHECK_SIZE
if (b.size() != length_)
{
throw std::runtime_error("Incompatible array sizes. gctl::array<T>::operator+=(...)");
throw std::runtime_error("[gctl::array<T>::operator+=] Incompatible array sizes.");
}
#endif // GCTL_CHECK_SIZE
@ -821,7 +848,7 @@ namespace gctl
#ifdef GCTL_CHECK_SIZE
if (b.size() != length_)
{
throw std::runtime_error("Incompatible array sizes. gctl::array<T>::operator-=(...)");
throw std::runtime_error("[gctl::array<T>::operator-=] Incompatible array sizes.");
}
#endif // GCTL_CHECK_SIZE
@ -838,7 +865,7 @@ namespace gctl
#ifdef GCTL_CHECK_SIZE
if (b.size() != length_)
{
throw std::runtime_error("Incompatible array sizes. gctl::array<T>::operator*=(...)");
throw std::runtime_error("[gctl::array<T>::operator*=] Incompatible array sizes.");
}
#endif // GCTL_CHECK_SIZE
@ -855,7 +882,7 @@ namespace gctl
#ifdef GCTL_CHECK_SIZE
if (b.size() != length_)
{
throw std::runtime_error("Incompatible array sizes. gctl::array<T>::operator/=(...)");
throw std::runtime_error("[gctl::array<T>::operator/=] Incompatible array sizes.");
}
#endif // GCTL_CHECK_SIZE
@ -877,7 +904,7 @@ namespace gctl
{
if (len == 0)
{
throw std::invalid_argument("Invalid array size. gctl::array<T>::resize(...)");
throw std::invalid_argument("[gctl::array<T>::resize] Invalid array size.");
}
if (length_ != len)
@ -906,7 +933,7 @@ namespace gctl
{
if (init_array == nullptr)
{
throw std::domain_error("Invalid pointer. gctl::array<T>::resize(...)");
throw std::domain_error("[gctl::array<T>::resize] Invalid pointer.");
}
resize(len);
@ -945,6 +972,14 @@ namespace gctl
return;
}
template <typename ArrValType>
void array<ArrValType>::resize(size_t s, ArrValType st, ArrValType inc)
{
resize(s);
sequence(st, inc);
return;
}
template <typename ArrValType>
void array<ArrValType>::clear()
{
@ -980,7 +1015,7 @@ namespace gctl
return;
}
throw std::runtime_error("Invalid segment range. From gctl::array<T>::assign(...)");
throw std::runtime_error("[gctl::array<T>::assign] Invalid segment range.");
return;
}
@ -1066,7 +1101,7 @@ namespace gctl
{
#ifdef GCTL_CHECK_BOUNDER
if (index >= length_)
throw std::out_of_range("Invalid index. gctl::array<T>::get(...)");
throw std::out_of_range("[gctl::array<T>::at] Invalid index.");
#endif // GCTL_CHECK_BOUNDER
return &val_[index];
@ -1077,7 +1112,7 @@ namespace gctl
{
#ifdef GCTL_CHECK_BOUNDER
if (index >= length_)
throw std::out_of_range("Invalid index. gctl::array<T>::at(...)");
throw std::out_of_range("[gctl::array<T>::at] Invalid index.");
#endif // GCTL_CHECK_BOUNDER
return val_[index];
@ -1088,7 +1123,7 @@ namespace gctl
{
#ifdef GCTL_CHECK_BOUNDER
if (index >= length_)
throw std::out_of_range("Invalid index. gctl::array<T>::at(...)");
throw std::out_of_range("[gctl::array<T>::at] Invalid index.");
#endif // GCTL_CHECK_BOUNDER
return val_[index];
@ -1561,17 +1596,14 @@ namespace gctl
}
template <typename ArrValType>
void array<ArrValType>::logspace(const ArrValType &base, const ArrValType &start,
const ArrValType &inc)
void array<ArrValType>::log2linear(const ArrValType &base)
{
static_assert(std::is_arithmetic<ArrValType>::value,
"gctl::array<T>::logspace(...) could only be used with an arithmetic type.");
if (length_ == 0) return;
for (size_t i = 0; i < length_; i++)
{
val_[i] = pow(base, start + i*inc);
val_[i] = pow(base, val_[i]);
}
return;
}
@ -1627,7 +1659,7 @@ namespace gctl
void array<ArrValType>::set2range(ArrValType min, ArrValType max, range_type_e rt)
{
static_assert(std::is_arithmetic<ArrValType>::value,
"gctl::array<T>::std_unbiased(...) could only be used with an arithmetic type.");
"gctl::array<T>::set2range(...) could only be used with an arithmetic type.");
ArrValType amin = val_[0], amax = val_[0];
for (size_t i = 1; i < length_; i++)