{"id":20189461,"url":"https://github.com/jackfsuia/vecparser","last_synced_at":"2025-06-21T01:34:06.470Z","repository":{"id":231879971,"uuid":"782940413","full_name":"jackfsuia/vecparser","owner":"jackfsuia","description":"Vectorize the Matlab/CVX for-loops as much as possible. ","archived":false,"fork":false,"pushed_at":"2025-02-13T04:37:27.000Z","size":316,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-04T06:51:10.221Z","etag":null,"topics":["convex","convex-optimization","cvx","cvxopt","cvxpy","lexer","matlab","matlab-script","modeling-and-simulation","octave","optimization","parser","python-script","vectorization"],"latest_commit_sha":null,"homepage":"https://ask.cvxr.com/t/how-to-vectorize-most-constraint-loops-in-cvx/9804","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jackfsuia.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,"publiccode":null,"codemeta":null}},"created_at":"2024-04-06T13:35:29.000Z","updated_at":"2025-02-14T07:40:21.000Z","dependencies_parsed_at":"2024-09-12T15:58:35.106Z","dependency_job_id":"b933fc7e-ec64-4d0b-8b37-22a1e249c858","html_url":"https://github.com/jackfsuia/vecparser","commit_stats":null,"previous_names":["jackfsuia/vecparser"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/jackfsuia/vecparser","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jackfsuia%2Fvecparser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jackfsuia%2Fvecparser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jackfsuia%2Fvecparser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jackfsuia%2Fvecparser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jackfsuia","download_url":"https://codeload.github.com/jackfsuia/vecparser/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jackfsuia%2Fvecparser/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261046647,"owners_count":23102318,"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":["convex","convex-optimization","cvx","cvxopt","cvxpy","lexer","matlab","matlab-script","modeling-and-simulation","octave","optimization","parser","python-script","vectorization"],"created_at":"2024-11-14T03:37:29.625Z","updated_at":"2025-06-21T01:34:01.455Z","avatar_url":"https://github.com/jackfsuia.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"## vecparser\n\nEnglish | [简体中文](README_zh.md)\n\nA parser that auto vectorizes your nested for-loops (in MATLAB, CVX) as much as possible, which is to save tons of run time (97% in some cases). This technique is based on my original post at https://ask.cvxr.com/t/how-to-vectorize-most-constraint-loops-in-cvx/9804 in 2022.\n\nHere is the [performance](#performance):\n\n\u003cp align=\"center\"\u003e\u003cimg src=\"images/loop.png\" width=\"60%\" \u003e\u003c/p\u003e\n\n## Table of Contents\n\n- [Table of Contents](#table-of-contents)\n- [Quick Start](#quick-start)\n- [Example](#example)\n  - [a Matlab example](#a-matlab-example)\n  - [a CVX example](#a-cvx-example)\n- [Performance](#performance)\n- [Notice](#notice)\n- [Future Work](#future-work)\n- [License](#license)\n- [Citation](#citation)\n- [Acknowledgement](#acknowledgement)\n## Quick Start\nRun\n```bash\ngit clone https://github.com/jackfsuia/vecparser.git \u0026\u0026 cd vecparser\n```\nThen install the requirements, run\n```bash\npip install sly\n```\nTo vectorize your for-loops from Matlab and CVX, write your loop (please first read the [Notice](#Notice)) to the [loop_eiditor.m](loop_eiditor.m), then run\n```bash\npython vecparser.py\n```\nThat's all! The results will be printed in [loop_eiditor.m](loop_editor.m) too, please refresh it.\n\n## Example\n### a Matlab example\nTo vectorize the Matlab loops, copy the loops you want to vectorize to [loop_eiditor.m](loop_editor.m), like this one:\n```matlab\n% loop_eiditor.m\nfor n1=1:N1\n    for n2=1:N2\n        x(n1,n2)= -(y(n1)+z(n2))*m(n2,n1);\n        if n1\u003en2*2\n            for n3=1:N3\n                for n4=1:N4\n                    if n1~=n2*n3 \u0026\u0026 n3\u003en4^3\n                        q(n4,n3,n2,n1)= -h(n2,n3,n1)+((n(n1,n3)+w(n4))*t(n2,n3,n1))^2;\n                    end\n                    u(n1,n2,n3,n4)= (p(n1,n3)+a(n4))*b(n2,n3,n1);\n                end\n            end\n        end\n    end\nend\n```\nthen run \n```bash\npython vecparser.py\n```\nThe result will be appended to [loop_eiditor.m](loop_eiditor.m) as\n```matlab\n% loop_eiditor.m\nfor n1=1:N1\n    for n2=1:N2\n        x(n1,n2)= -(y(n1)+z(n2))*m(n2,n1);\n        if n1\u003en2*2\n            for n3=1:N3\n                for n4=1:N4\n                    if n1~=n2*n3 \u0026\u0026 n3\u003en4^3\n                        q(n4,n3,n2,n1)= -h(n2,n3,n1)+((n(n1,n3)+w(n4))*t(n2,n3,n1))^2;\n                    end\n                    u(n1,n2,n3,n4)= (p(n1,n3)+a(n4))*b(n2,n3,n1);\n                end\n            end\n        end\n    end\nend\n\n%-------------------------vectorized by Vecparser as-----------------------\n\nx=-(repmat(y,1,N2)+permute(repmat(z,1,N1),[2,1])).*permute(m,[2,1]);\n\ncached_condition_for_this=((repmat((1:N1)',1,N2)\u003epermute(repmat((1:N2)'.*2,1,N1),[2,1])));\n\ncached_condition_for_this=(repmat((repmat((1:N1)',1,N2)\u003epermute(repmat((1:N2)'.*2,1,N1),[2,1])),1,1,N3,N4)\u0026permute((permute(repmat(repmat((1:N1)',1,N3,N2)~=permute(repmat(permute(repmat((1:N2)',1,N3),[2,1]).*repmat((1:N3)',1,N2),1,1,N1),[3,1,2]),1,1,1,N4),[1,4,3,2])\u0026permute(repmat(repmat((1:N3)',1,N4)\u003epermute(repmat((1:N4)'.^3,1,N3),[2,1]),1,1,N1,N2),[3,2,4,1])),[1,3,4,2]));\n\nq=permute(permute(permute((cached_condition_for_this),[1,4,2,3]).*(permute(repmat(-h,1,1,1,N4),[3,4,1,2])+(permute(repmat((repmat(n,1,1,N4)+permute(repmat(w,1,N1,N3),[2,3,1])),1,1,1,N2),[1,3,4,2]).*permute(repmat(t,1,1,1,N4),[3,4,1,2])).^2),[1,3,4,2])+permute(permute((1-permute((cached_condition_for_this),[1,3,4,2])),[1,3,4,2]).*permute(q,[4,1,3,2]),[1,3,4,2]),[4,3,2,1]);\n\ncached_condition_for_this=((repmat((1:N1)',1,N2)\u003epermute(repmat((1:N2)'.*2,1,N1),[2,1])));\n\nu=permute(permute(repmat((cached_condition_for_this),1,1,N3,N4).*permute((permute(repmat((repmat(p,1,1,N4)+permute(repmat(a,1,N1,N3),[2,3,1])),1,1,1,N2),[1,3,4,2]).*permute(repmat(b,1,1,1,N4),[3,4,1,2])),[1,3,4,2]),[1,4,2,3])+permute((1-permute((cached_condition_for_this),[1,3,4,2])),[1,3,4,2]).*permute(u,[1,4,2,3]),[1,3,4,2]);\n\n%-----Please clear this file each time before you write a new loop on------\n```\nNow copy the results to your matlab to replace the loops, and try them out.\n\n### a CVX example\nIt goes the same ways as Matlab, except all terms will be automatically moved to right side of inqualities for efficiency. So what you get will be like:\n```matlab\nfor n1=1:N1\n    for n2=1:N2\n        for n3=1:N3\n            if n1~=n2*n3 \u0026\u0026 n3\u003en2^3\n                x(n1) \u003e= y(n2) + z(n2, n3);\n            end\n        end\n    end\nend\n\n%-------------------------vectorized by Vecparser as-----------------------\n\ncached_condition_for_this=((repmat((1:N1)',1,N3,N2)~=permute(repmat(permute(repmat((1:N2)',1,N3),[2,1]).*repmat((1:N3)',1,N2),1,1,N1),[3,1,2])\u0026permute(repmat(repmat((1:N3)',1,N2)\u003epermute(repmat((1:N2)'.^3,1,N3),[2,1]),1,1,N1),[3,1,2])));\n\n0\u003e=(repmat(-(x),1,N3,N2)+permute(repmat((permute(repmat(y,1,N3),[2,1])+permute(z,[2,1])),1,1,N1),[3,1,2])).*(cached_condition_for_this);\n\n%-----Please clear this file each time before you write a new loop on------\n```\n\n\n\n## Performance\nI ran this performance test on my old computer: Intel(R) Xeon(R) CPU E5-2660 v2 @ 2.20GHz, RAM 16G. Here is what I got:\n![performance](images/loop.png)\n\nIt was observed that **when the loop of iterations is too big, vectorization of it might cause my computer to crash due to memory shortage,** therefore it ran slower than traditional loops in those extreme cases. It will be meaningful to see the trade off provided limited RAM, and how it'll perform when GPU come into play.\n\n## Notice\nNow it supports nested 'if' and non-if blocks anywhere in loop, but **don't support 'else'**, so please change it to 'if' instead. Support all the native element-wise operators: +, -, *, /, and so on. Natively support self-defined vectorized dimension-invariant functions. Support CVX style convex inequality like '\u003e=','\u003c=','=='. **It might have bugs, for being a experimental project.**\n\n## Future Work\n- Support reduce operators like `sum`, `norm`, `*`(matrix multiplication).\n- Explore its use on other languages (e.g., python)\n  \n## License\n\nVecparser is licensed under the Apache 2.0 License found in the [LICENSE](LICENSE) file in the root directory of this repository.\n\n## Citation\n\nIf this work is helpful, please kindly cite as:\n\n```bibtex\n@article{vecparser,\n  title={vecparser: a parser that vectorizes your nested for-loops (in MATLAB, CVX) as much as possible.}, \n  author={Yannan Luo},\n  year={2024},\n  url={https://github.com/jackfsuia/vecparser}\n}\n```\n## Acknowledgement\n\nThis repo uses the matlablexer from [pymatlabparser](https://github.com/jol-jol/pymatlabparser). This repo's [pymatlabparser](pymatlabparser) folder is entirely copied from there with nearly zero modifiacations. Thanks for their wonderful works.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjackfsuia%2Fvecparser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjackfsuia%2Fvecparser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjackfsuia%2Fvecparser/lists"}