scala/ScalaBook/chapter-01/complex.scala
//object InfixImplicit{
class Complex(Re: Double, Im: Double) {
def re = this.Re
def im = this.Im
def + (another: Complex)
= new Complex(Re + another.re, Im + another.im)
def - (another: Complex)
= new Complex(Re - another.re, Im - another.im)
def unary_- = new Complex(-Re, -Im)
def * (another: Complex)
= new Complex(re*another.re - Im*another.im,
Im*another.re + Re*another.im)
def ^ (exponent: Int): Complex
= if(exponent == 0) new Complex(1.0,0.0)
else if(exponent == 1) this
else this * (this^(exponent-1))
def toPolar = (radius, theta)
private def radius = scala.Math.sqrt(Re*Re + Im*Im)
private def theta = scala.Math.atan2(Re, Im)
override def toString = {
(re, im) match {
case (0, 0) => "zero"
case (re, 0) => re.toString
case (0, im) => im + "i"
case _ => re + "+" + im + "i"
}
}
}
object Complex{
def apply(re: Double)(implicit im: Double) = new Complex(re, im)
implicit val imzero : Double = 0.0
implicit def doubleToComplex(d: Double) = Complex(d)
implicit def intToComplex(int: Int) = Complex(int.toDouble)
}
import Complex._
object i extends Complex(0.0, 1.0)
def zeroReal = Complex (0.0)
def example2 = {
import Console.println
println(i^0)
println(i^1)
println(i^2)
println(i^3)
println(i^4) // starts to repeat as should
println(i^5)
println("ok!")
println((5 + 3.0*i) * (3 - 4*i) + (3 + 5*i))
println(-(4+4*i)-5.8)
}
//}
example2
//mencoder file.avi -o output.avi -oac copy -ovc copy -ss xronos -endpos poso_krataei