1 #if !defined(AILIBRARY_HH) 26 #define AI_DIRENT_SUPPORT 32 #define AI_GCC5_SUPPORT 38 #define AI_SHELL_SUPPORT 64 #if defined(AI_DIRENT_SUPPORT) 80 #define STRINGIFY(x) #x 85 #define TO_STRING(x) STRINGIFY(x) 89 #define PRINT_LINE() std::cout << "Line #" << __LINE__ << std::endl 133 std::ostringstream stream;
147 return text.size() >= prefix.size()
148 && 0 == text.compare(0, prefix.size(), prefix);
158 return text.size() >= suffix.size()
159 && 0 == text.compare(
160 text.size() - suffix.size(),
177 return std::string::npos != text.find(substring);
196 std::size_t position = text.find(substring);
197 std::size_t substringSize = substring.size();
198 std::size_t replacementSize = replacement.size();
200 while(std::string::npos != position){
201 text.replace(position, substringSize, replacement);
202 position = text.find(substring, position + replacementSize);
221 std::size_t position = text.find(substring);
222 std::size_t substringSize = substring.size();
223 std::size_t replacementSize = replacement.size();
225 while(std::string::npos != position){
226 text.replace(position, substringSize, replacement);
227 position = text.find(substring, position + replacementSize);
240 return string1 == string2;
251 std::transform(input.begin(), input.end(), input.begin(), ::toupper);
264 std::transform(input.begin(), input.end(), input.begin(), ::tolower);
284 const std::size_t symbolsBeforePoint,
285 const char symbolToPrepend =
'0' 289 const int counter = symbolsBeforePoint
292 for(
int i = 0; i <
counter; ++i){
315 static std::size_t count = 0;
335 static std::size_t count = 0;
352 std::cout <<
marker(value) << std::endl;
365 static const double e = 2.71828182845904523536;
369 static const double pi = 3.14159265358979323846;
384 return copysign(1, value);
429 T minimum = input[0];
431 for(std::size_t i = 1; i < input.size(); ++i){
432 minimum =
ai::min(minimum, input[i]);
448 T maximum = input[0];
450 for(std::size_t i = 1; i < input.size(); ++i){
451 maximum =
ai::max(maximum, input[i]);
466 INLINE T
min(
const std::vector< std::vector<T> > &input){
467 T minimum = input[0][0];
469 for(std::size_t i = 0; i < input.size(); ++i){
470 for(std::size_t j = 0; j < input[0].size(); ++j){
471 minimum =
ai::min(minimum, input[i][j]);
487 INLINE T
max(
const std::vector< std::vector<T> > &input){
488 T maximum = input[0][0];
490 for(std::size_t i = 0; i < input.size(); ++i){
491 for(std::size_t j = 0; j < input[0].size(); ++j){
492 maximum =
ai::max(maximum, input[i][j]);
512 T root = (T) round(sqrt(value));
514 return value == root * root;
526 return 0 < matrix.size() && matrix.size() == matrix[0].size();
543 std::vector<T> &vector,
544 const std::size_t length,
548 std::uniform_real_distribution<T> distribution(
min,
max);
549 std::random_device device;
553 std::chrono::high_resolution_clock::now()
554 ).time_since_epoch().count();
556 srand(device() * currentTime);
558 vector.resize(length);
563 [&device, &distribution](){
564 return distribution(device);
585 std::vector< std::vector<T> > &matrix,
586 const std::size_t xSize,
587 const std::size_t ySize,
591 matrix.resize(xSize);
593 for(
size_t i = 0; i < xSize; ++i){
594 matrix[i].resize(ySize);
618 std::vector< std::vector<T> > &matrix,
619 const std::size_t size,
638 std::vector< std::vector<T> > &matrix,
639 const bool rotateClockwise =
false 641 std::size_t length = matrix.size();
644 for(std::size_t i = 0; i < length / 2; ++i){
645 for(std::size_t j = i; j < length - i - 1; ++j){
646 T savedValue = matrix[i][j];
648 matrix[i][j] = matrix[length - 1 - j][i];
649 matrix[length - 1 - j][i] =
650 matrix[length - 1 - i][length - 1 - j];
651 matrix[length - 1 - i][length - 1 - j] =
652 matrix[j][length - 1 - i];
653 matrix[j][length - 1 - i] = savedValue;
657 for(std::size_t i = 0; i < length / 2; ++i){
658 for(std::size_t j = i; j < length - i - 1; ++j){
659 T savedValue = matrix[i][j];
661 matrix[i][j] = matrix[j][length - 1 - i];
662 matrix[j][length - 1 - i] =
663 matrix[length - 1 - i][length - 1 - j];
664 matrix[length - 1 - i][length - 1 - j] =
665 matrix[length - 1 - j][i];
666 matrix[length - 1 - j][i] = savedValue;
675 std::vector< std::vector<T> > matrix,
676 std::vector< std::vector<T> > &inverse
679 throw std::runtime_error(
685 const std::size_t length = matrix.size();
688 inverse.resize(length);
690 for(std::size_t i = 0; i < length; ++i){
691 inverse[i].resize(length);
693 inverse[i][i] = (T) 1.;
698 for(std::size_t i = 0; i < length; ++i){
699 for(std::size_t j = 0; j < length; ++j){
701 ratio = matrix[j][i] / matrix[i][i];
703 for(std::size_t k = 0; k < length; ++k){
704 matrix[j][k] -= ratio * matrix[i][k];
705 inverse[j][k] -= ratio * inverse[i][k];
710 for(std::size_t i = 0; i < length; ++i){
711 for(std::size_t j = 0; j < length; ++j){
712 inverse[i][j] /= matrix[i][i];
720 std::vector< std::vector<T> > &matrix
736 std::vector< std::vector<T> > &matrix,
737 std::vector<T> &vector
739 for(std::size_t i = 0; i < matrix.size(); ++i){
740 for(std::size_t j = 0; j < matrix[i].size(); ++j){
741 vector.push_back(matrix[i][j]);
758 std::vector<T> &vector,
759 std::vector< std::vector<T> > &matrix
762 throw std::runtime_error(
763 ai::string(
"Exception while converting vector into the ")
764 +
ai::string(
"square matrix: vector size should be square")
768 std::size_t size = (std::size_t) round(sqrt(vector.size()));
770 for(std::size_t i = 0; i < size; ++i){
773 for(std::size_t j = 0; j < size; ++j){
774 row.push_back(vector[i * size + j]);
777 matrix.push_back(row);
797 std::vector< std::vector<T> > &matrix,
798 std::vector<T> &source,
799 const bool moveToTheRight =
true 801 if(1 > source.size()){
802 throw std::runtime_error(
803 ai::string(
"Exception while creating a circulant: ")
804 +
ai::string(
"size of the source should be at least 1.")
808 std::size_t length = source.size();
810 std::size_t displacement = 0;
813 matrix.resize(length);
816 for(std::size_t i = 0; i < length; ++i){
817 matrix[i].resize(length);
819 std::size_t k = displacement;
821 for(std::size_t j = 0; j < length - displacement; ++j){
822 matrix[i][k] = source[j];
829 for(std::size_t j = length - displacement; j < length; ++j){
830 matrix[i][k] = source[j];
838 for(std::size_t i = 0; i < length; ++i){
839 matrix[i].resize(length);
843 for(std::size_t j = displacement; j < length; ++j){
844 matrix[i][k] = source[j];
849 for(std::size_t j = 0; j < displacement; ++j){
850 matrix[i][k] = source[j];
873 const std::vector< std::vector<T> > &left,
874 const std::vector< std::vector<T> > &right,
875 std::vector< std::vector<T> > &result
880 || left[0].size() != right.size()
882 throw std::runtime_error(
883 "Exception while multiplying: check the sizes of the input." 887 std::size_t vLength = left.size();
888 std::size_t hLength = right[0].size();
889 std::size_t fLength = left[0].size();
891 result.resize(vLength);
893 for(std::size_t i = 0; i < vLength; ++i){
894 result[i].resize(hLength);
896 for(std::size_t j = 0; j < hLength; ++j){
897 result[i][j] = (T) 0.;
899 for(std::size_t k = 0; k < fLength; ++k){
900 for(std::size_t j = 0; j < hLength; ++j){
901 result[i][j] += left[i][k] * right[k][j];
919 const std::vector< std::vector<T> > &left,
920 const std::vector<T> &right,
921 std::vector<T> &result
923 if(1 > left.size() || left[0].size() != right.size()){
924 throw std::runtime_error(
925 "Exception while multiplying: check the sizes of the input." 929 std::size_t vLength = left.size();
931 int hLength = (int) right.size();
934 result.resize(vLength);
936 for(
int i = 0; i < vLength; ++i){
940 for(; j <= hLength - 4; j += 4){
941 result[i] += left[i][j] * right[j]
942 + left[i][j + 1] * right[j + 1]
943 + left[i][j + 2] * right[j + 2]
944 + left[i][j + 3] * right[j + 3];
946 for(; j < hLength; ++j){
947 result[i] += left[i][j] * right[j];
964 const std::vector<T> &left,
965 const std::vector<T> &right,
968 if(left.size() != right.size()){
969 throw std::runtime_error(
970 "Exception while multiplying: check the sizes of the input." 974 int length = (int) left.size();
979 for(; i <= length - 4; i += 4){
980 result += left[i] * right[i]
981 + left[i + 1] * right[i + 1]
982 + left[i + 2] * right[i + 2]
983 + left[i + 3] * right[i + 3];
985 for(; i < length; ++i){
986 result += left[i] * right[i];
1002 template<
typename T>
1004 const std::vector< std::vector<T> > &left,
1005 const std::vector< std::vector<T> > &right,
1006 std::vector< std::vector<T> > &result
1011 || left.size() != right.size()
1012 || left[0].size() != right[0].size()
1014 throw std::runtime_error(
1015 "Exception while multiplying: check the sizes of the input." 1019 std::size_t vLength = left.size();
1020 std::size_t hLength = left[0].size();
1022 result.resize(vLength);
1024 for(std::size_t i = 0; i < vLength; ++i){
1025 result[i].resize(hLength);
1027 for(std::size_t j = 0; j < hLength; ++j){
1028 result[i][j] = left[i][j] * right[i][j];
1044 template<
typename T>
1046 const std::vector<T> &left,
1047 const std::vector<T> &right,
1048 std::vector<T> &result
1050 if(left.size() != right.size()){
1051 throw std::runtime_error(
1052 "Exception while multiplying: check the sizes of the input." 1056 int length = (int) left.size();
1058 result.resize(length);
1060 for(std::size_t i = 0; i < length; ++i){
1061 result[i] = left[i] * right[i];
1067 template<
typename T>
1069 const std::vector< std::vector<T> > &left,
1070 const std::vector< std::vector<T> > &right,
1071 std::vector< std::vector<T> > &result
1074 left.size() != right.size()
1075 || 2 != left[0].size()
1076 || 2 != right[0].size()
1078 throw std::runtime_error(
1079 "Exception while multiplying: check the sizes of the input." 1083 const std::size_t length = left.size();
1085 result.resize(length);
1087 for(std::size_t i = 0; i < length; ++i){
1088 result[i].resize(2);
1090 result[i][0] = left[i][0] * right[i][0]
1091 - left[i][1] * right[i][1];
1092 result[i][1] = left[i][0] * right[i][1]
1093 + left[i][1] * right[i][0];
1099 template<
typename T>
1107 template<
typename T>
1109 const std::size_t length = complexVector.size();
1111 for(std::size_t i = 0; i < length; ++i){
1112 complexVector[i][1] = -complexVector[i][1];
1118 template<
typename T>
1119 INLINE void fft(std::vector< std::vector<T> > &complexVector){
1120 const std::size_t length = complexVector.size();
1122 if(2 > length || !(length & (length - 1)) == 0){
1123 throw std::runtime_error(
1125 +
std::string(
"vector length must be a power of two.")
1130 std::size_t k = length;
1133 const double doubleTypeLength = (double) k;
1135 double thetaT =
ai::pi / doubleTypeLength;
1140 double phiT0 = cos(thetaT);
1141 double phiT1 = -sin(thetaT);
1149 phiT0 = swap0 * swap0 - swap1 * swap1;
1150 phiT1 = 2. * swap0 * swap1;
1153 for(std::size_t l = 0; l < k; ++l){
1154 for(std::size_t i = l; i < length; i += n){
1156 swap0 = complexVector[i][0] - complexVector[j][0];
1157 swap1 = complexVector[i][1] - complexVector[j][1];
1158 complexVector[i][0] += complexVector[j][0];
1159 complexVector[i][1] += complexVector[j][1];
1160 complexVector[j][0] = swap0 * T0 - swap1 * T1;
1161 complexVector[j][1] = swap0 * T1 + swap1 * T0;
1165 T0 = swap0 * phiT0 - T1 * phiT1;
1166 T1 = swap0 * phiT1 + T1 * phiT0;
1170 std::size_t m = (std::size_t) log2(doubleTypeLength);
1172 for(std::size_t i = 0; i < length; ++i){
1174 j = (((j & 0xaaaaaaaa) >> 1) | ((j & 0x55555555) << 1));
1175 j = (((j & 0xcccccccc) >> 2) | ((j & 0x33333333) << 2));
1176 j = (((j & 0xf0f0f0f0) >> 4) | ((j & 0x0f0f0f0f) << 4));
1177 j = (((j & 0xff00ff00) >> 8) | ((j & 0x00ff00ff) << 8));
1178 j = ((j >> 16) | (j << 16)) >> (32 - m);
1181 swap0 = complexVector[i][0];
1182 swap1 = complexVector[i][1];
1183 complexVector[i][0] = complexVector[j][0];
1184 complexVector[i][1] = complexVector[j][1];
1185 complexVector[j][0] = swap0;
1186 complexVector[j][1] = swap1;
1193 template<
typename T>
1195 const std::size_t length = complexVector.size();
1197 const double doubleTypeLength = (double) length;
1203 for(std::size_t i = 0; i < length; ++i){
1204 complexVector[i][0] /= doubleTypeLength;
1205 complexVector[i][1] /= doubleTypeLength;
1223 return std::chrono::high_resolution_clock::now();
1232 return std::chrono::system_clock::now();
1251 const std::chrono::high_resolution_clock::time_point start,
1252 const std::chrono::high_resolution_clock::time_point finish,
1256 return (
double) std::chrono::duration_cast
1257 <std::chrono::hours> (finish - start).count();
1261 return (
double) std::chrono::duration_cast
1262 <std::chrono::minutes> (finish - start).count();
1266 return (
double) std::chrono::duration_cast
1267 <std::chrono::seconds> (finish - start).count();
1271 return (
double) std::chrono::duration_cast
1272 <std::chrono::microseconds> (finish - start).count();
1276 return (
double) std::chrono::duration_cast
1277 <std::chrono::nanoseconds> (finish - start).count();
1280 return (
double) std::chrono::duration_cast
1281 <std::chrono::milliseconds> (finish - start).count();
1286 const std::chrono::high_resolution_clock::time_point start,
1294 const std::chrono::high_resolution_clock::time_point start,
1295 const std::chrono::high_resolution_clock::time_point finish,
1297 const std::size_t count = 0
1299 std::cout <<
"Timer #" <<
counter(count) <<
": " 1300 <<
ai::duration(start, finish, scale) << scale << std::endl;
1305 const std::chrono::high_resolution_clock::time_point start,
1307 const std::size_t count = 0
1309 std::chrono::high_resolution_clock::time_point finish =
ai::time();
1314 #if defined(AI_GCC5_SUPPORT) 1327 std::chrono::system_clock::time_point timePoint =
1328 std::chrono::system_clock::now()
1330 std::time_t date = std::chrono::system_clock::to_time_t(timePoint);
1332 std::ostringstream stream;
1334 stream << std::put_time(std::localtime(&date),
"%F %T");
1336 return stream.str();
1349 std::chrono::system_clock::time_point timePoint =
1350 std::chrono::system_clock::now()
1352 std::time_t date = std::chrono::system_clock::to_time_t(timePoint);
1354 std::ostringstream stream;
1356 stream << std::put_time(std::localtime(&date),
"%F");
1358 return stream.str();
1371 std::chrono::system_clock::time_point timePoint =
1372 std::chrono::system_clock::now()
1374 std::time_t date = std::chrono::system_clock::to_time_t(timePoint);
1376 std::ostringstream stream;
1378 stream << std::put_time(std::localtime(&date),
"%T");
1380 return stream.str();
1401 parameter = parameter.substr(name.size());
1410 template<
typename T>
1414 const std::vector< std::vector<T> > intervals
1416 for(std::size_t i = 0; i < intervals.size(); ++i){
1417 if(intervals[i][0] <= parameter && parameter <= intervals[i][1]){
1418 value = intervals[i][2];
1427 template<
typename T>
1432 const std::vector< std::vector<T> > intervals
1434 for(std::size_t i = 0; i < intervals.size(); ++i){
1435 if(intervals[i][0] <= parameter && parameter <= intervals[i][1]){
1436 firstValue = intervals[i][1];
1437 secondValue = intervals[i][2];
1468 parameter = parameter.substr(name.size());
1471 value = (char) parameter[0];
1491 value = parameter.substr(name.size());
1501 template<
typename T>
1510 parameter = parameter.substr(name.size());
1512 if(std::istringstream(parameter) >> value){
1529 parameter = parameter.substr(name.size());
1530 value = std::abs(strtod(parameter.c_str(),
nullptr));
1540 template<
typename T>
1566 std::cout <<
"\033[2J" <<
"\033[H";
1572 std::locale::global(std::locale(
"en_US.utf8"));
1574 std::locale::global(
1595 const int screenWidth = 80
1601 if(0.01 > progress){
1605 if(20 > screenWidth){
1606 throw std::runtime_error(
1607 ai::string(
"Screen width should be at least 20 (not ")
1613 const std::size_t barWidth = screenWidth - 7;
1615 int width = progress * barWidth;
1617 std::cout << std::fixed;
1618 std::cout.precision(1);
1623 <<
" " << progress * 100. <<
"%";
1638 const int screenWidth = 80
1640 if(20 > screenWidth){
1641 throw std::runtime_error(
1642 ai::string(
"Screen width should be at least 20 (not ")
1648 std::cout <<
"\r" << std::setw(screenWidth) << std::left << line
1663 template<
typename T>
1666 const char separator,
1667 std::vector< std::vector<T> > &matrix
1669 std::ifstream input(filename);
1672 throw std::runtime_error(
1673 ai::string(
"Exception while parsing the file into a matrix: ")
1682 for(
std::string line; std::getline(input, line);){
1687 std::istringstream stream(line);
1691 while(std::getline(stream, token, separator)){
1692 if(std::istringstream(token) >> value){
1693 row.push_back(value);
1697 matrix.push_back(row);
1706 template<
typename T>
1709 const char separator,
1710 std::vector<T> &vector
1712 std::ifstream input(filename);
1715 throw std::runtime_error(
1716 ai::string(
"Exception while parsing the file into a vector: ")
1725 if(
'\n' == separator){
1726 for(
std::string line; std::getline(input, line);){
1731 if(std::istringstream(line) >> value){
1732 vector.push_back(value);
1738 std::getline(input, line);
1740 std::istringstream stream(line);
1742 while(std::getline(stream, token, separator)){
1743 if(std::istringstream(token) >> value){
1744 vector.push_back(value);
1758 std::ifstream input(filename);
1761 throw std::runtime_error(
1762 ai::string(
"Exception while parsing the file into a string: ")
1767 std::ostringstream stream;
1768 stream << input.rdbuf();
1769 content = stream.str();
1776 template<
typename T>
1779 const char separator,
1780 std::vector< std::vector<T> > &matrix
1782 std::vector< std::vector<T> > matrixToAdd;
1786 for(std::size_t i = 0; i < matrix.size(); ++i){
1790 matrixToAdd[i].begin(),
1799 template<
typename T>
1802 const char separator,
1803 std::vector<T> &vector,
1804 const bool checkForNaN =
false 1806 std::vector<T> vectorToAdd;
1813 vectorToAdd.begin(),
1829 template<
typename T>
1831 const std::vector<std::vector <T> > &matrix,
1832 const bool transpose =
false,
1833 const int precision = 5
1835 if(1 > matrix.size()){
1836 throw std::runtime_error(
1837 ai::string(
"Exception while printing the matrix: ")
1842 std::cout << std::scientific;
1843 std::cout.precision(precision);
1845 std::cout <<
"Matrix[" << matrix.size() <<
"x" << matrix[0].size()
1846 <<
"] = {" << std::endl;
1849 for(std::size_t j = 0; j < matrix[0].size(); ++j){
1850 const std::size_t lastIndex = matrix.size() - 1;
1852 for(std::size_t i = 0; i < lastIndex; ++i){
1853 std::cout << matrix[i][j] <<
", ";
1856 std::cout << matrix[lastIndex][j] << std::endl;
1859 for(std::size_t i = 0; i < matrix.size(); ++i){
1860 const std::size_t lastIndex = matrix[i].size() - 1;
1862 for(std::size_t j = 0; j < lastIndex; ++j){
1863 std::cout << matrix[i][j] <<
", ";
1866 std::cout << matrix[i][lastIndex] << std::endl;
1870 std::cout <<
"}[" << matrix.size() <<
"x" << matrix[0].size()
1871 <<
"]" << std::endl;
1876 template<
typename T>
1878 const std::vector<T> &vector,
1879 const int precision = 5
1881 if(1 > vector.size()){
1882 throw std::runtime_error(
1883 ai::string(
"Exception while printing the vector: ")
1888 std::size_t lastIndex = vector.size() - 1;
1890 std::cout << std::scientific;
1891 std::cout.precision(precision);
1893 std::cout <<
"Vector[" << vector.size() <<
"] = {" << std::endl;
1895 for(std::size_t i = 0; i < lastIndex; ++i){
1896 std::cout << vector[i] <<
", ";
1898 std::cout << vector[lastIndex] << std::endl;
1900 std::cout <<
"}[" << vector.size() <<
"]" << std::endl;
1905 template<
typename T>
1907 const std::vector<std::vector <T> > &matrix,
1908 const bool transpose =
false,
1909 const int precision = 5
1916 template<
typename T>
1918 const std::vector<T> &vector,
1919 const int precision = 5
1926 template<
typename T>
1931 std::cout << name <<
" = " << value << std::endl;
1982 template<
typename T>
1984 std::cout << style << income <<
ai::reset << std::endl;
1989 template<
typename T>
1996 template<
typename T>
2003 template<
typename T>
2010 template<
typename T>
2017 template<
typename T>
2024 template<
typename T>
2031 template<
typename T>
2038 template<
typename T>
2045 template<
typename T>
2052 template<
typename T>
2059 auto printStyleTest = [](
2063 std::cout << colorName <<
": " << color <<
"normal, " <<
ai::bold 2072 printStyleTest(
ai::red,
"Red");
2107 template<
typename T>
2110 const std::vector<std::vector <T> > &matrix,
2112 const bool transpose =
false,
2115 const std::size_t tokenWidth = 14
2138 std::ofstream output(filename + extension);
2141 throw std::runtime_error(
2142 ai::string(
"Exception while saving the matrix into the file: ")
2148 output << comment << std::endl;
2154 for(std::size_t j = 0; j < matrix[0].size(); ++j){
2157 const std::size_t lastIndex = matrix.size() - 1;
2159 for(std::size_t i = 0; i < lastIndex; ++i){
2160 output << std::setw(tokenWidth) << matrix[i][j]
2164 output << std::setw(tokenWidth) << matrix[lastIndex][j]
2165 << suffix << std::endl;
2168 for(std::size_t i = 0; i < matrix.size(); ++i){
2171 const std::size_t lastIndex = matrix[i].size() - 1;
2173 for(std::size_t j = 0; j < lastIndex; ++j){
2174 output << std::setw(tokenWidth) << matrix[i][j]
2178 output << std::setw(tokenWidth) << matrix[i][lastIndex]
2179 << suffix << std::endl;
2191 template<
typename T>
2194 const std::vector<T> &vector,
2220 std::ofstream output(filename + extension);
2223 throw std::runtime_error(
2224 ai::string(
"Exception while saving the vector into the file: ")
2230 output << comment << std::endl;
2235 const std::size_t lastIndex = vector.size() - 1;
2237 for(std::size_t i = 0; i < lastIndex; ++i){
2238 output << vector[i] << delimiter;
2241 output << vector[lastIndex] << suffix;
2253 std::ofstream output(filename +
"_l.txt");
2256 throw std::runtime_error(
2257 ai::string(
"Exception while saving the line into the file: ")
2263 output << comment <<
"\n";
2266 output << line << std::endl;
2272 template<
typename T>
2275 const std::vector<std::vector <T> > &matrix,
2282 template<
typename T>
2285 const std::vector<T> &vector,
2292 template<
typename T>
2301 #if defined(AI_GCC5_SUPPORT) 2307 const bool timestamp =
false,
2310 std::ofstream output(filename, std::ios_base::app);
2313 throw std::runtime_error(
2314 ai::string(
"Exception while saving log into the file: ")
2323 output << log << std::endl;
2332 std::vector<std::string> &logs,
2333 const bool timestamp =
false,
2336 std::ofstream output(filename, std::ios_base::app);
2339 throw std::runtime_error(
2340 ai::string(
"Exception while saving log into the file: ")
2351 for(std::size_t i = 0; i < logs.size(); ++i){
2352 output << prefix << logs[i] << std::endl;
2363 template<
typename T>
2366 std::vector< std::vector<T> > &positions,
2369 std::ifstream input(filename, std::ios::binary | std::ios::in);
2372 throw std::runtime_error(
2373 ai::string(
"Exception while reading positions from the file: ")
2378 char numberOfParticlesValue[4];
2380 char radiusValue[8];
2384 input.read(numberOfParticlesValue, 4);
2385 input.read(startByte, 4);
2387 input.read(radiusValue, 8);
2388 input.seekg(reinterpret_cast<int&>(startByte));
2390 radius =
reinterpret_cast<double&
>(radiusValue);
2392 std::size_t numberOfParticles = (std::size_t) reinterpret_cast<int&>(
2393 numberOfParticlesValue
2396 positions.resize(numberOfParticles);
2398 for(std::size_t i = 0; i < numberOfParticles; ++i){
2399 positions[i].resize(3);
2401 for(std::size_t j = 0; j < 3; ++j){
2402 input.read(value, 4);
2403 positions[i][j] = (T) reinterpret_cast<float&>(value);
2406 std::cout << std::endl;
2417 template<
typename T>
2420 const std::vector< std::vector<T> > &positions,
2421 const double radius = 1
2423 std::ofstream output(
2424 filename +
std::string(
".a3r"), std::ios::binary | std::ios::out
2428 throw std::runtime_error(
2429 ai::string(
"Exception while saving positions into the file: ")
2434 const std::size_t numberOfParticles = positions.size();
2436 const int integerNumberOfParticles = (int) numberOfParticles;
2438 const int startByte = 36;
2439 const int futureValue = 0;
2441 const int intSize = 4;
2442 const int floatSize = 4;
2443 const int doubleSize = 8;
2445 output.write(
"a3r\0", 4);
2446 output.write((
char*) &integerNumberOfParticles, intSize);
2447 output.write((
char*) &startByte, intSize);
2448 output.write(
"AiLib 1.1.0\0", 12);
2449 output.write((
char*) &radius, doubleSize);
2450 output.write((
char*) &futureValue, intSize);
2452 if(2 == positions[0].size()){
2453 for(std::size_t i = 0; i < numberOfParticles; ++i){
2454 float x = (float) positions[i][0];
2455 float y = (float) positions[i][1];
2458 output.write((
char*) &x, floatSize);
2459 output.write((
char*) &y, floatSize);
2460 output.write((
char*) &z, floatSize);
2463 if(3 == positions[0].size()){
2464 for(std::size_t i = 0; i < numberOfParticles; ++i){
2465 float x = (float) positions[i][0];
2466 float y = (float) positions[i][1];
2467 float z = (float) positions[i][2];
2469 output.write((
char*) &x, floatSize);
2470 output.write((
char*) &y, floatSize);
2471 output.write((
char*) &z, floatSize);
2487 template<
typename T>
2490 std::vector< std::vector<T> > &matrix
2492 std::ifstream input(filename);
2495 throw std::runtime_error(
2496 ai::string(
"Exception while reading positions from the file: ")
2507 for(
std::string line; std::getline(input, line);){
2514 std::istringstream stream(line);
2518 std::size_t subCounter = 0;
2520 while(std::getline(stream, token,
' ')){
2525 if(std::istringstream(token) >> value){
2526 row.push_back(value);
2530 matrix.push_back(row);
2539 template<
typename T>
2542 const std::vector< std::vector<T> > &matrix,
2543 const std::vector<T> &tones,
2546 std::ofstream output(filename +
std::string(
".xyz"));
2549 throw std::runtime_error(
2550 ai::string(
"Exception while saving positions into the file: ")
2555 const std::size_t numberOfParticles = matrix.size();
2557 bool withColors = (tones.size() == numberOfParticles);
2559 output << numberOfParticles << std::endl << std::endl;
2561 for(std::size_t i = 0; i < numberOfParticles; ++i){
2562 output << elementName;
2564 for(std::size_t j = 0; j < matrix[i].size(); ++j){
2565 output <<
" " << matrix[i][j];
2569 double color = 1. - 2. * tones[i];
2579 output <<
" " << color;
2581 color = 1. - std::abs(2. * tones[i] - 1.);
2591 output <<
" " << color;
2593 color = 2. * tones[i] - 1.;
2603 output <<
" " << color;
2606 output << std::endl;
2614 template<
typename T>
2617 const std::vector< std::vector<T> > &matrix,
2620 saveXYZ(filename, matrix, std::vector<T>(), elementName);
2640 return 0 == stat(name.c_str(), &buffer);
2658 std::ifstream input(filename);
2661 throw std::runtime_error(
2662 ai::string(
"Exception while counting lines in the file: ")
2667 std::size_t count = 0;
2675 if(std::string::npos != line.find(token)){
2686 #if defined(AI_DIRENT_SUPPORT) 2692 const bool addPathToFileNames =
false 2698 dir = opendir(path.c_str());
2700 std::vector<std::string> files;
2703 while(NULL != (ent = readdir (dir))){
2705 DT_REG == ent->d_type
2708 files.push_back(prefix + ent->d_name);
2715 if(addPathToFileNames){
2718 for(std::size_t i = 0; i < files.size(); ++i){
2719 files[i] = path + files[i];
2729 #if defined(AI_SHELL_SUPPORT) 2740 const std::size_t bufferSize = 128;
2742 std::array<char, bufferSize> buffer;
2746 std::shared_ptr<FILE> pipe(popen(command.c_str(),
"r"), pclose);
2749 throw std::runtime_error(
2750 ai::string(
"Exception while executing the command: ")
2755 while(!feof(pipe.get())){
2756 if(
nullptr != fgets(buffer.data(), bufferSize, pipe.get())){
2757 result += buffer.data();
2772 #if defined(AI_FUTURE) INLINE bool assignStringParameter(const char *input, const std::string name, std::string &value)
Definition: ai.hh:1483
#define INLINE
If defined, funstions will be marked as inline.
Definition: ai.hh:19
INLINE void parseFileIntoString(const std::string filename, std::string &content)
Definition: ai.hh:1754
INLINE void printLine(const std::string line, const int screenWidth=80)
Definition: ai.hh:1636
static std::string underline("\3[4m")
Terminal style code for underline.
INLINE void printStyle(const T income, const std::string style)
Definition: ai.hh:1983
INLINE void translateVectorIntoSquareMatrix(std::vector< T > &vector, std::vector< std::vector< T > > &matrix)
Transform vector into a square matrix (if possible)
Definition: ai.hh:757
INLINE void printCyan(const T income)
Definition: ai.hh:2032
INLINE std::size_t counter(const std::size_t value=0)
Returns ID starting from zero or the specified value.
Definition: ai.hh:314
INLINE void saveLog(const std::string filename, std::string log, const bool timestamp=false, const std::string stampSeparator=std::string(" "))
Definition: ai.hh:2304
INLINE void saveMatrix(const std::string filename, const std::vector< std::vector< T > > &matrix, std::string comment=std::string(), const bool transpose=false, std::string type=std::string("text"), std::string delimiter=std::string(" "), const std::size_t tokenWidth=14)
Definition: ai.hh:2108
INLINE void saveVector(const std::string filename, const std::vector< T > &vector, std::string comment=std::string(), std::string type=std::string("text"), std::string delimiter=std::string("\))
Definition: ai.hh:2192
INLINE T min(const std::vector< std::vector< T > > &input)
Returns minimum of matrix values.
Definition: ai.hh:466
INLINE void printBlack(const T income)
Definition: ai.hh:1990
INLINE void generateRandomMatrix(std::vector< std::vector< T > > &matrix, const std::size_t xSize, const std::size_t ySize, const T min=std::numeric_limits< T >::min(), const T max=std::numeric_limits< T >::max())
Fill matrix with random values.
Definition: ai.hh:584
INLINE bool equal(const char *charString, const std::string string1)
Checks if a char string is equal to a std::string.
Definition: ai.hh:237
INLINE std::string parseParameter(const char *input, const std::string name)
Definition: ai.hh:1394
INLINE void printUnderline(const T income)
Definition: ai.hh:2053
static std::string black("\3[30m")
Terminal color code for black.
INLINE std::string execute(const std::string command)
Definition: ai.hh:2737
INLINE void clearStyles()
Definition: ai.hh:2082
INLINE std::string marker(const std::size_t value=0)
Returns a string containing the word "Marker" and its ID.
Definition: ai.hh:334
INLINE std::string getTime(std::chrono::system_clock::time_point timePoint=std::chrono::system_clock::now())
Get time.
Definition: ai.hh:1370
INLINE void multiply(const std::vector< std::vector< T > > &left, const std::vector< std::vector< T > > &right, std::vector< std::vector< T > > &result)
Definition: ai.hh:872
INLINE void printRed(const T income)
Definition: ai.hh:1997
INLINE void printMatrix(const std::vector< std::vector< T > > &matrix, const bool transpose=false, const int precision=5)
Definition: ai.hh:1830
INLINE void printBold(const T income)
Definition: ai.hh:2046
INLINE void printMagenta(const T income)
Definition: ai.hh:2025
INLINE void printGreen(const T income)
Definition: ai.hh:2004
INLINE void printAllStyles()
Definition: ai.hh:2058
INLINE void clearScreen()
Definition: ai.hh:1565
INLINE void saveXYZ(const std::string filename, const std::vector< std::vector< T > > &matrix, const std::vector< T > &tones, std::string elementName="C")
Definition: ai.hh:2540
INLINE void multiplyComplexElementWise(const std::vector< std::vector< T > > &left, const std::vector< std::vector< T > > &right, std::vector< std::vector< T > > &result)
Definition: ai.hh:1068
INLINE std::vector< std::string > listFilesWithExtension(std::string path, const std::string extension, const std::string prefix=std::string(), const bool addPathToFileNames=false)
Definition: ai.hh:2688
INLINE void printBlue(const T income)
Definition: ai.hh:2018
INLINE std::string complexIntoString(const std::vector< T > complexValue)
Definition: ai.hh:1100
INLINE void save(const std::string filename, const std::vector< std::vector< T > > &matrix, std::string comment=std::string())
Definition: ai.hh:2273
static std::string white("\3[37m")
Terminal color code for white.
void rotateMatrix(std::vector< std::vector< T > > &matrix, const bool rotateClockwise=false)
Rotate square matrix.
Definition: ai.hh:637
INLINE bool isSquare(const T value)
Checks if number is square.
Definition: ai.hh:507
INLINE void ifft(std::vector< std::vector< T > > &complexVector)
Definition: ai.hh:1194
INLINE void fft(std::vector< std::vector< T > > &complexVector)
Definition: ai.hh:1119
INLINE std::string getVersion()
Get version of the library.
Definition: ai.hh:112
INLINE void parseFileInMatrix(const std::string filename, const char separator, std::vector< std::vector< T > > &matrix)
Definition: ai.hh:1664
INLINE bool assignBooleanParameter(const char *input, const std::string name, bool &value)
Definition: ai.hh:1445
static std::string reset("\3[0m")
Terminal code to reset special formatting.
INLINE bool contains(const std::string &text, const std::string &substring)
Checks if a string contains a substring.
Definition: ai.hh:173
INLINE void accumulateFileInMatrix(const std::string filename, const char separator, std::vector< std::vector< T > > &matrix)
Definition: ai.hh:1777
INLINE void showProgressBar(double progress, const int screenWidth=80)
Definition: ai.hh:1593
INLINE void setLocale(const std::string locale)
Definition: ai.hh:1570
INLINE T min(const T a, const T b)
Returns minimum of two values.
Definition: ai.hh:395
INLINE std::string prependNumber(const T value, const std::size_t symbolsBeforePoint, const char symbolToPrepend='0')
Definition: ai.hh:282
INLINE void printMarker(const std::size_t value=0)
Calls marker() and prints result to stdout.
Definition: ai.hh:351
INLINE bool assignAbsDoubleParameter(const char *input, const std::string name, double &value)
Definition: ai.hh:1521
INLINE std::string toUpperCase(std::string input)
Convert a string to upper case.
Definition: ai.hh:250
INLINE bool assignParameter(const char *input, const std::string name, T &value)
Definition: ai.hh:1502
INLINE std::string getDate(std::chrono::system_clock::time_point timePoint=std::chrono::system_clock::now())
Get date.
Definition: ai.hh:1348
INLINE void conjugate(std::vector< std::vector< T > > &complexVector)
Definition: ai.hh:1108
static std::string cyan("\3[36m")
Terminal color code for cyan.
INLINE bool assignCharParameter(const char *input, const std::string name, char &value)
Definition: ai.hh:1460
INLINE std::chrono::high_resolution_clock::time_point time()
Returns current time point.
Definition: ai.hh:1222
INLINE void loadXYZ(const std::string filename, std::vector< std::vector< T > > &matrix)
Definition: ai.hh:2488
static const double e
Mathematical constant 'e'.
Definition: ai.hh:365
INLINE void assignFromVectorByIntervalCondition(T &value, const T parameter, const std::vector< std::vector< T > > intervals)
Definition: ai.hh:1411
INLINE void printVector(const std::vector< T > &vector, const int precision=5)
Definition: ai.hh:1877
INLINE void inverseMatrix(std::vector< std::vector< T > > matrix, std::vector< std::vector< T > > &inverse)
Definition: ai.hh:674
static const double pi
Mathematical constant 'pi'.
Definition: ai.hh:369
static std::string green("\3[32m")
Terminal color code for green.
INLINE bool assignByCheckingParameter(const char *input, const std::string parameter, T &value, const T supposed)
Definition: ai.hh:1541
INLINE void saveLine(const std::string filename, const std::string line, std::string comment=std::string())
Definition: ai.hh:2248
INLINE std::chrono::system_clock::time_point systemTime()
Returns current time point.
Definition: ai.hh:1231
static std::string yellow("\3[33m")
Terminal color code for yellow.
INLINE void generateCirculantMatrix(std::vector< std::vector< T > > &matrix, std::vector< T > &source, const bool moveToTheRight=true)
Definition: ai.hh:796
INLINE void translateMatrixIntoVector(std::vector< std::vector< T > > &matrix, std::vector< T > &vector)
Elongates matrix into a vector.
Definition: ai.hh:735
INLINE std::size_t countLinesInFile(const std::string filename, const std::string token=std::string())
Definition: ai.hh:2654
INLINE bool folderExists(const std::string name)
Check if folder exists.
Definition: ai.hh:2637
INLINE void print(const std::vector< std::vector< T > > &matrix, const bool transpose=false, const int precision=5)
Definition: ai.hh:1906
INLINE void printDuration(const std::chrono::high_resolution_clock::time_point start, const std::chrono::high_resolution_clock::time_point finish, const std::string scale=std::string("ms"), const std::size_t count=0)
Definition: ai.hh:1293
INLINE double duration(const std::chrono::high_resolution_clock::time_point start, const std::chrono::high_resolution_clock::time_point finish, const std::string scale=std::string("ms"))
Definition: ai.hh:1250
INLINE void accumulateFileInVector(const std::string filename, const char separator, std::vector< T > &vector, const bool checkForNaN=false)
Definition: ai.hh:1800
INLINE bool saveA3R(const std::string filename, const std::vector< std::vector< T > > &positions, const double radius=1)
Definition: ai.hh:2418
INLINE std::string getDateAndTime(std::chrono::system_clock::time_point timePoint=std::chrono::system_clock::now())
Get date and time.
Definition: ai.hh:1326
INLINE void applyReplace(std::string &text, const std::string &substring, const std::string &replacement)
Modifies your string by replacing all occurrences of a substring string with your text...
Definition: ai.hh:216
INLINE bool hasSuffix(const std::string &text, const std::string &suffix)
Checks if a string ends with a substring.
Definition: ai.hh:157
INLINE std::string string(const T value)
Converts input into a string.
Definition: ai.hh:132
INLINE std::string toLowerCase(std::string input)
Convert a string to lower case.
Definition: ai.hh:263
INLINE std::string replace(std::string text, const std::string &substring, const std::string &replacement)
Replaces all occurrences of a substring in a copy of the initial string with your text...
Definition: ai.hh:191
INLINE T max(const std::vector< std::vector< T > > &input)
Returns maximum of matrix values.
Definition: ai.hh:487
INLINE void parseFileInVector(const std::string filename, const char separator, std::vector< T > &vector)
Definition: ai.hh:1707
INLINE void printWhite(const T income)
Definition: ai.hh:2039
INLINE T sign(const T value)
Returns signum of the value.
Definition: ai.hh:379
INLINE void generateRandomVector(std::vector< T > &vector, const std::size_t length, const T min=std::numeric_limits< T >::min(), const T max=std::numeric_limits< T >::max())
Definition: ai.hh:542
static std::string bold("\3[1m")
Terminal style code for bold.
static std::string red("\3[31m")
Terminal color code for red.
INLINE bool hasPrefix(const std::string &text, const std::string &prefix)
Checks if a string begins with a substring.
Definition: ai.hh:146
INLINE void multiplyElementWise(const std::vector< std::vector< T > > &left, const std::vector< std::vector< T > > &right, std::vector< std::vector< T > > &result)
Definition: ai.hh:1003
INLINE bool loadA3R(const std::string filename, std::vector< std::vector< T > > &positions, double &radius)
Definition: ai.hh:2364
INLINE T max(const T a, const T b)
Returns maximum of two values.
Definition: ai.hh:411
INLINE void printYellow(const T income)
Definition: ai.hh:2011
static std::string blue("\3[34m")
Terminal color code for blue.
static std::string magenta("\3[35m")
Terminal color code for magenta.