SHARE
TWEET

Complex.mjs

a guest Jun 20th, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export default class Complex {
  2.   constructor(a = 0, b = 0) {
  3.     const z = this;
  4.     z.real = a;
  5.     z.imag = b;
  6.   }
  7.  
  8.   abs() {
  9.     const z = this;
  10.     const a = z.real;
  11.     const b = z.imag;
  12.     return Math.sqrt(a * a + b * b);
  13.   }
  14.  
  15.   arg() {
  16.     const z = this;
  17.     const a = z.real;
  18.     const b = z.imag;
  19.     return Math.atan2(b, a);
  20.   }
  21.  
  22.   add(w) {
  23.     const z = this;
  24.     if (!(w instanceof Complex)) return z.add(new Complex(w));
  25.     const a = z.real;
  26.     const b = z.imag;
  27.     const c = w.real;
  28.     const d = w.imag;
  29.     return new Complex(a + c, b + d);
  30.   }
  31.  
  32.   sub(w) {
  33.     const z = this;
  34.     if (!(w instanceof Complex)) return z.sub(new Complex(w));
  35.     return z.add(w.mul(-1));
  36.   }
  37.  
  38.   mul(w) {
  39.     const z = this;
  40.     if (!(w instanceof Complex)) return z.mul(new Complex(w));
  41.     const r = z.abs();
  42.     const theta = z.arg();
  43.     const s = w.abs();
  44.     const phi = w.arg();
  45.     return Complex.fromPolar(r * s, theta + phi);
  46.   }
  47.  
  48.   div(w) {
  49.     const z = this;
  50.     if (!(w instanceof Complex)) return z.div(new Complex(w));
  51.     return z.mul(w.pow(-1));
  52.   }
  53.  
  54.   conjugate() {
  55.     const z = this;
  56.     const a = z.real;
  57.     const b = z.imag;
  58.     return new Complex(a, -b);
  59.   }
  60.  
  61.   sqrt() {
  62.     const z = this;
  63.     return z.pow(1 / 2);
  64.   }
  65.  
  66.   pow(w) {
  67.     const z = this;
  68.     return z
  69.       .log()
  70.       .mul(w)
  71.       .exp();
  72.   }
  73.  
  74.   exp() {
  75.     const z = this;
  76.     const a = z.real;
  77.     const b = z.imag;
  78.     return Complex.fromPolar(Math.exp(a), b);
  79.   }
  80.  
  81.   log() {
  82.     const z = this;
  83.     const r = z.abs();
  84.     const theta = z.arg();
  85.     return new Complex(Math.log(r), theta);
  86.   }
  87.  
  88.   sin() {
  89.     const z = this;
  90.     return z.sub(Math.PI / 2).cos();
  91.   }
  92.  
  93.   cos() {
  94.     const z = this;
  95.     const a = z.real;
  96.     const b = z.imag;
  97.     return new Complex(Math.cos(a) * Math.cosh(b), -Math.sin(a) * Math.sinh(b));
  98.   }
  99.  
  100.   tan() {
  101.     const z = this;
  102.     return z.sin().div(z.cos());
  103.   }
  104.  
  105.   equals(w) {
  106.     const z = this;
  107.     if (!(w instanceof Complex)) return z.equals(new Complex(w));
  108.     const a = z.real;
  109.     const b = z.imag;
  110.     const c = w.real;
  111.     const d = w.imag;
  112.     return a === c && b === d;
  113.   }
  114.  
  115.   toString() {
  116.     const z = this;
  117.     const a = z.real;
  118.     const b = z.imag;
  119.     return b === -1 ? a === 0 ? '-i' : `${a}-i`
  120.       : b === 1 ? a === 0 ? 'i' : `${a}+i`
  121.       : b < 0 ? a === 0 ? `${b}i` : `${a}${b}i`
  122.       : b > 0 ? a === 0 ? `${b}i` : `${a}+${b}i`
  123.       : `${a}`;
  124.   }
  125. }
  126.  
  127. Complex.fromPolar = (r, theta) =>
  128.   new Complex(r * Math.cos(theta), r * Math.sin(theta));
  129.  
  130. Complex.I = new Complex(0, 1);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top