import ballerina/io; class Complex { float re; float im; function init(float re, float im) { self.re = re; self.im = im; } function neg() returns Complex { return new Complex(-self.re, -self.im); } function inv() returns Complex { float denom = self.re * self.re + self.im * self.im; return new Complex(self.re / denom, -self.im / denom); } function add(Complex other) returns Complex { return new Complex(self.re + other.re, self.im + other.im); } function sub(Complex other) returns Complex { return self.add(other.neg()); } function mul(Complex other) returns Complex { return new Complex( self.re * other.re - self.im * other.im, self.re * other.im + self.im * other.re ); } function div(Complex other) returns Complex { return self.mul(other.inv()); } function conj() returns Complex { return new Complex(self.re, -self.im); } function toString() returns string { if self.re === -0.0 { self.re = 0.0; } if self.im === -0.0 { self.im = 0.0; } if self.im >= 0.0 { return string `${self.re} + ${self.im}i`; } else { return string `${self.re} - ${-self.im}i`; } } } public function main() { var x = new Complex(1, 3); var y = new Complex(5, 2); io:println("x = ", x); io:println("y = ", y); io:println("x + y = ", x.add(y)); io:println("x - y = ", x.sub(y)); io:println("x * y = ", x.mul(y)); io:println("x / y = ", x.div(y)); io:println("-x = ", x.neg()); io:println("1 / x = ", x.inv()); io:println("x* = ", x.conj()); }