Skip to content

Conversation

colinleach
Copy link
Contributor

As requested, though we still don't have a Concept Exercise to pair with it. I'll mark it draft until we can get an introduction.md.

Meanwhile, I had a look through some of the concepts I PR'd back in 2023, and was shocked how bad they are. That was before I had the highly educational experience of working with Bethany on the Python track (she's a very careful teacher). I've changed their status to draft, and I'll try to rewrite them sometime when I have the time, energy and functional brainpower simultaneously. I haven't looked at the old Concept Exercises yet, I hope they are in better shape.

@colinleach colinleach marked this pull request as draft February 23, 2025 01:11
## Basics

A `complex` number in R is a single value.
In use, however, it can be thought of as a pair of numbers: usually but not always floating-point.
Copy link
Contributor

Choose a reason for hiding this comment

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

What do you mean here? I find it confusing as ?complex states that: "Internally, complex numbers are stored as a pair of double precision numbers, either or both of which can be NaN(including NA, see NA_complex_ and above) or plus or minus infinity."

Copy link
Contributor

@Nerwosolek Nerwosolek left a comment

Choose a reason for hiding this comment

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

I had some suggestions, if you don't find them interesting just merge.
If you don't have time and would like to implement some of them, let me know if I can help.

For now, all this means is that the imaginary part _by definition_ satisfies the following equality:

```r
> 1i * 1i == -1
Copy link
Contributor

Choose a reason for hiding this comment

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

I would even say that it satisfies: 1i * 1i == -1 + 0i.
R probably does some coercion before comparing different types or operator is defined that way it can compare different types.
But 1i * 1i results in complex number.


Any [mathematical][math-complex] or [electrical engineering][engineering-complex] introduction to complex numbers will cover this, should you want to dig into the topic.

Alternatively, Exercism has a `Complex Numbers` practice exercise where you can implement a complex number class with these operations from first principles.
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe add link to that exercise as we have it merged already?

> sqrt(z1)
[1] 1.414214+0.707107i

> sqrt(-1) # fails!
Copy link
Contributor

Choose a reason for hiding this comment

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

What about adding also positive example that sqrt(-1+0i) will work?

- The `(real, imag)` representation of `z1` in effect uses Cartesian coordinates on the complex plane.
- The same complex number can be represented in `(r, θ)` notation, using polar coordinates.
- Here, `r` and `θ` are given by `abs(z1)` and `Arg(z1)` respectively.

Copy link
Contributor

Choose a reason for hiding this comment

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

What do you think about adding some example how can we rotate 2D vectors in R easily.
Like in here we rotate vector (1,2) by 90 degrees leftwise:

x <- 1
y <- 2
angle <- pi/2
rot_z <- complex(modulus = 1, argument =  angle)
rot_z * (x + 1i*y)

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.

2 participants