Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -1,36 +1,17 @@
|
|||
def tol = 0.000000001 // tolerance: acceptable "wrongness" to account for rounding error
|
||||
import org.codehaus.groovy.runtime.DefaultGroovyMethods
|
||||
|
||||
println 'Demo 1: functionality as requested'
|
||||
def a = [5,3] as Complex
|
||||
println 'a == ' + a
|
||||
def b = [0.5,6] as Complex
|
||||
println 'b == ' + b
|
||||
class ComplexCategory {
|
||||
static Complex getI (Number a) { [0, a] as Complex }
|
||||
|
||||
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() < tol
|
||||
println "1/a == (${a}).recip() == " + (a.recip())
|
||||
println()
|
||||
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 }
|
||||
|
||||
println 'Demo 2: other functionality not requested, but important for completeness'
|
||||
println "a - b == (${a}) - (${b}) == " + (a - b)
|
||||
println "a / b == (${a}) / (${b}) == " + (a / b)
|
||||
println "a ** b == (${a}) ** (${b}) == " + (a ** b)
|
||||
println 'a.real == ' + a.real
|
||||
println 'a.imag == ' + a.imag
|
||||
println 'a.rho == ' + a.rho
|
||||
println 'a.theta == ' + a.theta
|
||||
println '|a| == ' + a.abs()
|
||||
println 'a_bar == ' + ~a
|
||||
|
||||
def rho = 10
|
||||
def piOverTheta = 3
|
||||
def theta = Math.PI / piOverTheta
|
||||
def fromPolar1 = Complex.fromPolar(rho, theta) // direct polar-to-cartesian conversion
|
||||
def fromPolar2 = Complex.exp(Complex.I * theta) * rho // Euler's equation
|
||||
println "rho*cos(theta) + rho*i*sin(theta) == ${rho}*cos(pi/${piOverTheta}) + ${rho}*i*sin(pi/${piOverTheta}) == " + fromPolar1
|
||||
println "rho * exp(i * theta) == ${rho} * exp(i * pi/${piOverTheta}) == " + fromPolar2
|
||||
assert (fromPolar1 - fromPolar2).abs() < tol
|
||||
println()
|
||||
static <T> T asType (Number a, Class<T> type) {
|
||||
type == Complex \
|
||||
? [a] as Complex
|
||||
: DefaultGroovyMethods.asType(a, type)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue