Changeset 144e032a in sasview for src/sas/sascalc/calculator/c_extensions/librefl.c
- Timestamp:
- Nov 14, 2017 10:57:07 AM (7 years ago)
- Branches:
- master, magnetic_scatt, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- f0ce6e2
- Parents:
- a57ae07
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sascalc/calculator/c_extensions/librefl.c
ra1daf86 r144e032a 112 112 #endif // NEED_ERF 113 113 114 complex cassign(real, imag) 115 double real, imag; 116 { 117 complex x; 118 x.re = real; 119 x.im = imag; 120 return x; 121 } 122 123 124 complex cplx_add(x,y) 125 complex x,y; 126 { 127 complex z; 128 z.re = x.re + y.re; 129 z.im = x.im + y.im; 130 return z; 131 } 132 133 complex rcmult(x,y) 134 double x; 135 complex y; 136 { 137 complex z; 138 z.re = x*y.re; 139 z.im = x*y.im; 140 return z; 141 } 142 143 complex cplx_sub(x,y) 144 complex x,y; 145 { 146 complex z; 147 z.re = x.re - y.re; 148 z.im = x.im - y.im; 149 return z; 150 } 151 152 153 complex cplx_mult(x,y) 154 complex x,y; 155 { 156 complex z; 157 z.re = x.re*y.re - x.im*y.im; 158 z.im = x.re*y.im + x.im*y.re; 159 return z; 160 } 161 162 complex cplx_div(x,y) 163 complex x,y; 164 { 165 complex z; 166 z.re = (x.re*y.re + x.im*y.im)/(y.re*y.re + y.im*y.im); 167 z.im = (x.im*y.re - x.re*y.im)/(y.re*y.re + y.im*y.im); 168 return z; 169 } 170 171 complex cplx_exp(b) 172 complex b; 173 { 174 complex z; 114 void cassign(Cplx *x, double real, double imag) 115 { 116 x->re = real; 117 x->im = imag; 118 } 119 120 121 void cplx_add(Cplx *z, Cplx x, Cplx y) 122 { 123 z->re = x.re + y.re; 124 z->im = x.im + y.im; 125 } 126 127 void rcmult(Cplx *z, double x, Cplx y) 128 { 129 z->re = x*y.re; 130 z->im = x*y.im; 131 } 132 133 void cplx_sub(Cplx *z, Cplx x, Cplx y) 134 { 135 z->re = x.re - y.re; 136 z->im = x.im - y.im; 137 } 138 139 140 void cplx_mult(Cplx *z, Cplx x, Cplx y) 141 { 142 z->re = x.re*y.re - x.im*y.im; 143 z->im = x.re*y.im + x.im*y.re; 144 } 145 146 void cplx_div(Cplx *z, Cplx x, Cplx y) 147 { 148 z->re = (x.re*y.re + x.im*y.im)/(y.re*y.re + y.im*y.im); 149 z->im = (x.im*y.re - x.re*y.im)/(y.re*y.re + y.im*y.im); 150 } 151 152 void cplx_exp(Cplx *z, Cplx b) 153 { 175 154 double br,bi; 176 155 br=b.re; 177 156 bi=b.im; 178 z.re = exp(br)*cos(bi); 179 z.im = exp(br)*sin(bi); 180 return z; 181 } 182 183 184 complex cplx_sqrt(z) //see Schaum`s Math Handbook p. 22, 6.6 and 6.10 185 complex z; 186 { 187 complex c; 157 z->re = exp(br)*cos(bi); 158 z->im = exp(br)*sin(bi); 159 } 160 161 162 void cplx_sqrt(Cplx *c, Cplx z) //see Schaum`s Math Handbook p. 22, 6.6 and 6.10 163 { 188 164 double zr,zi,x,y,r,w; 189 165 … … 193 169 if (zr==0.0 && zi==0.0) 194 170 { 195 c.re=0.0; 196 c.im=0.0; 197 return c; 198 } 199 else 200 { 171 c->re=0.0; 172 c->im=0.0; 173 } else { 201 174 x=fabs(zr); 202 175 y=fabs(zi); … … 205 178 r=y/x; 206 179 w=sqrt(x)*sqrt(0.5*(1.0+sqrt(1.0+r*r))); 207 } 208 else 209 { 180 } else { 210 181 r=x/y; 211 182 w=sqrt(y)*sqrt(0.5*(r+sqrt(1.0+r*r))); … … 213 184 if (zr >=0.0) 214 185 { 215 c.re=w; 216 c.im=zi/(2.0*w); 186 c->re=w; 187 c->im=zi/(2.0*w); 188 } else { 189 c->im=(zi >= 0) ? w : -w; 190 c->re=zi/(2.0*c->im); 217 191 } 218 else 219 { 220 c.im=(zi >= 0) ? w : -w; 221 c.re=zi/(2.0*c.im); 222 } 223 return c; 224 } 225 } 226 227 complex cplx_cos(b) 228 complex b; 229 { 230 complex zero,two,z,i,bi,negbi; 231 zero = cassign(0.0,0.0); 232 two = cassign(2.0,0.0); 233 i = cassign(0.0,1.0); 234 bi = cplx_mult(b,i); 235 negbi = cplx_sub(zero,bi); 236 z = cplx_div(cplx_add(cplx_exp(bi),cplx_exp(negbi)),two); 237 return z; 192 } 193 } 194 195 void cplx_cos(Cplx *z, Cplx b) 196 { 197 // cos(b) = (e^bi + e^-bi)/2 198 // = (e^b.im e^-i bi.re) + e^-b.im e^i b.re)/2 199 // = (e^b.im cos(-b.re) + e^b.im sin(-b.re) i)/2 + (e^-b.im cos(b.re) + e^-b.im sin(b.re) i)/2 200 // = e^b.im cos(b.re)/2 - e^b.im sin(b.re)/2 i + 1/e^b.im cos(b.re)/2 + 1/e^b.im sin(b.re)/2 i 201 // = (e^b.im + 1/e^b.im)/2 cos(b.re) + (-e^b.im + 1/e^b.im)/2 sin(b.re) i 202 // = cosh(b.im) cos(b.re) - sinh(b.im) sin(b.re) i 203 double exp_b_im = exp(b.im); 204 z->re = 0.5*(+exp_b_im + 1.0/exp_b_im) * cos(b.re); 205 z->im = -0.5*(exp_b_im - 1.0/exp_b_im) * sin(b.re); 238 206 } 239 207
Note: See TracChangeset
for help on using the changeset viewer.