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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
/*
* Complex.cc -- ePiX::Complex number class implementation
*
* This file is part of ePiX, a C++ library for creating high-quality
* figures in LaTeX
*
* Version 1.2.18
* Last Change: March 21, 2020
*
*
* Copyright (C) 2007, 2008, 2017, 2020
* Andrew D. Hwang <ahwang -at- holycross -dot- edu>
* Department of Mathematics and Computer Science
* College of the Holy Cross
* Worcester, MA, 01610-2395, USA
*
*
* ePiX is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* ePiX is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ePiX; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <cmath>
#include "constants.h"
#include "functions.h"
#include "angle_units.h"
#include "state.h"
#include "Complex.h"
namespace ePiX {
Complex::Complex(double real, double imag)
: m_real(real), m_imag(imag) { }
Complex& Complex::operator += (const Complex& arg)
{
m_real += arg.m_real;
m_imag += arg.m_imag;
return *this;
}
Complex& Complex::operator -= (const Complex& arg)
{
m_real -= arg.m_real;
m_imag -= arg.m_imag;
return *this;
}
Complex& Complex::operator *= (const Complex& arg)
{
double tmp_re(m_real*arg.m_real - m_imag*arg.m_imag);
double tmp_im(m_real*arg.m_imag + m_imag*arg.m_real);
m_real = tmp_re;
m_imag = tmp_im;
return *this;
}
Complex& Complex::operator /= (const Complex& arg)
{
double tmp_re( m_real*arg.m_real + m_imag*arg.m_imag);
double tmp_im(-m_real*arg.m_imag + m_imag*arg.m_real);
double tmp_denom(pow(hypot(arg.m_real, arg.m_imag), 2));
m_real = tmp_re/tmp_denom;
m_imag = tmp_im/tmp_denom;
return *this;
}
Complex& Complex::conj()
{
m_imag *= -1;
return *this;
}
const bool Complex::operator == (const Complex& arg) const
{
return m_real == arg.m_real && m_imag == arg.m_imag;
}
const bool Complex::operator != (const Complex& arg) const
{
return *this != arg;
}
double Complex::re() const
{
return m_real;
}
double Complex::im() const
{
return m_imag;
}
double Complex::Arg(int branch) const
{
if (norm() > EPIX_EPSILON)
return atan2(m_imag, m_real)/the_angle_style().to_radians(1)
+ branch*full_turn();
else
return 0; // arg(0, 0) = 0
}
double Complex::norm() const
{
return hypot(m_real, m_imag);
}
//// non-member functions ////
const Complex operator+ (Complex arg1, const Complex& arg2)
{
return arg1 += arg2;
}
const Complex operator- (Complex arg1, const Complex& arg2)
{
return arg1 -= arg2;
}
const Complex operator- (Complex arg)
{
return arg *= -1;
}
const Complex operator* (Complex arg1, const Complex& arg2)
{
return arg1 *= arg2;
}
const Complex operator/ (Complex arg1, const Complex& arg2)
{
return arg1 /= arg2;
}
double Arg(const Complex& arg, int branch)
{
return arg.Arg(branch);
}
double norm(const Complex& arg)
{
return arg.norm();
}
Complex expC(const Complex& arg)
{
double rad(exp(arg.re()));
return Complex(rad*Cos(arg.im()), rad*Sin(arg.im()));
}
Complex logC(const Complex& arg, int branch)
{
return Complex(log(arg.norm()), arg.Arg(branch));
}
Complex sqrtC(const Complex& arg, int branch)
{
double rad(sqrt(arg.norm())),
th(0.5*Arg(arg));
return Complex(rad*Cos(th), rad*Sin(th));
}
Complex rootC(const Complex& arg, int order, int branch)
{
bool neg(order<0);
double pwr(neg ? -1.0/order : 1.0/order); // nan if order == 0
double rad(pow(arg.norm(), pwr)),
th(arg.Arg(branch) * pwr);
if (neg)
{
rad = 1.0/rad;
th = -th;
}
return Complex(rad*Cos(th), rad*Sin(th));
}
Complex powC(const Complex& arg, int n)
{
bool neg(n<0);
if (neg)
n = -n;
Complex tmp(1,0);
while (n > 0)
{
tmp *= arg;
--n;
}
return (neg ? Complex(1,0)/tmp : tmp);
}
Complex SinC(const Complex& arg)
{
return Complex(Sin(arg.re())*cosh(arg.im()),
Cos(arg.re())*sinh(arg.im()));
}
Complex CosC(const Complex& arg)
{
return Complex(Cos(arg.re())*cosh(arg.im()),
-Sin(arg.re())*sinh(arg.im()));
}
Complex TanC(const Complex& arg)
{
return SinC(arg)/CosC(arg);
}
Complex CotC(const Complex& arg)
{
return CosC(arg)/SinC(arg);
}
Complex SecC(const Complex& arg)
{
return Complex(1)/CosC(arg);
}
Complex CscC(const Complex& arg)
{
return Complex(1)/SinC(arg);
}
Complex SinhC(const Complex& arg)
{
return Complex(-Cos(arg.im())*sinh(arg.re()),
Sin(arg.im())*cosh(arg.re()));
}
Complex CoshC(const Complex& arg)
{
return Complex(Cos(arg.im())*cosh(arg.re()),
Sin(arg.im())*sinh(arg.re()));
}
Complex TanhC(const Complex& arg)
{
return SinhC(arg)/CoshC(arg);
}
Complex CothC(const Complex& arg)
{
return CoshC(arg)/SinhC(arg);
}
Complex SechC(const Complex& arg)
{
return Complex(1)/CoshC(arg);
}
Complex CschC(const Complex& arg)
{
return Complex(1)/SinhC(arg);
}
} // end of namespace
|