Skip to content

Conversation

@NoahSchiro
Copy link

Describe your change:

I added the 6 trigonometric functions. For sin and cos I used a maclaurin series approximations. For tan, csc, sec, and cot, I used trig identities and my aforementioned sin and cos algorithm. No external library (including the math library that ships with python) was used. I did use math so that I could use a precise value of pi, but this only served for accurate testing of the algorithms and did not actually factor into the algorithms themselves.

I think the cosine algorithm needs some work, it has a variance around 1E-15 currently but I am unsure of how to improve the accuracy (there are some pythonic limits to how far I can take an infinite series. Namely an infinite series which contains a factorial).

Additionally, I think there should also be the inverse trigonometric functions in this repository but I am not currently aware of an algorithm and it might be better saved for a separate pull request

  • 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}.

@NoahSchiro NoahSchiro requested a review from Kush1101 as a code owner December 6, 2021 00:56
@ghost ghost added require descriptive names This PR needs descriptive function and/or variable names require tests Tests [doctest/unittest/pytest] are required labels Dec 6, 2021
Copy link

@ghost ghost 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.

# A lot of the maclaurin series require
# a factorial in their expressions. This
# is just a simple O(n) algo for factorial
def factorial(x: int) -> int:
Copy link

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 maths/trig_functions.py, please provide doctest for the function factorial

Please provide descriptive name for the parameter: x



def sine(
x: float,
Copy link

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

return sum

def cosine(
x: float,
Copy link

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


return sum

def tangent(x: float) -> float:
Copy link

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

except ZeroDivisionError:
pass

def cosecant(x: float) -> float:
Copy link

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

pass

def secant(
x: float,
Copy link

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

except ZeroDivisionError:
pass

def cotangent(x: float) -> float:
Copy link

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

@ghost ghost added the awaiting reviews This PR is ready to be reviewed label Dec 6, 2021
@NoahSchiro
Copy link
Author

As for the factorial problem: I thought since it is out of the scope of the algorithms that are being focused on here, and only exists as a helper function, doctests was unnecessary.

As for the descriptive parameter name problem: x = input into a function. This is a common practice within mathematics in this context and I believe the x is plenty descriptive within the context of these functions.

@ghost ghost added the tests are failing Do not merge until tests pass label Dec 6, 2021
Copy link

@ghost ghost 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.

# A lot of the maclaurin series require
# a factorial in their expressions. This
# is just a simple O(n) algo for factorial
def factorial(x: int) -> int:
Copy link

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 maths/trig_functions.py, please provide doctest for the function factorial

Please provide descriptive name for the parameter: x



def sine(
x: float,
Copy link

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

return sum

def cosine(
x: float,
Copy link

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


return sum

def tangent(x: float) -> float:
Copy link

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

except ZeroDivisionError:
pass

def cosecant(x: float) -> float:
Copy link

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

pass

def secant(
x: float,
Copy link

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

except ZeroDivisionError:
pass

def cotangent(x: float) -> float:
Copy link

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

@TheAlgorithms TheAlgorithms deleted a comment from wwowowo Jan 30, 2022
@TheAlgorithms TheAlgorithms deleted a comment from yumengDuang Jan 30, 2022
@cclauss cclauss closed this Jan 30, 2022
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 tests are failing Do not merge until tests pass

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants