gctl_examples/examples/array_ex.cpp
2024-09-10 20:19:20 +08:00

103 lines
3.2 KiB
C++

/********************************************************
* ██████╗ ██████╗████████╗██╗
* ██╔════╝ ██╔════╝╚══██╔══╝██║
* ██║ ███╗██║ ██║ ██║
* ██║ ██║██║ ██║ ██║
* ╚██████╔╝╚██████╗ ██║ ███████╗
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
* 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 "gctl/core.h"
#include "gctl/algorithm.h"
#include "gctl/io.h"
int main(int argc, char const *argv[])
{
try
{
// create a new array and give initial values
gctl::array<double> A;
gctl::linespace(1.1, 2.9, 10, A);
// show values
std::cout << "A = " << std::endl;
for (int i = 0; i < A.size(); i++)
{
std::cout << A[i] << std::endl;
}
// copy A to a new array
gctl::array<double> B = A;
gctl::normalize(B);
B.show();
gctl::array<double> S = A;
std::cout << "B + A = " << std::endl;
for (int i = 0; i < S.size(); i++)
{
S[i] += B[i];
}
S.show();
gctl::normalize(S);
S.show();
// create a new 2D array
gctl::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
gctl::save_matrix2binary("data/out/array_ex_out", C, "Int");
// import 2D array to a new object
gctl::matrix<int> D;
gctl::read_binary2matrix("data/out/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;
}
}
catch(std::exception &e)
{
GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0);
}
return 0;
}