// 複素数(お題:http://ja.doukaku.org/247/) // http://ja.doukaku.org/comment/----/ // snaさん作を改変( : http://ja.doukaku.org/comment/8851/) // 参考 : http://en.literateprograms.org/Complex_numbers_(Scala) trait Arithmetic[T<:AnyVal]{ def unary_+ : T def unary_- : T def + (that:T):T def - (that:T):T def * (that:T):T def / (that:T):T def abs : T // def lt0 : Boolean // > 0 def gt0 : Boolean // < 0 def eq0 : Boolean // == 0 // def eqp1 : Boolean // == 1 def eqm1 : Boolean // == -1 // } case class Complex[ T<%Arithmetic[T] ](real:T, imag:T) { // +(a + bi) = (a + bi) def unary_+ = this // -(a + bi) = (-a - bi) def unary_- = Complex[T](-real, -imag) // (a + bi) + (c + di) = (a + c) + (b + d)i def + (that: Complex[T]) = Complex(this.real + that.real, this.imag + that.imag) // (a + bi) - (c + di) = (a - c) + (b - d)i def - (that: Complex[T]) = Complex(this.real - that.real, this.imag - that.imag) // (a + bi) * (c + di) = (ac - bd) + (bc + ad)i def * (that: Complex[T]) = { val Complex(a, b) = this val Complex(c, d) = that Complex(a*c - b*d, b*c + a*d) } // (a + bi) / (c + di) = (ac + bd) / (c^2 + d^2) + (bc - ad) / (c^2 + d^2) def / (that: Complex[T]) = { val Complex(a, b) = this val Complex(c, d) = that val deno = c*c + d*d Complex((a*c + b*d) / deno, (b*c - a*d) / deno) } // Conjugate def conjugate() : Complex[T] = Complex(real,-imag) override def toString = this match { case Complex(re, im) if im.eq0 => re.toString case Complex(re, im) if re.eq0 && im.eqp1 => "i" case Complex(re, im) if re.eq0 && im.eqm1 => "-i" case Complex(re, im) if re.eq0 => im.toString + "i" case Complex(re, im) if im.eqp1 => re.toString + " + i" case Complex(re, im) if im.eqm1 => re.toString + " - i" case Complex(re, im) if im.gt0 => re.toString + " + " + im.toString + "i" case Complex(re, im) if im.lt0 => re.toString + " - " + im.abs.toString + "i" } } object test{ implicit def int2Arithmeic(n:Int) : Arithmetic[Int] = new Arithmetic[Int]{ def unary_+ : Int = n.unary_+ def unary_- : Int = n.unary_- def + (that:Int):Int = n+that def - (that:Int):Int = n-that def * (that:Int):Int = n*that def / (that:Int):Int = n/that def abs = Math.abs(n) // def lt0 = n<0 def gt0 = n>0 def eq0 = n==0 // def eqp1 = n==1 def eqm1 = n== -1 } implicit def double2Arithmeic(n:Double) : Arithmetic[Double] = new Arithmetic[Double]{ def unary_+ : Double = n.unary_+ def unary_- : Double = n.unary_- def + (that:Double):Double = n+that def - (that:Double):Double = n-that def * (that:Double):Double = n*that def / (that:Double):Double = n/that def abs = Math.abs(n) // def lt0 = n<0 def gt0 = n>0 def eq0 = n==0 // def eqp1 = n==1 def eqm1 = n== -1 } implicit def int2Complex (n:Int ):Complex[Int ] = Complex(n,0) implicit def double2Complex(n:Double):Complex[Double] = Complex(n,0) implicit def icomp2dcomp (z:Complex[Int]):Complex[Double] = Complex(z.real,z.imag) implicit def dcomp2richer (z:Complex[Double]) = new Proxy{ val self=z def abs = self match { case Complex(re, im) => Math.sqrt(re*re + im*im) } } implicit def icomp2richer (z:Complex[int]) = new Proxy{ val self=z def abs = self match { case Complex(re, im) => Math.sqrt(re*re + im*im) } } def main( args:Array[String] ) : Unit = { { val i = Complex(0,1) println( (3 + i ) + (4 - i ) ) println( (5 - 9*i) - (2 + 6*i) ) println( (5 + 3*i) * (5 + 8*i) ) println( (9 - 7*i) / (9 - 3*i) ) println( (2 + 3*i).abs ) } { val i = Complex(0.,1.) println( (3. + i ) + (4. - i ) ) println( (5. - 9.*i) - (2. + 6.*i) ) println( (5. + 3.*i) * (5. + 8.*i) ) println( (9. - 7.*i) / (9. - 3.*i) ) println( (2. + 3.*i).abs ) } } }