I'm stuck trying to get the value of what A[i][j] is pointing to. double a = A[i][j];. How do I correctly do it? Could someone please explain?
// g++ jacobi.cpp -O0 -o jacobi && ./jacobi
#include <iostream>
#include <iomanip>
#include <vector>
#include <climits>
using namespace std;
void print_matrix(vector<vector<double>>& m) {
for (int i = 0; i < m.size(); i++) {
for (int j = 0; j < m[0].size(); j++) {
cout << setw(5) << fixed << setprecision(2) << m[i][j] << " ";
}
cout << endl;
}
cout << "==================================" << endl;
}
// calculate average temperature based on average of adjacent cells
double avg_temp_at(vector<vector<double>>& matrix, int i, int j) {
return (
matrix[i][j] +
(j-1 >= 0 ? matrix[i][j-1] : 0) +
(i-1 >= 0 ? matrix[i-1][j] : 0) +
(j+1 < matrix[0].size() ? matrix[i][j+1] : 0) +
(i+1 < matrix.size() ? matrix[i+1][j] : 0)
) / 5;
}
// sequential Jacobi algorithm
vector<vector<double>> jacobi_relaxation(vector<vector<double>>& matrix, int& threshold) {
vector<vector<double>> B (matrix.size(), vector<double>(matrix[0].size(), 0));
vector<vector<double>>* A = &matrix;
double max_delta = INT_MAX;
while (max_delta > threshold) {
max_delta = 0;
for (int i = 0; i < matrix.size(); i++) {
for (int j = 0; j < matrix[0].size(); j++) {
B[i][j] = avg_temp_at(*A, i, j);
double a = A[i][j];
double delta = abs(B[i][j] - a);
max_delta = max(max_delta, delta);
}
}
print_matrix(B);
A = &B;
}
return *A;
}
int main() {
int threshold = 1;
int n = 6;
vector<vector<double>> matrix (n, vector<double>(n, 0));
matrix[1][2] = 100;
matrix[2][2] = 100;
matrix[3][2] = 100;
print_matrix(matrix);
vector<vector<double>> x = jacobi_relaxation(matrix, threshold);
}
I tried your code and it gave me error on this line:
double a = A[i][j];
Change that line into this:
double a = (*A)[i][j];
and it will work.
Explanation:
It's basically the same trick as in line B[i][j] = avg_temp_at(*A, i, j);. A is a pointer, which is pointing to a vector. To accessing to pointers "real data" you must use the *.
Here you can find more info about pointers.
Hope it helps.
Related
I have coded up a gauss seidel method that works just fine but i cannot seem to figure out how to convert that to jacobi... I know it should be easy so i must be missing something simple. For the assignment i had to make my own vector and matrix classes so that is why Vector is capital and called differently. Here is my working gauss seidel code:
else if (mode == 3) {
Vector temp;
temp.allocateData(b.numElems());
Vector old = temp;
Vector sum;
double f = 50;
int y = 4;
double tol = 1e-12;
double error = 10;
int max = 999999;
int count = 0;
while ( error > tol && max > count) {
for (int i = 0; i < row_; i++) {
temp.setVal(i, b.getVal(i) / M[i][i]);
for (int j = 0; j < col_; j++) {
if (j == i) {
continue;
}
temp.setVal(i, temp.getVal(i) - ((M[i][j] / M[i][i]) * temp.getVal(j)));
old.setVal(j, temp.getVal(i));
}
cout<<"x"<< i + 1 << "="<< temp.getVal(i) <<" \n";
error = abs(temp.getVal(i)-old.getVal(i))/abs(temp.getVal(i));
old = temp;
}
cout << "\n";
count++;
}
}
and here is my attempt at jacobi:
else if (mode == 2) {
Vector temp;
temp.allocateData(b.numElems());
Vector old = temp;
Vector sum;
double f = 50;
int y = 4;
double tol = 1e-12;
double error = 10;
int max = 999999;
int count = 0;
while ( error > tol && max > count) {
old.allocateData(b.numElems());
for (int i = 0; i < row_; i++) {
old.setVal(i, temp.getVal(i));
temp.setVal(i, b.getVal(i) / M[i][i]);
for (int j = 0; j < col_; j++) {
if (j == i) {
continue;
}
temp.setVal(i, temp.getVal(i) - ((M[i][j] / M[i][i]) * old.getVal(j)));
}
cout<<"x"<< i + 1 << "="<< temp.getVal(i) <<" \n";
error = abs(temp.getVal(i)-old.getVal(i))/abs(temp.getVal(i));
}
cout << "\n";
count++;
}
}
thanks everyone ahead of time for the help!!!
I need make Pascal Triangle matrix using vectors and then print it.
This algorithm would work with arrays, but somehow it doesn't work with matrix using vectors.
#include <iomanip>
#include <iostream>
#include <vector>
typedef std::vector<std::vector<int>> Matrix;
int NumberOfRows(Matrix m) { return m.size(); }
int NumberOfColumns(Matrix m) {
if (m.size() != 0)
return m[0].size();
return 0;
}
Matrix PascalTriangle(int n) {
Matrix mat;
int a;
for (int i = 1; i <= n; i++) {
a = 1;
for (int j = 1; j <= i; j++) {
if (j == 1)
mat.push_back(j);
else
mat.push_back(a);
a = a * (i - j) / j;
}
}
return mat;
}
void PrintMatrix(Matrix m, int width) {
for (int i = 0; i < NumberOfRows(m); i++) {
for (int j = 0; j < NumberOfColumns(m); j++)
std::cout << std::setw(width) << m[i][j];
std::cout << std::endl;
}
}
int main() {
Matrix m = PascalTriangle(7);
PrintMatrix(m, 10);
return 0;
}
I get nothing on screen, and here's the same code just without matrix using vectors program (which works fine).
Could you help me fix this code?
The main problem is that in PascalTriangle, you are starting out with an empty Matrix in both the number of rows and columns.
Since my comments mentioned push_back, here is the way to use it if you did not initialize the Matrix with the number of elements that are passed in.
The other issue is that NumberOfColumns should specify the row, not just the matrix vector.
The final issue is that you should be passing the Matrix by const reference, not by value.
Addressing all of these issues, results in this:
Matrix PascalTriangle(int n)
{
Matrix mat;
for (int i = 0; i < n; i++)
{
mat.push_back({}); // creates a new empty row
std::vector<int>& newRow = mat.back(); // get reference to this row
int a = 1;
for (int j = 0; j < i + 1; j++)
{
if (j == 0)
newRow.push_back(1);
else
newRow.push_back(a);
a = a * (i - j) / (j + 1);
}
}
return mat;
}
And then in NumberOfColumns:
int NumberOfColumns(const Matrix& m, int row)
{
if (!m.empty())
return m[row].size();
return 0;
}
And then, NumberOfRows:
int NumberOfRows(const Matrix& m) { return m.size(); }
And last, PrintMatrix:
void PrintMatrix(const Matrix& m, int width)
{
for (int i = 0; i < NumberOfRows(m); i++)
{
for (int j = 0; j < NumberOfColumns(m, i); j++)
std::cout << std::setw(width) << m[i][j];
std::cout << std::endl;
}
}
Here is a live demo
Your code won't compile because you have numerous errors in PascalTriangle.
For one, you initialize a matrix with no elements. Additionally, you use matrix indices starting at 1 rather than 0.
The following prints things for me:
Matrix PascalTriangle(int n) {
Matrix mat(n, std::vector<int>(n, 0)); // Construct Matrix Properly
int a;
for (int i = 0; i < n; i++) { // Start index at 0
a = 1;
for (int j = 0; j < i + 1; j++) { // Start index at 0
if (j == 0) // Changed 1 to 0
mat[i][j] = 1;
else
mat[i][j] = a;
a = a * (i - j) / (j+1); // Changed j to j+1 since j starts at 0
}
}
return mat;
}
Recently I engaged in programming. In my school were told to write a program to solve systems of linear equations Gauss method, that's what I did, but I an error "'abs' cannot be used as a function", please tell me how to fix.
#include <iostream>
#include <stdlib.h>
#include <cstdlib>
using namespace std;
// Вывод системы уравнений
void sysout(double **a, double *y, int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++){
cout << a[i][j] << "*x" << j;
if (j < n - 1) {
cout << " + ";
}
}
cout << " = " << y[i] << endl;
}
return;
}
double * gauss(double **a, double *y, int n) {
double *x, max;
int k, index;
const double eps = 0.00001; // точность
x = new double[n];
k = 0;
while (k < n) {
// Поиск строки с максимальным a[i][k]
int abs;
max = abs(a[k][k]);
index = k;
for (int i = k + 1; i < n; i++) {
if (abs(a[i][k]) > max) {
max = abs(a[i][k]);
index = i;
}
}
// Перестановка строк
if (max < eps) {
// нет ненулевых диагональных элементов
cout << "Решение получить невозможно из-за нулевого столбца " ;
cout << index << " матрицы A" << endl;
return 0;
}
for (int j = 0; j < n; j++) {
double temp = a[k][j];
a[k][j] = a[index][j];
a[index][j] = temp;
}
double temp = y[k];
y[k] = y[index];
y[index] = temp;
// Нормализация уравнений
for (int i = k; i < n; i++) {
double temp = a[i][k];
if (abs(temp) < eps) continue; // для нулевого коэффициента пропустить
for (int j = 0; j < n; j++) {
a[i][j] = a[i][j] / temp;
}
y[i] = y[i] / temp;
if (i == k) continue; // уравнение не вычитать само из себя
for (int j = 0; j < n; j++) {
a[i][j] = a[i][j] - a[k][j];
}
y[i] = y[i] - y[k];
}
k++;
}
// обратная подстановка
for (k = n - 1; k >= 0; k--) {
x[k] = y[k];
for (int i = 0; i < k; i++) {
y[i] = y[i] - a[i][k] * x[k];
}
}
return x;
}
int main() {
double **a, *y, *x;
int n;
system("chcp 1251>nul");
system("cls");
cout << "Введите количество уравнений: ";
cin >> n;
a = new double*[n];
y = new double[n];
for (int i = 0; i < n; i++) {
a[i] = new double[n];
for (int j = 0; j < n; j++) {
cout << "a[" << i << "][" << j << "]= ";
cin >> a[i][j];
}
}
for (int i = 0; i < n; i++) {
cout << "y[" << i << "]= ";
cin >> y[i];
}
sysout(a, y, n);
x = gauss(a, y, n);
for (int i = 0; i < n; i++){
cout << "x[" << i << "]=" << x[i] << endl;
}
cin.get(); cin.get();
return 0;
}
Change the variable to "fabs" tried to change to "std :: abs" tried. Compiler MiGW.
If you #include <cmath> instead of stdlib.h and cstdlib then it works:
#include <iostream>
#include <cmath>
using namespace std;
// Вывод системы уравнений
void sysout(double **a, double *y, int n) {
...
Also you should remove the int abs; in the while loop.
I'm not sure why #include <cstdlib> should cause problems here - can anyone explain?
Here's an online demo of the code compiling.
So I'm trying to read a matrix A from a text file, which it does correctly. A vector B is entered by the user. Then I want to perform Gaussian Elimination (Ax = b) to get the solution vector x. The values I get for x are -1.#IND and I have no idea why...I'm guessing something is going wrong in SystemSolution?
#include <iostream>
#include <vector>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
//this program does gaussian elimination for a matrix Ax=b
vector<double> SystemSolution(vector<vector<double>> A, vector<double> b)
{
//Determine and test size of a matrix
int n = A.size();
for (int i = 0; i < n; i++)
if (n != A[i].size())
throw "Error! Number of rows and columns of matrix must be equal!";
vector<double> x(b.size());
//x is the vector of solutions
for (int i = 0; i < n - 1; i++)
{
for (int j = i + 1; j < n; j++)
{
//Finding pivot
double pivot = A[i][i];
int index = i;
for (int k = i + 1; k < n; k++)
{
if (pivot > abs(A[k][i]))
{
index = k;
pivot = A[k][i];
}
}
//Row exchange
for (int k = 0; k < n; k++)
{
double tmp = A[i][k];
A[i][k] = A[index][k];
A[index][k] = tmp;
}
//Elimination
double coefficient = -(A[j][i] / A[i][i]);
for (int k = i; k < n; k++)
{
A[j][k] += coefficient*A[i][k];
}
b[j] += coefficient*b[i];
}
}
//Back-substitution
x[n - 1] = b[n - 1] / A[n - 1][n - 1];
for (int i = n - 2; i >= 0; i--)
{
double sum = 0;
for (int j = i; j < n; j++)
{
sum += x[j] * A[i][j];
}
x[i] = (b[i] - sum) / A[i][i];
}
return x;
}
void PrintVector(const vector<double> &b)
{
for (int i = 0; i < b.size(); i++)
cout << setiosflags(ios::showpoint | ios::fixed | ios::right)
<< setprecision(4)
<< setw(8) << b[i] << endl;
}
void PrintMatrix(const vector<vector<double> > &A)
{
for (int i = 0; i < A.size(); i++)
{
for (int j = 0; j < A[i].size(); j++)
cout << setiosflags(ios::showpoint | ios::fixed | ios::right)
<< setprecision(4)
<< setw(8) << A[i][j];
cout << endl;
}
}
int main()
{
int n;
cout << "Please enter the number of rows/columns:";
cin >> n;
ifstream matrixFile;
matrixFile.open("matrix.txt");
if (matrixFile.is_open()){
//matrix A values
vector<vector<double>> A(n, vector<double>(n));
vector<double> b(n);
string line;
int col = 0;
int row = 0;
while (getline(matrixFile, line)){
istringstream stream(line);
int x;
col = 0; //reset
while (stream >> x){
A[row][col] = x;
col++;
}
row++;
}
cout << "Please enter vector b:"<<endl;
//vector b values
for (int i = 0; i < row; i++)
{
cout << "b[" << i + 1 << "]= ";
cin >> b[i];
}
vector<double> x = SystemSolution(A, b);
cout << "- SOLUTIONS -" << endl;
cout << "Matrix:" << endl;
PrintMatrix(A);
cout << "\nVector x:" << endl;
PrintVector(x);
}
else{
cout << "File failed to open!";
}
matrixFile.close();
return 0;
}
There could be some divisions by zero in your code:
double coefficient = -(A[j][i] / A[i][i]);
/* .. */
x[n - 1] = b[n - 1] / A[n - 1][n - 1];
/* .. */
x[i] = (b[i] - sum) / A[i][i];
Check out Gauss-elimination here:
Square Matrix Inversion in C
Review and debug yours.
In function Determininant i keep getting an error....
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
const int maxsize = 10;
ifstream fin;
ofstream fout;
void transpose (double omatrix[][maxsize],double tmatrix [][maxsize], int array_size)
{
for(int i = 0; i < array_size; i++)
{
for(int j = 0; j < array_size; j++)
{
tmatrix[j][i] = omatrix[i][j];
}
}
}
void sub (double omatrix[][maxsize], double smatrix[][maxsize], int array_size, int i, int j)
{
int counter1 = 0, counter2 = 0;
for (int a = 0; a < array_size; a++)
{
if (a != i)
{
for (int b = 0; b < array_size; b++)
{
if (b != j)
{
smatrix[counter1][counter2] = omatrix[a][b];
counter2++;
}
}
counter1++;
}
}
}
double Determininant(double original_matrix[][maxsize], int array_size)
{
if(array_size == 1)
return original_matrix[0][0];
else if(array_size == 2)
return original_matrix[0][0] * original_matrix[1][1] - original_matrix[0][1] * original_matrix[1][0];
double d = 0.0;
double temp[maxsize][maxsize];
for(int i = 0; i < array_size; i++)
{
sub (original_matrix,temp,array_size, 0, i);
d += pow(-1.0,i) * original_matrix[0][i] * d(temp, array_size - 1);
}
return d;
}
void print (const double m[][maxsize], int array_size)
{
for(int i = 0; i < array_size; i++)
{
for(int j = 0; j < array_size; j++)
{
fout << m[i][j] << " ";
}
fout << "\n";
}
fout << "\n";
}
The error is error: 'd' cannot be used as a function.
Any ideas on whats wrong?
Exactly what the error message says: d is a double and you can't call it as a function. Perhaps you meant Determinant(temp, array_size - 1)?
It's the end of this line:
d += pow(-1.0,i) * original_matrix[0][i] * d(temp, array_size - 1);
As casablanca said, do you mean this?:
d += pow(-1.0,i) * original_matrix[0][i] * Determinant(temp, array_size - 1);
d += pow(-1.0,i) * original_matrix[0][i] * d(temp, array_size - 1);
The clause d(temp, array_size - 1) is telling C++ to call function d.