{"id":23065787,"url":"https://github.com/konrad1991/etr","last_synced_at":"2025-08-15T11:32:12.343Z","repository":{"id":108851228,"uuid":"421097116","full_name":"Konrad1991/ETR","owner":"Konrad1991","description":"Expression template for C++ similar to R syntax ","archived":false,"fork":false,"pushed_at":"2024-06-05T07:16:06.000Z","size":31685,"stargazers_count":6,"open_issues_count":4,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-04T20:17:39.775Z","etag":null,"topics":["cpp","cpp17","expression-template","header-only-library","r","rlanguage","rstats"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Konrad1991.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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}},"created_at":"2021-10-25T16:15:18.000Z","updated_at":"2025-02-04T19:43:49.000Z","dependencies_parsed_at":"2024-03-28T10:51:23.622Z","dependency_job_id":"1c780600-47b9-45c6-a2d0-376946e384eb","html_url":"https://github.com/Konrad1991/ETR","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Konrad1991/ETR","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Konrad1991%2FETR","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Konrad1991%2FETR/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Konrad1991%2FETR/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Konrad1991%2FETR/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Konrad1991","download_url":"https://codeload.github.com/Konrad1991/ETR/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Konrad1991%2FETR/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270563676,"owners_count":24607609,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-15T02:00:12.559Z","response_time":110,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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","cpp17","expression-template","header-only-library","r","rlanguage","rstats"],"created_at":"2024-12-16T05:10:07.141Z","updated_at":"2025-08-15T11:32:11.971Z","avatar_url":"https://github.com/Konrad1991.png","language":"C++","readme":"- [Overview](#overview)\n- [Integrate ETR in your project](#integrate-etr-in-your-project)\n- [How to use it](#how-to-use-it)\n    + [Hello World from ETR](#hello-world-from-etr)\n    + [Variable declaration \u0026 borrowing of raw pointers](#variable-declaration)\n    + [Basic arithmetic](#basic-arithmetic)\n    + [Subsetting](#subsetting)\n    + [Helper functions](#helper-functions)\n    + [Comparison functions](#comparison-functions)\n    + [Looping](#looping)\n    + [Math functions](#math-functions)\n    + [Interpolation](#interpolation)\n\n# Overview \n\nETR is an expression template library for C++ with syntax almost similar to R. Within this repository the standalone version can be found. As the C++ code is used in the R package ast2ast (https://github.com/Konrad1991/ast2ast) also R specific code is included. \nBy using *#define STANDALONE* the R specific code is removed. The library contains one main type:\n    \n    * The main type: template \u003ctypename T, typename R = Buffer\u003cT\u003e, typename Trait = VectorTrait\u003e struct Vec;\nThere exists also other container responsible for storing the memory (typename R). Within the standalone library the *Borrow* struct is most importantly. See in the documention above about how to use it.\nThe class Vec can hold a scalar, vector or a matrix. Moreover, the type can change dynamically at runtime. \n\n# Integrate ETR in your project\n\nNotably, it is a header-only library and thus can be easly integrated in other projects. Simply clone the repository and add the 'etr_bits' folder in your project (can be found in the 'include' folder). Afterwards you have to include the 'etr.hpp' file in your codebase (*#include 'etr.hpp'*). The file can also be found in the 'include' folder. Use of C++17 is required, i.e. compile a program using: 'g++ test.cpp -std=c++17'. There is also a problem with older versions of gcc. The tests all run successfully using gcc-10. Code coverage can be found in folder tests (84.3%).\nHow the program works in detail, is explained below using small examples. \n\n# How to use it\n\n### Hello World from ETR\n\nThe **print** function can be used to print strings, scalar values, vectors or matrices. If you want to add a empty line use the function without an argument. \n\n```Cpp\n#include \"etr.hpp\"\nusing namespace etr;\n\nint main() {\n  print(\"Hello World\");\n}\n```\n\n### Variable declaration \u0026 borrowing of raw pointers\n\nAll variables are of type **sexp**. This type can hold a scalar, a vector or a matrix (The only type that can be hold by these containers is a **double**). If you want to allocate memory you can use the functions **vector** or **matrix**. Another possibility to allocate memory are the functions **colon** and **coca**. The **colon** function excepts a start and a end value and returns a vector with n-values seperated by an interval with length 1 respectively. The **coca** functions concatenates the given input. \\\nNotably, it is possible to change the type of a variable within the program. You can see below that at the end the variable *scalar* contains a vector. \nBeyond that, it is possible to borrow a pointer. In the example below a raw pointer is used to allocate memory on the heap. Afterwards it is wrapped in a BorrowPtr class instance. On the one hand it is more convinient to use the Vector class. Furthermore, it protects from unallowed access i.e. you cannot assign longer elements than that which was borrowed.\n\nCurrently, I work on using other types as double. This is still work in progress even though many things work already. \n\n```Cpp\n#include \"etr.hpp\"\nusing namespace etr;\n\nint main() {\n\n  sexp scalar = 1.0; print(scalar);\n\n  sexp empty_vec = vector(10); // vector with 10 elements\n  sexp full_vec = vector(3.14, 10); // vector with 10 elements all contain 3.14\n\n  sexp emtpy_mat = matrix(5, 2); // matrix with 10 elements; 5 rows and 2 cols\n  sexp full_mat = matrix(10.5, 5, 4); // matrix with 10 elements; 5 rows and 4 cols all contain 10.5\n\n  sexp vec_range = colon(1, 10); // vector containing 1, 2, 3, ..., 10\n  sexp vec = coca(1, 5, 6, vec_range); // vector containing 1, 5, 6, vec_range\n\n  // change the type\n  scalar = vector(5.2, 12);\n  print(scalar);\n\n  // you can borrow a raw pointer\n   size_t size = 3; \n  double* ptr = new double[size];\n  \n  BorrowPtr v_borrow(ptr, size);\n  v_borrow = colon(1, 3);\n  print(v_borrow);\n  \n  ptr[0] = 3.14;\n  print(v_borrow);\n\n  std::cout \u003c\u003c ptr[0] \u003c\u003c \" \" \u003c\u003c ptr[1] \u003c\u003c \" \" \u003c\u003c ptr[2] \u003c\u003c std::endl;\n\n  delete[] ptr; // you are responsible to not use v_borrow after deleting ptr!\n}\n```\n\n### Basic arithmetic\n\nYou can apply basic functions like **+**, **-**, **/** and **\\*** to scalar, vector, matrices and any combinations. \n\n```Cpp\n#include \"etr.hpp\"\nusing namespace etr;\n\nint main() {\n\nsexp v = colon(1, 10);\n\nv = v/1.0 + 10.0;\nprint(v);\n\nsexp m = matrix(3., 5, 2);\nm = m - v;\nprint(m);\n}\n```\n\n### Subsetting\n\nYou can use the operator() to subset a variable. \n\n```Cpp\n#include \"etr.hpp\"\nusing namespace etr;\n\nint main() {\n\n  sexp a = coca(1, 2.2, 3);\n  print(a);\n  \n  sexp b = matrix(colon(1, 16), 4, 4);\n  print(b);\n\n  print(b(1));\n \n  b(3) = 4.5;\n\n  print(b);\n\n}\n```\n\n\n### Helper functions\n\nIn order to extract informations about variables. You can use the **length** function in order to determine number of stored objects in vector or matrix. In case the variable is a matrix the **dim** function returns a vector containg the number of rows in the first place followed by the number of columns. \nThere are some special print functions available: printType prints the type of an object, printTType prints the type of the type of an object, printAST prints the expression tree of the current object and printTAST prints the expression tree of a type of an object.\n\n```Cpp\n#include \"etr.hpp\"\nusing namespace etr;\n\nint main() {\n\n    sexp v = colon(1, 2);\n    print(length(v));\n\n    sexp m = matrix(5, 2);\n    print(length(m));\n    print(dim(m));\n\n    printType(v);\n    printT\u003csexp\u003e();\n    printAST(v + v*2.0);\n    printTAST\u003cdecltype(v - v*v)\u003e();\n\n}\n```\n\n### Comparison functions\n\nAs usual in R you can use **==**, **!=**, **\u003c=**, **\u003e=**, **\u003c** and **\u003e**. If one of the comparison functions returns more then one bool and is used in an **if** or **else if** the first element is used.\n\n```Cpp\n#include \"etr.hpp\"\nusing namespace etr;\n\nint main() {\n\n  sexp a = coca(1, 2, 3);\n  sexp b = coca(1, 2, 4);\n\n  sexp c = a == b;\n  print(c);\n  print(a == b);\n  print(a \u003e= b);\n  print(a \u003c= b);\n  print(a \u003c b);\n  print(a \u003e b);\n  print(a != b);\n\n  sexp d = coca(1, 2, 3, 4);\n  sexp e = coca(1, 4);\n\n  print(d == e);\n  print(e \u003e= d);\n\n  auto a_is_b = a == b;\n  printAST(a_is_b);\n  print(a_is_b);\n  if(a(1)) {\n    print(a==b);\n    print(1);\n    print(4.5);\n  }\n\n  print(d);\n  print(coca(1,2,d));\n\n}\n```\n\n### Looping\n\nYou can use range based for loops in order to loop over **sexp** objects. \n\n```Cpp\n#include \"etr.hpp\"\nusing namespace etr;\nHallo Sid,\n\ndanke dir.\nVG Konrad\nint main() {\n\n  sexp v = colon(1, 10);\n\n  for(auto\u0026 i: v) {\n    print(i);\n  }\n\n}\n\n```\n\n\n### Math functions\n\nThe following math functions can be used:\n\n- exponent: exp(variable, exponent); \n  - on non primitive types one can also write a^exponent\n- natural logarithm: ln(variable)\n- sinus(variable)\n- sinush(variable)\n- asinus(variable)\n- cosinus(variable)\n- cosinush(variable)\n- acosinus(variable)\n- tangens(variable)\n- tangensh(variable)\n- atangens(variable)\n\n\n### Interpolation\n\nIt is possible to use a catmull-rome interpolation function called **cmr**. The first argument is the position where the desired value should be calculated. The second argument is the x-variable (i.e. a vector), and the last argument are the y-values. See the example below how to use the function. \n\n```Cpp\n#include \"etr.hpp\"\nusing namespace etr;\n\nint main() {\n\n    sexp t = colon(1, 8);\n    sexp p = coca(1, 2, 3, 4, 5, 6, 7, 8);\n\n    sexp res = cmr(1, t, p);\n    print(res);\n\n    res = cmr(1.5, t, p);\n    print(res);\n\n    res = cmr(2.5, t, p);\n    print(res);\n\n    res = cmr(4, t, p);\n    print(res);\n\n    res = cmr(5, t, p);\n    print(res);\n\n    res = cmr(5.5, t, p);\n    print(res);\n\n    res = cmr(7.5, t, p);\n    print(res);\n\n    res = cmr(7.99, t, p);\n    print(res);\n    print();\n    res = cmr(8, t, p);\n    print(res);\n\n    print();\n    res = cmr(9, t, p);\n    print(res);\n\n    print();\n    res = cmr(-1, t, p);\n    print(res);\n\n}\n\n```\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkonrad1991%2Fetr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkonrad1991%2Fetr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkonrad1991%2Fetr/lists"}