The Wayback Machine - https://web.archive.org/web/20250521202525/https://github.com/TheAlgorithms/Python/pull/7493
Skip to content

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

Closed
wants to merge 2 commits into from

Conversation

zivdar001matin
Copy link

@zivdar001matin zivdar001matin commented Oct 21, 2022

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

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.
@algorithms-keeper algorithms-keeper bot added require descriptive names This PR needs descriptive function and/or variable names require tests Tests [doctest/unittest/pytest] are required labels Oct 21, 2022
Copy link

@algorithms-keeper algorithms-keeper bot left a 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 review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to 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:

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:

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:

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:

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:

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:

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

@algorithms-keeper algorithms-keeper bot added awaiting reviews This PR is ready to be reviewed tests are failing Do not merge until tests pass labels Oct 21, 2022
Copy link
Contributor

@CaedenPH CaedenPH left a 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]>
Copy link

@algorithms-keeper algorithms-keeper bot left a 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 review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to 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:

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:

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:

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:

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:

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:

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

@algorithms-keeper algorithms-keeper bot removed the tests are failing Do not merge until tests pass label Oct 21, 2022
@CaedenPH
Copy link
Contributor

@cclauss Can we get your review please

@tianyizheng02
Copy link
Contributor

@zivdar001matin you should also put Fixes: #{$ISSUE_NO} in your comment rather than just #{$ISSUE_NO} so that GitHub automatically closes the issue when this PR is merged

@zivdar001matin
Copy link
Author

@zivdar001matin you should also put Fixes: #{$ISSUE_NO} in your comment rather than just #{$ISSUE_NO} so that GitHub automatically closes the issue when this PR is merged

Thanks for your edit suggestion.

@tianyizheng02
Copy link
Contributor

@zivdar001matin you should also put Fixes: #{$ISSUE_NO} in your comment rather than just #{$ISSUE_NO} so that GitHub automatically closes the issue when this PR is merged

Thanks for your edit suggestion.

It should not have backticks around them, otherwise GitHub won't recognize it. It should just look like this:

Fixes: #7490

If it worked, then you should see something like the following on the right panel:
screenshot

@zivdar001matin
Copy link
Author

@zivdar001matin you should also put Fixes: #{$ISSUE_NO} in your comment rather than just #{$ISSUE_NO} so that GitHub automatically closes the issue when this PR is merged

Thanks for your edit suggestion.

It should not have backticks around them, otherwise GitHub won't recognize it. It should just look like this:

Fixes: #7490

If it worked, then you should see something like the following on the right panel: screenshot

Oops x)))
Thank you a lot for explaining.

Copy link
Contributor

@tianyizheng02 tianyizheng02 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Some refactoring suggestions (see comments)
  2. This algorithm belongs more in the maths/ directory than the other/ directory since this is a numerical analysis algorithm
  3. Add doctests to all functions
Copy link
Contributor

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

Comment on lines +103 to +111
if value < 0:
flag = True
else:
flag = False

if mode == CordicMode.ROTATION:
return flag
else:
return not flag
Copy link
Contributor

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:

Suggested change
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:
Copy link
Contributor

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

Comment on lines +131 to +133
x = x << SHIFT
y = y << SHIFT
z = z << SHIFT
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
x = x << SHIFT
y = y << SHIFT
z = z << SHIFT
x <<= SHIFT
y <<= SHIFT
z <<= SHIFT
Comment on lines +139 to +142
if mode == CordicMode.ROTATION:
flag = Cordic.rot_decision(mode, z)
else:
flag = Cordic.rot_decision(mode, y)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

Comment on lines +145 to +147
x = x + (y >> i)
y = y - (x_temp >> i)
z = z + ELEM_ANGLE[i]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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]
Comment on lines +149 to +151
x = x - (y >> i)
y = y + (x_temp >> i)
z = z - ELEM_ANGLE[i]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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:
Copy link
Contributor

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?

Copy link
Member

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:
Copy link
Contributor

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?

Copy link
Member

@cclauss cclauss Aug 3, 2023

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:
Copy link
Contributor

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?

Comment on lines +13 to +37
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,
]
Copy link
Member

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.

Suggested change
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,
)
@cclauss cclauss closed this Sep 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting reviews This PR is ready to be reviewed require descriptive names This PR needs descriptive function and/or variable names require tests Tests [doctest/unittest/pytest] are required
4 participants