diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt
index cbbb0e6..20af7b7 100644
--- a/example/CMakeLists.txt
+++ b/example/CMakeLists.txt
@@ -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)
\ No newline at end of file
+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
new file mode 100644
index 0000000..81fcda6
--- /dev/null
+++ b/example/array_ex.cpp
@@ -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 .
+ *
+ * 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 A(10, 0.0, 1.0);
+ A.log2linear(2);
+ A.show();
+ A.linear2log(2);
+ A.show();
+
+ // copy A to a new array
+ array B = A;
+ B.normalize();
+ B.show();
+
+ array S = A + B;
+ std::cout << "B + A = ";
+ S.show();
+
+ S.normalize();
+ S.show();
+
+ // create a new 2D array
+ matrix 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 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);
+}
\ No newline at end of file
diff --git a/lib/core/array.h b/lib/core/array.h
index 2ba4efa..eabe025 100644
--- a/lib/core/array.h
+++ b/lib/core/array.h
@@ -138,7 +138,7 @@ namespace gctl
* @param[in] b Original array.
*/
array(const array &b);
-
+
/**
* @brief Construct a new array object from std::initializer
*
@@ -146,6 +146,15 @@ namespace gctl
*/
array(std::initializer_list 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 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 &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
+ array::array(size_t s, ArrValType st, ArrValType inc)
+ {
+ length_ = 0;
+ val_ = nullptr;
+ resize(s, st, inc);
+ }
+
template
array &array::operator= (const array &b)
{
@@ -732,10 +759,10 @@ namespace gctl
#ifdef GCTL_CHECK_SIZE
if (b.size() != length_)
{
- throw std::runtime_error("Incompatible array sizes. gctl::array::operator+(...)");
+ 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++)
{
@@ -750,7 +777,7 @@ namespace gctl
#ifdef GCTL_CHECK_SIZE
if (b.size() != length_)
{
- throw std::runtime_error("Incompatible array sizes. gctl::array::operator-(...)");
+ throw std::runtime_error("[gctl::array::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::operator*(...)");
+ throw std::runtime_error("[gctl::array::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::operator/(...)");
+ throw std::runtime_error("[gctl::array::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::operator+=(...)");
+ throw std::runtime_error("[gctl::array::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::operator-=(...)");
+ throw std::runtime_error("[gctl::array::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::operator*=(...)");
+ throw std::runtime_error("[gctl::array::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::operator/=(...)");
+ throw std::runtime_error("[gctl::array::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::resize(...)");
+ throw std::invalid_argument("[gctl::array::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::resize(...)");
+ throw std::domain_error("[gctl::array::resize] Invalid pointer.");
}
resize(len);
@@ -945,6 +972,14 @@ namespace gctl
return;
}
+ template
+ void array::resize(size_t s, ArrValType st, ArrValType inc)
+ {
+ resize(s);
+ sequence(st, inc);
+ return;
+ }
+
template
void array::clear()
{
@@ -980,7 +1015,7 @@ namespace gctl
return;
}
- throw std::runtime_error("Invalid segment range. From gctl::array::assign(...)");
+ throw std::runtime_error("[gctl::array::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::get(...)");
+ throw std::out_of_range("[gctl::array::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::at(...)");
+ throw std::out_of_range("[gctl::array::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::at(...)");
+ throw std::out_of_range("[gctl::array::at] Invalid index.");
#endif // GCTL_CHECK_BOUNDER
return val_[index];
@@ -1561,17 +1596,14 @@ namespace gctl
}
template
- void array::logspace(const ArrValType &base, const ArrValType &start,
- const ArrValType &inc)
+ void array::log2linear(const ArrValType &base)
{
static_assert(std::is_arithmetic::value,
"gctl::array::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::set2range(ArrValType min, ArrValType max, range_type_e rt)
{
static_assert(std::is_arithmetic::value,
- "gctl::array::std_unbiased(...) could only be used with an arithmetic type.");
+ "gctl::array::set2range(...) could only be used with an arithmetic type.");
ArrValType amin = val_[0], amax = val_[0];
for (size_t i = 1; i < length_; i++)