Matrix determinant

/* 
 * File:   vypocetDeterminantu.cpp
 * Author: borovec
 *
 * Created on 8. listopad 2008, 10:41
 */
 
#include <sstream>
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
 
using namespace std;
 
bool readLine(string line, double * array, const int cols ){
 
    int i = 0;
    double a;
 
    istringstream iss (line);
 
    while(1) {
 
        if(i > cols) return(false);
 
        iss >> a;
        if(iss.fail())  return (false);
 
        array[i]=a;
 
        if(iss.eof()){
            if(i==(cols-1)) return (true);
            return (false); // nacteno mene
        }
        i++;
    }
 
}
 
double ** allocMatrix(int rows ,int cols){
 
    double ** matrix ;
 
    matrix = new double * [rows];
 
    for (int i = 0; i < rows; i++) {
        matrix[i]= new double[cols];
 
    }
 
    return matrix;
 
}
 
void deleteMatrix(double ** matrix , int rows){
 
    for (int i = 0; i < rows; i++) {
        delete [] matrix[i];
    }
 
    delete [] matrix;
}
 
void printMatrix(double ** matrix, int rows, int cols){
 
    for (int i = 0; i < rows; i++) {
 
        for (int j = 0; j < cols; j++) {
            cout << matrix[i][j] << " ";
        }
        cout << endl;
 
    }
 
}
 
void gausElMatrix(double ** matrix, int rows, int cols, int &sign){
 
       for (int j = 0; j < cols-1; j++) {
        for (int i = (rows-1); i > j ; i--) {
 
            if(matrix[(i-1)][j] == 0){
                if(matrix[i][j] == 0) continue;
                double * tmp = matrix[i];
                matrix[i]=matrix[i-1];
                matrix[i-1]=tmp;
                sign *= -1;
            }
 
            double koeficient = matrix[(i)][j] / matrix[(i-1)][j];
 
            for (int k = 0; k < cols; k++) {
                matrix[i][k] = matrix[i][k] - koeficient*matrix[(i-1)][k];
                if(abs(matrix[i][k])<1e-12) matrix[i][k] = 0;
 
            }
        }
    }
}
 
double determinant(double ** matrix, int rows, int cols, int &sign){
 
    double determinant = 1;
    for (int i = 0; i < rows; i++) determinant = determinant * matrix[i][i];
 
    determinant *= (double) sign;
 
    return determinant;
}
 
/*
 * 
 */
int main(int argc, char** argv) {
 
    int rows=0,cols=0,i=1,sign=1;
    double **matrix;
    double a , det = 1;
 
    string line;
    getline(cin,line);
    if(cin.eof()){
        cout << "Nespravny vstup." << endl ;
        return (EXIT_FAILURE);
    }
    istringstream iss (line);
 
    while(1){
        iss >> a;
        cols++;
        if(iss.eof()) break;
    }
 
    rows = cols;
 
    matrix = allocMatrix(rows,cols);
 
    if(!readLine(line,matrix[0],cols)){
                cout << "Nespravny vstup." << endl;
                deleteMatrix(matrix,rows);
                return (EXIT_FAILURE);
            }
 
    // nacitani vsech radku matice
    while(1) {
 
        getline(cin,line);
 
        if(i < rows) {
 
            if(!readLine(line,matrix[i],cols)){
                cout << "Nespravny vstup." << endl;
                deleteMatrix(matrix,rows);
                return (EXIT_FAILURE);
            }
 
        } else {
 
            if(cin.eof()) break;
 
            cout << "Nespravny vstup." << endl ;
            deleteMatrix(matrix,rows);
            return (EXIT_FAILURE);
 
        }
 
        i++;
    }
 
    gausElMatrix(matrix,rows,cols,sign);
 
    det = determinant(matrix,rows,cols,sign);
 
//    printMatrix(matrix,rows,cols);
 
    deleteMatrix(matrix,rows);
 
    cout << "Determinant: " << fixed << setprecision(6) << det << endl;
 
    return (EXIT_SUCCESS);
}
programming/c-cpp/determinant.txt · Last modified: 2018-06-21 19:48 (external edit)
CC Attribution-Noncommercial-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0