Skip to content

Commit

Permalink
Improve complex-numbers (#102)
Browse files Browse the repository at this point in the history
* Add complex-numbers exercise
* Remove non-implemented comment, and improve readability
* [no important files changed]
  • Loading branch information
ageron authored Sep 21, 2024
1 parent ad09ee5 commit 33345b3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 deletions.
30 changes: 15 additions & 15 deletions exercises/practice/complex-numbers/.meta/Example.roc
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,29 @@ imaginary : Complex -> F64
imaginary = \z -> z.im

add : Complex, Complex -> Complex
add = \z1, z2 -> {
re: z1.re + z2.re,
im: z1.im + z2.im,
add = \{ re: a, im: b }, { re: c, im: d } -> {
re: a + c,
im: b + d,
}

sub : Complex, Complex -> Complex
sub = \z1, z2 -> {
re: z1.re - z2.re,
im: z1.im - z2.im,
sub = \{ re: a, im: b }, { re: c, im: d } -> {
re: a - c,
im: b - d,
}

mul : Complex, Complex -> Complex
mul = \z1, z2 -> {
re: z1.re * z2.re - z1.im * z2.im,
im: z1.re * z2.im + z1.im * z2.re,
mul = \{ re: a, im: b }, { re: c, im: d } -> {
re: a * c - b * d,
im: a * d + b * c,
}

div : Complex, Complex -> Complex
div = \z1, z2 ->
denominator = z2.re * z2.re + z2.im * z2.im
div = \{ re: a, im: b }, { re: c, im: d } ->
denominator = c * c + d * d
{
re: (z1.re * z2.re + z1.im * z2.im) / denominator,
im: (z1.im * z2.re - z1.re * z2.im) / denominator,
re: (a * c + b * d) / denominator,
im: (b * c - a * d) / denominator,
}

conjugate : Complex -> Complex
Expand All @@ -41,8 +41,8 @@ conjugate = \z -> {
}

abs : Complex -> F64
abs = \z ->
z.re * z.re + z.im * z.im |> Num.sqrt
abs = \{ re: a, im: b } ->
a * a + b * b |> Num.sqrt

exp : Complex -> Complex
exp = \z ->
Expand Down
3 changes: 0 additions & 3 deletions exercises/practice/complex-numbers/.meta/template.j2
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,13 @@ expect
{%- else %}
result |> Num.isApproxEq {{ subcase["expected"] | to_roc }} {}
{%- endif %}

{%- elif subcase["input"]["z1"] %}
expect
z1 = {{ plugins.to_complex_number(subcase["input"]["z1"]) }}
z2 = {{ plugins.to_complex_number(subcase["input"]["z2"]) }}
result = z1 |> {{ subcase["property"] }} z2
expected = {{ plugins.to_complex_number(subcase["expected"]) }}
result |> isApproxEq expected
{%- else %}
# This test case is not implemented: perhaps you can implement it?
{%- endif %}

{% endfor %}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# These tests are auto-generated with test data from:
# https://github.com/exercism/problem-specifications/tree/main/exercises/complex-numbers/canonical-data.json
# File last updated on 2024-09-20
# File last updated on 2024-09-21
app [main] {
pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.15.0/SlwdbJ-3GR7uBWQo6zlmYWNYOxnvo8r6YABXD-45UOw.tar.br",
}
Expand Down

0 comments on commit 33345b3

Please sign in to comment.