{"id":21111650,"url":"https://github.com/jonathf/matlab2cpp","last_synced_at":"2025-04-06T11:09:27.180Z","repository":{"id":28101096,"uuid":"31599354","full_name":"jonathf/matlab2cpp","owner":"jonathf","description":"Convertion program from Matlab to C++ using Armadillo","archived":false,"fork":false,"pushed_at":"2022-07-15T19:58:09.000Z","size":2082,"stargazers_count":183,"open_issues_count":18,"forks_count":64,"subscribers_count":32,"default_branch":"master","last_synced_at":"2025-03-30T10:08:29.607Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jonathf.png","metadata":{"files":{"readme":"README.rst","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}},"created_at":"2015-03-03T13:20:32.000Z","updated_at":"2025-03-17T05:51:49.000Z","dependencies_parsed_at":"2022-07-11T22:01:11.500Z","dependency_job_id":null,"html_url":"https://github.com/jonathf/matlab2cpp","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonathf%2Fmatlab2cpp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonathf%2Fmatlab2cpp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonathf%2Fmatlab2cpp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonathf%2Fmatlab2cpp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jonathf","download_url":"https://codeload.github.com/jonathf/matlab2cpp/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247471521,"owners_count":20944158,"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","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":[],"created_at":"2024-11-20T01:10:06.040Z","updated_at":"2025-04-06T11:09:27.159Z","avatar_url":"https://github.com/jonathf.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":".. attention::\n\n    Matlab2cpp er currently unmaintained. As a mainteiner this project ended up\n    on the short end of the stick of what I unfortunatly have time for.\n\n    Anyone who want to make changes to it, might do so. I am very open to\n    a change in overship.\n\n    I am sorry for the inconvinience.\n\n    Jonathan\n\n==========\nMatlab2Cpp\n==========\n\n``matlab2cpp`` is a semi-automatic tool for converting code from Matlab to C++.\n\nAfter installing, the ``matlab2cpp`` command line executable ``m2cpp`` will be\navailable in path that can be used to convert Matlab code.\n\nNote that it is not meant as a complete tool for creating runnable C++ code.\nFor example, the `eval`-function can not be supported because there is no\ngeneral way to implement it in C++. Instead the program is a support tool,\nwhich aims at speed up the conversion process as much as possible for a user\nthat needs to convert Matlab programs by hand anyway. The software does this by\nconverting the basic structures of the Matlab-program (functions, branches,\nloops, etc.), adds variable declarations, and for some simple code, do\na complete translation. And any problem the program encounters during\nconversion will be written in a log-file. From there manual conversions can be\ndone by hand.\n\nCurrently, the code will not convert the large library collection of functions\nthat Matlab currently possesses. However, there is no reason for the code not\nto support these features in time. The extension library is easy to extend.\n\nInstallation\n------------\nInstallation by running the ``pip`` command::\n\n    pip install matlab2cpp\n\nThe source-to-source parser do not have any requirements beyond having Python\ninstalled. However, the generated output does have a few requirements to be\ncompilable. They are as follows.\n\n``C++11``\n    Code produces follows the ``C++11`` standard.\n``armadillo``\n    Armadillo is a linear algebra library for the C++ language. The Armadillo\n    library can be found at `http://arma.sourceforge.net`_. Some functionality\n    in Armadillo rely on a math library like LAPACK, BLAS, OpenBLAS or MKL.\n    When installing Armadillo, it will look for installed math libraries.\n\n    If Armadillo is installed, the library can be linked with the link flag\n    ``-l armadillo``. Armadillo can also be linked directly, see the ``FAQ`` at\n    the Armadillo webpage for more information.\n\n    I believe MKL is the fastest math library and it can be downloaded for free\n    at `https://software.intel.com/en-us/articles/free-mkl`_.\n``TBB``\n    By inserting pragmas in the code, for loops can be marked by the user. The\n    program can then either insert ``OpenMP`` or ``TBB`` code to parallelize\n    the for loop. To compile ``TBB`` code, the ``TBB`` library has to be\n    installed. See :ref:`parallel_flags` for more details.\n\nAn illustrating Example\n-----------------------\n\nAssuming Linux installation and `m2cpp` is available in path. Code works\nanalogous in Mac and Windows.\n\nConsider a file `example.m` with the following content::\n\n    function y=f(x)\n        y = x+4\n    end\n    function g()\n        x = [1,2,3]\n        f(x)\n    end\n\nRun conversion on the file: ::\n\n    $ m2cpp example.m\n\nThis will create two files: ``example.m.hpp`` and ``example.m.py``.\n\nIn ``example.m.hpp``, the translated C++ code is placed. It looks as follows::\n\n    #include \u003carmadillo\u003e\n    using namespace arma ;\n\n    TYPE f(TYPE x)\n    {\n      TYPE y ;\n      y = x+4 ;\n      return y ;\n    }\n\n    void g()\n    {\n      TYPE x ;\n      x = [1, 2, 3] ;\n      f(x) ;\n    }\n\nMatlab doesn't declare variables explicitly, so m2cpp is unable to complete\nthe translation.  To create a full conversion, the variables must be declared.\nDeclarations can be done in the file ``example.m.py``. After the first run, it\nwill look as follows::\n\n    # Supplement file\n    #\n    # Valid inputs:\n    #\n    # uint    int     float   double cx_double\n    # uvec    ivec    fvec    vec    cx_vec\n    # urowvec irowvec frowvec rowvec cx_rowvec\n    # umat    imat    fmat    mat    cx_mat\n    # ucube   icube   fcube   cube   cx_cube\n    #\n    # char    string  struct  structs func_lambda\n\n    functions = {\n      \"f\" : {\n        \"y\" : \"\",\n        \"x\" : \"\",\n      },\n      \"g\" : {\n        \"x\" : \"\",\n      },\n    }\n    includes = [\n      '#include \u003carmadillo\u003e',\n      'using namespace arma ;',\n    ]\n\nIn addition to defining includes at the bottom, it is possible to declare\nvariables manually by inserting type names into the respective empty strings.\nHowever, some times it is possible to guess some of the variable types from\ncontext.  To let the software try to guess variable types, run conversion with\nthe ``-s`` flag::\n\n    $ m2cpp example.m -s\n\nThe file ``example.m.py`` will then automatically be populated with data types\nfrom context::\n\n    # ...\n\n    functions = {\n      \"f\" : {\n        \"y\" : \"irowvec\",\n        \"x\" : \"irowvec\",\n      },\n      \"g\" : {\n        \"x\" : \"irowvec\",\n      },\n    }\n    includes = [\n      '#include \u003carmadillo\u003e',\n      'using namespace arma ;',\n    ]\n\nIt will not always be successful and some of the types might in some cases be\nwrong. It is therefore also possible to adjust these values manually at any\ntime.\n\nHaving run the conversion with the variables converted, creates a new output\nfor ``example.m.hpp``::\n\n    #include \u003carmadillo\u003e\n    using namespace arma ;\n\n    irowvec f(irowvec x)\n    {\n      irowvec y ;\n      y = x+4 ;\n      return y ;\n    }\n\n    void g()\n    {\n      irowvec x ;\n      int _x [] = [1, 2, 3] ;\n      x = irowvec(_x, 3, false) ;\n      f(x) ;\n    }\n\nThis is valid and runnable C++ code. For such a small example, no manual\nadjustments were necessary.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonathf%2Fmatlab2cpp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjonathf%2Fmatlab2cpp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonathf%2Fmatlab2cpp/lists"}