//
// Created by MariaRemark on 05.09.2021.
//

#include "math.h"
#include "../libraries/linalg.h"


/*
 * Working with double vectors in C
*/
void add_vectors(double* vec1, double* vec2, double* vec_result, size_t length) {
    for (size_t index = 0; index < length; index++) {
        vec_result[index] = vec1[index] + vec2[index];
    }
}

void diff_vectors(double* vec1, double* vec2, double* vec_result, size_t length) {
    for (size_t index = 0; index < length; index++) {
        vec_result[index] = vec1[index] - vec2[index];
    }
}

void vec_scalar_prod(double* vec1, double scalar, double* vec_result, size_t length) {
    for (size_t index = 0; index < length; index++) {
        vec_result[index] = vec1[index] * scalar;
    }
}

double vector_range(double* vector, size_t length) {
    double result = 0;
    for (size_t index = 0; index < length; index++) {
        result += vector[index] * vector[index];
    }
    return sqrt(result);
}

double vec_smul(double* vec1, double* vec2, size_t length) {
    double result = 0;
    for (int index = 0; index < length; index++) {
        result += vec1[index] * vec2[index];
    }
    return result;
}

void vec_prod(double vec1[3], double vec2[3], double vec_result[3]) {
    vec_result[0] = vec1[1] * vec2[2] - vec1[2] * vec2[1];
    vec_result[1] = vec1[2] * vec2[0] - vec1[0] * vec2[2];
    vec_result[2] = vec1[0] * vec2[1] - vec1[1] * vec2[0];
}

/*
 * Working with matrices in C
*/

void matmul(double** matrix1, double** matrix2, double** matrix_result,
            size_t nrows1, size_t ncols1, size_t nrows2, size_t ncols2) {
    double inner_sum;
    for (size_t row = 0; row < nrows1; row++) {
        for (size_t col = 0; col < ncols2; col++) {
            inner_sum = 0;
            for (size_t inner = 0; inner < ncols1; inner++) {
                inner_sum += matrix1[row][inner] * matrix2[inner][col];
            }
            matrix_result[row][col] = inner_sum;
        }
    }
}