Anonymous user
Arithmetic/Complex: Difference between revisions
→{{header|Groovy}}
m (Used Pi in Slate) |
|||
Line 407:
Complex recip() { (~this) / ((this * (~this)).real) }
/** derived angle θ (theta) for polar form.
* Normalized to 0 ≤ θ < 2π. */
Number getTheta() {
def theta = Math.atan2(imag,real)
Line 413 ⟶ 414:
}
/** derived magnitude ρ (rho) for polar form. */
Number getRho() { this.abs() }
/** Runs Euler's polar-to-Cartesian complex conversion,
* converting [ρ, θ] 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
Complex withTheta(Number theta) { fromPolar(this.rho, theta) }
/** Creates new complex with same angle
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: &#x03C1;: ρ (rho), &#x03B8;: θ (theta), and &#x03C0;: π (pi).
Test Program (patterned after the [[#Fortran|Fortran]] example):
|