{"text": "#include \n#include \n#include \n#include \n \nusing namespace std;\nusing namespace Eigen;\n\n// lee la matriz desde el archivo de texto path. Números separados por espacios\nMatrixXd load_matrix(string path) {\n\tvector raw_data;\n\tifstream file(path);\n\tstring line, element;\n\tstringstream ss;\n\tint rows=0,cols=0;\n\t\n\twhile ( getline(file,line) ) { // leer línea del archivo\n\t\tss = stringstream(line); // convertir la línea en un stream\n\t\twhile( ss >> element ) { // leer cada elemento de la línea, separado por espacios\n\t\t\traw_data.push_back(stod(element)); // convertir a double y echar al vector\n\t\t}\n\t\trows++; // por cada fila terminada se aumenta el número de filas\n\t}\n\tfile.close();\n\tcols = raw_data.size()/rows;\n\treturn Map>(raw_data.data(),rows,cols);\n}\n\n// guarda la matriz como un archivo de texto en path. Números separados por espacios\nvoid save_matrix(string path, MatrixXd matrix) {\n\tofstream file(path);\n\tfile << matrix; // echar matriz al archivo\n\tfile.close();\n}\n\n// retorna una copia de la matriz M sin la fila dada por el índice\nMatrixXd remove_row(MatrixXd M, int remove_index) {\n\tint rows = M.rows()-1;\n\tint cols = M.cols();\n\tM.block(remove_index,0,rows-remove_index,cols) = M.bottomRows(rows-remove_index);\n\tM.conservativeResize(rows,cols);\n\treturn M;\n}\n// retorna una copia de la matriz M sin la columna dada por el índice\nMatrixXd remove_col(MatrixXd M, int remove_index) {\n\tint rows = M.rows();\n\tint cols = M.cols()-1;\n\tM.block(0,remove_index,rows,cols-remove_index) = M.rightCols(cols-remove_index);\n\tM.conservativeResize(rows,cols);\n\treturn M;\n}\n// retorna una copia de la matriz M sin la columna J y sin la fila I\nMatrixXd minor_matrix(MatrixXd M, int I, int J) {\n\treturn remove_col(remove_row(M,I),J);\n}\n\n// calcula el determinante de una matriz cuadrada M\ndouble det(MatrixXd M){\n\tint rows = M.rows();\n\tint cols = M.cols();\n\t\n\tdouble d = 0.0;\n\t\n\tif (rows != cols) {\n\t\tcerr << \"cannot take det of non-square matrix:\" << endl << M << endl;\n\t\tabort();\n\t}\n\telse if (rows == 1 && cols == 1) {\n\t\td = M(0,0);\n\t} else {\n\t\tfor(int j=0; j=0; j--) {\n\t\tfor (int i=0; i> input_filename;\n\t}\n\t\n\tMatrixXd m = load_matrix(input_filename);\n\t\n\tsave_matrix(\"eigen.dat\",m.inverse());\n\tsave_matrix(\"gauss_jordan.dat\",gauss_jordan_inverse(m));\n\tsave_matrix(\"cramer.dat\",cramer_inverse(m));\n}", "meta": {"hexsha": "74af02fe49c794cf5efbf01b78be850b84402d73", "size": 4766, "ext": "cpp", "lang": "C++", "max_stars_repo_path": "soluciones/j.guerrero/tarea2/solucion.cpp", "max_stars_repo_name": "japeinado/FISI2028-202120", "max_stars_repo_head_hexsha": "6b16a779f3e34bcbf35d8b5e0ea345cf50ffdadd", "max_stars_repo_licenses": ["MIT"], "max_stars_count": 3.0, "max_stars_repo_stars_event_min_datetime": "2021-08-17T19:19:11.000Z", "max_stars_repo_stars_event_max_datetime": "2021-11-08T12:26:41.000Z", "max_issues_repo_path": "soluciones/j.guerrero/tarea2/solucion.cpp", "max_issues_repo_name": "japeinado/FISI2028-202120", "max_issues_repo_head_hexsha": "6b16a779f3e34bcbf35d8b5e0ea345cf50ffdadd", "max_issues_repo_licenses": ["MIT"], "max_issues_count": 12.0, "max_issues_repo_issues_event_min_datetime": "2021-09-18T01:33:58.000Z", "max_issues_repo_issues_event_max_datetime": "2021-10-16T00:11:45.000Z", "max_forks_repo_path": "soluciones/j.guerrero/tarea2/solucion.cpp", "max_forks_repo_name": "japeinado/FISI2028-202120", "max_forks_repo_head_hexsha": "6b16a779f3e34bcbf35d8b5e0ea345cf50ffdadd", "max_forks_repo_licenses": ["MIT"], "max_forks_count": 28.0, "max_forks_repo_forks_event_min_datetime": "2021-09-17T22:38:23.000Z", "max_forks_repo_forks_event_max_datetime": "2021-10-02T19:59:49.000Z", "avg_line_length": 25.486631016, "max_line_length": 84, "alphanum_fraction": 0.6412085606, "num_tokens": 1475, "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.960361158630024, "lm_q2_score": 0.9304582554941719, "lm_q1q2_score": 0.8935759683032539}} {"text": "#include \n#include \n#include \n#include \n#include \n\nnamespace recursionAlgorithms\n{ \n using namespace std;\n using namespace boost::numeric::ublas;\n \n /*9.1\n count ways to cover N steps by hopping either \n 1,2 or 3 steps at a time\n */\n int waysToHopSlow(int N)\n {\n if (N==1 || N==0) { return 1; }\n if (N<0) { return 0; }\n return waysToHopSlow(N-1)+waysToHopSlow(N-2)+waysToHopSlow(N-3);\n };\n \n int waysToHopFast(int N, std::vector &cache)\n {\n if (N==0 || N==1) { return 1; }\n if (N<0) { return 0; }\n if (cache[N]>0) { return cache[N]; }\n cache[N] = waysToHopFast(N-1,cache)\n + waysToHopFast(N-2,cache)\n + waysToHopFast(N-3,cache);\n return cache[N];\n };\n \n /*9.2\n Return the number of ways connecting the top-left corner\n of a MxN matrix/grid to the bottom-right corner,\n going only downward and rightward.\n Off-limits grid points are allow by specifying a -1 value\n in the cache input matrix\n */\n int ways2D(int r, int c, matrix &cache)\n {\n if (r == cache.size1()-1 && c == cache.size2()-1 ) { return 1;}\n if (r >= cache.size1() || c >= cache.size2() || cache(r,c) == -1) \n { return 0; }\n if (cache(r,c)>0) { return cache(r,c); }\n \n cache(r,c) = ways2D(r,c+1,cache) + ways2D(r+1,c,cache);\n return cache(r,c);\n };\n \n template \n void printMatrix(matrix &mat)\n {\n for (int row=0;row input(12,1);\n //input <<= 1,0,1,2,2,1,3,3,3,0,10,11;\n \n // test 9.1\n //int Nin = 35;\n //std::vector cache(Nin,0);\n //cout << waysToHopSlow(Nin) << endl; \n //cout << waysToHopFast(Nin,cache) << endl; \n \n // test 9.2\n int M,N;\n M = 10; N= 10;\n matrix input(M,N);\n //input(1,1) = -1;\n printMatrix(input);\n cout << \"calculated number of ways = \"<< ways2D(0,0,input) << endl;\n cout << \"true number of ways = \" << binomial_coefficient(M+N-2,M-1) << endl;\n printMatrix(input);\n \n \n \n \n return 0;\n}", "meta": {"hexsha": "7c6932698dcd328d5f0ea9a9641c7efacd43d132", "size": 2841, "ext": "cpp", "lang": "C++", "max_stars_repo_path": "cpp/recursionAlgorithms.cpp", "max_stars_repo_name": "chaohan/code-samples", "max_stars_repo_head_hexsha": "0ae7da954a36547362924003d56a8bece845802c", "max_stars_repo_licenses": ["MIT"], "max_stars_count": null, "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "cpp/recursionAlgorithms.cpp", "max_issues_repo_name": "chaohan/code-samples", "max_issues_repo_head_hexsha": "0ae7da954a36547362924003d56a8bece845802c", "max_issues_repo_licenses": ["MIT"], "max_issues_count": null, "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "cpp/recursionAlgorithms.cpp", "max_forks_repo_name": "chaohan/code-samples", "max_forks_repo_head_hexsha": "0ae7da954a36547362924003d56a8bece845802c", "max_forks_repo_licenses": ["MIT"], "max_forks_count": null, "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null, "avg_line_length": 30.5483870968, "max_line_length": 91, "alphanum_fraction": 0.489968321, "num_tokens": 794, "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES\n\n", "lm_q1_score": 0.9511422186079557, "lm_q2_score": 0.9324533069832974, "lm_q1q2_score": 0.8868957071524187}} {"text": "#include \n\n#include \n#include \n\nconst std::complex I(0,1); // imaginary unit\nconst double PI = 3.14159265359;\n\nstruct Newton {\n\tNewton(const Eigen::VectorXd &x) : _x(x), _a(x.size()) { }\n\tvoid Interpolate(const Eigen::VectorXd &y);\n\tdouble operator()(double x) const;\n\nprivate:\n\tEigen::VectorXd _x;\t// nodes\n\tEigen::VectorXd _a;\t// coefficients\n};\n\n// Compute the coefficients in the Newton basis.\nvoid Newton::Interpolate(const Eigen::VectorXd &y) {\n\tint size = _x.size();\n\n\tEigen::MatrixXd M = Eigen::MatrixXd::Zero(size, size);\n\tM.col(0) = Eigen::VectorXd::Ones(size);\n\n\tfor(int col = 1; col < size; col++) {\n\t\tfor(int row = col; row < size; row++) {\n\t\t\tM(row, col) = M(row, col - 1) * (_x(row) - _x(col - 1));\n\t\t}\n\t}\n\n\t_a = M.triangularView().solve(y);\n}\n\n// Evaluate the interpolant at x.\ndouble Newton::operator()(double x) const {\n\tdouble p = 0;\n\n\tfor(int i = _x.size() - 1; i >= 0; i--) {\n\t\tp = p * (x - _x(i)) + _a(i);\n\t}\n\n\treturn p;\n}\n\nstruct Lagrange {\n\tLagrange(const Eigen::VectorXd &x);\n\tvoid Interpolate(const Eigen::VectorXd &y) { _y = y; }\n\tdouble operator()(double x) const;\n\nprivate:\n\tEigen::VectorXd _x;\t// nodes\n\tEigen::VectorXd _l;\t// weights\n\tEigen::VectorXd _y;\t// coefficients\n};\n\n// Compute the weights l for given nodes x.\nLagrange::Lagrange(const Eigen::VectorXd &x) : _x(x), _l(x.size()), _y(x.size()) {\n\tfor(int i = 0; i < x.size(); i++) {\n\t\tdouble lambda_i = 1;\n\n\t\tfor(int j = 0; j < x.size(); j++) {\n\t\t\tif(i != j) {\n\t\t\t\tlambda_i *= (x(i) - x(j));\n\t\t\t}\n\t\t}\n\n\t\t_l(i) = 1 / lambda_i;\n\t}\n}\n\n// Evaluate the interpolant at x.\ndouble Lagrange::operator()(double x) const {\n\tdouble p = 0;\n\tdouble omega = 1;\n\n\tfor(int i = 0; i < _x.size(); i++) {\n\t\tomega *= (x - _x(i));\n\t}\n\n\tfor(int i = 0; i < _x.size(); i++) {\n\t\tp += _y(i) * omega * _l(i) / (x - _x(i));\n\t}\n\n\treturn p;\n}\n\nstruct Chebychev {\n\tChebychev(const Eigen::VectorXd &x) : _x(x), _a(x.size()) {};\n\tvoid Interpolate(const Eigen::VectorXd &y);\n\tdouble operator()(double x) const;\n\nprivate:\n\tEigen::VectorXd _x; // nodes\n\tEigen::VectorXd _a; // coefficients\n};\n\nvoid Chebychev::Interpolate(const Eigen::VectorXd &y) {\n\t// the lecture notes model the data output points as [y_0, y_1, ..., y_n]^T\n\tint n = y.size() - 1;\n\tEigen::VectorXcd b(2*n + 2);\n\n\tfor(int k = 0; k < b.size(); k++) {\n\t\t// z_k from lecture notes (5.40)\n\t\tdouble z_k = k > n ? y(2 * n + 1 - k) : y(k);\n\n\t\t// b(k) from lecture notes (5.41)\n\t\tb(k) = z_k * std::exp(-PI * n * k / (n + 1) * I);\n\t}\n\n\tEigen::FFT fft;\n\tEigen::VectorXcd c = fft.inv(b);\n\tEigen::VectorXd beta(c.size());\n\n\t// extract beta's according to lecture notes (5.41) and index substitution k = j + n\n\t// to only deal with non-negative indices\n\tfor(int k = 0; k < c.size(); k++) {\n\t\t// possible error in lecture notes should be j = -n, ..., n + 1 instead of j = 0, ..., 2n + 1\n\t\tbeta(k) = (c(k) * std::exp(PI * (k - n) / (2 * (n + 1)) * I)).real();\n\t}\n\n\t// from lecture notes (5.38)\n\t// we have to acces beta(j + n) because of the substitution we did above\n\t// at the end we are only interested in alpha_j's from j = 0,...,n like in (5.32)\n\tfor(int j = 0; j <= n; j++) {\n\t\tif(j == 0) {\n\t\t\t_a(j) = beta(j + n);\n\t\t}\n\t\telse {\n\t\t\t_a(j) = 2 * beta(j + n);\n\t\t}\n\t}\n}\n\n// Evaluate the interpolant at x.\ndouble Chebychev::operator()(double x) const {\n\t// Clenshaw algorithm from lecture notes (5.35) and (5.36)\n\tint n = _a.size() - 1;\n\tEigen::VectorXd beta(n + 3);\n\tbeta(n + 2) = 0;\n\tbeta(n + 1) = 0;\n\n\tfor(int k = n; k >= 0; k--) {\n\t\tbeta(k) = (k == 0 ? 2 : 1) * _a(k) + 2 * x * beta(k + 1) - beta(k + 2);\n\t}\n\n\treturn 0.5 * (beta(0) - beta(2));\n}\n\n// Runge function\nEigen::VectorXd r(const Eigen::VectorXd &x) {\n\treturn (1.0 / (1.0 + 25.0 * x.array() * x.array())).matrix();\n}\n\nint main() {\n\tint n = 5;\n\n\t/*\n\tEigen::VectorXd x;\n\tx.setLinSpaced(5, -1.0, 1.0);\n\tEigen::VectorXd y = r(x);\n\n\tNewton p(x);\n\tp.Interpolate(y); // correct result: p._a = [0.0384615, 0.198939, 1.5252, -3.31565, 3.31565]\n\n\tLagrange q(x); // correct result: p._l = [0.666667, -2.66667, 4, -2.66667, 0.666667]\n\tq.Interpolate(y);\n\t*/\n\n\t// Use Chebychev nodes instead of linearly spaced nodes\n\tEigen::VectorXd x(n + 1);\n\n\tfor(int i = 0; i < x.size(); i++) {\n\t\tx(i) = std::cos((2 * i + 1) * PI / (2 * (n + 1)));\n\t}\n\n\tEigen::VectorXd y = r(x);\n\n\tNewton p(x);\n\tp.Interpolate(y);\n\n\tLagrange q(x);\n\tq.Interpolate(y);\n\n\tChebychev c(x);\n\tc.Interpolate(y);\n\n\t// Compute difference of p and q.\n\tint m = 22;\n\tdouble offset = 0.08333333333;\n\tx.setLinSpaced(m, -1.0 + offset, 1.0 - offset);\n\n\tdouble norm2 = .0;\n\tfor (int i = 0; i < m; ++i) {\n\t\tdouble d = p(x(i)) - q(x(i));\n\t\tnorm2 += d * d;\n\t}\n\n\t// By uniquenss of the interpolation polynomial, we expect p = q.\n\tstd::cout << \"This number should be close to zero: \" << norm2 << std::endl;\n\n\tnorm2 = 0;\n\tfor(int i = 0; i < m; ++i) {\n\t\tdouble d = c(x(i)) - p(x(i));\n\t\tnorm2 += d * d;\n\t}\n\n\t// By uniquenss of the interpolation polynomial, we expect c = p.\n\tstd::cout << \"This number should be close to zero: \" << norm2 << std::endl;\n\n\tnorm2 = 0;\n\tfor(int i = 0; i < m; ++i) {\n\t\tdouble d = c(x(i)) - q(x(i));\n\t\tnorm2 += d * d;\n\t}\n\n\t// By uniquenss of the interpolation polynomial, we expect c = q.\n\tstd::cout << \"This number should be close to zero: \" << norm2 << std::endl;\n\n\treturn 0;\n}\n", "meta": {"hexsha": "424006f689dba907bbb1f22c6445078db2f4594c", "size": 5280, "ext": "cpp", "lang": "C++", "max_stars_repo_path": "exercise_5/interpolation.cpp", "max_stars_repo_name": "azurite/numCSE18-code", "max_stars_repo_head_hexsha": "f7104305375954a0528d366f8460bf8033d20f0a", "max_stars_repo_licenses": ["MIT"], "max_stars_count": 1.0, "max_stars_repo_stars_event_min_datetime": "2019-01-13T19:08:32.000Z", "max_stars_repo_stars_event_max_datetime": "2019-01-13T19:08:32.000Z", "max_issues_repo_path": "exercise_5/interpolation.cpp", "max_issues_repo_name": "azurite/numCSE18-code", "max_issues_repo_head_hexsha": "f7104305375954a0528d366f8460bf8033d20f0a", "max_issues_repo_licenses": ["MIT"], "max_issues_count": null, "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "exercise_5/interpolation.cpp", "max_forks_repo_name": "azurite/numCSE18-code", "max_forks_repo_head_hexsha": "f7104305375954a0528d366f8460bf8033d20f0a", "max_forks_repo_licenses": ["MIT"], "max_forks_count": null, "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null, "avg_line_length": 23.7837837838, "max_line_length": 95, "alphanum_fraction": 0.5789772727, "num_tokens": 1879, "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9579122696813392, "lm_q2_score": 0.9161096130168221, "lm_q1q2_score": 0.8775526386818374}} {"text": "/*\n2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.\nWhat is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?\n*/\n#include \n#include \n#include \n\nlong long int n_max = 20;\n\nlong long int calc_gcd(long long int a, long long int b) {\n while (b != 0) {\n long long int remainder = b;\n b = a % b;\n a = remainder;\n }\n return a;\n}\n\nlong long int calc_lcm(long long int a, long long int b) {\n return a*b/calc_gcd(a, b);\n}\n\nint main()\n{\n long long int result = 1;\n for (long long int i = 1; i <= n_max; i++) {\n result = calc_lcm(result, i);\n std::cout << \"i: \" << i << \" gcd: \" << calc_gcd(result, i) << std::endl;\n std::cout << \"i: \" << i << \" lcm: \" << result << std::endl;\n }\n\n std::cout << result << std::endl;\n}\n\n", "meta": {"hexsha": "c36a1558ef0254bda346a0f34e26c35436601006", "size": 902, "ext": "cpp", "lang": "C++", "max_stars_repo_path": "problem_005.cpp", "max_stars_repo_name": "JlnZhou/ProjtecEuler", "max_stars_repo_head_hexsha": "6bbc4cbed2bf6596346d6d84e07b5355a36304c9", "max_stars_repo_licenses": ["MIT"], "max_stars_count": null, "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "problem_005.cpp", "max_issues_repo_name": "JlnZhou/ProjtecEuler", "max_issues_repo_head_hexsha": "6bbc4cbed2bf6596346d6d84e07b5355a36304c9", "max_issues_repo_licenses": ["MIT"], "max_issues_count": null, "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "problem_005.cpp", "max_forks_repo_name": "JlnZhou/ProjtecEuler", "max_forks_repo_head_hexsha": "6bbc4cbed2bf6596346d6d84e07b5355a36304c9", "max_forks_repo_licenses": ["MIT"], "max_forks_count": null, "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null, "avg_line_length": 25.0555555556, "max_line_length": 106, "alphanum_fraction": 0.5820399113, "num_tokens": 264, "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9669140197044659, "lm_q2_score": 0.9032941988938414, "lm_q1q2_score": 0.8734078248281695}} {"text": "\n/* \n * Asignatura: Métodos Computacionales en Ciencias\n * Nombre: María Camila Casas Díaz\n * Código: 201813057\n */\n\n#include \n#include \n#include \n#include \n#include \n\nusing namespace Eigen;\nusing namespace std;\n\nMatrixXd LeerMatrizIngresada(string path) {\n\tvector data;\n\tfstream file(path);\n\tstring line, value;\n\tint filas=0;\n\tint columnas=0;\n\t\n\twhile (getline(file,line)) {\n\t\tstringstream ss (line);\n\t\twhile( ss >> value ) { \n\t\t\tdata.push_back(stod(value));\n\t\t}\n\t\tfilas++;\n\t}\n\tfile.close();\n\tcolumnas = data.size()/filas;\n\treturn Map>(data.data(),filas,columnas);\n}\n\ndouble determinante(MatrixXd Matriz) {\n\tdouble det = Matriz.determinant();\n\treturn det;\n}\n\nMatrixXd MatrizFilaElimiadaI(MatrixXd Matriz, int i) {\n\tint filas = Matriz.rows()-1;\n\tint columnas = Matriz.cols();\n\tMatriz.block(i,0,filas-i,columnas) = Matriz.bottomRows(filas-i);\n\tMatriz.conservativeResize(filas,columnas);\n\treturn Matriz;\n}\n\nMatrixXd MatrizColumnaEliminadaJ(MatrixXd Matriz, int j) {\n\tint filas = Matriz.rows();\n\tint columnas = Matriz.cols()-1;\n\tMatriz.block(0,j,filas,columnas-j) = Matriz.rightCols(columnas-j);\n\tMatriz.conservativeResize(filas,columnas);\n\treturn Matriz;\n}\n\nMatrixXd submatriz(MatrixXd Matriz, int i, int j) {\n\treturn MatrizFilaElimiadaI(MatrizColumnaEliminadaJ(Matriz,j),i);\n}\n\ndouble cofactor(MatrixXd Matriz, int i, int j) {\n\treturn pow(-1,i+j)*determinante(submatriz(Matriz,i,j));\n}\n\nMatrixXd Multiplicacion(double k, int i, int n){\n\tMatrixXd Matriz = MatrixXd::Identity(n,n);\n\tMatriz(i,i) = k;\n\treturn Matriz;\n}\n\nMatrixXd Suma(double k, int i, int j, int n){\n\tMatrixXd Matriz = MatrixXd::Identity(n,n);\n\tMatriz(i,j) = k;\n\treturn Matriz;\n}\n\nMatrixXd Intercambio(int i, int j, int n){\n\tMatrixXd Matriz = MatrixXd::Identity(n,n);\n\tMatriz(i,i) = 0;\n\tMatriz(i,j) = 1;\n\tMatriz(j,j) = 0;\n\tMatriz(j,i) = 1;\n\treturn Matriz;\n}\n\nMatrixXd InversaGaussJordan(MatrixXd Matriz) {\n\t\n\tif (determinante(Matriz) == 0) {\n\t\tcout << \"La Matriz es singular, det = \" << determinante(Matriz) << endl; abort();\n\t}\n\tint n = Matriz.rows(), k;\n\tMatrixXd MatrizIdentidad = MatrixXd::Identity(n,n);\n\tMatrixXd Cambios;\n\t\n\tfor (int i=0; i=0; j--) {\n\t\tfor (int i=0; i> archivo;\n\tMatrixXd matriz = LeerMatrizIngresada(archivo);\n\tsaveData(\"InversaGaussJordan.txt\",InversaGaussJordan(matriz));\n\tsaveData(\"InversaCramer.txt\",InversaCramer(matriz));\n\tsaveData(\"InversaEigen.txt\",InversaEigen(matriz));\n\n}\n\n", "meta": {"hexsha": "7eced55b26e76dee076da5393eb45912407d66de", "size": 4152, "ext": "cpp", "lang": "C++", "max_stars_repo_path": "soluciones/mc.casasd/tarea2/solucion.cpp", "max_stars_repo_name": "japeinado/FISI2028-202120", "max_stars_repo_head_hexsha": "6b16a779f3e34bcbf35d8b5e0ea345cf50ffdadd", "max_stars_repo_licenses": ["MIT"], "max_stars_count": 3.0, "max_stars_repo_stars_event_min_datetime": "2021-08-17T19:19:11.000Z", "max_stars_repo_stars_event_max_datetime": "2021-11-08T12:26:41.000Z", "max_issues_repo_path": "soluciones/mc.casasd/tarea2/solucion.cpp", "max_issues_repo_name": "japeinado/FISI2028-202120", "max_issues_repo_head_hexsha": "6b16a779f3e34bcbf35d8b5e0ea345cf50ffdadd", "max_issues_repo_licenses": ["MIT"], "max_issues_count": 12.0, "max_issues_repo_issues_event_min_datetime": "2021-09-18T01:33:58.000Z", "max_issues_repo_issues_event_max_datetime": "2021-10-16T00:11:45.000Z", "max_forks_repo_path": "soluciones/mc.casasd/tarea2/solucion.cpp", "max_forks_repo_name": "japeinado/FISI2028-202120", "max_forks_repo_head_hexsha": "6b16a779f3e34bcbf35d8b5e0ea345cf50ffdadd", "max_forks_repo_licenses": ["MIT"], "max_forks_count": 28.0, "max_forks_repo_forks_event_min_datetime": "2021-09-17T22:38:23.000Z", "max_forks_repo_forks_event_max_datetime": "2021-10-02T19:59:49.000Z", "avg_line_length": 23.3258426966, "max_line_length": 100, "alphanum_fraction": 0.6724470135, "num_tokens": 1323, "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9621075755433747, "lm_q2_score": 0.9019206798249232, "lm_q1q2_score": 0.8677447185987891}} {"text": "\n// http://eigen.tuxfamily.org/dox-devel/group__LeastSquares.html\n\n#include \n#include \n\n#include \n\nusing namespace std;\nusing namespace Eigen;\n\nint main() {\n MatrixXf A = MatrixXf::Random(3, 2);\n cout << \"Here is the matrix A:\\n\" << A << endl;\n VectorXf b = VectorXf::Random(3);\n cout << \"Here is the right hand side b:\\n\" << b << endl;\n cout << \"The least-squares solution is:\\n\"\n << A.bdcSvd(ComputeThinU | ComputeThinV).solve(b) << endl;\n\n // http://textbooks.math.gatech.edu/ila/least-squares.html\n // https://github.com/QBobWatson/ila\n //\n // https://math.stackexchange.com/questions/2591061/exponential-least-squares-equation\n //\n // https://eigen.tuxfamily.org/dox/GettingStarted.html\n // https://eigen.tuxfamily.org/dox/group__TutorialMatrixClass.html\n //\n // points: (0,6), (1,0) and (2,0)\n //\n // 6 = M . 0 + B\n // 0 = M . 1 + B\n // 0 = M . 2 + B\n //\n // 0 1 6\n // A = 1 1 e = M b = 0 where ê = -3\n // 2 1 B 0 5\n //\n // Solution is: M=-3 B=5, for y=Mx + B\n //\n MatrixXf M2{{0, 1}, {1, 1}, {2, 1}};\n VectorXf b2{{6}, {0}, {0}};\n\n // The three methods discussed on this page are the SVD decomposition,\n // the QR decomposition and normal equations.\n // Of these, the SVD decomposition is generally the most accurate but the\n // slowest, normal equations is the fastest but least accurate,\n // and the QR decomposition is in between.\n\n cout << \"The SVD linear least-squares solution is:\\n\"\n << M2.bdcSvd(ComputeThinU | ComputeThinV).solve(b2) << endl;\n // The SVD linear least-squares solution is: \\n -3 \\n 5\n //\n cout << \"The solution using the QR decomposition is:\\n\"\n << M2.colPivHouseholderQr().solve(b2) << endl;\n cout << \"The solution using normal equations is:\\n\"\n << (M2.transpose() * M2).ldlt().solve(M2.transpose() * b2) << endl;\n // least squares solution is: y = -3x + 5\n\n // ( − 1,1 / 2 ) , ( 1, − 1 ) , ( 2, − 1 / 2 ) , ( 3,2 )\n // y = Bx²+Cx+D\n\n // 1/2 = B(-1)² + C(-1) + D\n // -1 = B(1)² + C(1) + D\n // -1/2 = B(2)² + C(2) + D\n // 2 = B(3)² + C(3) + D\n //\n // 1 -1 1 B 1/2\n // A = 1 1 1 x= C b = -1\n // 4 2 1 D -1/2\n // 9 3 1 2\n //\n // best-fit parabola: B=53/88 C=-379/440 D=-41/44\n\n MatrixXf M3{{1, -1, 1}, {1, 1, 1}, {4, 2, 1}, {9, 3, 1}};\n VectorXf b3{{1 / 2.0}, {-1}, {-1 / 2.0}, {2}};\n cout << \"The solution using normal equations is:\\n\"\n << (M3.transpose() * M3).ldlt().solve(M3.transpose() * b3) << endl;\n cout << \"B=53/88=\" << 53 / 88.0 << \" C=-379/440=\" << -379 / 440.0\n << \" D=-82=\" << -82 << std::endl;\n\n // BASIC EXPONENTIAL: y = a*e^{-bx}\n /*\n1 8558\n2 5411\n3 2830\n4 2267\n5 760\n6 549\n7 249\n8 67\n9 47\n10 43\n*/\n\n int N = 10;\n\n // auto ones = MatrixXd::Ones(N, 1);\n\n // YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY\n // MatrixXf = ... MatrixXd ...\n\n MatrixXf M4 = MatrixXf::Zero(N, 2);\n for (unsigned i = 0; i < N; i++) {\n // M4.topRows(i) = {i, 1};\n // M4.col(1) = ones.col(0);\n M4(i, 0) = i;\n M4(i, 1) = 1.0;\n }\n\n //{{0, 1}, {1, 1}, {2, 1}};\n // VectorXf b2{{6}, {0}, {0}};\n\n // https://gist.github.com/gocarlos/c91237b02c120c6319612e42fa196d77\n // https://math.stackexchange.com/questions/2591061/exponential-least-squares-equation\n\n // example MI: https://mycurvefit.com\n\n VectorXf v = VectorXf::Zero(N);\n std::vector vf = {8558, 5411, 2830, 2267, 760, 549, 249, 67, 47, 43};\n\n // BASIC EXPONENTIAL: y = a*e^{-bx}\n // ln(y) = ln(a) - bx\n // ZY = ln(y)\n // ZA = ln(a)\n // ZB = -b\n // ZY = ZB x + ZA\n\n for (unsigned i = 0; i < N; i++)\n v[i] = ::log(vf[i]); // log natural\n\n // VectorXf res = M4.bdcSvd(ComputeThinU | ComputeThinV).solve(v);\n VectorXf res = (M4.transpose() * M4).ldlt().solve(M4.transpose() * v);\n\n cout << \"The solution using normal equations is:\\n\" << res << endl;\n double ZB = res[0];\n double ZA = res[1];\n double realA = ::exp(ZA);\n double realB = -ZB;\n\n cout << \"y = \" << realA << \" * e^{\" << -realB << \" * x }\" << endl;\n double ss_res = 0;\n double ss_tot = 0;\n double mean = 0;\n // https://en.wikipedia.org/wiki/Coefficient_of_determination\n for (unsigned i = 0; i < N; i++)\n mean += vf[i];\n mean = mean / N;\n\n for (unsigned i = 0; i < N; i++) {\n double yest = realA * ::exp(-realB * i);\n double ydiff = (vf[i] - mean) * (vf[i] - mean);\n double ydiff2 = (yest - vf[i]) * (yest - vf[i]);\n ss_res += ydiff2;\n ss_tot += ydiff;\n std::cout << \"y[\" << i << \"] ~~ \" << yest << \" y=\" << vf[i]\n << \" (mean=\" << mean << \") diff2=\" << ydiff2\n << \" / diff=\" << ydiff << \"(R2 = \" << (1 - (ydiff2 / ydiff))\n << \")\" << std::endl;\n std::cout << \"SS_res = \" << ss_res << std::endl;\n std::cout << \"SS_tot = \" << ss_tot << std::endl;\n double r2 = 1 - (ss_res / ss_tot);\n std::cout << \"r2 = \" << r2 << std::endl;\n }\n\n return 0;\n}\n", "meta": {"hexsha": "33f3417803ac1afb3ab1a5ff8a4727173e3ab775", "size": 5051, "ext": "cpp", "lang": "C++", "max_stars_repo_path": "sometests.cpp", "max_stars_repo_name": "igormcoelho/optstats", "max_stars_repo_head_hexsha": "6d95cf06fbb96b1cc047fa570690c6eb3d21ece4", "max_stars_repo_licenses": ["MIT"], "max_stars_count": null, "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "sometests.cpp", "max_issues_repo_name": "igormcoelho/optstats", "max_issues_repo_head_hexsha": "6d95cf06fbb96b1cc047fa570690c6eb3d21ece4", "max_issues_repo_licenses": ["MIT"], "max_issues_count": null, "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "sometests.cpp", "max_forks_repo_name": "igormcoelho/optstats", "max_forks_repo_head_hexsha": "6d95cf06fbb96b1cc047fa570690c6eb3d21ece4", "max_forks_repo_licenses": ["MIT"], "max_forks_count": null, "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null, "avg_line_length": 30.245508982, "max_line_length": 118, "alphanum_fraction": 0.5341516531, "num_tokens": 1949, "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9525741227833249, "lm_q2_score": 0.9099069987088003, "lm_q1q2_score": 0.8667538611094434}} {"text": "#include \"iostream\"\nusing namespace std;\n\n#include \n// include Core and Dense compuation parts from Eigen\n#include \n#include \n\nusing namespace Eigen;\n\n# define MATRIX_SIZE 50\n\n/**************************************************\n * A demo for the basic types in Eigen\n * to install Eigen, use: sudo apt-get install libeigen3-dev\n * Official documentation: https://eigen.tuxfamily.org/index.php?title=Main_Page\n * https://eigen.tuxfamily.org/dox/GettingStarted.html\n * *************************************************/\n\nint main(int argc, char** argv){\n // 1. Declaration\n // All metrices and vector in Eigen use type Eigen::Matrix, which is a template class. \n // The first 3 generic parameters are: data type, row, column.\n // Declare a 2*3 float matrix with\n Matrix matrix_23;\n\n // Meanwhile, Eigen also provides many built-in types via typedef\n // but their bottom is still Eigen::Matrix<>\n // e.g. Vector3d is the same as Eigen::Matrix\n Vector3d v_3d;\n Matrix vd_3d;\n\n // Matrix3d is Eigen::Matrix\n Matrix3d matrix_33 = Matrix3d::Zero(); // initialized to 0\n\n // Eigen also provides matrix with dynamic size\n // the size is not deteremined during complie time, and can be stored in some runtime variable \n // fixed size matrix would have better performance, so it's recommended if you know the dim.\n Matrix matrix_dynamic;\n // shortcut of dynmaic size matrix: MatrixXd\n MatrixXd matrix_x;\n\n\n // 2. Operation\n // 2.1 initialization\n matrix_23 << 1, 2, 3, 4, 5, 6;\n // To print the matrix, can put it to the output stream directly\n cout << \"matrix 2x3 from 1 to 6: \\n\" << matrix_23 << endl;\n\n // 2.2 Access the element using (i, j) instead of [][] in array\n cout << \"print matrix 2x3 one by one\" << endl;\n for(int i = 0; i < 2; i++){\n for(int j = 0; j < 3; j++)\n cout << matrix_23(i, j) << \"\\t\";\n cout << endl;\n }\n\n // 2.3 Matrix multiplication\n v_3d << 3, 2, 1;\n vd_3d << 4, 5, 6;\n // Note: cannot mix two matrices with different types (e.g. matrix_23: float, v_3d: double)\n // the following is wrong: Matrix result_wrong_type = matrix_23 * v_3d\n // Need to do explicit casting first: matrix.cast()\n Matrix result = matrix_23.cast() * v_3d;\n cout << \"[1,2,3;4,5,6]∗[3,2,1]=\" << result.transpose() << endl;\n\n // both matrix_23 and vd_3d contain float, so the following is ok\n Matrix result2 = matrix_23 * vd_3d;\n cout << \"[1,2,3;4,5,6]∗[4,5,6]: \" << result2.transpose() << endl;\n\n // 2.4 Other matrix operations\n // + - * / are allowed. Note that / is usually matrix / scalar\n // The operations reduing the matrix into a value in Eigen are called \"Reduction operations\"\n matrix_33 = Matrix3d::Random();\n cout << \"random matrix: \\n\" << matrix_33 << endl;\n cout << \"transpose: \\n\" << matrix_33.transpose() << endl;\n cout << \"sum: \" << matrix_33.sum() << endl;\n cout << \"trace: \" << matrix_33.trace() << endl;\n cout << \"times 10: \\n\" << 10 * matrix_33 << endl;\n cout << \"inverse: \\n\" << matrix_33.inverse() << endl;\n cout << \"det: \" << matrix_33.determinant() << endl;\n\n // Eigenvalues\n // Real symmetric matrix is guaranteed to be diagonalizable. Here use A^T*A as an example\n SelfAdjointEigenSolver eigen_solver(matrix_33.transpose() * matrix_33);\n cout << \"Eigen values = \\n\" << eigen_solver.eigenvalues() << endl;\n cout << \"Eigen vectors = \\n\" << eigen_solver.eigenvectors() << endl;\n\n // Solve equations\n // Solve matrixNN * x = v_Nd, N = 60 as defined before by macro\n\n // Method 1: Take the inverse directly and do multiplication, which is time-consumptive\n Matrix matrix_NN = MatrixXd::Random(MATRIX_SIZE, MATRIX_SIZE); // dynamic size, determined during runtime\n // We still consider A^T*A as an example\n matrix_NN = matrix_NN * matrix_NN.transpose();\n Matrix v_Nd = MatrixXd::Random(MATRIX_SIZE, 1);\n // do the time counting\n clock_t time_start = clock(); \n // Take the inverse directly\n Matrix x = matrix_NN.inverse() * v_Nd;\n cout << \"time of normal inverse is \" << 1000 * (clock() - time_start) / (double) CLOCKS_PER_SEC << \"ms\" << endl;\n cout << \"x = \" << x.transpose() << endl;\n\n // Method 2: Use matrix decomposition to solve (e.g. QR decomposition)\n time_start = clock();\n x = matrix_NN.colPivHouseholderQr().solve(v_Nd);\n cout << \"time of QR decomp is: \" << 1000*(clock() - time_start) / (double) CLOCKS_PER_SEC << \"ms\" << endl;\n cout << \"x = \" << x.transpose() << endl;\n\n // Method 3: for positive definite matrix, can use Cholesky decomp. to solve\n time_start = clock();\n x = matrix_NN.ldlt().solve(v_Nd);\n cout << \"time of ldlt decomp is: \" << 1000*(clock() - time_start) / (double) CLOCKS_PER_SEC << \"ms\" << endl;\n cout << \"x = \" << x.transpose() << endl;\n\n\n // 3. Other stuffs\n // 3.0 Partial reductions\n MatrixXf mat(2, 4);\n mat << 1, 2, 6, 9,\n 3, 1, 7, 2;\n // take the maxCoeff() at each column\n cout << \"Column's maximum: \" << endl << mat.colwise().maxCoeff() << endl;\n cout << \"Column's maximum: \" << endl << mat.rowwise().maxCoeff().transpose() << endl;\n\n // 3.1 Broadcasting\n // similar to partial reductions. The difference is that broadcasting constructs an expression where a vector\n // (column or row) is interpreted as a matrix by replicating it in one direction\n VectorXf v(2);\n v << 0,\n 1;\n // add v to each column of m\n mat.colwise() += v;\n\n cout << \"Broadcasting result: \" << endl;\n cout << mat << endl;\n\n // example of broadcasting: find the nearest neighbor\n // Broadcasting can also be combined with other operations, such as Matrix or Array operations, reductions and partial reductions\n // finds the nearest neighbour of a vector v within the columns of matrix m. \n // Note: use squaredNorm() to compute the squared Euclidean distance\n\n // For more info, refer to: https://eigen.tuxfamily.org/dox/group__TutorialReductionsVisitorsBroadcasting.html\n MatrixXf m(2, 4);\n v << 2,\n 3;\n m << 1, 23, 6, 9,\n 3, 11, 7, 2;\n MatrixXf::Index index;\n // find the nearest neighbor\n (m.colwise() - v).colwise().squaredNorm().minCoeff(&index);\n\n cout << \"Nearest neighbor is column \" << index << \":\" << endl;\n cout << m.col(index) << endl;\n\n return 0;\n}\n\n\n\n", "meta": {"hexsha": "3a09d15986735e8b64239eb7dba25c6953cc52ad", "size": 6633, "ext": "cpp", "lang": "C++", "max_stars_repo_path": "ch3/useEigen/eigenMatrix.cpp", "max_stars_repo_name": "henryxuy/slam-codeInBook-en", "max_stars_repo_head_hexsha": "ec3c8ec8d5facfdeb0832be121de105cea73790b", "max_stars_repo_licenses": ["MIT"], "max_stars_count": null, "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "ch3/useEigen/eigenMatrix.cpp", "max_issues_repo_name": "henryxuy/slam-codeInBook-en", "max_issues_repo_head_hexsha": "ec3c8ec8d5facfdeb0832be121de105cea73790b", "max_issues_repo_licenses": ["MIT"], "max_issues_count": null, "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "ch3/useEigen/eigenMatrix.cpp", "max_forks_repo_name": "henryxuy/slam-codeInBook-en", "max_forks_repo_head_hexsha": "ec3c8ec8d5facfdeb0832be121de105cea73790b", "max_forks_repo_licenses": ["MIT"], "max_forks_count": null, "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null, "avg_line_length": 41.198757764, "max_line_length": 144, "alphanum_fraction": 0.6268656716, "num_tokens": 1923, "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9539660989095221, "lm_q2_score": 0.9073122238669025, "lm_q1q2_score": 0.865545102695232}} {"text": "/*\n\tStudent T Test Implementation.\n*/\n\n#ifndef T_TEST_HPP\n#define T_TEST_HPP\n\n#include \"iostream\"\n#include \n\n#include \n\nusing namespace std;\nusing namespace boost::math;\n\ntemplate\nT mean(std::vector data)\n{\n\tif(data.empty())\n\t\treturn 0;\n\n\tT sum;\n\tfor (typename std::vector::const_iterator it = data.begin(); it != data.end(); ++it)\n\t{\n\t\tsum += *it;\n\t}\n\n\treturn (sum/(int)data.size());\n}\n\ntemplate\nT sample_variance(std::vector data, T mean)\n{\n\tT _sample_variance, _sample_square_dist;\n\tfor (typename std::vector::const_iterator it = data.begin(); it != data.end(); ++it)\n\t{\n\t\t_sample_square_dist += pow((*it - mean), 2);\n\t}\n\n\t_sample_variance = _sample_square_dist / ((int)data.size() - 1);\n\n\treturn _sample_variance;\n}\n\n\ntemplate\nstd::pair t_statistics(std::vector sample1, std::vector sample2)\n{\n\tT _mean_diff, _t_statistic, _degrees_of_freedom, _sample_1_mean, _sample_2_mean, _sample_1_variance, \n\t\t_sample_2_variance, _sample_1_avg_variance, _sample_2_avg_variance, _sample_avg_variance;\n\tint _sample_1_size, _sample_2_size;\n\n\t_sample_1_size = (int)sample1.size(); \n\t_sample_2_size = (int)sample2.size();\n\n\t_sample_1_mean = mean(sample1);\n\t_sample_2_mean = mean(sample2);\n\n\t_sample_1_variance = sample_variance(sample1, _sample_1_mean);\n\t_sample_2_variance = sample_variance(sample2, _sample_2_mean);\n\n\t_mean_diff = abs(_sample_1_mean - _sample_2_mean);\n\n\t_sample_1_avg_variance = _sample_1_variance/_sample_1_size;\n\t_sample_2_avg_variance = _sample_2_variance/_sample_2_size;\n\t_sample_avg_variance = (_sample_1_avg_variance + _sample_2_avg_variance);\n\t\n\t_t_statistic = _mean_diff/( sqrt(_sample_avg_variance) );\n\t_degrees_of_freedom = pow(_sample_avg_variance, 2) / ( (pow(_sample_1_avg_variance, 2)/(_sample_1_size - 1)) + (pow(_sample_2_avg_variance, 2)/(_sample_2_size - 1)) );\n\n\treturn std::pair(_t_statistic, _degrees_of_freedom);\n}\n\n\ntemplate\ndouble similarity_probability(std::vector sample1, std::vector sample2)\n{\n\tif(sample1.empty() || sample2.empty())\n\t{\n\t\tstd::cout << \"Empty sample(s) sent for calculating similarity.\" << endl;\n\t\treturn (T)0;\n\t}\n\n\tstd::pair _t_statistics = t_statistics(sample1, sample2);\n\n\tstudents_t dist(_t_statistics.second);\n\n \tdouble _similarity_probability = cdf(complement(dist, fabs(_t_statistics.first)));\n\n \treturn (2*_similarity_probability);\n}\n\n#endif", "meta": {"hexsha": "d57a4ef8dcfe9ab579a6c73d327624b232ba2e42", "size": 2456, "ext": "hpp", "lang": "C++", "max_stars_repo_path": "data/train/cpp/d57a4ef8dcfe9ab579a6c73d327624b232ba2e42t_test.hpp", "max_stars_repo_name": "harshp8l/deep-learning-lang-detection", "max_stars_repo_head_hexsha": "2a54293181c1c2b1a2b840ddee4d4d80177efb33", "max_stars_repo_licenses": ["MIT"], "max_stars_count": 84.0, "max_stars_repo_stars_event_min_datetime": "2017-10-25T15:49:21.000Z", "max_stars_repo_stars_event_max_datetime": "2021-11-28T21:25:54.000Z", "max_issues_repo_path": "data/train/cpp/d57a4ef8dcfe9ab579a6c73d327624b232ba2e42t_test.hpp", "max_issues_repo_name": "vassalos/deep-learning-lang-detection", "max_issues_repo_head_hexsha": "cbb00b3e81bed3a64553f9c6aa6138b2511e544e", "max_issues_repo_licenses": ["MIT"], "max_issues_count": 5.0, "max_issues_repo_issues_event_min_datetime": "2018-03-29T11:50:46.000Z", "max_issues_repo_issues_event_max_datetime": "2021-04-26T13:33:18.000Z", "max_forks_repo_path": "data/train/cpp/d57a4ef8dcfe9ab579a6c73d327624b232ba2e42t_test.hpp", "max_forks_repo_name": "vassalos/deep-learning-lang-detection", "max_forks_repo_head_hexsha": "cbb00b3e81bed3a64553f9c6aa6138b2511e544e", "max_forks_repo_licenses": ["MIT"], "max_forks_count": 24.0, "max_forks_repo_forks_event_min_datetime": "2017-11-22T08:31:00.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-27T01:22:31.000Z", "avg_line_length": 26.4086021505, "max_line_length": 168, "alphanum_fraction": 0.7414495114, "num_tokens": 696, "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9603611631680358, "lm_q2_score": 0.8991213691605412, "lm_q1q2_score": 0.8634812439162542}} {"text": "/* ------------------------------------------------------------\n * @file: fact_cholesky.cpp\n * @dependencias: armadillo, math\n * @version 3.6\n * ------------------------------------------------------------*/\n\n// Compile and run:\n// [1] $ g++ fact_cholesky.cpp -o fc.out -std=c++11 \n// [2] $ ./fc.out\n\n#include \n#include \n\nusing namespace std;\nusing namespace arma;\n\n\n/**\n * @brief Verifica que una matriz sea simétrica.\n * @param A Matriz de tamaño nxn.\n * @return True si es simétrica, false si no lo es.\n */ \nbool is_simetric(mat A){\n return approx_equal(A, A.t(),\"absdiff\", 0);\n}\n\n\n// Source: https://www.tutorialspoint.com/cplusplus-program-to-compute-determinant-of-a-matrix\n// Nota: Se implementó porque la función de armadillo arma::det(mat) presentó problemas.\n/**\n * @brief Calcula el determinante de una matriz cuadrada.\n * @param A Matriz de tamaño nxn.\n * @param n Tamaño de la matriz.\n * @return El valor del determinante de la matriz.\n */ \ndouble determinant(mat A, int n) {\n double det = 0;\n mat sub_matrix(10,10);\n \n if (n == 1){\n // Caso 0: Si la matriz es de orden 1\n return A(0,0);\n } else if (n == 2){\n // Caso base: Determinante de una matriz 2x2\n return (A(0,0) * A(1,1) - A(1,0) * A(0,1));\n } else {\n // Caso de reducciones de la matriz\n for (int x = 0; x < n; x++){\n int sub_i = 0;\n for (int i = 1; i < n; i++){\n int sub_j = 0;\n for (int j = 0; j < n; j++){\n if (j == x)\n continue;\n sub_matrix(sub_i,sub_j) = A(i,j);\n sub_j++;\n }\n sub_i++;\n }\n det += (pow(-1,x)* A(0,x) * determinant(sub_matrix, n-1));\n }\n }\n return det;\n}\n\n\n/**\n * @brief Determina si una matrix dada es Definida Positiva.\n * @param A Matriz de tamaño nxn.\n * @return True si es definida positiva, false si no es.\n */ \nbool is_positive_definite(mat A){\n int n = A.n_rows;\n\n for (int i = 0; i < n; i++){\n mat A_t = A.submat(0,0,i,i);\n double det_result = determinant(A_t,i+1);\n //cout << \"Det(A_t): \"<< det_result<< endl;\n if (det_result < 0){\n return false;\n }\n }\n return true;\n}\n\n\n\n/**\n * @brief Resuelve un sistema de ecuaciones por el método de la sustitución\n * hacia atrás.\n * @param A Matriz de tamaño nxn. Debe ser triangular superior.\n * @param b Vector de tamaño n.\n * @return El vector con la solución al sistema de ecuaciones.\n */\nvec back_substitution(mat A, vec b){\n int n = A.n_rows;\n vec x(n);\n x.zeros();\n\n for (int i = n-1; i >= 0; i--){\n double sum = 0;\n for (int j = i+1; j < n; j++){\n sum += A(i,j) * x(j);\n }\n x(i) = (1/A(i,i)) * (b(i) - sum);\n } \n\n return x;\n}\n\n\n/**\n * @brief Resuelve un sistema de ecuaciones por el método de la sustitución\n * hacia adelante.\n * @param A Matriz de tamaño nxn. Debe ser triangular inferior.\n * @param b Vector de tamaño n.\n * @return El vector con la solución al sistema de ecuaciones.\n */\nvec forward_substitution(mat A, vec b){\n int n = A.n_rows;\n vec x(n);\n x.zeros();\n\n for (int i = 0; i < n; i++){\n double sum = 0;\n for (int j = 0; j < i; j++){\n sum += A(i,j) * x(j);\n }\n x(i) = (1/A(i,i)) * (b(i) - sum);\n } \n\n return x;\n}\n\n\n/**\n * @brief Soluciona un sistema de ecuaciones utilizando la Factorización de Cholesky.\n * @param A Matriz de coeficientes. Deber ser cuadrada, simétrica y positiva definida.\n * @param b Vector de términos independientes.\n */ \nvoid fact_cholesky(mat A, vec b){\n\n if (is_simetric(A) && is_positive_definite(A)){\n int n = A.n_rows;\n mat L(n,n);\n L.zeros();\n\n // Calcular la matriz L\n for (int i = 0; i < n; i++){\n\n for (int j = 0; j < i+1; j++){\n double sum = 0;\n\n if (i == j){\n for (int k = 0; k < j; k++)\n sum += pow(L(j,k),2);\n L(j,j) = sqrt(A(j,j) - sum);\n } else {\n for (int k = 0; k < j; k++)\n sum += L(i,k) * L(j,k);\n L(i,j) = (A(i,j) - sum) / L(j,j);\n }\n }\n }\n\n // Resolve Ly=b, para luego solucionar L_t x = y.\n vec y = forward_substitution(L,b);\n vec x = back_substitution(L.t(),y);\n\n // Presentar la solución del Sistema.\n x.print(\"x: \");\n\n } else\n cout<< \"[Error 504] El sistema no cumple con las condiciones para resolverse por Cholesky.\"<< endl;\n}\n\n\nint main(int argc, char const *argv[]){\n\n // Test Cholesky\n // ----------------------------\n mat A(4,4);\n vec b(4);\n\n A = {{ 25, 15, -5,-10},\n { 15, 10, 1, -7},\n { -5, 1, 21, 4},\n {-10, -7, 4, 18}};\n\n b = {-25, -19, -21, -5};\n\n cout << \"Factorización Cholesky\" << endl;\n A.print(\"A:\\n\");\n b.print(\"b:\\n\");\n fact_cholesky(A,b);\n\n // Solución del ejemplo -> x = [-1, -1, -1, -1]\n\n return 0;\n}\n", "meta": {"hexsha": "38adca7b3d8377a08cc7dcb4c515ebd5357cbd2d", "size": 5159, "ext": "cpp", "lang": "C++", "max_stars_repo_path": "Catalogo/03. Sistemas de Ecuaciones/C++/fact_cholesky.cpp", "max_stars_repo_name": "ce-box/CE3102-Numerical-Methods-Catalog", "max_stars_repo_head_hexsha": "f9b70a719286a5aea9d826b0941d5e5d9c0514e4", "max_stars_repo_licenses": ["MIT"], "max_stars_count": null, "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "Catalogo/03. Sistemas de Ecuaciones/C++/fact_cholesky.cpp", "max_issues_repo_name": "ce-box/CE3102-Numerical-Methods-Catalog", "max_issues_repo_head_hexsha": "f9b70a719286a5aea9d826b0941d5e5d9c0514e4", "max_issues_repo_licenses": ["MIT"], "max_issues_count": null, "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "Catalogo/03. Sistemas de Ecuaciones/C++/fact_cholesky.cpp", "max_forks_repo_name": "ce-box/CE3102-Numerical-Methods-Catalog", "max_forks_repo_head_hexsha": "f9b70a719286a5aea9d826b0941d5e5d9c0514e4", "max_forks_repo_licenses": ["MIT"], "max_forks_count": null, "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null, "avg_line_length": 25.6666666667, "max_line_length": 108, "alphanum_fraction": 0.4888544292, "num_tokens": 1536, "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9609517072737737, "lm_q2_score": 0.8976953003183443, "lm_q1q2_score": 0.8626418314525559}} {"text": "#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\nusing namespace arma;\n\nvec ConjugateGradient(mat A, vec b, vec x0, int dim){\n int IterMax, i;\n const double tolerance = 1.0e-14;\n vec x(dim),r(dim),p(dim),t(dim);\n double c,alpha,d;\n\n IterMax = dim;\n x = x0;\n r = b - A*x;\n p = r;\n c = dot(r,r);\n i = 0;\n while (i <= IterMax || sqrt(dot(p,p)) < tolerance ){\n if(sqrt(dot(p,p))\n#include \n#include \n#include \n#include \n\n//a namespace to hold various GCD algorithms. I was thinking about making the namespace\n//a non-constructable class because some algorithms use sub-functions which should not\n//be accessible anywhere except the function that calls it. However, there is a low chance of\n//name clashing. Perhaps if there was risk, we should just put sub-functions into another namespace..\n//NOTE: look up later, \"does inlining actually do anything now-adays?\"\nnamespace GCD{\n\t\n\t//Euclidean algorithms\n\ttemplate IntegerType Euclid_Recursive(IntegerType a, IntegerType b);\n\ttemplate IntegerType Euclid_Iterative(IntegerType a, IntegerType b);\n\ttemplate IntegerType Euclid_Unrolled(IntegerType a, IntegerType b);\n\ttemplate IntegerType inline Euclid_Bit(IntegerType a, IntegerType b);\n\t\n\t//Binary algorithms\n\ttemplate IntegerType Binary_Recursive(IntegerType a, IntegerType b);\n\ttemplate IntegerType Binary_Iterative(IntegerType a, IntegerType b);\n\ttemplate IntegerType Binary_Iterative_V2(IntegerType a, IntegerType b);\n\t\n\t//Counting Trailing Zeros algorithm\n\ttemplate IntegerType CTZ(IntegerType a, IntegerType b);\n\t\n\t//Brute Force algorithms\n\ttemplate IntegerType Brute_Force(IntegerType a, IntegerType b);\n\t\n\t//Human algorithm\n\ttemplate IntegerType Factor(IntegerType a, IntegerType b);\n\t\n\t//Boost implementation (looks similar to Euclid_Unrolled)\n\ttemplate inline IntegerType Boost(IntegerType a, IntegerType b);\n\t\n\t//utility functions\n\ttemplate void Sieve(std::vector & primes, IntegerType n);\n\ttemplate void Factor(IntegerType n, std::vector & factors, const std::vector & primes);\n\ttemplate void Common_Factors(const std::vector & A, const std::vector & B, std::vector & common);\n\ttemplate IntegerType School(IntegerType a, IntegerType b, const std::vector & primes);\n\ttemplate IntegerType Count_Trailing_Zeros(IntegerType n);\n\n};\n\n//Euclidean algorithms\ntemplate IntegerType GCD::Euclid_Recursive(IntegerType a, IntegerType b){\n if (b==0) {\n return a;\n } else {\n return Euclid_Recursive(b, a%b);\n }\n}\ntemplate IntegerType GCD::Euclid_Iterative(IntegerType a, IntegerType b){\n\tIntegerType c;\n\twhile (b != 0){\n\t\tc = b;\n\t\tb = a%b;\n\t\ta = c;\n\t}\n\treturn a;\n}\ntemplate IntegerType GCD::Euclid_Unrolled(IntegerType a, IntegerType b){\n\t\n\twhile (b != 0){\n\t\t\n\t\ta = a%b;\n\t\tif(a==0){return b;}\n\t\t\n\t\tb = b%a;\n\t\tif(b==0){return a;}\n\t}\n\t\n\treturn a;\n}\ntemplate inline IntegerType GCD::Euclid_Bit(IntegerType a, IntegerType b){\n\twhile(b) b ^= a ^= b ^= a %= b;\n\treturn a;\n}\n\n//Binary algorithms\ntemplate IntegerType GCD::Binary_Recursive(IntegerType a, IntegerType b){\n\t\n if (a == b){return a;}\n if (a == 0){return b;}\n if (b == 0){return a;}\n \n if (~a & 1) {\n if (b & 1){\n\t\t\treturn Binary_Recursive(a >> 1, b);\n\t\t}\n else {\n\t\t\treturn Binary_Recursive(a >> 1, b >> 1) << 1;\n\t\t}\n }\n \n if (~b & 1){\n\t\treturn Binary_Recursive(a, b >> 1);\n\t}\n\t\n if (a > b){\n\t\treturn Binary_Recursive((a - b) >> 1, b);\n\t}\n \n return Binary_Recursive((b - a) >> 1, a);\n}\ntemplate IntegerType GCD::Binary_Iterative(IntegerType a, IntegerType b){\n\n\t//if (a == 0 && b == 0){return 0;} //convention-- *NEVERMIND already covered\n\tif (a == 0){ return b;}\n\tif (b == 0){ return a;}\n\t\n\tIntegerType c;\n\tfor (c = 0; ((a | b) & 1) == 0; ++c) {\n\t\ta >>= 1;\n\t\tb >>= 1;\n\t}\n\t\n\twhile ((a & 1) == 0){\n\t\ta >>= 1;\n\t}\n\t\n\tdo {\n\t\t\n\t\twhile ((b & 1) == 0){\n\t\t\tb >>= 1;\n\t\t}\n\t\n\t\tif (a > b) {\n\t\t\tIntegerType d = b; b = a; a = d;} //inline swap\n\t\tb = b - a;\n\t\t} while (b != 0);\n\t\n\treturn a << c;\n}\ntemplate IntegerType GCD::Binary_Iterative_V2(IntegerType a, IntegerType b){\n\t\n\tIntegerType c = 0;\n\t\n\twhile ( a && b && a!=b ) {\n\t\tbool ea = !(a & 1);\n\t\tbool eb = !(b & 1);\n\t\t\n\t\tif ( ea && eb ) {\n\t\t\t++c;\n\t\t\ta >>= 1;\n\t\t\tb >>= 1;\n\t\t}\n\t\telse if ( ea && !eb ) a >>= 1;\n\t\telse if ( !ea && eb ) b >>= 1;\n\t\telse if ( a>=b ) a = (a-b)>>1;\n\t\telse {\n\t\tIntegerType tmp = a;\n\t\ta = (b-a)>>1;\n\t\tb = tmp;\n\t\t}\n\t}\n \n\treturn !a? b< IntegerType GCD::CTZ(IntegerType a, IntegerType b){\n \n\tif (a == 0){return b;}\n if (b == 0){return a;}\n \n IntegerType c = Count_Trailing_Zeros(a|b);\n \n a >>= Count_Trailing_Zeros(a);\n \n while (true){\n b >>= Count_Trailing_Zeros(b);\n if (a == b){break;}\n if (a > b){IntegerType d = b; b = a; a = d;} //inline swap\n if (a == 1){break;}\n b -= a;\n }\n \n return a << c;\n}\n\t\n//Brute Force algorithms\ntemplate IntegerType GCD::Brute_Force(IntegerType a, IntegerType b){\n\t\n IntegerType c = std::min(a,b);\n\tif (c == 0){return std::max(a,b);} //<- fix a floating point error when a number is zero\n while (a%c != 0 or b%c !=0) {\n --c;\n }\n\n return c;\n}\n\t\n//human algorithm\ntemplate IntegerType GCD::Factor(IntegerType a, IntegerType b){\n IntegerType g;\n\tif (a == 0 && b ==0){return 0;}\n std::vector primeList;\n Sieve(primeList, std::max(a,b));\n return School(a,b,primeList);\n}\n\n//Boost implementation (looks similar to Euclid_Unrolled)\ntemplate inline IntegerType GCD::Boost(IntegerType a, IntegerType b){\n\treturn boost::math::gcd(a,b);\n}\n\n//utility functions\ntemplate void GCD::Sieve(std::vector & primes, IntegerType n){\n using namespace std;\n\tIntegerType i,j;\n vector A;\n\n primes.erase(primes.begin(), primes.end());\n\n for(i=0;i<=n;i++) {\n A.push_back(true);\n }\n\n for(i=2;i<=sqrt(n);i++) {\n if(A[i]) {\n primes.push_back(i);\n for(j=i*i;j<=n;j+= i) {\n\t A[j] = false;\n\t }\n }\n }\n\n for(j=i;j<=n;j++) {\n if(A[j]) {\n\t primes.push_back(j);\n\t}\n }\n\n return;\n}\ntemplate void GCD::Factor(IntegerType n, std::vector & factors, const std::vector & primes){\n IntegerType i;\n IntegerType num;\n\n factors.erase(factors.begin(), factors.end());\n\n\n i = 0;\n num = n;\n\n while(num > 1) {\n if (num%primes[i] == 0) {\n factors.push_back(primes[i]);\n\t num = num/primes[i];\n } else {\n i++;\n }\n }\n\n\n\n return;\n}\ntemplate void GCD::Common_Factors(const std::vector & A, const std::vector & B, std::vector & common){\n\n IntegerType i,j;\n\n common.erase(common.begin(),common.end());\n\n i = 0;\n j = 0;\n while (i < A.size() and j < B.size()) {\n if (A[i] == B[j]) {\n\t // same, common, push it and move both\n common.push_back(A[i]);\n\t i++;\n\t j++;\n\t} else if (A[i] > B[j]) {\n\t // A is too big, so bump B;\n\t j++;\n\t} else {\n\t i++;\n\t}\n }\n\n return;\n}\ntemplate IntegerType GCD::School(IntegerType a, IntegerType b, const std::vector & primes){\n\tusing namespace std;\n\tvector aFactors, bFactors, common;\n IntegerType gcd=1;\n IntegerType i;\n\n Factor(a, aFactors,primes);\n Factor(b, bFactors,primes);\n Common_Factors(aFactors, bFactors, common);\n\n for(i=0;i IntegerType GCD::Count_Trailing_Zeros(IntegerType n){\n \n\tif (n == 0) {\n return -1;\n }\n IntegerType count = 0;\n while (n % 2 == 0) {\n count++;\n n >>= 1;\n }\n \n return count;\n}\n", "meta": {"hexsha": "2fb9fcb26724cf66982e0b16ef6a8ef04c6ff358", "size": 8119, "ext": "hpp", "lang": "C++", "max_stars_repo_path": "source/code/scratch/old_repos/edinboro/CSCI-385/GCD-Test-Suite/src/code/gcd_algorithms.hpp", "max_stars_repo_name": "luxe/CodeLang-compiler", "max_stars_repo_head_hexsha": "78837d90bdd09c4b5aabbf0586a5d8f8f0c1e76a", "max_stars_repo_licenses": ["MIT"], "max_stars_count": 33.0, "max_stars_repo_stars_event_min_datetime": "2019-05-30T07:43:32.000Z", "max_stars_repo_stars_event_max_datetime": "2021-12-30T13:12:32.000Z", "max_issues_repo_path": "source/code/scratch/old_repos/edinboro/CSCI-385/GCD-Test-Suite/src/code/gcd_algorithms.hpp", "max_issues_repo_name": "luxe/CodeLang-compiler", "max_issues_repo_head_hexsha": "78837d90bdd09c4b5aabbf0586a5d8f8f0c1e76a", "max_issues_repo_licenses": ["MIT"], "max_issues_count": 371.0, "max_issues_repo_issues_event_min_datetime": "2019-05-16T15:23:50.000Z", "max_issues_repo_issues_event_max_datetime": "2021-09-04T15:45:27.000Z", "max_forks_repo_path": "source/code/scratch/old_repos/edinboro/CSCI-385/GCD-Test-Suite/src/code/gcd_algorithms.hpp", "max_forks_repo_name": "UniLang/compiler", "max_forks_repo_head_hexsha": "c338ee92994600af801033a37dfb2f1a0c9ca897", "max_forks_repo_licenses": ["MIT"], "max_forks_count": 6.0, "max_forks_repo_forks_event_min_datetime": "2019-08-22T17:37:36.000Z", "max_forks_repo_forks_event_max_datetime": "2020-11-07T07:15:32.000Z", "avg_line_length": 25.6930379747, "max_line_length": 164, "alphanum_fraction": 0.6329597241, "num_tokens": 2343, "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9518632316144274, "lm_q2_score": 0.9019206745523101, "lm_q1q2_score": 0.8585051279392262}} {"text": "/*\n* Level_9 Group B:\n* Source file for OptionExtras methods\n*\n* option operation function, reference to OptionExtras.cpp in the cource example code\n*\n* @file OptionExtras.cpp\n* @author Chunyu Yuan\n* @version 1.0 02/28/2021\n*\n*/\n\n\n#include //library for using string\n#include //library for using vector\n\n#include \"OptionExtras.hpp\" //header file for OptionExtras methods\n#include //header file for using normal distribution \n#include // library for using math formula\n#include // Standard Input / Output Streams Library\n\nusing namespace boost::math;//namespace declaration for using boost::math\nusing namespace std; //namespace declaration for using std\n\n//A simple mesher on a 1d domain\nvector GenerateMeshArray(double begin, double end, int n)\n{\n\tvector vec;\n\t// NB Full array (includes end points)\n\tdouble h = (end - begin) / (n - 1);\n\t//for loop to iterate to add valve to vector\n\tfor (int i = 0; i < n; i++)\n\t{\n\t\tvec.push_back(begin + h * i);\n\t}\n\n\treturn vec;\n}\n\n//price of call with T,K,sig, r, U, b. For formula information, please check course materials\ndouble call_price(double T, double K, double sig, double r, double U, double b)\n{\n\tdouble tmp = sig * sqrt(T);\n\tdouble d1 = (log(U / K) + (b + (sig * sig) * 0.5) * T) / tmp;\n\tdouble d2 = d1 - tmp;\n\n\t//using boost library's normal distribution \n\tnormal_distribution<> normalDist(0, 1);\n\n\treturn (U * exp((b - r) * T) * cdf(normalDist, d1)) - (K * exp(-r * T) * cdf(normalDist, d2));\n}\n\n//price of put with T,K,sig, r, U, b. For formula information, please check course materials\ndouble put_price(double T, double K, double sig, double r, double U, double b)\n{\n\tdouble tmp = sig * sqrt(T);\n\tdouble d1 = (log(U / K) + (b + (sig * sig) * 0.5) * T) / tmp;\n\tdouble d2 = d1 - tmp;\n\t//using boost library's normal distribution \n\tnormal_distribution<> normalDist(0, 1);\n\n\treturn (K * exp(-r * T) * cdf(normalDist, -d2)) - (U * exp((b - r) * T) * cdf(normalDist, -d1));\n}\n//delta of call with T,K,sig, r, U, b. For formula information, please check course materials\ndouble call_delta(double T, double K, double sig, double r, double U, double b)\n{//using boost library's normal distribution \n\tnormal_distribution<> normalDist(0, 1);\n\n\tdouble tmp = sig * sqrt(T);\n\tdouble d1 = (log(U / K) + (b + (sig * sig) * 0.5) * T) / tmp;\n\treturn exp((b - r) * T) * cdf(normalDist, d1);\n}\n//delta of put with T,K,sig, r, U, b. For formula information, please check course materials\ndouble put_delta(double T, double K, double sig, double r, double U, double b)\n{\n\treturn call_delta(T, K, sig, r, U, b) - exp((b - r) * T);\n}\n//gamma of call with T,K,sig, r, U, b. For formula information, please check course materials\ndouble call_gamma(double T, double K, double sig, double r, double U, double b)\n{//using boost library's normal distribution \n\tnormal_distribution<> normalDist(0, 1);\n\tdouble tmp = sig * sqrt(T);\n\tdouble d1 = (log(U / K) + (b + (sig * sig) * 0.5) * T) / tmp;\n\treturn pdf(normalDist, d1) * exp((b - r) * T) / (U * sig * sqrt(T));\n}\n//gamma of put with T,K,sig, r, U, b. For formula information, please check course materials\ndouble put_gamma(double T, double K, double sig, double r, double U, double b)\n{\n\treturn call_gamma(T, K, sig, r, U, b);\n}\n\n////vega of call with T,K,sig, r, U, b. For formula information, please check course materials\ndouble call_vega(double T, double K, double sig, double r, double U, double b)\n{//using boost library's normal distribution \n\tnormal_distribution<> normalDist(0, 1);\n\tdouble tmp = sig * sqrt(T);\n\tdouble d1 = (log(U / K) + (b + (sig * sig) * 0.5) * T) / tmp;\n\treturn U * sqrt(T) * exp((b - r) * T) * pdf(normalDist, d1);\n}\n////vega of put with T,K,sig, r, U, b. For formula information, please check course materials\ndouble put_vega(double T, double K, double sig, double r, double U, double b)\n{\n\treturn call_vega(T, K, sig, r, U, b);\n}\n//theta of call with T,K,sig, r, U, b. For formula information, please check course materials\ndouble call_theta(double T, double K, double sig, double r, double U, double b)\n{//using boost library's normal distribution \n\tnormal_distribution<> normalDist(0, 1);\n\tdouble tmp = sig * sqrt(T);\n\tdouble d1 = (log(U / K) + (b + (sig * sig) * 0.5) * T) / tmp;\n\tdouble d2 = d1 - tmp;\n\treturn -U * sig * exp((b - r) * T) * pdf(normalDist, d1) / (2 * sqrt(T))\n\t\t- (b - r) * U * exp((b - r) * T) * cdf(normalDist, d1) - r * K * exp(-r * T) * cdf(normalDist, d2);\n}\n//theta of put with T,K,sig, r, U, b. For formula information, please check course materials\ndouble put_theta(double T, double K, double sig, double r, double U, double b)\n{\n\treturn call_theta(T, K, sig, r, U, b) + r * K * exp(-r * T) + U * exp((b - r) * T) * (b - r);\n}", "meta": {"hexsha": "aaeed0e7a83e3b3122304808d0798583c5da8a32", "size": 4752, "ext": "cpp", "lang": "C++", "max_stars_repo_path": "Level9/Level9/Level9/Group B/OptionExtras.cpp", "max_stars_repo_name": "chunyuyuan/My-Solution-for-C-Programming-for-Financial-Engineering", "max_stars_repo_head_hexsha": "478b414714edbea1ebdc2f565baad6f04f54bc70", "max_stars_repo_licenses": ["MIT"], "max_stars_count": 1.0, "max_stars_repo_stars_event_min_datetime": "2021-09-12T08:15:57.000Z", "max_stars_repo_stars_event_max_datetime": "2021-09-12T08:15:57.000Z", "max_issues_repo_path": "Level9/Level9/Level9/Group B/OptionExtras.cpp", "max_issues_repo_name": "chunyuyuan/My-Solution-for-C-Programming-for-Financial-Engineering", "max_issues_repo_head_hexsha": "478b414714edbea1ebdc2f565baad6f04f54bc70", "max_issues_repo_licenses": ["MIT"], "max_issues_count": null, "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "Level9/Level9/Level9/Group B/OptionExtras.cpp", "max_forks_repo_name": "chunyuyuan/My-Solution-for-C-Programming-for-Financial-Engineering", "max_forks_repo_head_hexsha": "478b414714edbea1ebdc2f565baad6f04f54bc70", "max_forks_repo_licenses": ["MIT"], "max_forks_count": null, "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null, "avg_line_length": 39.9327731092, "max_line_length": 101, "alphanum_fraction": 0.6622474747, "num_tokens": 1464, "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9664104914476339, "lm_q2_score": 0.8872046011730965, "lm_q1q2_score": 0.8574038346342943}} {"text": "\r\n\r\n#include \"MatricesAndVectors.h\"\r\n#include \t\t/* printf */\r\n#include \t\t/* pow, sqrt */\r\n#include \r\nusing Eigen::Matrix;\r\n/* Create a 3x3 diagonal matrix from a 3x1 vector */\r\n// Matrix3f Diag3(double vec[3]){\r\n// \tMat3x3 output;\r\n// \tfor (int i = 0; i < 3; i++){\r\n// \t\tfor (int j = 0; j < 3; j++){\r\n// \t\t\tif (i == j)\r\n// \t\t\t\toutput.M[i][j] = vec[i];\r\n// \t\t\telse\r\n// \t\t\t\toutput.M[i][j] = 0;\r\n// \t\t}\r\n// \t}\r\n\r\n// \treturn output;\r\n// }\r\n\r\n// Multiply 3x3 matrices: M_out = M1*M2\r\n// Mat3x3 MultiplyMat3x3(Mat3x3 M1, Mat3x3 M2){\r\n// \tMat3x3 M_out;\r\n\r\n// \tfor (int i = 0; i < 3; i++){\r\n// \t\tfor (int j = 0; j < 3; j++){\r\n// \t\t\tM_out.M[i][j] = 0;\r\n// \t\t\tfor (int k = 0; k < 3; k++){\r\n// \t\t\t\tM_out.M[i][j] += M1.M[i][k] * M2.M[k][j];\r\n// \t\t\t}\r\n// \t\t}\r\n// \t}\r\n\r\n// \treturn M_out;\r\n// }\r\n\r\n// Sum 3x3 matrices: M_out = M1 + M2\r\n// Mat3x3 AddMat3x3(Mat3x3 M1, Mat3x3 M2){\r\n// \tMat3x3 M_out;\r\n\r\n// \tfor (int i = 0; i < 3; i++){\r\n// \t\tfor (int j = 0; j < 3; j++){\r\n// \t\t\tM_out.M[i][j] = M1.M[i][j] + M2.M[i][j];\r\n// \t\t}\r\n// \t}\r\n\r\n// \treturn M_out;\r\n// }\r\n\r\n\r\n// // Subtract 3x3 matrices: M_out = M1 - M2\r\n// Mat3x3 SubtractMat3x3(Mat3x3 M1, Mat3x3 M2){\r\n// \tMat3x3 M_out;\r\n\r\n// \tfor (int i = 0; i < 3; i++){\r\n// \t\tfor (int j = 0; j < 3; j++){\r\n// \t\t\tM_out.M[i][j] = M1.M[i][j] - M2.M[i][j];\r\n// \t\t}\r\n// \t}\r\n\r\n// \treturn M_out;\r\n// }\r\n\r\n// // Transpose 3x3 matrix: M_out = M_in'\r\n// Mat3x3 TransposeMat3x3(Mat3x3 M_in){\r\n// \tMat3x3 M_out;\r\n\r\n// \tfor (int i = 0; i < 3; i++){\r\n// \t\tfor (int j = 0; j < 3; j++){\r\n// \t\t\tM_out.M[i][j] = M_in.M[j][i];\r\n// \t\t}\r\n// \t}\r\n\r\n// \treturn M_out;\r\n// }\r\n\r\n// Calculate Skew symmetric matrix from vector\r\n// Mat3x3 skew(Vec3 V){\r\n// \tMat3x3 M;\r\n// \tM.M[0][0] = 0;\r\n// \tM.M[0][1] = -V(2);\r\n// \tM.M[0][2] = V(1);\r\n\r\n// \tM.M[1][0] = V(2);\r\n// \tM.M[1][1] = 0;\r\n// \tM.M[1][2] = -V(0);\r\n\r\n// \tM.M[2][0] = -V(1);\r\n// \tM.M[2][1] = V(0);\r\n// \tM.M[2][2] = 0;\r\n\r\n// \treturn M;\r\n\r\n// }\r\n\r\n// Inverse operation for Skew symmetric matrices\r\nMatrix invSkew(Matrix Mat){\r\n\tMatrix w;\r\n\r\n\tw << -Mat(1,2),\r\n\t\t Mat(0,2),\r\n\t\t -Mat(0,1);\r\n\r\n\treturn w;\r\n}\r\n\r\n// Vec3 invSkew(Mat3x3 Mat){\r\n// \tVec3 w;\r\n\r\n// \tw(0) = -Mat.M[1][2];\r\n// \tw(1) = Mat.M[0][2];\r\n// \tw(2) = -Mat.M[0][1];\r\n\r\n// \treturn w;\r\n// }\r\n\r\n//Cross product between two vectors\r\n// Vec3 cross(Vec3 V1, Vec3 V2){\r\n\t\r\n// \treturn MultiplyMat3x3Vec3(skew(V1), V2);\r\n\r\n// }\r\n\r\n//Calculate the p-norm of a 3x1 vector\r\n// double p_normVec3(Vec3 V, int p){\r\n// \treturn pow(pow(V(0), p) + pow(V(1), p) + pow(V(2), p), 1.0 / p);\r\n// }\r\n\r\nMatrix normalizeVec3(Matrix V){\r\n\tdouble normV = V.norm();\r\n\tif (normV > 0){\r\n\t\treturn V*(1.0 / normV);\r\n\t}\r\n\telse{\r\n\t\treturn V;\r\n\t}\r\n}\r\n\r\n//Inner product between two matrices\r\n// double innerProd(Vec3 V1, Vec3 V2){\r\n// \treturn V1(0)*V2(0) + V1(1)*V2(1) + V1(2)*V2(2);\r\n// }\r\n\r\n/* Print 3x3 matrices for debugging*/\r\n// void PrintMat3x3(Mat3x3 Mat){\r\n// \tfor (int i = 0; i < 3; i++){\r\n// \t\tfor (int j = 0; j < 3; j++){\r\n// \t\t\tprintf(\"%f\\t\", Mat.M[i][j]);\r\n// \t\t}\r\n// \t\tprintf(\"\\n\");\r\n// \t}\r\n// \tprintf(\"\\n\");\r\n// }\r\n\r\n/* Print 4x4 matrices for debugging*/\r\nvoid PrintMat4x4(Matrix Mat){\r\n\tfor (int i = 0; i < 4; i++){\r\n\t\tfor (int j = 0; j < 4; j++){\r\n\t\t\tprintf(\"%f\\t\", Mat(i,j));\r\n\t\t}\r\n\t\tprintf(\"\\n\");\r\n\t}\r\n\tprintf(\"\\n\");\r\n}\r\n\r\n/* Print 3x1 vectors for debugging*/\r\nvoid PrintVec3(Matrix V, char const *Text){\r\n\tprintf(\"%s = \\t\", Text);\r\n\tfor (int i = 0; i < 3; i++){\r\n\t\tprintf(\"%f \\t\", V(i));\r\n\t}\r\n\tprintf(\"\\n\");\r\n}\r\n\r\n/* Print 4x1 vectors for debugging*/\r\nvoid PrintVec4(Matrix V, char const *Text){\r\n\tprintf(\"%s = \\t\", Text);\r\n\tfor (int i = 0; i < 4; i++){\r\n\t\tprintf(\"%f \\t\", V(i));\r\n\t}\r\n\tprintf(\"\\n\");\r\n}\r\n\r\n// Multiply 3x3 matrix by a 3x1 vectos: V_out = M*V\r\n// Vec3 MultiplyMat3x3Vec3(Mat3x3 Mat, Vec3 V){\r\n// \tVec3 V_out;\r\n\r\n// \tfor (int i = 0; i < 3; i++){\r\n// \t\tV_out.v[i] = 0;\r\n// \t\tfor (int k = 0; k < 3; k++){\r\n// \t\t\tV_out.v[i] += Mat.M[i][k] * V.v[k];\r\n// \t\t}\r\n// \t}\r\n\r\n// \treturn V_out;\r\n// }\r\n\r\n// Scale 3x1 vector: V_out = c.V_in, where c is a constant\r\n// Vec3 ScaleVec3(Vec3 V_in, float c){\r\n// \tVec3 V_out;\r\n\r\n// \tfor (int i = 0; i < 3; i++){\r\n// \t\tV_out.v[i] = c*V_in.v[i];\r\n// \t}\r\n\r\n// \treturn V_out;\r\n// }\r\n\r\n// Add 3x1 vectors: V_out = V1 + V2\r\n// Vec3 Add3x1Vec(Vec3 V1, Vec3 V2){\r\n// \tVec3 V_out;\r\n// \tV_out(0) = V1(0) + V2(0);\r\n// \tV_out(1) = V1(1) + V2(1);\r\n// \tV_out(2) = V1(2) + V2(2);\r\n\r\n// \treturn V_out;\r\n// }\r\n\r\n// Subtract 3x1 vectors: V_out = V1 - V2\r\n// Vec3 Subtract3x1Vec(Vec3 V1, Vec3 V2){\r\n// \tVec3 V_out;\r\n// \tV_out(0) = V1(0) - V2(0);\r\n// \tV_out(1) = V1(1) - V2(1);\r\n// \tV_out(2) = V1(2) - V2(2);\r\n\r\n// \treturn V_out;\r\n// }\r\n\r\n// Multiply 4x4 matrix by a 4x1 vector: V_out = M*V\r\n// Vec4 MultiplyMat4x4Vec4(Mat4x4 Mat, Vec4 V){\r\n// \tVec4 V_out;\r\n\r\n// \tfor (int i = 0; i < 4; i++){\r\n// \t\tV_out.v[i] = 0;\r\n// \t\tfor (int k = 0; k < 4; k++){\r\n// \t\t\tV_out.v[i] += Mat.M[i][k] * V.v[k];\r\n// \t\t}\r\n// \t}\r\n\r\n// \treturn V_out;\r\n// }\r\n\r\n// //Concatenate three vectors into a 3x3 matrix\r\n// Mat3x3 Concatenate3Vec3_2_Mat3x3(Vec3 V1, Vec3 V2, Vec3 V3){\r\n// \tMat3x3 M;\r\n// \tM.M[0][0] = V1(0);\r\n// \tM.M[1][0] = V1(1);\r\n// \tM.M[2][0] = V1(2);\r\n\r\n// \tM.M[0][1] = V2(0);\r\n// \tM.M[1][1] = V2(1);\r\n// \tM.M[2][1] = V2(2);\r\n\r\n// \tM.M[0][2] = V3(0);\r\n// \tM.M[1][2] = V3(1);\r\n// \tM.M[2][2] = V3(2);\r\n\r\n// \treturn M;\r\n// }\r\n\r\n\r\n//Verify if any of the terms in a Vec3 is NaN\r\nint isNanVec3(Matrix V){\r\n\tif(isnan(V(0)) || isnan(V(1)) || isnan(V(2))){\r\n\t\tprintf(\"Not a number found!\\n\");\r\n\t\treturn 1;\r\n\t}\r\n\telse{\r\n\t\treturn 0;\r\n\t}\r\n\r\n}", "meta": {"hexsha": "8d3f0df45b26d6839701b240ac0656226febc543", "size": 5590, "ext": "cpp", "lang": "C++", "max_stars_repo_path": "data/AGNC-Lab_Quad/multithreaded/control/MatricesAndVectors.cpp", "max_stars_repo_name": "khairulislam/phys", "max_stars_repo_head_hexsha": "fc702520fcd3b23022b9253e7d94f878978b4500", "max_stars_repo_licenses": ["MIT"], "max_stars_count": null, "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "data/AGNC-Lab_Quad/multithreaded/control/MatricesAndVectors.cpp", "max_issues_repo_name": "khairulislam/phys", "max_issues_repo_head_hexsha": "fc702520fcd3b23022b9253e7d94f878978b4500", "max_issues_repo_licenses": ["MIT"], "max_issues_count": null, "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "data/AGNC-Lab_Quad/multithreaded/control/MatricesAndVectors.cpp", "max_forks_repo_name": "khairulislam/phys", "max_forks_repo_head_hexsha": "fc702520fcd3b23022b9253e7d94f878978b4500", "max_forks_repo_licenses": ["MIT"], "max_forks_count": null, "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null, "avg_line_length": 20.401459854, "max_line_length": 69, "alphanum_fraction": 0.4788908766, "num_tokens": 2359, "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9449947055100817, "lm_q2_score": 0.9059898121338505, "lm_q1q2_score": 0.8561555757125623}} {"text": "// CPP program to print fermat numbers\n#include \n#include \nusing namespace boost::multiprecision;\n#define llu int128_t\nusing namespace std;\n \n/* Iterative Function to calculate (x^y) in O(log y) */\nllu power(llu x, llu y)\n{\n llu res = 1; // Initialize result\n \n while (y > 0) {\n // If y is odd, multiply x with the result\n if (y & 1)\n res = res * x;\n \n // n must be even now\n y = y >> 1; // y = y/2\n x = x * x; // Change x to x^2\n }\n return res;\n}\n \n// Function to find nth fermat number\nllu Fermat(llu i)\n{\n // 2 to the power i\n llu power2_i = power(2, i);\n \n // 2 to the power 2^i\n llu power2_2_i = power(2, power2_i);\n \n return power2_2_i + 1;\n}\n \n// Function to find first n Fermat numbers\nvoid Fermat_Number(llu n)\n{\n \n for (llu i = 0; i < n; i++) {\n \n // Calculate 2^2^i\n cout << Fermat(i);\n \n if(i!=n-1)\n cout << \", \";\n }\n}\n \n// Driver code\nint main()\n{\n llu n = 7;\n \n // Function call\n Fermat_Number(n);\n \n return 0;\n}\n", "meta": {"hexsha": "711d8647a0872f162e0fa8c8086261e2205ef63a", "size": 1123, "ext": "cpp", "lang": "C++", "max_stars_repo_path": "Fermat Numbers/fermat_numbers.cpp", "max_stars_repo_name": "ajtroyer/Integer-Sequences", "max_stars_repo_head_hexsha": "d54a84c74b2cfa95e3df8489d5878fa4e8881d73", "max_stars_repo_licenses": ["Unlicense"], "max_stars_count": 48.0, "max_stars_repo_stars_event_min_datetime": "2021-06-28T05:53:43.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-17T10:37:26.000Z", "max_issues_repo_path": "Fermat Numbers/fermat_numbers.cpp", "max_issues_repo_name": "PremApk/Integer-Sequences", "max_issues_repo_head_hexsha": "89a409006d5718c5e9cd9d95748c312337600209", "max_issues_repo_licenses": ["Unlicense"], "max_issues_count": 99.0, "max_issues_repo_issues_event_min_datetime": "2021-06-28T03:16:51.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-17T00:18:50.000Z", "max_forks_repo_path": "Fermat Numbers/fermat_numbers.cpp", "max_forks_repo_name": "PremApk/Integer-Sequences", "max_forks_repo_head_hexsha": "89a409006d5718c5e9cd9d95748c312337600209", "max_forks_repo_licenses": ["Unlicense"], "max_forks_count": 140.0, "max_forks_repo_forks_event_min_datetime": "2021-06-28T06:29:19.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-30T11:15:45.000Z", "avg_line_length": 18.4098360656, "max_line_length": 55, "alphanum_fraction": 0.5316117542, "num_tokens": 359, "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9496693674025231, "lm_q2_score": 0.897695295528596, "lm_q1q2_score": 0.8525137234248626}}