#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "header.h"

double dot_prod(double* v1,double* v2, double* p ,int n,int power1,int power2) {
	double ans = 0;
	for (int i = 0;i < n;i++) {
		ans += p[i]*pow(v1[i], power1) * pow(v2[i], power2);
	}
	return ans;
}

double* MLS_coef(double* xh, double* yh, double* p, int m, int n) {
	double** slae = malloc(sizeof(double*) * m);
	double* left_v = malloc(sizeof(double) * m);
	double* coefs;

	for (int i = 0;i < m;i++) {
		slae[i] = malloc(sizeof(double) * m);
		for (int j = 0;j < m;j++) {
			slae[i][j] = dot_prod(xh,xh,p, n, i, j);
		}
		left_v[i] = dot_prod(yh, xh, p,n,1, i);
	}
	
	coefs = LDR_sol(slae,left_v,m);


	free(slae);
	free(left_v);
	return coefs;

}
double MLS(double x, double* coefs, int m) {
	double ans = 0;
	for (int i = 0;i < m;i++) {
		ans += pow(x, i) * coefs[i];
	}
	return ans;
}