00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #ifndef lint
00041 static const char RCScomplex[] = "@(#)$Header: /cvsroot/brlcad/brlcad/src/libbn/complex.c,v 14.10 2006/09/02 14:02:14 lbutler Exp $ (BRL)";
00042 #endif
00043
00044 #include "common.h"
00045
00046
00047
00048 #include <stdio.h>
00049 #include <math.h>
00050 #include "machine.h"
00051 #include "bu.h"
00052 #include "vmath.h"
00053 #include "bn.h"
00054
00055
00056 #define SIGN( x ) ((x) == 0 ? 0 : (x) > 0 ? 1 : -1)
00057 #define ABS( a ) ((a) >= 0 ? (a) : -(a))
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067 void
00068 bn_cx_div(register bn_complex_t *ap, register const bn_complex_t *bp)
00069 {
00070 FAST fastf_t r, s;
00071 FAST fastf_t ap__re;
00072
00073
00074 ap__re = ap->re;
00075 r = bp->re;
00076 s = bp->im;
00077 if ( ABS( r ) >= ABS( s ) ) {
00078 if( NEAR_ZERO( r, SQRT_SMALL_FASTF ) )
00079 goto err;
00080 r = s / r;
00081 s = 1.0 / (bp->re + r * s);
00082 ap->re = (ap->re + ap->im * r) * s;
00083 ap->im = (ap->im - ap__re * r) * s;
00084 return;
00085 } else {
00086
00087 if( NEAR_ZERO( s, SQRT_SMALL_FASTF ) )
00088 goto err;
00089 r = r / s;
00090 s = 1.0 / (s + r * bp->re);
00091 ap->re = (ap->re * r + ap->im) * s;
00092 ap->im = (ap->im * r - ap__re) * s;
00093 return;
00094 }
00095 err:
00096 bu_log("bn_cx_div: division by zero: %gR+%gI / %gR+%gI\n",
00097 ap->re, ap->im, bp->re, bp->im );
00098 ap->re = ap->im = 1.0e20;
00099 }
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111 void
00112 bn_cx_sqrt(bn_complex_t *op, register const bn_complex_t *ip)
00113 {
00114 FAST fastf_t ampl, temp;
00115
00116 register int re_sign;
00117 register int im_sign;
00118
00119
00120 im_sign = SIGN( ip->im );
00121 if( (re_sign = SIGN(ip->re)) == 0 ) {
00122 if ( im_sign == 0 )
00123 op->re = op->im = 0;
00124 else if ( im_sign > 0 )
00125 op->re = op->im = sqrt( ip->im * 0.5 );
00126 else
00127 op->re = -(op->im = sqrt( ip->im * -0.5 ));
00128 } else if ( im_sign == 0 ) {
00129 if ( re_sign > 0 ) {
00130 op->re = sqrt( ip->re );
00131 op->im = 0.0;
00132 } else {
00133 op->im = sqrt( -ip->re );
00134 op->re = 0.0;
00135 }
00136 } else {
00137
00138 ampl = bn_cx_ampl( ip );
00139 if( (temp = (ampl - ip->re) * 0.5) < 0.0 ) {
00140
00141
00142
00143
00144
00145
00146 op->im = 0;
00147 } else
00148 op->im = sqrt( temp );
00149
00150 if( (temp = (ampl + ip->re) * 0.5) < 0.0 ) {
00151 op->re = 0.0;
00152 } else {
00153 if( im_sign > 0 )
00154 op->re = sqrt(temp);
00155 else
00156 op->re = -sqrt(temp);
00157 }
00158 }
00159 }
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169