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.
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.
we use too much, and i liked Kotlin ,
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.
Catch the opportunity to publish your code on GitHub/GitLab
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
okey, thank every body
I’ll start building it as a small library
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
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.
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.
A couple years ago I published Komplex.
It then turned out someone else already had the name so any next release would have to be renamed, but it was extracted from a project where I was writing a calculator.
A couple years on, and I’m unsatisfied with even my own Complex class because it restricts me to working with Double. KMath solves nothing in this regard as even its Real class hard-codes to Double under the hood.
My current calculator has an implementation of Real that gives as many decimal places as you want, and I wanted a Complex class which worked with that. So I was forced to write my own Complex class and all the functions to go with it.
Why not parameterise the type? That’s what I see them doing over in Boost.