1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <fstream>
#include <limits>
#include <sstream>
#include <string>
#include <utility>
template <typename T> struct BasicDynArr;
template <typename T>
std::ostream& operator<< (std::ostream&, const BasicDynArr<T>&);
template <typename T> struct BasicDynArr {
int size {};
int capacity {};
T* arr { nullptr };
int rows {};
explicit BasicDynArr( int = 0 ) noexcept;
BasicDynArr( const BasicDynArr& ) noexcept;
BasicDynArr& operator= ( BasicDynArr ) noexcept;
BasicDynArr( BasicDynArr&& ) noexcept;
BasicDynArr& operator= ( BasicDynArr&& ) noexcept;
~BasicDynArr();
void double_me();
void push_back( T );
//friend:
friend std::ostream& operator<< <>(std::ostream&, const BasicDynArr<T>&);
};
template <typename T> BasicDynArr<T>::BasicDynArr( int size_arg ) noexcept
: size { size_arg }
, capacity { size }
, arr { new T[capacity] {} }
{
}
template<typename T> BasicDynArr<T>::BasicDynArr(const BasicDynArr& rhs) noexcept
: size { rhs.size }
, capacity { rhs.capacity }
{
delete [] arr;
arr = new T[capacity] {};
for (int i {}; i < size; ++i) {
arr[i] = rhs.arr[i];
}
rows = rhs.rows;
}
template<typename T>
BasicDynArr<T>&
BasicDynArr<T>::operator=(BasicDynArr rhs) noexcept
{
using std::swap;
swap( *this, rhs );
return *this;
}
template<typename T> BasicDynArr<T>::BasicDynArr(BasicDynArr&& rhs) noexcept
: size( std::exchange( rhs.size, size ) )
, capacity( std::exchange( rhs.capacity, capacity ) )
, arr( std::exchange( rhs.arr, arr ) )
, rows( std::exchange( rhs.rows, rows ) )
{
}
template<typename T>
BasicDynArr<T>& BasicDynArr<T>::operator=( BasicDynArr&& rhs ) noexcept
{
size = std::exchange( rhs.size, size );
capacity = std::exchange( rhs.capacity, capacity );
arr = std::exchange( rhs.arr, arr );
rows = std::exchange( rhs.rows, rows );
}
template<typename T> BasicDynArr<T>::~BasicDynArr()
{
delete [] arr;
}
template <typename T> void BasicDynArr<T>::double_me()
{
if ( !capacity ) {
capacity = 1;
arr = new T[capacity] {};
return;
}
capacity *= 2;
T* tmp { new T[capacity] {} };
for (int i {}; i < size; ++i) {
tmp[i] = arr[i];
}
delete [] arr;
arr = tmp;
}
template <typename T> void BasicDynArr<T>::push_back( T val )
{
if (size == capacity) {
double_me();
}
arr[size++] = val;
}
template <typename T>
std::ostream&
operator<< (std::ostream& os, const BasicDynArr<T>& rhs)
{
int cols { rhs.size / rhs.rows };
for (int i {}; i < rhs.size; ++i) {
if ( i && !(i % cols) ) {
std::cout << '\n';
}
std::cout << std::fixed << std::setprecision(4) << rhs.arr[i] << ' ';
}
return os;
}
int fileToArray ( std::string name, BasicDynArr<double>& bda );
int find_pivot (double * const arr, int row, int column);
void change_rows (double * const arr, int pivot, int row_number, int pivot_row);
void change_rows_b (double * const arr, int row1, int row2);
void elimination (double * const arrA, double * const arrb, int row_number);
void back_substitution (double * const arrA,
double * const arrb,
double * const arrx,
int row_number);
void inverse (double * const arr);
double max_2 (double num1, double num2);
double abs_udef (double num);
double cond_inf (double * const arr);
double cond_one (double * const arr);
bool is_singular (double * const arr, int row_number);
void arrayToFile (double * const arr, int row_number);
int main ()
{
std::cout << "Please enter the name of the file that contains the matrix A "
"(in full detail)\n>>> " ;
std::string nameA ;
std::cin >> nameA ;
BasicDynArr<double> bda_a;
if (-1 == fileToArray(nameA, bda_a)) {
std::cout << "\nPlease check the file name for matrix A...\n" ;
return 0;
}
std::cout << "First array:\nNumber of lines: " << bda_a.rows << '\n'
<< bda_a << '\n';
std::cout << "\nPlease enter the name of the file that contains the vector b"
" (in full detail):\n>>> " ;
std::string nameb;
std::cin >> nameb ;
BasicDynArr<double> bda_b;
if (-1 == fileToArray(nameb, bda_b)) {
std::cout << "\nPlease check the file name for vector b...\n" ;
return 0;
}
std::cout << "First array:\nNumber of lines: " << bda_b.rows << '\n'
<< bda_b << '\n';
double condition_one {};
double condition_inf {};
if (2 == bda_b.rows) { // this part will be skipped as for your files
condition_one = cond_one(bda_a.arr);
inverse(bda_a.arr);
condition_inf = cond_inf(bda_a.arr);
}
elimination(bda_a.arr, bda_b.arr, bda_b.rows);
if (is_singular(bda_a.arr, bda_b.rows)) {
std::cout << "\nGiven matrix A is a singular matrix... \n" ;
} else {
double* arrx { new double [bda_b.rows] } ;
back_substitution(bda_a.arr, bda_b.arr, arrx, bda_b.rows) ;
arrayToFile(arrx, bda_b.rows) ;
delete[] arrx ;
if (bda_b.rows == 2) {
std::cout << "\nCondition number at one is: " << condition_one
<< "\nCondition number at infinity is: " << condition_inf
<< '\n';
}
}
return 0 ;
}
|