Complex Numbres

Can you j oporator to define a complex numbers?
like this
val complex = 3 + j4
or
val complex = 3 + i4

data class Complex(val re: Double, val im: Double)

val Double.j: Complex
  get() = Complex(0, this)

operator fun Double.plus(c: Complex): Complex{
  return Complex(this + c.re, c.im)
}

val res = 3 + 4.j

Of course you need to redeclare all mathematical operators. Also I recommend to use Commons Math or Commons Number complex implementation under the Complex class hood.

4 Likes

that’s great
But I want them to add them to the basic Library of the language

What do you mean by “add them to the basic Library”? Using darksnakes example and also using Commons Math/Numbers for the calculations should work fine for you in your projects. If you are talking about a PR to the Kotlin Standard Library I don’t think there is a high chance it will be accepted as complex numbers normally aren’t part of the standard library as they are not that widely used.

1 Like

we use too much, and i liked Kotlin :star_struck::star_struck: ,
is there any place to put my suggestion ?

The reality is that complex numbers will never be a part of standard library. At least for kotlin. I was a bit shocked to find them in c++ standard library, but c++ does not have packaging system and it tends to accumulate everything in standard library.

I am working with a wrapper for commons math and I could decouple it from my project and launch as a separate library, but only if there are people wanting to contribute, because I currently do not have time to do it all on my own.

3 Likes

Catch the opportunity to publish your code on GitHub/GitLab :wink:

2 Likes

Having small, independent libraries is the way to go. Take a look at the XML APIs of the JDK to see what should be avoided … By the way, that is also the way Rust goes with its small standard library. There is a small crate for complex numbers as there are libs for thousands of other things.

So, maybe it’s a good chance to start such a project on GitHub as it seems like two potential contributors had just found each other :wink:

2 Likes

okey, thank every body :kissing_heart::kissing_heart:
I’ll start building it as a small library :star_struck: :star_struck: :star_struck:

Could you share a link here to the library?
I would like to use it and contribute and implement some operators if you left any :wink:

3 Likes

I’ve started to create a modular mathematical library here. The progress will be very slow for the next few weeks since I have absolutely no time for side projects, but I hope to work on it during summer.

Complex number are already implemented. I use similar, but slightly different approach from the one used by commons-numbers, since they have all the operations in the field element and field is used only as a marker class and in my case all operations are implemented in field.

I plan to add full-scale vector algebra, grid generators and useful wrappers on top of commons math library. I’ve created a multi-platform architecture, but I do not plan to invest efforts in JS back-end for now. Contributions and ideas about what to add are welcome.

1 Like

Something I noticed, however, is that there’s no immediate way to get the angle for the complex number (other than manually calling atan2(im,re) yourself). Theta was implemented as atan(im/re) which may violate the principle of least astonishment for some users.

Which library do you mean?

I was talking about this implementation:

Please file an issue if you think it should be changed. I never use this function, so it is possible I missed something.

1 Like