Initial commit
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
[
|
||||
{
|
||||
"label": "Compile & Run C++ (current file)",
|
||||
"command": "make && ./programma && make clean",
|
||||
"use_new_terminal": false,
|
||||
"allow_concurrent_runs": false,
|
||||
"reveal": "always",
|
||||
"show_command": false,
|
||||
"show_summary": false
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,10 @@
|
||||
CXX = g++
|
||||
CXXFLAGS = -std=c++17 -Wall -Wextra
|
||||
TARGET = programma
|
||||
SOURCES = main.cpp vector.cpp matrix.cpp
|
||||
|
||||
$(TARGET): $(SOURCES)
|
||||
@$(CXX) $(CXXFLAGS) $(SOURCES) -o $(TARGET)
|
||||
|
||||
clean:
|
||||
@rm -f $(TARGET)
|
||||
@@ -0,0 +1,105 @@
|
||||
#include "vector.hpp"
|
||||
#include "matrix.hpp"
|
||||
#include <stdexcept>
|
||||
|
||||
|
||||
Matrix computeR(Matrix A) {
|
||||
Matrix R = A;
|
||||
for(int i=0; i<R.size()[0];i++) {
|
||||
R(i,i)=0.0;
|
||||
}
|
||||
return R;
|
||||
}
|
||||
|
||||
|
||||
Vector computeInvD(const Matrix& A) {
|
||||
Vector invD(A.size()[0]);
|
||||
for(int i=0; i<invD.size();i++) {
|
||||
if(A(i,i)==0.0) {
|
||||
throw std::runtime_error("A matrix contains a 0 on diagonal");
|
||||
}
|
||||
invD(i)=1/A(i,i);
|
||||
}
|
||||
return invD;
|
||||
}
|
||||
|
||||
|
||||
Vector Jacobi(const Vector& B, const Matrix& R, const Vector& InvD) {
|
||||
int size = B.size();
|
||||
Vector k(size);
|
||||
Vector k_1(size);
|
||||
int iter=1;
|
||||
double errorNorm;
|
||||
|
||||
// D⁻¹b
|
||||
Vector firstPartial(size);
|
||||
for(int i=0; i<size; i++) {
|
||||
firstPartial(i)=InvD(i)*B(i);
|
||||
}
|
||||
// D⁻¹R
|
||||
Matrix secondPartial(size,size);
|
||||
for(int i=0; i<size; i++) {
|
||||
for(int j=0; j<size; j++) {
|
||||
secondPartial(i,j)=InvD(i)*R(i,j);
|
||||
}
|
||||
}
|
||||
|
||||
do {
|
||||
k=k_1;
|
||||
// finalizing
|
||||
Vector finalSecondPartial(size);
|
||||
for(int i=0; i<size; i++) {
|
||||
for(int j=0; j<size; j++) {
|
||||
finalSecondPartial(i)+=secondPartial(i,j)*k(j);
|
||||
}
|
||||
}
|
||||
Vector error(size);
|
||||
for(int i=0; i<size; i++) {
|
||||
k_1(i)=firstPartial(i)-finalSecondPartial(i);
|
||||
error(i)=k(i)-k_1(i);
|
||||
}
|
||||
iter+=1;
|
||||
errorNorm=error.norm();
|
||||
} while (errorNorm>1e-6 && iter<100);
|
||||
|
||||
std::cout << "Converge in " << iter << " tentativi.\n";
|
||||
k_1.print();
|
||||
return k_1;
|
||||
}
|
||||
|
||||
int main() {
|
||||
Matrix A(4,4);
|
||||
A(0,0)=4;
|
||||
A(0,1)=2;
|
||||
A(0,2)=0;
|
||||
A(0,3)=1;
|
||||
A(1,0)=3;
|
||||
A(1,1)=5;
|
||||
A(1,2)=2;
|
||||
A(1,3)=0;
|
||||
A(2,0)=1;
|
||||
A(2,1)=0;
|
||||
A(2,2)=3;
|
||||
A(2,3)=0;
|
||||
A(3,0)=3;
|
||||
A(3,1)=2;
|
||||
A(3,2)=2;
|
||||
A(3,3)=8;
|
||||
|
||||
Vector B(4);
|
||||
B(0)=5;
|
||||
B(1)=0;
|
||||
B(2)=4;
|
||||
B(3)=2;
|
||||
|
||||
Matrix R = computeR(A);
|
||||
|
||||
Vector InvD = computeInvD(A);
|
||||
|
||||
// x^(k+1) = D⁻¹b - D⁻¹R · x^(k)
|
||||
Jacobi(B, R, InvD);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
#include "matrix.hpp"
|
||||
#include <array>
|
||||
|
||||
Matrix::Matrix(int rows, int cols) : data(rows*cols, 0.0), rows(rows), cols(cols) {}
|
||||
|
||||
std::array<int, 2> Matrix::size() const {return {rows, cols};}
|
||||
|
||||
double& Matrix::operator()(int i, int j) {return data[i*cols+j];}
|
||||
double Matrix::operator()(int i, int j) const {return data[i*cols+j];}
|
||||
|
||||
void Matrix::print() const {
|
||||
std::cout << "---\n";
|
||||
for (int x=0; x<rows; x++) {
|
||||
for (int y=0; y<cols; y++) {
|
||||
std::cout << data[x*cols+y] << " ";
|
||||
}
|
||||
std::cout << "\n";
|
||||
}
|
||||
std::cout << "---";
|
||||
std::cout << std::endl;
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
#ifndef MATRIX_HPP
|
||||
#define MATRIX_HPP
|
||||
|
||||
#include <array>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
|
||||
class Matrix {
|
||||
public:
|
||||
Matrix(int rows, int cols);
|
||||
std::array<int, 2> size() const;
|
||||
double& operator()(int i,int j);
|
||||
double operator()(int i,int j) const;
|
||||
void print() const;
|
||||
|
||||
private:
|
||||
std::vector<double> data;
|
||||
int rows;
|
||||
int cols;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
#include "vector.hpp"
|
||||
#include <cmath>
|
||||
|
||||
Vector::Vector(int size) : data(size, 0.0) {}
|
||||
|
||||
int Vector::size() const {return data.size();}
|
||||
|
||||
double& Vector::operator()(int i) {return data[i];}
|
||||
double Vector::operator()(int i) const {return data[i];}
|
||||
|
||||
void Vector::print() const {
|
||||
std::cout << "---\n";
|
||||
for (double x : data) {
|
||||
std::cout << x << "\n";
|
||||
}
|
||||
std::cout << "---";
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
double Vector::norm() const {
|
||||
double somma = 0.0;
|
||||
for (double x : data) {
|
||||
somma += x * x;
|
||||
}
|
||||
return std::sqrt(somma);
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
#ifndef VECTOR_HPP
|
||||
#define VECTOR_HPP
|
||||
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
|
||||
class Vector {
|
||||
public:
|
||||
Vector(int size);
|
||||
int size() const;
|
||||
double& operator()(int i);
|
||||
double operator()(int i) const;
|
||||
void print() const;
|
||||
double norm() const;
|
||||
|
||||
private:
|
||||
std::vector<double> data;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user