Skip to content
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

Create Complex Numbers Utils #5128

Open
wants to merge 22 commits into
base: master
Choose a base branch
from

Conversation

rozi26
Copy link

@rozi26 rozi26 commented Apr 29, 2024

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized it.
  • All filenames are in PascalCase.
  • All functions and variable names follow Java naming conventions.
  • All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
  • All new code is formatted with clang-format -i --style=file path/to/your/file.java

Overview

This contribution introduces algorithms for performing arithmetic and trigonometric operations on complex numbers.

Key Features of the Code

  • Basic Arithmetic Operations: Includes methods to add, subtract, multiply, divide, power, and to use logarithm on complex numbers.
  • Trigonometric Functions : Encompasses a full set of trigonometric operations adapted for complex numbers, such as sine, cosine, tangent, cotangent, arcsine, arccosine, arctangent, and arccotangent.
  • Exception Handling : Incorporates error handling to manage undefined operations, such as division by zero or logarithm of zero.
  • Comprehensive Test Suite: Accompanied by a detailed test suite which verifies the functionality and accuracy of all operations.

@rozi26 rozi26 marked this pull request as ready for review April 29, 2024 18:37
@codecov-commenter
Copy link

codecov-commenter commented Apr 29, 2024

Codecov Report

Attention: Patch coverage is 90.56604% with 5 lines in your changes are missing coverage. Please review.

Project coverage is 37.52%. Comparing base (ede3e46) to head (799ba61).

Files Patch % Lines
...ava/com/thealgorithms/maths/ComplexNumberUtil.java 90.56% 1 Missing and 4 partials ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #5128      +/-   ##
============================================
+ Coverage     37.36%   37.52%   +0.15%     
- Complexity     2350     2374      +24     
============================================
  Files           517      518       +1     
  Lines         15618    15671      +53     
  Branches       2971     2976       +5     
============================================
+ Hits           5836     5880      +44     
- Misses         9495     9498       +3     
- Partials        287      293       +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Comment on lines +41 to +43
public static ComplexNumber add(ComplexNumber num1, ComplexNumber num2) {
return new ComplexNumber(num1.REAL + num2.REAL, num1.IMAGINARY + num2.IMAGINARY);
}
Copy link
Member

Choose a reason for hiding this comment

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

Why did you decide to make it a static method? In the standard implementation of Complex add is an object method.

Copy link
Author

Choose a reason for hiding this comment

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

I decided to make it a static method for a few reasons:

Uniformity in Code: The file MatrixUtil.java, which is similar to this, uses static methods for the operations.
Ease of Expansion: Creating independent static methods makes it easier to add more advanced operations for complex numbers than integrating additional code into the ComplexNumber class itself.
Clarity: I find it easier to conceptualize operations as direct interactions between two entities, A and B (i.e., how do you add them), rather than how one adds B to A.

These are my reasons, but if you believe it should be an instance method, I would be happy to make that change.

Copy link
Member

Choose a reason for hiding this comment

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

Making them static you loose all of the object oriented magic. The whole thing could be then simpler, because the ComplexNumberUtil would not be needed. Furthermore, you have marked both of the fields REAL and IMAGINARY as final, so it is pretty clear that the add method will not change them.

Copy link
Member

@vil02 vil02 left a comment

Choose a reason for hiding this comment

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

Please add the missing tests.

Copy link
Member

Choose a reason for hiding this comment

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

Checking the defined functions just on single inputs might not be enough. Please express the important tests as parametrized tests. Start first with add.

Furthermore I would still highly recommend to reach the legendary 100% coverage here.

@github-actions github-actions bot force-pushed the feature/improve_code_look branch from 95cae76 to 3495410 Compare May 7, 2024 08:04
@github-actions github-actions bot force-pushed the feature/improve_code_look branch from 6f12753 to 592f443 Compare May 7, 2024 08:05
@vil02
Copy link
Member

vil02 commented May 9, 2024

There is some build error.

Also please update your branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants