1 /* 2 Copyright (c) 2013-2021 Timur Gafarov 3 4 Boost Software License - Version 1.0 - August 17th, 2003 5 6 Permission is hereby granted, free of charge, to any person or organization 7 obtaining a copy of the software and accompanying documentation covered by 8 this license (the "Software") to use, reproduce, display, distribute, 9 execute, and transmit the Software, and to prepare derivative works of the 10 Software, and to permit third-parties to whom the Software is furnished to 11 do so, all subject to the following: 12 13 The copyright notices in the Software and this entire statement, including 14 the above license grant, this restriction and the following disclaimer, 15 must be included in all copies of the Software, in whole or in part, and 16 all derivative works of the Software, unless such copies or derivative 17 works are solely in the form of machine-executable object code generated by 18 a source language processor. 19 20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 DEALINGS IN THE SOFTWARE. 27 */ 28 29 /** 30 * Complex numbers 31 * 32 * Copyright: Timur Gafarov 2013-2021. 33 * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 * Authors: Timur Gafarov 35 */ 36 module dlib.math.complex; 37 38 import std.math; 39 import std.range; 40 import std.format; 41 42 /// Complex number representation 43 struct Complex(T) 44 { 45 T re; 46 T im; 47 48 this(T r, T i) 49 { 50 re = r; 51 im = i; 52 } 53 54 this(T r) 55 { 56 re = r; 57 im = 0.0; 58 } 59 60 Complex!(T) opUnary(string op) () if (op == "-") 61 { 62 return Complex!(T)(re, -im); 63 } 64 65 Complex!(T) opBinary(string op)(Complex!(T) c) if (op == "+") 66 { 67 return Complex!(T)(re + c.re, im + c.im); 68 } 69 70 Complex!(T) opBinary(string op)(Complex!(T) c) if (op == "-") 71 { 72 return Complex!(T)(re - c.re, im - c.im); 73 } 74 75 Complex!(T) opBinary(string op)(Complex!(T) c) if (op == "*") 76 { 77 return Complex!(T)( 78 re * c.re - im * c.im, 79 re * c.im + im * c.re); 80 } 81 82 Complex!(T) opBinary(string op)(Complex!(T) c) if (op == "/") 83 { 84 T denominator = c.re * c.re + c.im * c.im; 85 return Complex!(T)( 86 (re * c.re + im * c.im) / denominator, 87 (im * c.re - re * c.im) / denominator); 88 } 89 90 Complex!(T) opOpAssign(string op)(Complex!(T) c) if (op == "+") 91 { 92 re += c.re; 93 im += c.im; 94 return this; 95 } 96 97 Complex!(T) opOpAssign(string op)(Complex!(T) c) if (op == "-") 98 { 99 re -= c.re; 100 im -= c.im; 101 return this; 102 } 103 104 Complex!(T) opOpAssign(string op)(Complex!(T) c) if (op == "*") 105 { 106 T temp = re; 107 re = re * c.re - im * c.im; 108 im = im * c.re + temp * c.im; 109 return this; 110 } 111 112 Complex!(T) opOpAssign(string op)(Complex!(T) c) if (op == "/") 113 { 114 T denominator = c.re * c.re + c.im * c.im; 115 T temp = re; 116 re = (re * c.re + im * c.im) / denominator; 117 im = (im * c.re - temp * c.im) / denominator; 118 return this; 119 } 120 121 Complex!(T) opBinary(string op)(T scalar) if (op == "+") 122 { 123 return Complex!(T)(re + scalar, im + scalar); 124 } 125 126 Complex!(T) opBinary(string op)(T scalar) if (op == "-") 127 { 128 return Complex!(T)(re + scalar, im + scalar); 129 } 130 131 Complex!(T) opBinary(string op)(T scalar) if (op == "*") 132 { 133 return Complex!(T)(re * scalar, im * scalar); 134 } 135 136 Complex!(T) opBinary(string op)(T scalar) if (op == "/") 137 { 138 return Complex!(T)(re / scalar, im / scalar); 139 } 140 141 Complex!(T) reciprocal() 142 { 143 T scale = re * re + im * im; 144 return Complex!(T)(re / scale, -im / scale); 145 } 146 147 T magnitude() 148 { 149 return sqrt(re * re + im * im); 150 } 151 152 T norm() 153 { 154 return (re * re + im * im); 155 } 156 157 string toString() 158 { 159 auto writer = appender!string(); 160 formattedWrite(writer, "%s + %si", re, im); 161 return writer.data; 162 } 163 } 164 165 /// Complex abs 166 T abs(T)(Complex!T x) 167 { 168 return sqrt(x.re * x.re + x.im * x.im); 169 } 170 171 /// Complex atan2 172 T atan2(T)(Complex!T x) 173 { 174 return atan2(x.im, x.re); 175 } 176 177 /// Complex pow 178 Complex!T pow(T)(Complex!T x, Complex!T n) 179 { 180 T r = abs(x); 181 T t = arg(x); 182 T c = n.re; 183 T d = n.im; 184 185 Complex!T res; 186 res.re = std.math.pow(r, c) * std.math.exp(-d*t) * cos(c*t + d*log(r)); 187 res.im = std.math.pow(r, c) * std.math.exp(-d*t) * sin(c*t + d*log(r)); 188 189 return res; 190 } 191 192 /// Complex exp 193 Complex!T exp(T)(Complex!T s) 194 { 195 return Complex!T( 196 std.math.exp(s.re) * cos(s.im), 197 std.math.exp(s.re) * sin(s.im)); 198 } 199 200 /** 201 * Riemann zeta function: 202 * ζ(s) = 1/1^s + 1/2^s + 1/3^s + ... 203 */ 204 Complex!T zeta(T)(Complex!T s) 205 { 206 return Complex!T(1.0) + 207 (s + Complex!T(3.0)) / (s - Complex!T(1.0)) * 208 Complex!T(1.0) / pow(Complex!T(2.0), s + Complex!T(1.0)); 209 } 210 211 /// Alias for single precision Complex specialization 212 alias Complexf = Complex!float;