📄 complex.asc
字号:
/* complex.asc -- written by Alexis WILKE for Made to Order Software, Ltd. (c) 2005-2006 *//*Copyright (c) 2005-2006 Made to Order Software, Ltd.Permission is hereby granted, free of charge, to anyperson obtaining a copy of this software andassociated documentation files (the "Software"), todeal in the Software without restriction, includingwithout limitation the rights to use, copy, modify,merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whomthe Software is furnished to do so, subject to thefollowing conditions:The above copyright notice and this permission noticeshall be included in all copies or substantialportions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OFANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOTLIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESSFOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NOEVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BELIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,ARISING FROM, OUT OF OR IN CONNECTION WITH THESOFTWARE OR THE USE OR OTHER DEALINGS IN THESOFTWARE.*/package Extension{ class Complex { function Complex(Void) : Complex { a = 0.0; b = 0.0; } function Complex(real : Double) : Complex { a = real; b = 0; } function Complex(_a : Double, _b : Double) : Complex { a = _a; b = _b; }// operator overloading function "+" (Void) : Complex { return this; } function "-" (Void) : Complex { a = -a; b = -b; return this; } function "+=" (c : Complex) : Complex { a += c.a; b += c.b; return this; } function "+" (c : Complex) : Complex { return Complex(a + c.a, b + c.b); } function "+=" (c : Double) : Complex { a += c; return this; } function "+" (c : Double) : Complex { return Complex(a + c, b); } function "-=" (c : Complex) : Complex { a -= c.a; b -= c.b; return this; } function "-" (c : Complex) : Complex { return Complex(a - c.a, b - c.b); } function "-=" (c : Double) : Complex { a -= c; b -= c; return this; } function "-" (c : Double) : Complex { return Complex(a - c, b - c); } function "*=" (c : Complex) : Complex { var ta : Double, tb : Double; ta = a * c.a - b * c.b; tb = a * c.b + b * c.a; a = ta; b = tb; return this; } function "*" (c : Complex) : Complex { return Complex(a * c.a - b * c.b, a * c.b + b * c.a); } function "*=" (c : Double) : Complex { a *= c; b *= c; return this; } function "*" (c : Double) : Complex { return Complex(a * c, b * c); } function "/=" (c : Complex) : Complex { var sqr : Double; sqr = a * a + b * b; a /= sqr; b /= sqr; return this; } function "/" (c : Complex) : Complex { var sqr : Double; sqr = a * a + b * b; return Complex(a / sqr, b / sqr); } function "/=" (c : Double) : Complex { a /= c; b /= c; return this; } function "/" (c : Double) : Complex { return Complex(a / c, b / c); }// comparison function "==" (c : Complex) : Complex { return a == c.a && b == c.b; } function "==" (c : Double) : Complex { return a == c && b == 0.0; } function "===" (c : Complex) : Complex { return a === c.a && b === c.b; } function "===" (c : Double) : Complex { return a === c && b === 0.0; } function "!=" (c : Complex) : Complex { return a != c.a || b != c.b; } function "!=" (c : Double) : Complex { return a != c || b != 0.0; } function "!==" (c : Complex) : Complex { return a !== c.a || b !== c.b; } function "!==" (c : Double) : Complex { return a !== c || b !== 0.0; }// transcendentals function abs(Void) : Double { var m : Double = max(abs(a), abs(b)); if(m == 0) { return 0 } var ra : Double = a / m; var rb : Double = b / m; return m * sqrt(ra ** 2 + rb ** 2); } function arg(Void) : Double { return atan2(b, a); } function conj(Void) : Complex { return Complex(a, -b); } function cos(Void) : Complex { return Complex(cos(a) * cosh(b), -sin(a) * sinh(b)); } function cosh(Void) : Complex { return Complex(cosh(a) * cos(b), sinh(a) * sin(b)); } function exp(Void) : Complex { return polar(expr(a), b); } function imag(Void) : Double { return b; } function log(Void) : Complex { return Complex(log(abs()), arg()); } function log10(Void) : Complex { return Complex(log() / log(Complex(10))); } function norm(Void) : Double { //return a ** 2 + b ** 2; return abs() ** 2; } static function polar(rho : Double, theta : Double) { return Complex(rho * cos(theta), rho * sin(theta)); } function pow(n : Double) : Complex { return exp(n * log()); } function pow(c : Complex) : Complex { return exp(c * log()); } function real(Void) : Double { return a; } function sin(Void) : Complex { return Complex(sin(a) * cosh(b), cos(a) * sinh(b)); } function sinh(Void) : Complex { return Complex(sinh(a) * cos(b), cosh(a) * sin(b)); } function sqrt(Void) : Complex { if(a == 0) { var t = sqrt(abs(b) / 2); return Complex(t, b < 0 ? -t : t); } else { var t = sqrt(2 * (abs() + abs(a))); var u = t / 2; return x > 0 ? Complex(u, b / t) : Complex(abs(b) / t, y < 0 ? -u : u); } } function tan(Void) : Complex { var s : Complex = sin(); var c : Complex = cos(); return s / c; } function tanh(Void) : Complex { var s : Complex = sinh(); var c : Complex = cosh(); return s / c; }// misc. function toString(Void) : String { return a.toString() + (b == 0 ? "" : " + " + b.toString() + "i"); } var a : Double; var b : Double; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -