-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Added CORDIC algorithm #7493
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added CORDIC algorithm #7493
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Click here to look at the relevant links ⬇️
🔗 Relevant Links
Repository:
Python:
Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.
algorithms-keeper commands and options
algorithms-keeper actions can be triggered by commenting on this PR:
@algorithms-keeper reviewto trigger the checks for only added pull request files@algorithms-keeper review-allto trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.
| ) | ||
|
|
||
| @staticmethod | ||
| def arctan_div_cordic(x: int, y: int) -> float: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide descriptive name for the parameter: x
Please provide descriptive name for the parameter: y
| return Cordic.fixed_to_float(Cordic.cordic(x, y, 0, CordicMode.VECTOR)["z"]) | ||
|
|
||
| @staticmethod | ||
| def arctan_cordic(y: int) -> float: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide descriptive name for the parameter: y
| return Cordic.fixed_to_float(Cordic.cordic(1, y, 0, CordicMode.VECTOR)["z"]) | ||
|
|
||
| @staticmethod | ||
| def rot_decision(mode: CordicMode, value: int) -> bool: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file other/cordic.py, please provide doctest for the function rot_decision
| return not flag | ||
|
|
||
| @staticmethod | ||
| def cordic(x: int, y: int, z: int, mode: CordicMode) -> dict: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file other/cordic.py, please provide doctest for the function cordic
Please provide descriptive name for the parameter: x
Please provide descriptive name for the parameter: y
Please provide descriptive name for the parameter: z
| return result | ||
|
|
||
| @staticmethod | ||
| def fixed_to_float(fixed: int) -> float: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file other/cordic.py, please provide doctest for the function fixed_to_float
| return fixed / SHIFT_BASE | ||
|
|
||
| @staticmethod | ||
| def float_to_fixed(flt: float) -> int: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file other/cordic.py, please provide doctest for the function float_to_fixed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small changes, but otherwise looks awesome
Co-authored-by: Caeden Perelli-Harris <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Click here to look at the relevant links ⬇️
🔗 Relevant Links
Repository:
Python:
Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.
algorithms-keeper commands and options
algorithms-keeper actions can be triggered by commenting on this PR:
@algorithms-keeper reviewto trigger the checks for only added pull request files@algorithms-keeper review-allto trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.
| ) | ||
|
|
||
| @staticmethod | ||
| def arctan_div_cordic(x: int, y: int) -> float: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide descriptive name for the parameter: x
Please provide descriptive name for the parameter: y
| return Cordic.fixed_to_float(Cordic.cordic(x, y, 0, CordicMode.VECTOR)["z"]) | ||
|
|
||
| @staticmethod | ||
| def arctan_cordic(y: int) -> float: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide descriptive name for the parameter: y
| return Cordic.fixed_to_float(Cordic.cordic(1, y, 0, CordicMode.VECTOR)["z"]) | ||
|
|
||
| @staticmethod | ||
| def rot_decision(mode: CordicMode, value: int) -> bool: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file other/cordic.py, please provide doctest for the function rot_decision
| return not flag | ||
|
|
||
| @staticmethod | ||
| def cordic(x: int, y: int, z: int, mode: CordicMode) -> dict: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file other/cordic.py, please provide doctest for the function cordic
Please provide descriptive name for the parameter: x
Please provide descriptive name for the parameter: y
Please provide descriptive name for the parameter: z
| return result | ||
|
|
||
| @staticmethod | ||
| def fixed_to_float(fixed: int) -> float: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file other/cordic.py, please provide doctest for the function fixed_to_float
| return fixed / SHIFT_BASE | ||
|
|
||
| @staticmethod | ||
| def float_to_fixed(flt: float) -> int: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file other/cordic.py, please provide doctest for the function float_to_fixed
|
@cclauss Can we get your review please |
|
@zivdar001matin you should also put |
Thanks for your edit suggestion. |
It should not have backticks around them, otherwise GitHub won't recognize it. It should just look like this:
If it worked, then you should see something like the following on the right panel: |
Oops x))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Some refactoring suggestions (see comments)
- This algorithm belongs more in the
maths/directory than theother/directory since this is a numerical analysis algorithm - Add doctests to all functions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This algorithm belongs in the maths/ directory, IMO
| if value < 0: | ||
| flag = True | ||
| else: | ||
| flag = False | ||
|
|
||
| if mode == CordicMode.ROTATION: | ||
| return flag | ||
| else: | ||
| return not flag |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Notice that this function only returns true when value < 0 and mode == CordicMode.ROTATION are both the same value:
value < 0 |
mode == CordicMode.ROTATION |
rot_decision |
|---|---|---|
True |
True |
True |
True |
False |
False |
False |
True |
False |
False |
False |
True |
This makes sense:
- If we're in rotation mode, then we just return the flag (meaning that we return true when the flag is also true).
- If we're not in rotation mode, then we negate the flag (meaning that we return true when the flag is also false).
Thus, you can simplify the logic to just this:
| if value < 0: | |
| flag = True | |
| else: | |
| flag = False | |
| if mode == CordicMode.ROTATION: | |
| return flag | |
| else: | |
| return not flag | |
| return (value < 0) == (mode == CordicMode.ROTATION) |
| return not flag | ||
|
|
||
| @staticmethod | ||
| def cordic(x: int, y: int, z: int, mode: CordicMode) -> dict: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please specify the types within the dict
| x = x << SHIFT | ||
| y = y << SHIFT | ||
| z = z << SHIFT |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| x = x << SHIFT | |
| y = y << SHIFT | |
| z = z << SHIFT | |
| x <<= SHIFT | |
| y <<= SHIFT | |
| z <<= SHIFT |
| if mode == CordicMode.ROTATION: | ||
| flag = Cordic.rot_decision(mode, z) | ||
| else: | ||
| flag = Cordic.rot_decision(mode, y) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| if mode == CordicMode.ROTATION: | |
| flag = Cordic.rot_decision(mode, z) | |
| else: | |
| flag = Cordic.rot_decision(mode, y) | |
| rot_check_val = z if mode == CordicMode.ROTATION else y | |
| flag = Cordic.rot_decision(rot_check_val) |
Feel free to choose a better variable name, but the idea still stands
| x = x + (y >> i) | ||
| y = y - (x_temp >> i) | ||
| z = z + ELEM_ANGLE[i] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| x = x + (y >> i) | |
| y = y - (x_temp >> i) | |
| z = z + ELEM_ANGLE[i] | |
| x += (y >> i) | |
| y -= (x_temp >> i) | |
| z += ELEM_ANGLE[i] |
| x = x - (y >> i) | ||
| y = y + (x_temp >> i) | ||
| z = z - ELEM_ANGLE[i] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| x = x - (y >> i) | |
| y = y + (x_temp >> i) | |
| z = z - ELEM_ANGLE[i] | |
| x -= (y >> i) | |
| y += (x_temp >> i) | |
| z -= ELEM_ANGLE[i] |
| VECTOR = 1 | ||
|
|
||
|
|
||
| class Cordic: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since "CORDIC" is usually written in all caps, could we make it all caps in class and function names here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not a fan of this request. I would rather stick with https://beta.ruff.rs/docs/rules/#pep8-naming-n
|
|
||
| class Cordic: | ||
| @staticmethod | ||
| def cos_cordic(theta: int) -> float: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we make numerical variables float or int | float rather than just int?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If so then some of our tests need to use float input.
| ) | ||
|
|
||
| @staticmethod | ||
| def arctan_div_cordic(x: int, y: int) -> float: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For functions that pass in both the x- and y-coords of a vector, why not pass in the vector as a tuple?
| ELEM_ANGLE = [ | ||
| 2949120, | ||
| 1740967, | ||
| 919789, | ||
| 466945, | ||
| 234379, | ||
| 117304, | ||
| 58666, | ||
| 29335, | ||
| 14668, | ||
| 7334, | ||
| 3667, | ||
| 1833, | ||
| 917, | ||
| 458, | ||
| 229, | ||
| 115, | ||
| 57, | ||
| 29, | ||
| 14, | ||
| 7, | ||
| 4, | ||
| 2, | ||
| 1, | ||
| ] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WIll this work? Tuples take less memory than lists and they are a signal to the reader that the contents will not change at runtime.
| ELEM_ANGLE = [ | |
| 2949120, | |
| 1740967, | |
| 919789, | |
| 466945, | |
| 234379, | |
| 117304, | |
| 58666, | |
| 29335, | |
| 14668, | |
| 7334, | |
| 3667, | |
| 1833, | |
| 917, | |
| 458, | |
| 229, | |
| 115, | |
| 57, | |
| 29, | |
| 14, | |
| 7, | |
| 4, | |
| 2, | |
| 1, | |
| ] | |
| ELEM_ANGLE = ( | |
| 2949120, | |
| 1740967, | |
| 919789, | |
| 466945, | |
| 234379, | |
| 117304, | |
| 58666, | |
| 29335, | |
| 14668, | |
| 7334, | |
| 3667, | |
| 1833, | |
| 917, | |
| 458, | |
| 229, | |
| 115, | |
| 57, | |
| 29, | |
| 14, | |
| 7, | |
| 4, | |
| 2, | |
| 1, | |
| ) |



Describe your change:
I have added this algorithm so that it can help others to use it and get to know how simply CORDIC works.
Fixes: #7490
Checklist:
Fixes: #{$ISSUE_NO}.