|
| 1 | +# Mathematical operations with the Complex struct |
| 2 | + |
| 3 | +################################################################ |
| 4 | +# Basic operations |
| 5 | +# Perhaps these should be macros? I don't know... |
| 6 | +func c_add(Complex a, Complex b) Complex { |
| 7 | + return Complex{ |
| 8 | + real: $a.real + $b.real, |
| 9 | + imag: $a.imag + $b.imag |
| 10 | + }; |
| 11 | +} |
| 12 | + |
| 13 | +func c_sub(Complex a, Complex b) Complex { |
| 14 | + return Complex{ |
| 15 | + real: $a.real - $b.real, |
| 16 | + imag: $a.imag - $b.imag |
| 17 | + }; |
| 18 | +} |
| 19 | + |
| 20 | +func c_mul(Complex a, Complex b) Complex { |
| 21 | + return Complex{ |
| 22 | + real: $a.real * $b.real - $a.imag * $b.imag, |
| 23 | + imag: $a.real * $b.imag + $a.imag * $b.real |
| 24 | + }; |
| 25 | +} |
| 26 | + |
| 27 | +func c_div(Complex a, Complex b) Complex { |
| 28 | + local denom = $b.real * $b.real + $b.imag * $b.imag; |
| 29 | + |
| 30 | + return Complex{ |
| 31 | + real: ($a.real * $b.real + $a.imag * $b.imag) / denom, |
| 32 | + imag: ($a.imag * $b.real - $a.real * $b.imag) / denom |
| 33 | + }; |
| 34 | +} |
| 35 | + |
| 36 | +# No power operator here because it uses exp and ln |
| 37 | + |
| 38 | +################################################################ |
| 39 | +# Advanced functions |
| 40 | + |
| 41 | +func c_abs(Complex z) { |
| 42 | + return sqrt($z.real * $z.real + $z.imag * $z.imag); |
| 43 | +} |
| 44 | + |
| 45 | +func c_arg(Complex z) { |
| 46 | + return ATAN2($z.imag, $z.real); |
| 47 | +} |
| 48 | + |
| 49 | +func c_sgn(complex z) Complex { |
| 50 | + # Complex number on the unit circle that is in the direction of the complex number z |
| 51 | + return c_div( |
| 52 | + $z, Complex{ |
| 53 | + real: c_abs($z), |
| 54 | + imag: 0 |
| 55 | + } |
| 56 | + ); |
| 57 | +} |
| 58 | + |
| 59 | +func c_exp(Complex z) Complex { |
| 60 | + # Same as antiln |
| 61 | + return Complex { |
| 62 | + real: antiln($z.real) * cos($z.imag * 57.2957795131), |
| 63 | + imag: antiln($z.real) * sin($z.imag * 57.2957795131) |
| 64 | + }; |
| 65 | +} |
| 66 | + |
| 67 | +func c_ln(Complex z) Complex { |
| 68 | + return Complex { |
| 69 | + real: ln(c_abs($z)), |
| 70 | + imag: ATAN2($z.imag, $z.real) |
| 71 | + }; |
| 72 | +} |
| 73 | + |
| 74 | +func c_pow(Complex a, Complex b) Complex { |
| 75 | + return c_exp( |
| 76 | + c_mul($b, c_ln($a)) |
| 77 | + ); |
| 78 | +} |
| 79 | + |
| 80 | +func c_conj(Complex z) Complex { |
| 81 | + return Complex { |
| 82 | + real: $z.real, |
| 83 | + imag: -$z.imag |
| 84 | + }; |
| 85 | +} |
| 86 | + |
| 87 | +func c_sqrt(Complex z) Complex { |
| 88 | + return c_pow($z, Complex{real: 0.5, imag: 0}); |
| 89 | +} |
| 90 | + |
| 91 | +################################################################ |
| 92 | +# Trigonometric functions |
| 93 | + |
| 94 | +func c_sin(Complex z) Complex { |
| 95 | + return c_div(c_sub( |
| 96 | + c_exp(Complex{real: -$z.imag, imag: $z.real}), |
| 97 | + c_exp(Complex{real: $z.imag, imag: -$z.real}) |
| 98 | + ), Complex{real: 0, imag:2}); |
| 99 | +} |
| 100 | + |
| 101 | +func c_cos(Complex z) Complex { |
| 102 | + return c_div(c_add( |
| 103 | + c_exp(Complex{real: -$z.imag, imag: $z.real}), |
| 104 | + c_exp(Complex{real: $z.imag, imag: -$z.real}) |
| 105 | + ), Complex{real: 2, imag:0}); |
| 106 | +} |
| 107 | + |
| 108 | +func c_tan(Complex z) Complex { |
| 109 | + # Yeah I didn't have this pre-coded so this is a lazy implementation |
| 110 | + return c_div( |
| 111 | + c_sin($z), c_cos($z) |
| 112 | + ); |
| 113 | +} |
| 114 | + |
| 115 | +################################################################ |
| 116 | +# Inverse trigonometric functions |
| 117 | + |
| 118 | +func c_asin(Complex z) Complex { |
| 119 | + return c_mul( |
| 120 | + c_ln( |
| 121 | + c_add( |
| 122 | + Complex{real:-$z.imag, imag:$z.real}, |
| 123 | + c_sqrt( |
| 124 | + c_sub( |
| 125 | + Complex{real:1, imag:0}, |
| 126 | + c_mul($z, $z) |
| 127 | + ) |
| 128 | + ) |
| 129 | + ) |
| 130 | + ), |
| 131 | + Complex{real:0, imag:-1} |
| 132 | + ); |
| 133 | +} |
| 134 | + |
| 135 | +func c_acos(Complex z) Complex { |
| 136 | + local Complex a = c_sqrt( |
| 137 | + c_sub( |
| 138 | + Complex{real:1, imag:0}, |
| 139 | + c_mul($z, $z) |
| 140 | + ) |
| 141 | + ); |
| 142 | + |
| 143 | + return |
| 144 | + c_mul( |
| 145 | + c_ln( |
| 146 | + c_add( |
| 147 | + $z, |
| 148 | + Complex{ |
| 149 | + real:-a.imag, imag:a.real |
| 150 | + } |
| 151 | + ) |
| 152 | + ), |
| 153 | + Complex{real:0, imag:-1} |
| 154 | + ); |
| 155 | +} |
| 156 | + |
| 157 | +func c_atan(Complex z) Complex { |
| 158 | + return |
| 159 | + c_mul(c_ln( |
| 160 | + c_div( |
| 161 | + Complex{real: $z.real, imag: $z.imag + 1}, |
| 162 | + Complex{real: -$z.real, imag: 1-$z.imag} |
| 163 | + ) |
| 164 | + ), Complex{real: 0, imag:0.5}); |
| 165 | +} |
| 166 | + |
| 167 | +################################################################ |
| 168 | +# Hyperbolic trigonometric functions |
| 169 | +func c_sinh(Complex z) Complex { |
| 170 | + return Complex{ |
| 171 | + real: SINH($z.real) * cos($z.imag * 57.2957795131), |
| 172 | + imag: COSH($z.real) * sin($z.imag * 57.2957795131) |
| 173 | + }; |
| 174 | +} |
| 175 | + |
| 176 | +func c_cosh(Complex z) Complex { |
| 177 | + return Complex{ |
| 178 | + real: COSH($z.real) * cos($z.imag * 57.2957795131), |
| 179 | + imag: -SINH($z.real) * sin($z.imag * 57.2957795131) |
| 180 | + }; |
| 181 | +} |
| 182 | + |
| 183 | +################################################################ |
| 184 | +# Inverse hyperbolic trigonometric functions |
| 185 | + |
| 186 | +func c_asinh(Complex z) Complex { |
| 187 | + # This might need to be improved |
| 188 | + return |
| 189 | + c_ln( |
| 190 | + c_add( |
| 191 | + $z, |
| 192 | + c_sqrt( |
| 193 | + c_add( |
| 194 | + c_mul($z, $z), |
| 195 | + Complex{real:1, imag:0} |
| 196 | + ) |
| 197 | + ) |
| 198 | + ) |
| 199 | + ); |
| 200 | +} |
| 201 | + |
| 202 | +func c_acosh(Complex z) Complex { |
| 203 | + local Complex a = c_mul($z, $z); |
| 204 | + |
| 205 | + return |
| 206 | + c_ln( |
| 207 | + c_div( |
| 208 | + $z, |
| 209 | + c_sqrt(Complex{real:-a.real, imag:a.imag}) |
| 210 | + ) |
| 211 | + ); |
| 212 | +} |
0 commit comments