Arithmetic/Complex: Difference between revisions

m (Used Pi in Slate)
Line 407:
Complex recip() { (~this) / ((this * (~this)).real) }
/** derived angle θ (theta) for polar form. */
* Normalized to 0 &#x2264; &#x03B8; < 2&#x03C0;. */
Number getTheta() {
def theta = Math.atan2(imag,real)
Line 413 ⟶ 414:
}
/** derived magnitude &#x03C1; (rho) for polar form. */
Number getRho() { this.abs() }
/** Runs Euler's polar-to-Cartesian complex conversion, converting [rho, theta] inputs
* converting [&#x03C1;, &#x03B8;] inputs into a [real, imag]-based complex number */
static Complex fromPolar(Number rho, Number theta) {
[rho * Math.cos(theta), rho * Math.sin(theta)] as Complex
}
/** Creates new complex with same magnitude rho&#x03C1;, but different angle theta&#x03B8; */
Complex withTheta(Number theta) { fromPolar(this.rho, theta) }
/** Creates new complex with same angle theta&#x03B8;, but different magnitude rho&#x03C1; */
Complex withRho(Number rho) { fromPolar(rho, this.theta) }
Line 462 ⟶ 463:
}
}</lang>
Javadoc on the polar-related methods uses the following Greek alphabet encoded entities: &amp;#x03C1;: &#x03C1; (rho), &amp;#x03B8;: &#x03B8; (theta), and &amp;#x03C0;: &#x03C0; (pi).
 
Test Program (patterned after the [[#Fortran|Fortran]] example):
Anonymous user