Skip to content

Commit

Permalink
Update complex to support polar form and pure imaginary number lite…
Browse files Browse the repository at this point in the history
…rals

Signed-off-by: yamacir-kit <[email protected]>
  • Loading branch information
yamacir-kit committed Sep 14, 2022
1 parent 70bbc7d commit 252b1e6
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 18 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ sudo rm -rf /usr/local/share/meevax

| Target Name | Description
|:-------------------|:--
| `all` (default) | Build shared-library `libmeevax.0.4.230.so` and executable `meevax`.
| `all` (default) | Build shared-library `libmeevax.0.4.231.so` and executable `meevax`.
| `test` | Test executable `meevax`.
| `package` | Generate debian package `meevax_0.4.230_amd64.deb`.
| `package` | Generate debian package `meevax_0.4.231_amd64.deb`.
| `install` | Copy files into `/usr/local` __(1)__.
| `install.deb` | `all` + `package` + `sudo apt install <meevax>.deb`
| `safe-install.deb` | `all` + `test` + `package` + `sudo apt install <meevax>.deb`
Expand All @@ -122,7 +122,7 @@ __(1)__ Meevax installed by `make install` cannot be uninstalled by the system's
## Usage

```
Meevax Lisp System, version 0.4.230
Meevax Lisp System, version 0.4.231
Usage: meevax [OPTION...] [FILE...]
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.4.230
0.4.231
2 changes: 0 additions & 2 deletions include/meevax/kernel/complex.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ inline namespace kernel

auto imag() const noexcept -> const_reference;

static auto pattern() -> std::regex const&;

auto real() const noexcept -> const_reference;

explicit operator std::complex<double>();
Expand Down
24 changes: 15 additions & 9 deletions src/kernel/complex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,22 @@ inline namespace kernel
{
complex::complex(std::string const& token, int radix)
{
if (std::smatch result; std::regex_match(token, result, pattern()))
std::regex static const rectangular { R"(([+-]?.*)([+-].*)i)" };

std::regex static const polar { R"(([+-]?.*)@([+-]?.*))" };

if (std::smatch result; std::regex_match(token, result, rectangular))
{
std::get<0>(*this) = string_to_real(result[1].length() == 0 ? "0" : result.str(1), radix);
std::get<1>(*this) = string_to_real(result[2].length() == 1 ? result.str(2) + "1" : result.str(2), radix);
}
else if (std::regex_match(token, result, polar))
{
std::get<0>(*this) = string_to_real(result.str(1), radix);
std::get<1>(*this) = string_to_real(result.str(2), radix);
auto const magnitude = string_to_real(result.str(1), radix);
auto const angle = string_to_real(result.str(2), radix);

std::get<0>(*this) = magnitude * apply<cos>(angle);
std::get<1>(*this) = magnitude * apply<sin>(angle);
}
else
{
Expand All @@ -51,12 +63,6 @@ inline namespace kernel
return second;
}

auto complex::pattern() -> std::regex const&
{
std::regex static const pattern { R"(([+-]?.*)([+-].*)[ij])" };
return pattern;
}

auto complex::real() const noexcept -> const_reference
{
return first;
Expand Down
2 changes: 1 addition & 1 deletion src/kernel/number.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ inline namespace kernel
auto operator * (complex const& a, complex const& b) -> complex { return complex(a.real() * b.real() - a.imag() * b.imag(), a.imag() * b.real() + a.real() * b.imag()); }
auto operator / (complex const& a, complex const& b) -> complex { auto x = a.real() * b.real() + a.imag() * b.imag(); auto y = a.imag() * b.real() - a.real() * b.imag(); auto d = b.real() * b.real() + b.imag() * b.imag(); return complex(x / d, y / d); }
auto operator % (complex const& , complex const& ) -> complex { throw std::invalid_argument("unsupported operation"); }
auto operator ==(complex const& a, complex const& b) -> bool { return apply<equal_to>(a.real(), b.real()) and apply<equal_to>(a.imag(), b.imag()); }
auto operator ==(complex const& a, complex const& b) -> bool { return apply<equal_to>(a.real(), b.real()).as<bool>() and apply<equal_to>(a.imag(), b.imag()).as<bool>(); }
auto operator !=(complex const& a, complex const& b) -> bool { return not (a == b); }
auto operator < (complex const& , complex const& ) -> bool { throw std::invalid_argument("unsupported operation"); }
auto operator <=(complex const& , complex const& ) -> bool { throw std::invalid_argument("unsupported operation"); }
Expand Down
4 changes: 2 additions & 2 deletions test/r7rs.ss
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,7 @@

(check (sqrt 9) => 3)

; (check (sqrt -1) => +i)
(check (sqrt -1) => +i)

; (check (exact-integer-sqrt 4) => #,(values 2 0))

Expand Down Expand Up @@ -1555,4 +1555,4 @@

(check-report)

(exit (check-passed? 391))
(exit (check-passed? 392))

0 comments on commit 252b1e6

Please sign in to comment.