Can you j oporator to define a complex numbers?

like this

val complex = 3 + j4

or

val complex = 3 + i4

# Complex Numbres

**ahmeddaw**#1

**darksnake**#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.

**Wasabi375**#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.

**darksnake**#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.

**medium**#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

**stangl0r**#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

**darksnake**#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.