Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
101
Task/Arithmetic-Complex/Groovy/arithmetic-complex-1.groovy
Normal file
101
Task/Arithmetic-Complex/Groovy/arithmetic-complex-1.groovy
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
class Complex {
|
||||
final Number real, imag
|
||||
|
||||
static final Complex i = [0,1] as Complex
|
||||
|
||||
Complex(Number r, Number i = 0) { (real, imag) = [r, i] }
|
||||
|
||||
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. */
|
||||
Complex bitwiseNegate () { [real, -imag] as Complex }
|
||||
|
||||
/** the magnitude of this complex number. */
|
||||
// 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 reciprocal of this complex number. */
|
||||
Complex getRecip() { (~this) / (ρ**2) }
|
||||
/** the reciprocal of this complex number. */
|
||||
Complex recip() { this.recip }
|
||||
|
||||
/** derived polar angle θ (theta) for polar form. Normalized to 0 ≤ θ < 2π. */
|
||||
Number getTheta() {
|
||||
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 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 ρ, Number θ) {
|
||||
[ρ * Math.cos(θ), ρ * Math.sin(θ)] as Complex
|
||||
}
|
||||
|
||||
/** 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 ρ) { 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) }
|
||||
|
||||
static Complex log(Complex c) { [Math.log(c.rho), c.theta] as Complex }
|
||||
|
||||
Complex power(Complex c) {
|
||||
def zero = [0] as Complex
|
||||
(this == zero && c != zero) \
|
||||
? zero \
|
||||
: c == 1 \
|
||||
? this \
|
||||
: exp( log(this) * c )
|
||||
}
|
||||
|
||||
Complex power(Number n) { this ** ([n, 0] as Complex) }
|
||||
|
||||
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() }
|
||||
|
||||
String toString() {
|
||||
def realPart = "${real}"
|
||||
def imagPart = imag.abs() == 1 ? "i" : "${imag.abs()}i"
|
||||
real == 0 && imag == 0 \
|
||||
? "0" \
|
||||
: real == 0 \
|
||||
? (imag > 0 ? '' : "-") + imagPart \
|
||||
: imag == 0 \
|
||||
? realPart \
|
||||
: realPart + (imag > 0 ? " + " : " - ") + imagPart
|
||||
}
|
||||
}
|
||||
17
Task/Arithmetic-Complex/Groovy/arithmetic-complex-2.groovy
Normal file
17
Task/Arithmetic-Complex/Groovy/arithmetic-complex-2.groovy
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
import org.codehaus.groovy.runtime.DefaultGroovyMethods
|
||||
|
||||
class ComplexCategory {
|
||||
static Complex getI (Number a) { [0, a] as Complex }
|
||||
|
||||
static Complex plus (Number a, Complex b) { b + a }
|
||||
static Complex minus (Number a, Complex b) { -b + a }
|
||||
static Complex multiply (Number a, Complex b) { b * a }
|
||||
static Complex div (Number a, Complex b) { ([a] as Complex) / b }
|
||||
static Complex power (Number a, Complex b) { ([a] as Complex) ** b }
|
||||
|
||||
static <N extends Number,T> T asType (N a, Class<T> type) {
|
||||
type == Complex \
|
||||
? [a as Number] as Complex
|
||||
: DefaultGroovyMethods.asType(a, type)
|
||||
}
|
||||
}
|
||||
64
Task/Arithmetic-Complex/Groovy/arithmetic-complex-3.groovy
Normal file
64
Task/Arithmetic-Complex/Groovy/arithmetic-complex-3.groovy
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
import static Complex.*
|
||||
|
||||
Number.metaClass.mixin ComplexCategory
|
||||
Integer.metaClass.mixin ComplexCategory
|
||||
|
||||
def ε = 0.000000001 // tolerance (epsilon): acceptable "wrongness" to account for rounding error
|
||||
|
||||
println 'Demo 1: functionality as requested'
|
||||
def a = [5,3] as Complex
|
||||
def a1 = [real:5, imag:3] as Complex
|
||||
def a2 = 5 + 3.i
|
||||
def a3 = 5 + 3*i
|
||||
assert a == a1 && a == a2 && a == a3
|
||||
println 'a == ' + a
|
||||
def b = [0.5,6] as Complex
|
||||
println 'b == ' + b
|
||||
|
||||
println "a + b == (${a}) + (${b}) == " + (a + b)
|
||||
println "a * b == (${a}) * (${b}) == " + (a * b)
|
||||
assert a + (-a) == 0
|
||||
println "-a == -(${a}) == " + (-a)
|
||||
assert (a * a.recip - 1).abs < ε
|
||||
println "1/a == (${a}).recip == " + (a.recip)
|
||||
println "a * 1/a == " + (a * a.recip)
|
||||
println()
|
||||
|
||||
println 'Demo 2: other functionality not requested, but important for completeness'
|
||||
def c = 10
|
||||
def d = 10 as Complex
|
||||
assert d instanceof Complex && c instanceof Number && d == c
|
||||
assert a + c == c + a
|
||||
println "a + 10 == 10 + a == " + (c + a)
|
||||
assert c - a == -(a - c)
|
||||
println "10 - a == -(a - 10) == " + (c - a)
|
||||
println "a - b == (${a}) - (${b}) == " + (a - b)
|
||||
assert c * a == a * c
|
||||
println "10 * a == a * 10 == " + (c * a)
|
||||
assert (c / a - (a / c).recip).abs < ε
|
||||
println "10 / a == 1 / (a / 10) == " + (c / a)
|
||||
println "a / b == (${a}) / (${b}) == " + (a / b)
|
||||
assert (a ** 2 - a * a).abs < ε
|
||||
println "a ** 2 == a * a == " + (a ** 2)
|
||||
println "0.9 ** b == " + (0.9 ** b)
|
||||
println "a ** b == (${a}) ** (${b}) == " + (a ** b)
|
||||
println 'a.real == ' + a.real
|
||||
println 'a.imag == ' + a.imag
|
||||
println '|a| == ' + a.abs
|
||||
println 'a.rho == ' + a.rho
|
||||
println 'a.ρ == ' + a.ρ
|
||||
println 'a.theta == ' + a.theta
|
||||
println 'a.θ == ' + a.θ
|
||||
println '~a (conjugate) == ' + ~a
|
||||
|
||||
def ρ = 10
|
||||
def π = Math.PI
|
||||
def n = 3
|
||||
def θ = π / n
|
||||
|
||||
def fromPolar1 = fromPolar(ρ, θ) // direct polar-to-cartesian conversion
|
||||
def fromPolar2 = exp(θ.i) * ρ // Euler's equation
|
||||
println "ρ*cos(θ) + i*ρ*sin(θ) == ${ρ}*cos(π/${n}) + i*${ρ}*sin(π/${n})"
|
||||
println " == 10*0.5 + i*10*√(3/4) == " + fromPolar1
|
||||
println "ρ*exp(i*θ) == ${ρ}*exp(i*π/${n}) == " + fromPolar2
|
||||
assert (fromPolar1 - fromPolar2).abs < ε
|
||||
Loading…
Add table
Add a link
Reference in a new issue