/*
* Copyright (c) 2026 Jakub Górnikowski
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see
*/
#include "vectors.hpp"
// Drukuje elegancko sformatowany wektor jako wiersz
std::ostream& operator<<(std::ostream& os, const std::vector& v)
{
os << "Vector: [ ";
for (size_t i = 0; i < v.size(); i++)
{
os << std::fixed << std::setprecision(6) << v.at(i) << " ";
}
os << "]";
return os;
}
// Poniższe kilka funckji ma za zadanie przedłużyć funkcjonalność wektorów dodając
// opcje takie jak mnożenie przez skalar czy sumowanie wektorów (które nie są
// zaimplementowane natywnie w ten sposób)
// Dodawanie wektorów
std::vector operator+(const std::vector& a, const std::vector& b)
{
// Sprawdzenie równości wymiarów
assert(a.size() == b.size() && "Vector addition size mismatch");
std::vector result;
// Pre-zajęcie pamięci
result.reserve(a.size());
std::transform(a.begin(), a.end(), b.begin(),
std::back_inserter(result), std::plus());
return result;
}
// Odejmowanie wektorów
std::vector operator-(const std::vector& a, const std::vector& b)
{
assert(a.size() == b.size() && "Vector subtraction size mismatch");
std::vector result;
result.reserve(a.size());
std::transform(a.begin(), a.end(), b.begin(),
std::back_inserter(result), std::minus());
return result;
}
// Lewostronne mnożenie przez skalar
std::vector operator*(const double a, const std::vector& v)
{
std::vector result;
result.reserve(v.size());
std::transform(v.begin(), v.end(), std::back_inserter(result),
[a](const double& element) { return a * element; });
return result;
}
// Prawostronne mnożenie przez skalar
std::vector operator*(const std::vector& v, const double a)
{
// Wykonuje mnożenie lewostronne ręcznie zamieniając kolejność argumentów
return a * v;
}
// Nadpisanie wektora pomnożonego o skalar
std::vector& operator*=(std::vector& v, const double a)
{
std::transform(v.begin(), v.end(), v.begin(),
[a](const double& element) { return a * element; });
return v;
}