{"id":16739450,"url":"https://github.com/williamfalcon/pytorch-complex-tensor","last_synced_at":"2025-03-17T01:31:57.197Z","repository":{"id":37602717,"uuid":"169622985","full_name":"williamFalcon/pytorch-complex-tensor","owner":"williamFalcon","description":"Unofficial complex tensor and scalar support for Pytorch","archived":false,"fork":false,"pushed_at":"2022-12-08T01:35:43.000Z","size":154,"stargazers_count":79,"open_issues_count":12,"forks_count":8,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-02-27T16:22:05.300Z","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/williamFalcon.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}},"created_at":"2019-02-07T18:44:45.000Z","updated_at":"2024-12-20T16:51:24.000Z","dependencies_parsed_at":"2023-01-24T04:00:14.987Z","dependency_job_id":null,"html_url":"https://github.com/williamFalcon/pytorch-complex-tensor","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/williamFalcon%2Fpytorch-complex-tensor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/williamFalcon%2Fpytorch-complex-tensor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/williamFalcon%2Fpytorch-complex-tensor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/williamFalcon%2Fpytorch-complex-tensor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/williamFalcon","download_url":"https://codeload.github.com/williamFalcon/pytorch-complex-tensor/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243836015,"owners_count":20355615,"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-10-13T00:51:11.997Z","updated_at":"2025-03-17T01:31:56.690Z","avatar_url":"https://github.com/williamFalcon.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n\u003c!--   \u003ca href=\"https://williamfalcon.github.io/test-tube/\"\u003e\n    \u003cimg alt=\"react-router\" src=\"https://raw.githubusercontent.com/williamfalcon/test-tube/master/imgs/test_tube_logo.png\" width=\"50\"\u003e\n  \u003c/a\u003e --\u003e\n\u003c/p\u003e\n\u003ch2 align=\"center\"\u003e\n  Pytorch Complex Tensor\n\u003c/h2\u003e\n\u003cp align=\"center\"\u003e\n  Unofficial complex Tensor support for Pytorch \n\u003c/p\u003e\n\u003cp align=\"center\"\u003e\n\u003ca href=\"https://badge.fury.io/py/pytorch-complex-tensor\"\u003e\u003cimg src=\"https://badge.fury.io/py/pytorch-complex-tensor.svg\" alt=\"PyPI version\" height=\"18\"\u003e\u003c/a\u003e\u003c!--   \u003ca href=\"https://williamfalcon.github.io/test-tube/\"\u003e\u003cimg src=\"https://readthedocs.org/projects/test-tube/badge/?version=latest\"\u003e\u003c/a\u003e --\u003e\n  \u003ca href=\"https://github.com/williamFalcon/pytorch-complex-tensor/blob/master/LICENSE\"\u003e\u003cimg src=\"https://img.shields.io/badge/License-MIT-yellow.svg\"\u003e\u003c/a\u003e\n      \u003ca href=\"https://circleci.com/gh/williamFalcon/pytorch-complex-tensor/\"\u003e\u003cimg src=\"https://circleci.com/gh/williamFalcon/pytorch-complex-tensor.svg?style=svg\"\u003e\u003c/a\u003e\n\n\u003c/p\u003e   \n\n### How it works\n\nTreats first half of tensor as real, second as imaginary.  A few arithmetic operations are implemented to emulate complex arithmetic. Supports gradients.   \n\n### Installation\n```bash\npip install pytorch-complex-tensor\n```\n\n### Example:   \nEasy import    \n\n```python   \nfrom pytorch_complex_tensor import ComplexTensor\n```   \n\nInit tensor    \n\n```python\n# equivalent to:\n# np.asarray([[1+3j, 1+3j, 1+3j], [2+4j, 2+4j, 2+4j]]).astype(np.complex64)\nC = ComplexTensor([[1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4]])\nC.requires_grad = True\n```   \n\nPretty printing\n\n```python\nprint(C)\n# tensor([['(1.0+3.0j)' '(1.0+3.0j)' '(1.0+3.0j)'],\n#         ['(2.0+4.0j)' '(2.0+4.0j)' '(2.0+4.0j)']])\n```\n\nhandles absolute value properly for complex tensors\n\n```python\n# complex absolute value implementation\nprint(C.abs())\n# tensor([[3.1623, 3.1623, 3.1623],\n#         [4.4721, 4.4721, 4.4721]], grad_fn=\u003cSqrtBackward\u003e)\n```\n\n\nprints correct sizing treating first half of matrix as real, second as imag\n```python\nprint(C.size())\n# torch.Size([2, 3])\n```\n\nmultiplies both complex and real tensors\n```python\n# show matrix multiply with real tensor\n# also works with complex tensor\nx = torch.Tensor([[3, 3], [4, 4], [2, 2]])\nxy = C.mm(x)\nprint(xy)\n# tensor([['(9.0+27.0j)' '(9.0+27.0j)'],\n#         ['(18.0+36.0j)' '(18.0+36.0j)']])\n```\n\nreduce ops return ComplexScalar\n```python\nxy = xy.sum()\n\n# this is now a complex scalar (thin wrapper with .real, .imag)\nprint(type(xy))\n# pytorch_complex_tensor.complex_scalar.ComplexScalar\n\nprint(xy)\n# (54+126j)\n```\n\nwhich can be used for gradients without breaking anything... (differentiates wrt the real part)\n```python\n# calculate dxy / dC\n# for complex scalars, grad is wrt the real part\nxy.backward()\nprint(C.grad)\n# tensor([['(6.0-0.0j)' '(8.0-0.0j)' '(4.0-0.0j)'],\n#         ['(6.0-0.0j)' '(8.0-0.0j)' '(4.0-0.0j)']])\n```\n\nsupports all section ops...  \n```python\nprint(C[-1])\nprint(C[0, 0:-2, ...])\nprint(C[0, ..., 0])\n```\n\n\n### Supported ops:\n| Operation | complex tensor | real tensor | complex scalar | real scalar |\n| ----------| :-------------:|:-----------:|:--------------:|:-----------:|   \n| addition | Y | Y | Y | Y |\n| subtraction | Y | Y | Y | Y |\n| multiply | Y | Y | Y | Y |\n| mm | Y | Y | Y | Y |\n| abs | Y | - | - | - |\n| t | Y | - | - | - |\n| grads | Y | Y | Y | Y |   \n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwilliamfalcon%2Fpytorch-complex-tensor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwilliamfalcon%2Fpytorch-complex-tensor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwilliamfalcon%2Fpytorch-complex-tensor/lists"}