@@ -5,42 +5,39 @@ import mathParser.Evaluate
5
5
import ComplexBinaryOperator .*
6
6
import ComplexUnitaryOperator .*
7
7
8
- object ComplexEvaluate {
8
+ final class ComplexEvaluate [ V ] extends Evaluate [ ComplexUnitaryOperator , ComplexBinaryOperator , Complex , V ] {
9
9
10
10
import Math ._
11
11
12
- def apply [V ]: Evaluate [ComplexUnitaryOperator , ComplexBinaryOperator , Complex , V ] =
13
- new Evaluate [ComplexUnitaryOperator , ComplexBinaryOperator , Complex , V ] {
14
- override def executeUnitary (uo : ComplexUnitaryOperator , a : Complex ): Complex = uo match {
15
- case Neg => complexNeg(a)
16
- case Sin => complexSin(a)
17
- case Cos => complexCos(a)
18
- case Tan => complexTan(a)
19
- case Asin => complexAsin(a)
20
- case Acos => complexAcos(a)
21
- case Atan => complexAtan(a)
22
- case Sinh => complexSinh(a)
23
- case Cosh => complexCosh(a)
24
- case Tanh => complexTanh(a)
25
- case Exp => complexExp(a)
26
- case Log => complexLog(a)
27
- }
28
-
29
- override def executeBinaryOperator (bo : ComplexBinaryOperator , a : Complex , b : Complex ): Complex =
30
- bo match {
31
- case Plus => complexPlus(a, b)
32
- case Minus => complexMinus(a, b)
33
- case Times => complexTimes(a, b)
34
- case Divided => complexDivided(a, b)
35
- case Power => complexPower(a, b)
36
- }
12
+ def executeUnitary (uo : ComplexUnitaryOperator , a : Complex ): Complex = uo match {
13
+ case Neg => complexNeg(a)
14
+ case Sin => complexSin(a)
15
+ case Cos => complexCos(a)
16
+ case Tan => complexTan(a)
17
+ case Asin => complexAsin(a)
18
+ case Acos => complexAcos(a)
19
+ case Atan => complexAtan(a)
20
+ case Sinh => complexSinh(a)
21
+ case Cosh => complexCosh(a)
22
+ case Tanh => complexTanh(a)
23
+ case Exp => complexExp(a)
24
+ case Log => complexLog(a)
25
+ }
26
+
27
+ def executeBinaryOperator (bo : ComplexBinaryOperator , a : Complex , b : Complex ): Complex =
28
+ bo match {
29
+ case Plus => complexPlus(a, b)
30
+ case Minus => complexMinus(a, b)
31
+ case Times => complexTimes(a, b)
32
+ case Divided => complexDivided(a, b)
33
+ case Power => complexPower(a, b)
37
34
}
38
35
39
- @ inline private final def sq (a : Double ): Double = a * a
36
+ inline private final def sq (a : Double ): Double = a * a
40
37
41
- @ inline private final def abs (c : Complex ): Double = Math .sqrt(sq(c.real) + sq(c.imag))
38
+ inline private final def abs (c : Complex ): Double = Math .sqrt(sq(c.real) + sq(c.imag))
42
39
43
- @ inline private final def complexSqrt (z : Complex ): Complex = {
40
+ inline private final def complexSqrt (z : Complex ): Complex = {
44
41
val length_z : Double = abs(z)
45
42
val b = sqrt((length_z + z.real) / 2.0 )
46
43
val c = sqrt((length_z - z.real) / 2.0 )
@@ -49,21 +46,21 @@ object ComplexEvaluate {
49
46
else
50
47
Complex (b, c)
51
48
}
52
- @ inline private def complexPlus (a : Complex , b : Complex ) =
49
+ inline private def complexPlus (a : Complex , b : Complex ) =
53
50
Complex (a.real + b.real, a.imag + b.imag)
54
51
55
- @ inline private def complexMinus (a : Complex , b : Complex ) =
52
+ inline private def complexMinus (a : Complex , b : Complex ) =
56
53
Complex (a.real - b.real, a.imag - b.imag)
57
54
58
- @ inline private def complexTimes (a : Complex , b : Complex ) =
55
+ inline private def complexTimes (a : Complex , b : Complex ) =
59
56
Complex (a.real * b.real - a.imag * b.imag, a.real * b.imag + a.imag * b.real)
60
57
61
- @ inline private def complexDivided (a : Complex , b : Complex ) = {
58
+ inline private def complexDivided (a : Complex , b : Complex ) = {
62
59
val denom = sq(b.real) + sq(b.imag)
63
60
Complex ((a.real * b.real + a.imag * b.imag) / denom, (a.imag * b.real - a.real * b.imag) / denom)
64
61
}
65
62
66
- @ inline private def complexPower (a : Complex , b : Complex ) = {
63
+ inline private def complexPower (a : Complex , b : Complex ) = {
67
64
val length_a = abs(a)
68
65
if (abs(b) == 0d ) {
69
66
Complex (1d , 0d )
@@ -77,62 +74,62 @@ object ComplexEvaluate {
77
74
}
78
75
}
79
76
80
- @ inline private def complexNeg (a : Complex ) =
77
+ inline private def complexNeg (a : Complex ) =
81
78
Complex (- a.real, - a.imag)
82
79
83
- @ inline private def complexSin (a : Complex ) =
80
+ inline private def complexSin (a : Complex ) =
84
81
Complex (sin(a.real) * cosh(a.imag), cos(a.real) * sinh(a.imag))
85
82
86
- @ inline private def complexCos (a : Complex ) =
83
+ inline private def complexCos (a : Complex ) =
87
84
Complex (cos(a.real) * cosh(a.imag), - sin(a.real) * sinh(a.imag));
88
85
89
- @ inline private def complexTan (a : Complex ) = {
86
+ inline private def complexTan (a : Complex ) = {
90
87
val r2 = a.real + a.real
91
88
val i2 = a.imag + a.imag
92
89
val d = cos(r2) + cosh(i2)
93
90
new Complex (sin(r2) / d, sinh(i2) / d)
94
91
}
95
92
96
- @ inline private def complexAsin (z : Complex ) = {
93
+ inline private def complexAsin (z : Complex ) = {
97
94
val z2 = complexTimes(z, z)
98
95
val s = complexSqrt(Complex (1.0 - z2.real, - z2.imag))
99
96
val l = complexLog(Complex (s.real - z.imag, s.imag + z.real))
100
97
Complex (l.imag, - l.real);
101
98
}
102
99
103
- @ inline private def complexAcos (z : Complex ) = {
100
+ inline private def complexAcos (z : Complex ) = {
104
101
val z2 = complexTimes(z, z)
105
102
val s = complexSqrt(Complex (1.0 - z2.real, - z2.imag))
106
103
val l = complexLog(Complex (z.real + s.imag, z.real + s.imag))
107
104
Complex (l.real, - l.imag)
108
105
}
109
106
110
- @ inline private def complexAtan (z : Complex ) = {
107
+ inline private def complexAtan (z : Complex ) = {
111
108
val n = Complex (z.real, z.imag + 1.0 );
112
109
val d = Complex (- z.real, 1.0 - z.imag);
113
110
val l = complexLog(complexDivided(n, d));
114
111
Complex (l.imag / - 2.0 , l.real / 2.0 );
115
112
}
116
113
117
- @ inline private def complexSinh (z : Complex ) =
114
+ inline private def complexSinh (z : Complex ) =
118
115
Complex (sinh(z.real) * cos(z.imag), cosh(z.real) * sin(z.imag))
119
116
120
- @ inline private def complexCosh (z : Complex ) =
117
+ inline private def complexCosh (z : Complex ) =
121
118
Complex (cosh(z.real) * cos(z.imag), sinh(z.real) * sin(z.imag))
122
119
123
- @ inline private def complexTanh (z : Complex ) = {
120
+ inline private def complexTanh (z : Complex ) = {
124
121
val r2 = z.real + z.real
125
122
val i2 = z.imag + z.imag
126
123
val d = cos(r2) + cosh(i2)
127
124
Complex (sinh(r2) / d, sin(i2) / d)
128
125
}
129
126
130
- @ inline private def complexExp (a : Complex ) = {
127
+ inline private def complexExp (a : Complex ) = {
131
128
val f = exp(a.real)
132
129
Complex (f * cos(a.imag), f * sin(a.imag))
133
130
}
134
131
135
- @ inline private def complexLog (a : Complex ) =
132
+ inline private def complexLog (a : Complex ) =
136
133
if (abs(a) == 0.0 )
137
134
Complex (Double .NaN , Double .NaN )
138
135
else
0 commit comments