#include <iostream>
#include <cmath>
#include <sstream>
using namespace std;
class VectorN {
private:
int n;
double* data;
public:
VectorN() {
n = 0;
data = nullptr;
}
VectorN(int size, double* values) {
n = size;
data = new double[n];
for (int i = 0; i < n; i++) {
data[i] = values[i];
}
}
VectorN(const VectorN& other) {
n = other.n;
data = new double[n];
for (int i = 0; i < n; i++) {
data[i] = other.data[i];
}
}
VectorN& operator=(const VectorN& other) {
if (this != &other) {
delete[] data;
n = other.n;
data = new double[n];
for (int i = 0; i < n; i++) {
data[i] = other.data[i];
}
}
return *this;
}
~VectorN() {
delete[] data;
}
double getNorm() {
double sum = 0;
for (int i = 0; i < n; i++) {
sum += data[i] * data[i];
}
return sqrt(sum);
}
void normalize() {
double norm = getNorm();
if (norm == 0) return;
for (int i = 0; i < n; i++) {
data[i] /= norm;
}
}
double getDistance(const VectorN& other) {
double sum = 0;
for (int i = 0; i < n; i++) {
double diff = data[i] - other.data[i];
sum += diff * diff;
}
return sqrt(sum);
}
//string toString() ?
VectorN operator+(const VectorN& other) {
VectorN result(n, data);
for (int i = 0; i < n; i++) {
result.data[i] += other.data[i];
}
return result;
}
VectorN operator+(double value) {
VectorN result(n, data);
for (int i = 0; i < n; i++) {
result.data[i] += value;
}
return result;
}
VectorN operator-(const VectorN& other) {
VectorN result(n, data);
for (int i = 0; i < n; i++) {
result.data[i] -= other.data[i];
}
return result;
}
VectorN operator-(double value) {
VectorN result(n, data);
for (int i = 0; i < n; i++) {
result.data[i] -= value;
}
return result;
}
double operator*(const VectorN& other) {
double sum = 0;
for (int i = 0; i < n; i++) {
sum += data[i] * other.data[i];
}
return sum;
}
VectorN operator*(double value) {
VectorN result(n, data);
for (int i = 0; i < n; i++) {
result.data[i] *= value;
}
return result;
}
friend ostream& operator<<(ostream& os, const VectorN& v) {
os << "(";
for (int i = 0; i < v.n; i++) {
os << v.data[i];
if (i < v.n - 1) os << ", ";
}
os << ")";
return os;
}
};
// “<<” не знаю як зробити
int main() {
double arr1[] = {3, 4};
double arr2[] = {1, 2};
VectorN v1(2, arr1);
VectorN v2(2, arr2);
cout << v1 << endl;
cout << v2 << endl;
cout << "v1 + v2 = " << v1 + v2 << endl;
cout << "v1 - v2 = " << v1 - v2 << endl;
cout << "v1 * v2 = " << (v1 * v2) << endl;
cout << "Норма v1: " << v1.getNorm() << endl;
v1.normalize();
cout << "v1 після нормалізації: " << v1 << endl;
cout << "Відстань між векторами: " << v1.getDistance(v2) << endl;
return 0;
}