-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathcomplex_numbers.ahk
161 lines (127 loc) · 4.04 KB
/
complex_numbers.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
; Link: https://www.autohotkey.com/boards/viewtopic.php?f=6&t=35809
; Author: jeeswg
; Date: 14.08.2017
; for: AHK_L
/*
;note: this script puts a list of complex numbers onto the clipboard
;note: the comments (algebra) can be pasted into (used with) wolframalpha.com
;note: complex exponentiation is multi-valued (JEE_CPow only returns a single value)
vPi := 3.141592653589793
MsgBox, % JEE_CDisp(JEE_CAdd([1,2],[3,4])) ;(1+2i)+(3+4i)
MsgBox, % JEE_CDisp(JEE_CSub([1,2],[3,4])) ;(1+2i)-(3+4i)
MsgBox, % JEE_CDisp(JEE_CMul([1,2],[3,4])) ;(1+2i)*(3+4i)
MsgBox, % JEE_CDisp(JEE_CDiv([1,2],[3,4])) ;(1+2i)/(3+4i)
MsgBox, % JEE_CDisp(JEE_CPow([1,2],[3,4])) ;(1+2i)^(3+4i)
MsgBox, % JEE_CDisp(JEE_CPow([Exp(1),0],[0,vPi])) ;e^((pi)i)
MsgBox, % JEE_CDisp(JEE_CPow([0,1],[0,1])) ;i^i
MsgBox, % JEE_CDisp(JEE_CConj([1,2])) ;conj(1+2i)
MsgBox, % JEE_CDisp(JEE_CConj([3,4])) ;conj(3+4i)
MsgBox, % JEE_CAbs([1,2]) " " JEE_CAbs([3,4]) ;abs(1+2i) abs(3+4i)
MsgBox, % JEE_CArg([1,2]) " " JEE_CArg([3,4]) ;arg(1+2i) arg(3+4i)
;test the JEE_CDisp function:
vR := -5
Loop, 21
{
vI := -5
Loop, 21
{
vOutput .= JEE_CDisp([vR,vI]) "`r`n"
vI += 0.5
}
vR += 0.5
}
Clipboard := vOutput
MsgBox, % vOutput
*/
;==================================================
;key possibilities for JEE_CDisp to consider:
;if real part is 0/nonzero (e.g. real part: 3 or 0)
;if imag part is positive/negative/1/-1 (e.g. imag part: 3 or 1 or -3 or -1)
;[3]+[3]i [3]i
;[3]+[]i []i
;[3][-3]i [-3]i
;[3][-]i [-]i
JEE_CDisp(obj2)
{
local obj := obj2.Clone()
;crop trailing zeros after decimal point (remove decimal point if required)
if InStr(obj.1, ".")
obj.1 := RegExReplace(obj.1, "\.0+$|\..*?\K0+$")
if InStr(obj.2, ".")
obj.2 := RegExReplace(obj.2, "\.0+$|\..*?\K0+$")
if !obj.1 && !obj.2
return 0
else if !obj.2
return obj.1
else if !obj.1
obj.1 := ""
else if (obj.2 > 0)
obj.1 .= "+"
if (obj.2 = 1)
return obj.1 "i"
else if (obj.2 = -1)
return obj.1 "-i"
else
return obj.1 obj.2 "i"
}
;==================================================
JEE_CAdd(obj1, obj2)
{
return [obj1.1+obj2.1, obj1.2+obj2.2]
}
;==================================================
JEE_CSub(obj1, obj2)
{
return [obj1.1-obj2.1, obj1.2-obj2.2]
}
;==================================================
JEE_CMul(obj1, obj2)
{
;(a+bi)(c+di) = (ac-bd) + i(ad+bc)
return [(obj1.1*obj2.1)-(obj1.2*obj2.2), (obj1.1*obj2.2)+(obj1.2*obj2.1)]
}
;==================================================
;Complex Division -- from Wolfram MathWorld
;http://mathworld.wolfram.com/ComplexDivision.html
JEE_CDiv(obj1, obj2)
{
;real part: (ac+bd) / (c^2+d^2)
;imag part: (-ad+bc) / (c^2+d^2)
return [((obj1.1*obj2.1)+(obj1.2*obj2.2))/(obj2.1**2+obj2.2**2), ((-obj1.1*obj2.2)+(obj1.2*obj2.1))/(obj2.1**2+obj2.2**2)]
}
;==================================================
;Complex Exponentiation -- from Wolfram MathWorld
;http://mathworld.wolfram.com/ComplexExponentiation.html
;Complex number - Wikipedia
;https://en.wikipedia.org/wiki/Complex_number#Exponentiation
;note: complex exponentiation is multi-valued (JEE_CPow only returns a single value)
JEE_CPow(obj1, obj2)
{
local a := obj1.1, b := obj1.2, c := obj2.1, d := obj2.2, vTemp, vR, vI
vTemp := ((a**2+b**2)**(c/2))*Exp(-d*JEE_CArg(obj1))
vR := vTemp * Cos(c*JEE_CArg(obj1)+0.5*d*Ln(a**2+b**2))
vI := vTemp * Sin(c*JEE_CArg(obj1)+0.5*d*Ln(a**2+b**2))
return [vR,vI]
}
;==================================================
;Complex Modulus -- from Wolfram MathWorld
;http://mathworld.wolfram.com/ComplexModulus.html
JEE_CAbs(obj)
{
return Sqrt(obj.1**2+obj.2**2)
}
;==================================================
;Complex Argument -- from Wolfram MathWorld
;http://mathworld.wolfram.com/ComplexArgument.html
JEE_CArg(obj)
{
return DllCall("msvcrt\atan2", "Double",obj.2, "Double",obj.1, "Cdecl Double")
}
;==================================================
;Complex Conjugate -- from Wolfram MathWorld
;http://mathworld.wolfram.com/ComplexConjugate.html
JEE_CConj(obj)
{
return [obj.1,-obj.2]
}
;==================================================