{"id":23552514,"url":"https://github.com/xlisp/rcpp-algorithms","last_synced_at":"2026-04-27T20:31:26.440Z","repository":{"id":127745263,"uuid":"106782711","full_name":"xlisp/rcpp-algorithms","owner":"xlisp","description":"Rcpp algorithms, Compare R and C++, Interop R and C++, R package develop","archived":false,"fork":false,"pushed_at":"2017-10-14T05:51:58.000Z","size":10,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-15T17:14:30.087Z","etag":null,"topics":["cpp","r","rcpp"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xlisp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-10-13T05:42:28.000Z","updated_at":"2024-08-24T12:47:54.000Z","dependencies_parsed_at":"2023-08-17T09:45:59.047Z","dependency_job_id":null,"html_url":"https://github.com/xlisp/rcpp-algorithms","commit_stats":null,"previous_names":["xlisp/rcpp-algorithms"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/xlisp/rcpp-algorithms","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xlisp%2Frcpp-algorithms","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xlisp%2Frcpp-algorithms/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xlisp%2Frcpp-algorithms/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xlisp%2Frcpp-algorithms/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xlisp","download_url":"https://codeload.github.com/xlisp/rcpp-algorithms/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xlisp%2Frcpp-algorithms/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32354564,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-27T20:07:02.737Z","status":"ssl_error","status_checked_at":"2026-04-27T20:07:00.910Z","response_time":128,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpp","r","rcpp"],"created_at":"2024-12-26T11:11:25.602Z","updated_at":"2026-04-27T20:31:26.412Z","avatar_url":"https://github.com/xlisp.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# Rcpp Algorithms, R的快速建模 和 C++无限的资源互操作\n* Compare R and C++\n* Interop R and C++(使用无限的C++算法库资源,OpenCV,TensorFlow,各种机器学习和深度学习,机器视觉,语音识别算法库)\n* R package develop\n\n\n\n- [Rcpp Algorithms, R的快速建模 和 C++无限的资源互操作](#rcpp-algorithms-r%E7%9A%84%E5%BF%AB%E9%80%9F%E5%BB%BA%E6%A8%A1-%E5%92%8C-c%E6%97%A0%E9%99%90%E7%9A%84%E8%B5%84%E6%BA%90%E4%BA%92%E6%93%8D%E4%BD%9C)\n    - [cppFunction \u0026 sourceCpp](#cppfunction--sourcecpp)\n    - [Matrix \u0026 Vector](#matrix--vector)\n        - [C++ 操作 R数值向量](#c-%E6%93%8D%E4%BD%9C-r%E6%95%B0%E5%80%BC%E5%90%91%E9%87%8F)\n        - [C++ 向量输出`NumericVector outVector(ys.size())`创建一个长度为n的数值向量](#c-%E5%90%91%E9%87%8F%E8%BE%93%E5%87%BAnumericvector-outvectoryssize%E5%88%9B%E5%BB%BA%E4%B8%80%E4%B8%AA%E9%95%BF%E5%BA%A6%E4%B8%BAn%E7%9A%84%E6%95%B0%E5%80%BC%E5%90%91%E9%87%8F)\n        - [c++操作矩阵, 一层层for操作(就像生成学号一样)](#c%E6%93%8D%E4%BD%9C%E7%9F%A9%E9%98%B5-%E4%B8%80%E5%B1%82%E5%B1%82for%E6%93%8D%E4%BD%9C%E5%B0%B1%E5%83%8F%E7%94%9F%E6%88%90%E5%AD%A6%E5%8F%B7%E4%B8%80%E6%A0%B7)\n        - [c++ 矩阵输出](#c-%E7%9F%A9%E9%98%B5%E8%BE%93%E5%87%BA)\n\n\n\n\n### cppFunction \u0026 sourceCpp\n```r\n(require (Rcpp))\n\n(cppFunction ('int one() {\n  return 1;\n}'))\n\n(one ()) #=\u003e \"[1] 1\" 标量输出\n```\n* fib_cpp_1.cpp\n```c++\n#include \u003cRcpp.h\u003e\nusing namespace Rcpp;\n\n//[[Rcpp::export]]\nint fib_cpp_1(int n)\n{\n    if(n==1||n==2) return 1;\n    return fib_cpp_1(n-1)+fib_cpp_1(n-2);\n}\n```\n```r\n\n\u003e require(Rcpp)\n载入需要的程辑包：Rcpp\n\u003e\n\n\u003e  sourceCpp('fib_cpp_1.cpp')\n\n\u003e fib_cpp_1(50)\n[1] -298632863\n\n\u003e fib_cpp_1(10)\n[1] 55\n\u003e fib_cpp_1(20)\n[1] 6765\n\u003e fib_cpp_1(30)\n[1] 832040\n\n\u003e system.time(fib_cpp_1(50))\n  用户   系统   流逝\n27.053  0.356 28.620\n\u003e\n\n```\n### Matrix \u0026 Vector\n##### C++ 操作 R数值向量\n```r\n(cppFunction ('double sumC(NumericVector x) {\n  int n = x.size();\n  double total = 0;\n  for(int i = 0; i \u003c n; ++i) {\n    total += x[i];\n  }\n  return total;\n}'))\n\n(sumC (c (1, 1, 3))) #=\u003e [1] 5\n```\n##### C++ 向量输出`NumericVector outVector(ys.size())`创建一个长度为n的数值向量\n```r\n(cppFunction ('NumericVector pdistC(double x, NumericVector ys) {\n  int n = ys.size();\n  NumericVector out(n);\n\n  for(int i = 0; i \u003c n; ++i) {\n    out[i] = sqrt(pow(ys[i] - x, 2.0));\n  }\n  return out;\n}'))\n\n(pdistC (-1, (c (2, 2)))) #=\u003e [1] 3 3\n```\n##### c++操作矩阵, 一层层for操作(就像生成学号一样)\n```r\n# x(i, j) c++中用()对矩阵进行子集选取\n(cppFunction ('NumericVector rowSumsC(NumericMatrix x) {\n  int nrow = x.nrow(), ncol = x.ncol();\n  NumericVector out(nrow);\n\n  for (int i = 0; i \u003c nrow; i++) {\n    double total = 0;\n    for (int j = 0; j \u003c ncol; j++) {\n      total += x(i, j);\n    }\n    out[i] = total;\n  }\n  return out;\n}'))\n\n(rowSumsC (matrix ((sample (100)), 10))) #=\u003e [1] 489 515 476 415 547 551 357 663 533 504\n```\n##### c++ 矩阵输出\n```r\n(cppFunction ('NumericMatrix zeroMat(int x, int y) {\n  NumericMatrix outMat(x, y);\n  return outMat;\n}'))\n\n(zeroMat (3, 2))\n#=\u003e\n      [,1] [,2]\n [1,]    0    0\n [2,]    0    0\n [3,]    0    0\n```\n### 将C++函数和类导出到R中 -\u003e Rcpp Modules\n```r\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxlisp%2Frcpp-algorithms","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxlisp%2Frcpp-algorithms","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxlisp%2Frcpp-algorithms/lists"}