-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComplexOperatorAid.mjs
185 lines (155 loc) · 5.82 KB
/
ComplexOperatorAid.mjs
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
export const _I = {x:-1,y:0};export const I = {x:1,y:0};export const II = {x:2,y:0};export const III = {x:3,y:0};export const O = {x:0,y:0};
export const a = { x: -0.5, y: Math.sqrt(3)/2 };
export const a2 = complexMultiplication(a, a);
export const _a = { x: 0.5, y: -Math.sqrt(3)/2 };
export const _a2 = complexMultiplication(_I, a2);
export const a_a2 = complexAdd(a,_a2);
export const a2_a = complexAdd(a2,_a);
export const a_I = complexAdd(a,_I);
export const I_a2 = complexAdd(I,_a2);
export const I_a = complexAdd(I,_a);
export const a2_I = complexAdd(a2,_I);
export const d0 = complexAdd(I,_a2);
export const d1 = complexAdd(a,_I);
export const d2 = complexAdd(a2,_a);
export const _d0 = complexAdd(_I,a2);
export const _d1 = complexAdd(_a,I);
export const _d2 = complexAdd(_a2,a);
export function convertToPolar(a, isPolar) {
if (isPolar) {
var r = Math.sqrt(a.x*a.x + a.y*a.y);
var theta = Math.atan2(a.y, a.x);
// console.log("a.y",a.y,"a.x",a.x);
return {
magnitude: r,
angle: (theta * 180) / Math.PI
};
} else {
return {
x: a.x,
y: a.y
};
}
}
export function convertToCartesian(r, theta, isCartesian) {
if (isCartesian) {
var a = r * Math.cos(theta/180);
var b = r * Math.sin(theta/180);console.log(theta/180,"b",b);
return {
x: a,
y: b
};
} else {
return {
magnitude: r,
angle: theta
};
}
}
// Complex division function
export function complexDivision(a, b) {
const denominator = b.x * b.x + b.y * b.y;
if (denominator === 0) {
return { x: NaN, y: NaN };
}
const real = (a.x * b.x + a.y * b.y) / denominator;
const imaginary = (a.y * b.x - a.x * b.y) / denominator;
return { x: real, y: imaginary };
}
// Complex inverse function
export function complexInverse(b) {
const denominator = b.x * b.x + b.y * b.y;
if (denominator === 0) {
return { x: NaN, y: NaN };
}
const real = (b.x ) / denominator;
const imaginary = (- b.y) / denominator;
return { x: real, y: imaginary };
}
// Complex multiplication function
export function complexMultiplication(a, b) {
const real = a.x * b.x - a.y * b.y;
const imaginary = a.y * b.x + a.x * b.y;
return { x: real, y: imaginary };
}
// Complex multiplication3 function
export function complexMultiplication3(a, b,c) {
const real_aux = a.x * b.x - a.y * b.y;
const imaginary_aux = a.y * b.x + a.x * b.y;
const real = real_aux * c.x - imaginary_aux * c.y;
const imaginary = imaginary_aux * c.x + real_aux * c.y;
return { x: real, y: imaginary };
}
// Complex add function
export function complexAdd(a, b) {
const real = a.x + b.x;
const imaginary = a.y + b.y;
return { x: real, y: imaginary };
}
// Complex substract function
export function complexSub(a, b) {
const real = a.x - b.x;
const imaginary = a.y - b.y;
return { x: real, y: imaginary };
}
export function complexAdd3(a, b, c) {
const real = a.x + b.x + c.x;
const imaginary = a.y + b.y + c.y;
return { x: real, y: imaginary };
}
export function complexAdd4(a, b, c, d) {
const real = a.x + b.x + c.x + d.x;
const imaginary = a.y + b.y + c.y + d.y;
return { x: real, y: imaginary };
}
export function multiplyMatrices(matrix1, matrix2) {
var result = [];
for (var i = 0; i < matrix1.length; i++) {
result[i] = [];
for (var j = 0; j < matrix2[0].length; j++) {
var sum = 0;
for (var k = 0; k < matrix2.length; k++) {
sum += matrix1[i][k] * matrix2[k][j];
}
result[i][j] = sum;
}
}
return result;
}
export function inverseMatrix(matrix) {
var det = matrix[0][0] * (matrix[1][1] * matrix[2][2] - matrix[2][1] * matrix[1][2]) -
matrix[0][1] * (matrix[1][0] * matrix[2][2] - matrix[2][0] * matrix[1][2]) +
matrix[0][2] * (matrix[1][0] * matrix[2][1] - matrix[2][0] * matrix[1][1]);
var invDet = 1 / det;
var result = [];
result[0] = [
(matrix[1][1] * matrix[2][2] - matrix[2][1] * matrix[1][2]) * invDet,
(matrix[0][2] * matrix[2][1] - matrix[0][1] * matrix[2][2]) * invDet,
(matrix[0][1] * matrix[1][2] - matrix[0][2] * matrix[1][1]) * invDet
];
result[1] = [
(matrix[1][2] * matrix[2][0] - matrix[1][0] * matrix[2][2]) * invDet,
(matrix[0][0] * matrix[2][2] - matrix[0][2] * matrix[2][0]) * invDet,
(matrix[1][0] * matrix[0][2] - matrix[0][0] * matrix[1][2]) * invDet
];
result[2] = [
(matrix[1][0] * matrix[2][1] - matrix[2][0] * matrix[1][1]) * invDet,
(matrix[2][0] * matrix[0][1] - matrix[0][0] * matrix[2][1]) * invDet,
(matrix[0][0] * matrix[1][1] - matrix[1][0] * matrix[0][1]) * invDet
];
return result;
}
export function PI_to_Y (Z_S,Z_E,Z_U){
Zl = complexDivision(complexMultiplication(Z_U,Z_S),complexAdd3(Z_S,Z_E,Z_U));
Zj = complexDivision(complexMultiplication(Z_S,Z_E),complexAdd3(Z_S,Z_E,Z_U));
Zk = complexDivision(complexMultiplication(Z_E,Z_U),complexAdd3(Z_S,Z_E,Z_U));
return {Zl,Zj,Zk}
}
export function Seq_012(v) {
let z = {
Z0 : {x : complexDivision(complexAdd3(v.ZA, v.ZB, v.ZC),III).x , y : complexDivision(complexAdd3(v.ZA, v.ZB, v.ZC),III).y},
Z1 : {x : complexDivision(complexAdd3(v.ZA,complexMultiplication(v.ZB, a),complexMultiplication(v.ZC, a2)),III).x , y : complexDivision(complexAdd3(v.ZA,complexMultiplication(v.ZB, a),complexMultiplication(v.ZC, a2)),III).y},
Z2 : {x : complexDivision(complexAdd3(v.ZA,complexMultiplication(v.ZB, a2),complexMultiplication(v.ZC, a)),III).x , y : complexDivision(complexAdd3(v.ZA,complexMultiplication(v.ZB, a2),complexMultiplication(v.ZC, a)),III).y}
}
return z
}