File size: 4,453 Bytes
74e10df | 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 | /*************************************************************************************************************
The MIT License
Copyright (C) 2006-2019 David Dubbeldam, Sofia Calero, Thijs Vlugt, Donald E. Ellis, and Randall Q. Snurr.
D.Dubbeldam@uva.nl http://www.uva.nl/profiel/d/u/d.dubbeldam/d.dubbeldam.html
scaldia@upo.es http://www.upo.es/raspa/
t.j.h.vlugt@tudelft.nl http://homepage.tudelft.nl/v9k6y
don-ellis@northwestern.edu http://dvworld.northwestern.edu/
snurr@northwestern.edu http://zeolites.cqe.northwestern.edu/
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
**************************************************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "complex.h"
REAL Re(Complex a)
{
return a.re;
}
REAL Im(Complex a)
{
return a.im;
}
// creates a Complex number from the real and imaginary part
Complex MakeComplex(REAL re,REAL im)
{
Complex c;
c.re=re;
c.im=im;
return c;
}
// returns the Complex addition of Complex a and Complex b
Complex ComplexAdd(Complex a,Complex b)
{
Complex c;
c.re=a.re+b.re;
c.im=a.im+b.im;
return c;
}
// returns the Complex subtraction of Complex a and Complex b
Complex ComplexSubtraction(Complex a,Complex b)
{
Complex c;
c.re=a.re-b.re;
c.im=a.im-b.im;
return c;
}
// returns the Complex multiplication of Complex a and Complex b
Complex ComplexMultiplication(Complex a,Complex b)
{
Complex c;
c.re=a.re*b.re-a.im*b.im;
c.im=a.im*b.re+a.re*b.im;
return c;
}
// returns the Complex division of Complex a and Complex b (without under/overflow)
Complex ComplexDivision(Complex a,Complex b)
{
Complex c;
REAL r,den;
if (fabs(b.re)>=fabs(b.im))
{
r=b.im/b.re;
den=b.re+r*b.im;
c.re=(a.re+r*a.im)/den;
c.im=(a.im-r*a.re)/den;
}
else
{
r=b.re/b.im;
den=b.im+r*b.re;
c.re=(a.re*r+a.im)/den;
c.im=(a.im*r-a.re)/den;
}
return c;
}
// returns the multiplication of real x with Complex a
Complex ComplexRealMuliplication(REAL x,Complex a)
{
Complex c;
c.re=x*a.re;
c.im=x*a.im;
return c;
}
// returns the conjugate of Complex z
Complex Conjugate(Complex z)
{
Complex c;
c.re=z.re;
c.im=-z.im;
return c;
}
// returns the absolute value of Complex z (without under/overflow)
REAL ComplexAbs(Complex z)
{
REAL x,y,ans,temp;
x=z.re>=0.0?z.re:-z.re;
y=z.im>=0.0?z.im:-z.im;
if(x==0.0) ans=y;
else if(y==0.0) ans=x;
else if(x>y)
{
temp=y/x;
ans=x*sqrt(1.0+temp*temp);
}
else
{
temp=x/y;
ans=y*sqrt(1.0+temp*temp);
}
return ans;
}
// returns the squareroot of Complex z (without under/overflow)
Complex ComplexSqrt(Complex z)
{
Complex c;
REAL x,y,w,r;
if ((z.re==0.0)&&(z.im==0.0))
{
c.re=0.0;
c.im=0.0;
return c;
}
else
{
x=z.re>=0.0?z.re:-z.re;
y=z.im>=0.0?z.im:-z.im;
if (x >= y)
{
r=y/x;
w=sqrt(x)*sqrt(0.5*(1.0+sqrt(1.0+r*r)));
}
else
{
r=x/y;
w=sqrt(y)*sqrt(0.5*(r+sqrt(1.0+r*r)));
}
if (z.re>=0.0)
{
c.re=w;
c.im=z.im/(2.0*w);
}
else
{
c.im=(z.im>=0.0)?w:-w;
c.re=z.im/(2.0*c.im);
}
return c;
}
}
Complex ComplexLog(Complex a)
{
Complex c;
c.re=log(sqrt(a.re*a.re+a.im*a.im));
c.im=atan2(a.im,a.re);
return c;
}
|