commit bdda0c3880ddf1cfae557aaa30e00d2348847f71 Author: aleandro Date: Mon Aug 24 19:27:25 2026 +0200 Initial commit diff --git a/.zed/tasks.json b/.zed/tasks.json new file mode 100644 index 0000000..e9164dc --- /dev/null +++ b/.zed/tasks.json @@ -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 + } +] diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..da5dcc0 --- /dev/null +++ b/Makefile @@ -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) \ No newline at end of file diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..e78188c --- /dev/null +++ b/main.cpp @@ -0,0 +1,105 @@ +#include "vector.hpp" +#include "matrix.hpp" +#include + + +Matrix computeR(Matrix A) { + Matrix R = A; + for(int i=0; i1e-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; +} + + diff --git a/matrix.cpp b/matrix.cpp new file mode 100644 index 0000000..7554545 --- /dev/null +++ b/matrix.cpp @@ -0,0 +1,21 @@ +#include "matrix.hpp" +#include + +Matrix::Matrix(int rows, int cols) : data(rows*cols, 0.0), rows(rows), cols(cols) {} + +std::array 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 +#include +#include + + +class Matrix { + public: + Matrix(int rows, int cols); + std::array size() const; + double& operator()(int i,int j); + double operator()(int i,int j) const; + void print() const; + + private: + std::vector data; + int rows; + int cols; + +}; + +#endif diff --git a/vector.cpp b/vector.cpp new file mode 100644 index 0000000..3c713c2 --- /dev/null +++ b/vector.cpp @@ -0,0 +1,26 @@ +#include "vector.hpp" +#include + +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); +} \ No newline at end of file diff --git a/vector.hpp b/vector.hpp new file mode 100644 index 0000000..f98404d --- /dev/null +++ b/vector.hpp @@ -0,0 +1,22 @@ +#ifndef VECTOR_HPP +#define VECTOR_HPP + +#include +#include + + +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 data; + +}; + +#endif