gctl/example/array_ex.cpp
2024-10-24 12:15:09 +08:00

84 lines
2.7 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 "../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.for_each([](double &a, size_t i){ a += 1;});
A.show();
A.sequence(1.0, 0.5, 3, 4, 1);
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();
array<point3dc> P(5);
P.sequence(point3dc(0, 0, 0), point3dc(2, 1, 0.5));
P.show(std::cout, '\n');
// create a new 2D array
matrix<int> C(5, 5, 1);
C.sequence(0, 1, 10);
std::cout << "C = \n";
C.show();
// 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 = \n";
D.show();
return 0;
}
catch(std::exception &e)
{
GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0);
}