/********************************************************
 *  ██████╗  ██████╗████████╗██╗
 * ██╔════╝ ██╔════╝╚══██╔══╝██║
 * ██║  ███╗██║        ██║   ██║
 * ██║   ██║██║        ██║   ██║
 * ╚██████╔╝╚██████╗   ██║   ███████╗
 *  ╚═════╝  ╚═════╝   ╚═╝   ╚══════╝
 * 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 "iostream"

int main(int argc, char const *argv[])
{
	int n_size = 5;
	gctl::matrix<double> B;
	// A=[10,1,2,0,1;
	//2,20,1,0,1;
	//1,3,30,2,0;
	//2,3,0,40,1;
	//5,6,1,0,50]
	/*
	gctl::array2d<double> A(n_size, n_size, 0.0);
	A[0][0] = 10.0; A[0][1] = 1.0; A[0][2] = 2.0; A[0][3] = 0.0; A[0][4] = 1.0;
	A[1][0] = 2.0; A[1][1] = 20.0; A[1][2] = 1.0; A[1][3] = 0.0; A[1][4] = 1.0;
	A[2][0] = 1.0; A[2][1] = 3.0; A[2][2] = 30.0; A[2][3] = 2.0; A[2][4] = 0.0;
	A[3][0] = 2.0; A[3][1] = 3.0; A[3][2] = 0.0; A[3][3] = 40.0; A[3][4] = 1.0;
	A[4][0] = 5.0; A[4][1] = 6.0; A[4][2] = 1.0; A[4][3] = 0.0; A[4][4] = 50.0;
	*/

	gctl::spmat<double> A(n_size, n_size, 0.0);
	A.insert(0, 0, 10.0);
	A.insert(1, 1, 20.0);
	A.insert(2, 2, 30.0);
	A.insert(3, 3, 40.0);
	A.insert(4, 4, 10.0);
	A.insert(0, 3, 1.0);
	A.insert(1, 2, 2.0);
	A.insert(2, 4, 5.0);
	A.insert(3, 0, 3.0);
	A.insert(4, 1, 4.0);

	std::cout << "epsilon = " << gctl::newton_inverse(A, B, 1e-12) << std::endl;

	std::cout << "A = " << std::endl;
	A.show_matrix();

	std::cout << "A^-1 = " << std::endl;
	for (int i = 0; i < n_size; i++)
	{
		for (int j = 0; j < n_size; j++)
		{
			std::cout << B[i][j] << " ";
		}
		std::cout << std::endl;
	}

	double ele;
	std::cout << "A * A^-1 = " << std::endl;

	gctl::array<double> tmp_arr(n_size);
	for (int i = 0; i < n_size; i++)
	{
		for (int j = 0; j < n_size; j++)
		{
			//ele = 0.0;
			//for (int k = 0; k < n_size; k++)
			//{
			//	ele += A[i][k] * B[k][j];
			//}
			for (int k = 0; k < n_size; k++)
			{
				tmp_arr[k] =  B[k][j];
			}
			ele = A.multiply_vector(tmp_arr, i);

			if (fabs(ele) < 1e-12)
				ele = 0.0;

			std::cout << ele << " ";
		}
		std::cout << std::endl;
	}
	return 0;
}