{"id":19079697,"url":"https://github.com/devzhk/pytorch-linalg","last_synced_at":"2025-04-30T05:43:54.418Z","repository":{"id":129491979,"uuid":"488059845","full_name":"devzhk/Pytorch-linalg","owner":"devzhk","description":null,"archived":false,"fork":false,"pushed_at":"2022-05-03T03:13:37.000Z","size":190,"stargazers_count":10,"open_issues_count":1,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-30T05:43:49.722Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","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/devzhk.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,"zenodo":null}},"created_at":"2022-05-03T02:54:45.000Z","updated_at":"2025-04-12T03:53:12.000Z","dependencies_parsed_at":"2023-04-25T16:02:21.828Z","dependency_job_id":null,"html_url":"https://github.com/devzhk/Pytorch-linalg","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devzhk%2FPytorch-linalg","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devzhk%2FPytorch-linalg/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devzhk%2FPytorch-linalg/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devzhk%2FPytorch-linalg/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/devzhk","download_url":"https://codeload.github.com/devzhk/Pytorch-linalg/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251651221,"owners_count":21621702,"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-09T02:15:39.507Z","updated_at":"2025-04-30T05:43:54.413Z","avatar_url":"https://github.com/devzhk.png","language":"Python","readme":"# Solving linear system in Pytorch\nThis repository implements most commonly used iterative methods for solving linear system Ax=b in Pytorch that can run on GPUs. \n\nThis repository includes Conjugate Gradient (CG) and GMRES. \n\nThe figures below compare my implementation (`torch gmres`, `torch cg` in the figures)\nof CG and GMRES against the implementation in Scipy and JAX with single and double precisions. \n\nA few useful features:\n1. Fully implemented in Pytorch and can run on GPUs.\n2. Not only support matrix A, but also custom linear operator that can produce Ax. \n3. Stable convergence. \n\n![](figs/relative_cg_test_4096.png)\n\n![](figs/relative_gmres_test_2048.png)\n\n## How to use\n\n### Demo 1\n```python\nimport torch\nfrom linalg import CG, GMRES\n\nA = torch.tensor([[3.0, 1.0, 0.0], \n                  [1.0, 2.0, -1.0], \n                  [0.0, -1.0, 1.0]])\n\nb = torch.tensor([1.0, 2.0, 3.0])\n\nsol1, info = CG(A, b)\nprint(f'Solution by CG: {sol1}')\n\nsol2, info = GMRES(A, b)\nprint(f'Solution by GMRES: {sol2}')\n\n```\nRemark: `info` is a tuple where `info[0]` is the number of iterations and `info[1]` is a list of relative residual error at each iteration. \n\n### Demo 2\n\n```python\nimport torch\nfrom linalg import CG, GMRES\nfrom functools import partial\n\nA = torch.tensor([[3.0, 1.0, 0.0], \n                  [1.0, 2.0, -1.0], \n                  [0.0, -1.0, 1.0]])\n\nb = torch.tensor([1.0, 2.0, 3.0])\n\ndef Avp(A, vec):\n    return A @ vec\n\n# create custom linear operator that produces Ax\nLinOp = partial(Avp, A)\n\nsol3, info = CG(LinOp, b)\nprint(f'Solution by CG: {sol3}')\n\nsol4, info = GMRES(LinOp, b)\nprint(f'Solution by GMRES: {sol4}')\n\n```\nSee more examples in `test.py`. ","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevzhk%2Fpytorch-linalg","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevzhk%2Fpytorch-linalg","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevzhk%2Fpytorch-linalg/lists"}