Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -1,62 +1,68 @@
|
|||
class Complex {
|
||||
final Number real, imag
|
||||
|
||||
static final Complex I = [0,1] as Complex
|
||||
static final Complex i = [0,1] as Complex
|
||||
|
||||
Complex(Number real) { this(real, 0) }
|
||||
Complex(Number r, Number i = 0) { (real, imag) = [r, i] }
|
||||
|
||||
Complex(real, imag) { this.real = real; this.imag = imag }
|
||||
Complex(Map that) { (real, imag) = [that.real ?: 0, that.imag ?: 0] }
|
||||
|
||||
Complex plus (Complex c) { [real + c.real, imag + c.imag] as Complex }
|
||||
|
||||
Complex plus (Number n) { [real + n, imag] as Complex }
|
||||
|
||||
Complex minus (Complex c) { [real - c.real, imag - c.imag] as Complex }
|
||||
|
||||
Complex minus (Number n) { [real - n, imag] as Complex }
|
||||
|
||||
Complex multiply (Complex c) { [real*c.real - imag*c.imag , imag*c.real + real*c.imag] as Complex }
|
||||
|
||||
Complex multiply (Number n) { [real*n , imag*n] as Complex }
|
||||
|
||||
Complex div (Complex c) { this * c.recip() }
|
||||
|
||||
Complex div (Number n) { this * (1/n) }
|
||||
|
||||
Complex negative () { [-real, -imag] as Complex }
|
||||
|
||||
/** the complex conjugate of this complex number.
|
||||
* Overloads the bitwise complement (~) operator. */
|
||||
/** the complex conjugate of this complex number. Overloads the bitwise complement (~) operator. */
|
||||
Complex bitwiseNegate () { [real, -imag] as Complex }
|
||||
|
||||
/** the magnitude of this complex number. */
|
||||
// could also use Math.sqrt( (this * (~this)).real )
|
||||
Number abs () { Math.sqrt( real*real + imag*imag ) }
|
||||
// could also use Math.sqrt( (this * (~this)).real )
|
||||
Number getAbs() { Math.sqrt( real*real + imag*imag ) }
|
||||
/** the magnitude of this complex number. */
|
||||
Number abs() { this.abs }
|
||||
|
||||
/** the complex reciprocal of this complex number. */
|
||||
Complex recip() { (~this) / ((this * (~this)).real) }
|
||||
/** the reciprocal of this complex number. */
|
||||
Complex getRecip() { (~this) / (ρ**2) }
|
||||
/** the reciprocal of this complex number. */
|
||||
Complex recip() { this.recip }
|
||||
|
||||
/** derived angle θ (theta) for polar form.
|
||||
* Normalized to 0 ≤ θ < 2π. */
|
||||
/** derived polar angle θ (theta) for polar form. Normalized to 0 ≤ θ < 2π. */
|
||||
Number getTheta() {
|
||||
def theta = Math.atan2(imag,real)
|
||||
theta = theta < 0 ? theta + 2 * Math.PI : theta
|
||||
def θ = Math.atan2(imag,real)
|
||||
θ = θ < 0 ? θ + 2 * Math.PI : θ
|
||||
}
|
||||
/** derived polar angle θ (theta) for polar form. Normalized to 0 ≤ θ < 2π. */
|
||||
Number getΘ() { this.theta } // this is greek uppercase theta
|
||||
|
||||
/** derived magnitude ρ (rho) for polar form. */
|
||||
Number getRho() { this.abs() }
|
||||
/** derived polar magnitude ρ (rho) for polar form. */
|
||||
Number getRho() { this.abs }
|
||||
/** derived polar magnitude ρ (rho) for polar form. */
|
||||
Number getΡ() { this.abs } // this is greek uppercase rho, not roman P
|
||||
|
||||
/** 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
|
||||
* converting [ρ, θ] inputs into a [real, imag]-based complex number */
|
||||
static Complex fromPolar(Number ρ, Number θ) {
|
||||
[ρ * Math.cos(θ), ρ * Math.sin(θ)] as Complex
|
||||
}
|
||||
|
||||
/** Creates new complex with same magnitude ρ, but different angle θ */
|
||||
Complex withTheta(Number theta) { fromPolar(this.rho, theta) }
|
||||
/** Creates new complex with same magnitude ρ, but different angle θ */
|
||||
Complex withTheta(Number θ) { fromPolar(this.rho, θ) }
|
||||
/** Creates new complex with same magnitude ρ, but different angle θ */
|
||||
Complex withΘ(Number θ) { fromPolar(this.rho, θ) }
|
||||
|
||||
/** Creates new complex with same angle θ, but different magnitude ρ */
|
||||
Complex withRho(Number rho) { fromPolar(rho, this.theta) }
|
||||
/** Creates new complex with same angle θ, but different magnitude ρ */
|
||||
Complex withRho(Number ρ) { fromPolar(ρ, this.θ) }
|
||||
/** Creates new complex with same angle θ, but different magnitude ρ */
|
||||
Complex withΡ(Number ρ) { fromPolar(ρ, this.θ) } // this is greek uppercase rho, not roman P
|
||||
|
||||
static Complex exp(Complex c) { fromPolar(Math.exp(c.real), c.imag) }
|
||||
|
||||
|
|
@ -72,10 +78,10 @@ class Complex {
|
|||
|
||||
Complex power(Number n) { this ** ([n, 0] as Complex) }
|
||||
|
||||
boolean equals(other) {
|
||||
other != null && (other instanceof Complex \
|
||||
? [real, imag] == [other.real, other.imag] \
|
||||
: other instanceof Number && [real, imag] == [other, 0])
|
||||
boolean equals(that) {
|
||||
that != null && (that instanceof Complex \
|
||||
? [this.real, this.imag] == [that.real, that.imag] \
|
||||
: that instanceof Number && [this.real, this.imag] == [that, 0])
|
||||
}
|
||||
|
||||
int hashCode() { [real, imag].hashCode() }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue