-
Notifications
You must be signed in to change notification settings - Fork 0
/
Complex.js
232 lines (196 loc) · 5.46 KB
/
Complex.js
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
(function(top){
'use strict';
var Complex = function(number){
this.re = this.im = 0;
if(typeof number == "string"){ // If is a complex number definitios (A+Bi)
if(/(i|j)/.test(number.toString())){
if(/(\d+|\d+(\.\d+))(\+|\-)((\d+|\d+(\.\d+)))(i|j)/g.test(number.toString())){
var separado = number.toString().replace(/(\d+|\d+(\.\d+))(\+|\-)((\d+|\d+(\.\d+)))(i|j)/g, "$1,$2,$3,$4").split(",");
this.re = Complex.analiza(separado[0]);
if(separado[2] == "-"){
this.im = -Complex.analiza(separado[3]);
}else{
this.im = Complex.analiza(separado[3]);
}
}else{
if(!isNaN(parseFloat(number))){
this.im = parseFloat(number);
}
}
}else{
if(!isNaN(parseFloat(number))){
this.re = parseFloat(number);
}
}
}else if(!isNaN(number)){
this.re = number;
}else if(typeof number === "object"){
if(number.re !== undefined){
this.re = number.re;
}
if(number.im !== undefined){
this.im = number.im;
}
}
this.real = function(){ return this.re; }
this.imag = function(){ return this.im; }
this.toString = function(){
return this.re.toString()+((this.im<0)? this.im : "+"+this.im)+"i";
}
}
Complex.prototype.add = function(cmplx){
if(cmplx.constructor.name === "Complex"){
return new Complex({re: (this.real()+cmplx.re), im: (this.imag()+cmplx.im)});
}else{
if(cmplx.constructor.name === "Number"){
return new Complex({re: (this.real()+cmplx), im: this.imag()});
}
}
}
Complex.prototype.sub = function(cmplx){
if(cmplx.constructor.name === "Complex"){
return new Complex({re: (this.real()-cmplx.re), im: (this.imag()-cmplx.im)});
}else{
if(cmplx.constructor.name === "Number"){
return new Complex({re: (this.real()-cmplx), im: this.imag()});
}
}
}
Complex.prototype.conj = function(){
return new Complex({re: this.real(), im: -this.imag()});
}
Complex.prototype.mul = function(cmplx){
if(cmplx.constructor.name === "Complex"){
return new Complex({re: (this.real()*cmplx.re-this.imag()*cmplx.im), im: (this.imag()*cmplx.re+this.real()*cmplx.im)});
}else{
if(cmplx.constructor.name === "Number"){
return new Complex({re: (this.real()*cmplx), im: (this.imag()*cmplx)});
}
if(cmplx.constructor.name === "String"){
return Complex.prototype.mul(new Complex(cmplx));
}
}
}
Complex.prototype.div = function(cmplx){ // this/cmplx
if(cmplx.constructor.name === "Complex"){
var _d = cmplx.re*cmplx.re+cmplx.im*cmplx.im;
return new Complex({re: (this.real()*cmplx.re+this.imag()*cmplx.im)/_d, im: (this.imag()*cmplx.re-this.real()*cmplx.im)/_d});
}else{
if(cmplx.constructor.name === "Number"){
return new Complex({re: (this.real()/cmplx), im: (this.imag()/cmplx)});
}
if(cmplx.constructor.name === "String"){
return this.div(new Complex(cmplx));
}
}
}
Complex.exp = function(cmplx){
if(cmplx.constructor.name === "Complex"){
var a = Math.exp(cmplx.re);
return new Complex({re: (a*Math.cos(cmplx.im)), im: (a*Math.sin(cmplx.im))});
}else{
if(cmplx.constructor.name === "Number"){
return new Complex({re: Math.exp(cmplx)});
}
}
}
Complex.sin = function(cmplx){
return new Complex({re: Math.sin(cmplx.re)*Math.cosh(cmplx.im), im: Math.cos(cmplx.re)*Math.sinh(cmplx.im)});
}
Complex.cos = function(cmplx){
return new Complex({re: Math.cos(cmplx.re)*Math.cosh(cmplx.im), im: -Math.sin(cmplx.re)*Math.sinh(cmplx.im)});
}
Complex.tan = function(cmplx){
return Complex.sin(cmplx).div(Complex.cos(cmplx));
}
/**
function: Complex.sqrt
Returns the square root of a complex number.
*/
Complex.sqrt = function(cmplx){
if("Complex"===cmplx.constructor.name){
var r, theta, sqr;
r=cmplx.abs();
theta=cmplx.angle();
sqr=Math.sqrt(r);
return new Complex({re: sqr*Math.cos(theta/2), im: sqr*Math.sin(theta/2)});
}
if("Number"===cmplx.constructor.name){
if(cmplx<0){
return new Complex({re: 0, im: Math.sqrt(-cmplx)});
}else{
return new Complex({re: Math.sqrt(cmplx), im: 0});
}
}
return false;
}
/**
Function: Complex.abs
Returns the modulus of the Complex number.
*/
Complex.prototype.abs = function(){
return Math.sqrt(this.imag()*this.imag()+this.real()*this.real());
}
/**
Function: Complex.mod
Returns the modulus of the Complex number (Calls Complex.prototype.abs)
*/
Complex.prototype.mod = Complex.prototype.abs;
/**
Function: Complex.angle
Returns the phase of the Complex number.
Return range : [0, 2PI]
*/
Complex.prototype.angle = function(){
return Math.atan2(this.imag(), this.real());
}
Complex.analiza = function(number){
number = number.toString().replace(/pi/g, Math.PI);
number = number.toString().replace(/e/g, Math.E);
return parseFloat(number); // Tratar como un float
}
var _Array = function(N){
this.__array = {};
this.length=N;
for(var i = 0; i<N; i++){
this.__array[i] = new Complex(0);
}
}
_Array.prototype.set=function(a,b){
if(b.constructor.name=="Complex"){
this.__array[a]=b;
return true;
}else{
if(b.constructor.name=="Number"){
this.__array[a].re=a;
this.__array[a].im=0;
return true;
}
}
return false;
}
_Array.prototype.get=function(a){
if(a<N){
return this.__array[a];
}
return false;
}
_Array.prototype.push=function(a){
if(a.constructor.name=="Complex"){
this.__array[this.length++]=a;
return true;
}else{
if(a.constructor.name=="Number"){
this.__array[this.length++]=new Complex({re:a});
return true;
}
}
return false;
}
_Array.prototype.length=function(){
return this.length;
}
window.Complex=Complex;
window.Complex.Array=_Array;
}
)(window);