Complex Numbres


#1

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


#2
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

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


#5

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.


#6

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


#7

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.


#8

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


#9

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:


#10

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


#11

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:


#12

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.