Skip to content

Commit

Permalink
Abs of complex number
Browse files Browse the repository at this point in the history
  • Loading branch information
fintarin committed Aug 26, 2023
1 parent 07bc0c2 commit f24a078
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 16 deletions.
10 changes: 10 additions & 0 deletions include/fintamath/numbers/ComplexFunctions.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#include "fintamath/numbers/Complex.hpp"
#include "fintamath/numbers/Rational.hpp"

namespace fintamath {

Complex abs(const Complex &rhs);

}
8 changes: 8 additions & 0 deletions src/fintamath/functions/arithmetic/Abs.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "fintamath/functions/arithmetic/Abs.hpp"

#include "fintamath/functions/powers/Sqrt.hpp"
#include "fintamath/numbers/Complex.hpp"
#include "fintamath/numbers/Integer.hpp"
#include "fintamath/numbers/IntegerFunctions.hpp"
#include "fintamath/numbers/Rational.hpp"
Expand Down Expand Up @@ -31,6 +33,12 @@ std::unique_ptr<IMathObject> Abs::multiAbsSimplify(const INumber &rhs) {
return abs(inRhs).toMinimalObject();
});

outMultiAbs.add<Complex>([](const Complex &inRhs) {
// https://en.wikipedia.org/wiki/Absolute_value#Complex_numbers
auto sumOfSquares = *(inRhs.real() * inRhs.real()) + *(inRhs.imag() * inRhs.imag());
return Sqrt()(*sumOfSquares);
});

return outMultiAbs;
}();

Expand Down
7 changes: 3 additions & 4 deletions tests/src/expressions/ExpressionTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -952,10 +952,9 @@ TEST(ExpressionTests, stringConstructorTest) {
EXPECT_EQ(Expression("asin(2)").toString(), "asin(2)");
EXPECT_EQ(Expression("acos(2)").toString(), "acos(2)");

// TODO! implement abs
EXPECT_EQ(Expression("abs(I)").toString(), "abs(I)");
EXPECT_EQ(Expression("abs(I + 1)").toString(), "abs(1 + I)");
EXPECT_EQ(Expression("abs(3I + 2)").toString(), "abs(2 + 3 I)");
EXPECT_EQ(Expression("abs(I)").toString(), "1");
EXPECT_EQ(Expression("abs(I + 1)").toString(), "sqrt(2)");
EXPECT_EQ(Expression("abs(3I + 2)").toString(), "sqrt(13)");

// TODO: implement
EXPECT_EQ(Expression("sin(I + 1)").toString(), "sin(1 + I)");
Expand Down
23 changes: 11 additions & 12 deletions tests/src/functions/arithmetic/AbsTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,17 @@ TEST(AbsTests, callTest) {
EXPECT_EQ(f(Real("8465132.321651651"))->toString(), "8465132.321651651");
EXPECT_EQ(f(Real("-98465136846516354684651.351"))->toString(), "98465136846516354684651.351");

// TODO! implement
EXPECT_EQ(f(Complex(2, 0))->toString(), "abs(2)");
EXPECT_EQ(f(Complex(0, 2))->toString(), "abs(2 I)");
EXPECT_EQ(f(Complex(2, 2))->toString(), "abs(2 + 2 I)");
EXPECT_EQ(f(Complex(3, 2))->toString(), "abs(3 + 2 I)");
EXPECT_EQ(f(Complex(2, 3))->toString(), "abs(2 + 3 I)");
EXPECT_EQ(f(Complex(-2, 0))->toString(), "abs(-2)");
EXPECT_EQ(f(Complex(0, -2))->toString(), "abs(-2 I)");
EXPECT_EQ(f(Complex(2, -2))->toString(), "abs(2 - 2 I)");
EXPECT_EQ(f(Complex(-3, 2))->toString(), "abs(-3 + 2 I)");
EXPECT_EQ(f(Complex(2, -3))->toString(), "abs(2 - 3 I)");
EXPECT_EQ(f(Complex(-2, -3))->toString(), "abs(-2 - 3 I)");
EXPECT_EQ(f(Complex(2, 0))->toString(), "2");
EXPECT_EQ(f(Complex(0, 2))->toString(), "2");
EXPECT_EQ(f(Complex(2, 2))->toString(), "2 sqrt(2)");
EXPECT_EQ(f(Complex(3, 2))->toString(), "sqrt(13)");
EXPECT_EQ(f(Complex(2, 3))->toString(), "sqrt(13)");
EXPECT_EQ(f(Complex(-2, 0))->toString(), "2");
EXPECT_EQ(f(Complex(0, -2))->toString(), "2");
EXPECT_EQ(f(Complex(2, -2))->toString(), "2 sqrt(2)");
EXPECT_EQ(f(Complex(-3, 2))->toString(), "sqrt(13)");
EXPECT_EQ(f(Complex(2, -3))->toString(), "sqrt(13)");
EXPECT_EQ(f(Complex(-2, -3))->toString(), "sqrt(13)");

EXPECT_EQ(f(Variable("a"))->toString(), "abs(a)");

Expand Down

0 comments on commit f24a078

Please sign in to comment.