#include "task_3.h"
#include <cassert>
#include <cmath>
#include <vector>
using namespace std;


double scalar(vector<double> const &x, vector<double> const &y) {
    assert(x.size() == y.size());
    size_t const N = x.size();
    double sum = 0.0;
    for (size_t i = 0; i < N; ++i) {
        sum += x[i] * y[i];
    }
    return sum;
}


vector<double> matrix_vec(vector<double> const &A, vector<double> const &x) {
    size_t const N = x.size();
    size_t const M = A.size() / N;
    vector<double> b(M);

    for (size_t i = 0; i < M; ++i) {
        for (size_t j = 0; j < N; ++j) {
            b[i] += A[i*N + j] * x[j];
        }
    }
    return b;
}


vector<double> matrix_matrix(vector<double> const &A, vector<double> const &B, size_t const &M) {
    size_t const L = A.size() / M;
    size_t const N = B.size() / L;
    vector<double> C(M*N,0);

    for (size_t i = 0; i < M; ++i) {
        for (size_t k = 0; k < L; ++k) {
            for (size_t j = 0; j < N; ++j) {
                C[i*N + j] += A[i*L + k] * B[k*N + j];
            }
        }
    }
    return C;
}


vector<double> poly(vector<double> const &x, vector<double> const &a) {
    size_t N = x.size();
    size_t p = a.size();
    vector<double> y(N);
    for (size_t i = 0; i < N; ++i) {
        y[i] = a[p];
        for (size_t k = 1; k < p; ++k) {
            y[i] = y[i]*x[i] + a[p-k];
        }
    }
    return y;
}