How to resolve the algorithm Arithmetic/Complex step by step in the Scala programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Arithmetic/Complex step by step in the Scala programming language
Table of Contents
Problem Statement
A complex number is a number which can be written as:
a + b × i
{\displaystyle a+b\times i}
(sometimes shown as:
b + a × i
{\displaystyle b+a\times i}
where
a
{\displaystyle a}
and
b
{\displaystyle b}
are real numbers, and
i
{\displaystyle i}
is √ -1
Typically, complex numbers are represented as a pair of real numbers called the "imaginary part" and "real part", where the imaginary part is the number to be multiplied by
i
{\displaystyle i}
.
By definition, the complex conjugate of
a + b i
{\displaystyle a+bi}
is
a − b i
{\displaystyle a-bi}
Some languages have complex number libraries available. If your language does, show the operations. If your language does not, also show the definition of this type.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Arithmetic/Complex step by step in the Scala programming language
Source code in the scala programming language
package org.rosettacode
package object ArithmeticComplex {
val i = Complex(0, 1)
implicit def fromDouble(d: Double) = Complex(d)
implicit def fromInt(i: Int) = Complex(i.toDouble)
}
package ArithmeticComplex {
case class Complex(real: Double = 0.0, imag: Double = 0.0) {
def this(s: String) =
this("[\\d.]+(?!i)".r findFirstIn s getOrElse "0" toDouble,
"[\\d.]+(?=i)".r findFirstIn s getOrElse "0" toDouble)
def +(b: Complex) = Complex(real + b.real, imag + b.imag)
def -(b: Complex) = Complex(real - b.real, imag - b.imag)
def *(b: Complex) = Complex(real * b.real - imag * b.imag, real * b.imag + imag * b.real)
def inverse = {
val denom = real * real + imag * imag
Complex(real / denom, -imag / denom)
}
def /(b: Complex) = this * b.inverse
def unary_- = Complex(-real, -imag)
lazy val abs = math.hypot(real, imag)
override def toString = real + " + " + imag + "i"
def i = { require(imag == 0.0); Complex(imag = real) }
}
object Complex {
def apply(s: String) = new Complex(s)
def fromPolar(rho:Double, theta:Double) = Complex(rho*math.cos(theta), rho*math.sin(theta))
}
}
scala> import org.rosettacode.ArithmeticComplex._
import org.rosettacode.ArithmeticComplex._
scala> 1 + i
res0: org.rosettacode.ArithmeticComplex.Complex = 1.0 + 1.0i
scala> 1 + 2 * i
res1: org.rosettacode.ArithmeticComplex.Complex = 1.0 + 2.0i
scala> 2 + 1.i
res2: org.rosettacode.ArithmeticComplex.Complex = 2.0 + 1.0i
scala> res0 + res1
res3: org.rosettacode.ArithmeticComplex.Complex = 2.0 + 3.0i
scala> res1 * res2
res4: org.rosettacode.ArithmeticComplex.Complex = 0.0 + 5.0i
scala> res2 / res0
res5: org.rosettacode.ArithmeticComplex.Complex = 1.5 + -0.5i
scala> res1.inverse
res6: org.rosettacode.ArithmeticComplex.Complex = 0.2 + -0.4i
scala> -res6
res7: org.rosettacode.ArithmeticComplex.Complex = -0.2 + 0.4i
You may also check:How to resolve the algorithm Bitmap/Histogram step by step in the Haskell programming language
You may also check:How to resolve the algorithm Wilson primes of order n step by step in the J programming language
You may also check:How to resolve the algorithm Enforced immutability step by step in the Scala programming language
You may also check:How to resolve the algorithm Count in octal step by step in the Logo programming language
You may also check:How to resolve the algorithm CSV data manipulation step by step in the VBA programming language