fortran2cc2fortran


Namefortran2cc2fortran JSON
Version 0.10 PyPI version JSON
download
home_pagehttps://github.com/hansalemaos/fortran2cc2fortran
SummaryFortran to C / C to Fortran (nested) index converter
upload_time2024-01-06 06:08:59
maintainer
docs_urlNone
authorJohannes Fischer
requires_python
licenseMIT
keywords numpy c fortran
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
# Fortran to C / C to Fortran (nested) index converter   

## pip install fortran2cc2fortran

### Tested against Windows 10 / Python 3.11 / Anaconda 



```python

FUNCTIONS
    calculate_flat_index_c(nested_index, array_shape)
        Calculate the flat index from a given nested index in C order.
        
        Parameters:
        - nested_index (list): Nested index in C order.
        - array_shape (tuple): Shape of the original array.
        
        Returns:
        - int: Flat index corresponding to the nested index in C order.
    
    calculate_flat_index_f(nested_index, array_shape)
        Calculate the flat index from a given nested index in Fortran order.
        
        Parameters:
        - nested_index (list): Nested index in Fortran order.
        - array_shape (tuple): Shape of the original array.
        
        Returns:
        - int: Flat index corresponding to the nested index in Fortran order.
    
    calculate_nested_index_c_order(flat_index, array_shape)
        Calculate the nested index of a given flat index in C order.
        
        Parameters:
        - flat_index (int): The flat index to be converted.
        - array_shape (tuple): Shape of the original array.
        
        Returns:
        - list: List of indices in C order corresponding to the flat index.
    
    calculate_nested_index_f_order(flat_index, array_shape)
        Calculate the nested index of a given flat index in Fortran order.
        
        Parameters:
        - flat_index (int): The flat index to be converted.
        - array_shape (tuple): Shape of the original array.
        
        Returns:
        - list: List of indices in Fortran order corresponding to the flat index.
    
    convert_c_to_f_index(c_index, array_shape)
        Convert C order to Fortran order and return the corresponding nested index.
        
        Parameters:
        - c_index (list): Nested index in C order.
        - array_shape (tuple): Shape of the original array.
        
        Returns:
        - list: Nested index in Fortran order corresponding to the C order index.
    
    convert_f_to_c_index(f_index, array_shape)
        Convert Fortran order to C order and return the corresponding nested index.
        
        Parameters:
        - f_index (list): Nested index in Fortran order.
        - array_shape (tuple): Shape of the original array.
        
        Returns:
        - list: Nested index in C order corresponding to the Fortran order index.
		
		
import numpy as np
from fortran2cc2fortran import (
    convert_f_to_c_index,
    convert_c_to_f_index,
    calculate_nested_index_c_order,
    calculate_flat_index_c,
    calculate_nested_index_f_order,
    calculate_flat_index_f,
)

indlist = [10, 5, 4, 2, 3]
listex = np.ascontiguousarray(
    np.copy(np.arange(np.product(indlist)).reshape(tuple(indlist)))
)
liste = listex.ravel(order="F")
liste2 = listex.ravel(order="C")
if not liste.flags["F_CONTIGUOUS"]:
    liste = np.asfortranarray(liste)
if not liste2.flags["C_CONTIGUOUS"]:
    liste2 = np.ascontiguousarray(liste2)
for l in range(len(liste)):
    value = liste[l]
    value2 = liste2[l]
    c_order_index = calculate_nested_index_c_order(
        flat_index=l, array_shape=listex.shape
    )
    flatindex1 = calculate_flat_index_f(
        nested_index=c_order_index, array_shape=listex.shape
    )
    f_order_index = calculate_nested_index_f_order(
        flat_index=l, array_shape=listex.shape
    )
    flatindex2 = calculate_flat_index_f(
        nested_index=f_order_index, array_shape=listex.shape
    )
    from_c_to_f = convert_c_to_f_index(c_index=c_order_index, array_shape=listex.shape)
    from_f_to_c = convert_f_to_c_index(f_index=f_order_index, array_shape=listex.shape)
    print(f"values/flat index c: {value2}")
    print(f"values/flat index c_order_index: {c_order_index}")

    print(f"values/flat index flatindex1: {flatindex1}")
    print(f"values/flat index f: {value}")
    print(f"values/flat index f_order_index: {f_order_index}")
    print(f"values/flat index flatindex2: {flatindex2}")
    print("00000000000000000000000000000000000000000000000000")
    print(f"values/flat index from_c_to_f: {from_c_to_f}")
    print(f"values/flat index from_f_to_c: {from_f_to_c}")
    print(
        "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    )

# ......
# values/flat index c: 10
# values/flat index c_order_index: [0, 0, 1, 1, 1]
# values/flat index flatindex1: 10
# values/flat index f: 24
# values/flat index f_order_index: [0, 1, 0, 0, 0]
# values/flat index flatindex2: 24
# 00000000000000000000000000000000000000000000000000
# values/flat index from_c_to_f: [0, 1, 0, 0, 0]
# values/flat index from_f_to_c: [0, 0, 1, 1, 1]
# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# values/flat index c: 11
# values/flat index c_order_index: [0, 0, 1, 1, 2]
# values/flat index flatindex1: 11
# values/flat index f: 144
# values/flat index f_order_index: [1, 1, 0, 0, 0]
# values/flat index flatindex2: 144
# 00000000000000000000000000000000000000000000000000
# values/flat index from_c_to_f: [1, 1, 0, 0, 0]
# values/flat index from_f_to_c: [0, 0, 1, 1, 2]
# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# ......
		
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/hansalemaos/fortran2cc2fortran",
    "name": "fortran2cc2fortran",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "numpy,C,fortran",
    "author": "Johannes Fischer",
    "author_email": "aulasparticularesdealemaosp@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/6f/3d/27cae45a288bca1c6ea0b4f9683f28261e713f18c251c3956280f2dae8e8/fortran2cc2fortran-0.10.tar.gz",
    "platform": null,
    "description": "\r\n# Fortran to C / C to Fortran (nested) index converter   \r\n\r\n## pip install fortran2cc2fortran\r\n\r\n### Tested against Windows 10 / Python 3.11 / Anaconda \r\n\r\n\r\n\r\n```python\r\n\r\nFUNCTIONS\r\n    calculate_flat_index_c(nested_index, array_shape)\r\n        Calculate the flat index from a given nested index in C order.\r\n        \r\n        Parameters:\r\n        - nested_index (list): Nested index in C order.\r\n        - array_shape (tuple): Shape of the original array.\r\n        \r\n        Returns:\r\n        - int: Flat index corresponding to the nested index in C order.\r\n    \r\n    calculate_flat_index_f(nested_index, array_shape)\r\n        Calculate the flat index from a given nested index in Fortran order.\r\n        \r\n        Parameters:\r\n        - nested_index (list): Nested index in Fortran order.\r\n        - array_shape (tuple): Shape of the original array.\r\n        \r\n        Returns:\r\n        - int: Flat index corresponding to the nested index in Fortran order.\r\n    \r\n    calculate_nested_index_c_order(flat_index, array_shape)\r\n        Calculate the nested index of a given flat index in C order.\r\n        \r\n        Parameters:\r\n        - flat_index (int): The flat index to be converted.\r\n        - array_shape (tuple): Shape of the original array.\r\n        \r\n        Returns:\r\n        - list: List of indices in C order corresponding to the flat index.\r\n    \r\n    calculate_nested_index_f_order(flat_index, array_shape)\r\n        Calculate the nested index of a given flat index in Fortran order.\r\n        \r\n        Parameters:\r\n        - flat_index (int): The flat index to be converted.\r\n        - array_shape (tuple): Shape of the original array.\r\n        \r\n        Returns:\r\n        - list: List of indices in Fortran order corresponding to the flat index.\r\n    \r\n    convert_c_to_f_index(c_index, array_shape)\r\n        Convert C order to Fortran order and return the corresponding nested index.\r\n        \r\n        Parameters:\r\n        - c_index (list): Nested index in C order.\r\n        - array_shape (tuple): Shape of the original array.\r\n        \r\n        Returns:\r\n        - list: Nested index in Fortran order corresponding to the C order index.\r\n    \r\n    convert_f_to_c_index(f_index, array_shape)\r\n        Convert Fortran order to C order and return the corresponding nested index.\r\n        \r\n        Parameters:\r\n        - f_index (list): Nested index in Fortran order.\r\n        - array_shape (tuple): Shape of the original array.\r\n        \r\n        Returns:\r\n        - list: Nested index in C order corresponding to the Fortran order index.\r\n\t\t\r\n\t\t\r\nimport numpy as np\r\nfrom fortran2cc2fortran import (\r\n    convert_f_to_c_index,\r\n    convert_c_to_f_index,\r\n    calculate_nested_index_c_order,\r\n    calculate_flat_index_c,\r\n    calculate_nested_index_f_order,\r\n    calculate_flat_index_f,\r\n)\r\n\r\nindlist = [10, 5, 4, 2, 3]\r\nlistex = np.ascontiguousarray(\r\n    np.copy(np.arange(np.product(indlist)).reshape(tuple(indlist)))\r\n)\r\nliste = listex.ravel(order=\"F\")\r\nliste2 = listex.ravel(order=\"C\")\r\nif not liste.flags[\"F_CONTIGUOUS\"]:\r\n    liste = np.asfortranarray(liste)\r\nif not liste2.flags[\"C_CONTIGUOUS\"]:\r\n    liste2 = np.ascontiguousarray(liste2)\r\nfor l in range(len(liste)):\r\n    value = liste[l]\r\n    value2 = liste2[l]\r\n    c_order_index = calculate_nested_index_c_order(\r\n        flat_index=l, array_shape=listex.shape\r\n    )\r\n    flatindex1 = calculate_flat_index_f(\r\n        nested_index=c_order_index, array_shape=listex.shape\r\n    )\r\n    f_order_index = calculate_nested_index_f_order(\r\n        flat_index=l, array_shape=listex.shape\r\n    )\r\n    flatindex2 = calculate_flat_index_f(\r\n        nested_index=f_order_index, array_shape=listex.shape\r\n    )\r\n    from_c_to_f = convert_c_to_f_index(c_index=c_order_index, array_shape=listex.shape)\r\n    from_f_to_c = convert_f_to_c_index(f_index=f_order_index, array_shape=listex.shape)\r\n    print(f\"values/flat index c: {value2}\")\r\n    print(f\"values/flat index c_order_index: {c_order_index}\")\r\n\r\n    print(f\"values/flat index flatindex1: {flatindex1}\")\r\n    print(f\"values/flat index f: {value}\")\r\n    print(f\"values/flat index f_order_index: {f_order_index}\")\r\n    print(f\"values/flat index flatindex2: {flatindex2}\")\r\n    print(\"00000000000000000000000000000000000000000000000000\")\r\n    print(f\"values/flat index from_c_to_f: {from_c_to_f}\")\r\n    print(f\"values/flat index from_f_to_c: {from_f_to_c}\")\r\n    print(\r\n        \"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\"\r\n    )\r\n\r\n# ......\r\n# values/flat index c: 10\r\n# values/flat index c_order_index: [0, 0, 1, 1, 1]\r\n# values/flat index flatindex1: 10\r\n# values/flat index f: 24\r\n# values/flat index f_order_index: [0, 1, 0, 0, 0]\r\n# values/flat index flatindex2: 24\r\n# 00000000000000000000000000000000000000000000000000\r\n# values/flat index from_c_to_f: [0, 1, 0, 0, 0]\r\n# values/flat index from_f_to_c: [0, 0, 1, 1, 1]\r\n# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n# values/flat index c: 11\r\n# values/flat index c_order_index: [0, 0, 1, 1, 2]\r\n# values/flat index flatindex1: 11\r\n# values/flat index f: 144\r\n# values/flat index f_order_index: [1, 1, 0, 0, 0]\r\n# values/flat index flatindex2: 144\r\n# 00000000000000000000000000000000000000000000000000\r\n# values/flat index from_c_to_f: [1, 1, 0, 0, 0]\r\n# values/flat index from_f_to_c: [0, 0, 1, 1, 2]\r\n# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n# ......\r\n\t\t\r\n```\r\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Fortran to C / C to Fortran (nested) index converter",
    "version": "0.10",
    "project_urls": {
        "Homepage": "https://github.com/hansalemaos/fortran2cc2fortran"
    },
    "split_keywords": [
        "numpy",
        "c",
        "fortran"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ea7e121c6696888b4912dd4fdcef1532001665b773311232017cdb2cf1bc948f",
                "md5": "6b6a2d2828eec23b4d93a77bc39d3a18",
                "sha256": "59626bca5c2487f75f5f9c50de882b07dec9b6c64d872eb38f8dd913fccf6690"
            },
            "downloads": -1,
            "filename": "fortran2cc2fortran-0.10-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "6b6a2d2828eec23b4d93a77bc39d3a18",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 5539,
            "upload_time": "2024-01-06T06:08:58",
            "upload_time_iso_8601": "2024-01-06T06:08:58.035474Z",
            "url": "https://files.pythonhosted.org/packages/ea/7e/121c6696888b4912dd4fdcef1532001665b773311232017cdb2cf1bc948f/fortran2cc2fortran-0.10-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "6f3d27cae45a288bca1c6ea0b4f9683f28261e713f18c251c3956280f2dae8e8",
                "md5": "834961555cfec8775e150deb417f5ef0",
                "sha256": "6b9b00614ff1e5b5388f0be7a63baab7af1d650876a8098a69aa9a9adf439825"
            },
            "downloads": -1,
            "filename": "fortran2cc2fortran-0.10.tar.gz",
            "has_sig": false,
            "md5_digest": "834961555cfec8775e150deb417f5ef0",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 4324,
            "upload_time": "2024-01-06T06:08:59",
            "upload_time_iso_8601": "2024-01-06T06:08:59.772063Z",
            "url": "https://files.pythonhosted.org/packages/6f/3d/27cae45a288bca1c6ea0b4f9683f28261e713f18c251c3956280f2dae8e8/fortran2cc2fortran-0.10.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-01-06 06:08:59",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "hansalemaos",
    "github_project": "fortran2cc2fortran",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "fortran2cc2fortran"
}
        
Elapsed time: 0.16642s