Changeset eb2946f in sasmodels for sasmodels/models/lib/sas_J1.c


Ignore:
Timestamp:
May 18, 2017 5:10:29 PM (7 years ago)
Author:
Paul Kienzle <pkienzle@…>
Branches:
master, core_shell_microgels, costrafo411, magnetic_model, ticket-1257-vesicle-product, ticket_1156, ticket_1265_superball, ticket_822_more_unit_tests
Children:
1b85b55
Parents:
452b168
Message:

improve precision of sasmodels special functions

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/models/lib/sas_J1.c

    r473a9f1 reb2946f  
    109109{ 
    110110 
    111     double w, z, p, q, xn; 
     111    double w, z, p, q, xn, abs_x, sign_x; 
    112112 
    113113    const double Z1 = 1.46819706421238932572E1; 
     
    116116    const double SQ2OPI = 0.79788456080286535588; 
    117117 
    118     w = x; 
    119     if( x < 0 ) 
    120         w = -x; 
    121  
    122     if( w <= 5.0 ) { 
    123         z = x * x; 
     118    // 2017-05-18 PAK - mathematica and mpmath use J1(-x) = -J1(x) 
     119    if (x < 0) { 
     120        abs_x = -x; 
     121        sign_x = -1.0; 
     122    } else { 
     123        abs_x = x; 
     124        sign_x = 1.0; 
     125    } 
     126 
     127    if( abs_x <= 5.0 ) { 
     128        z = abs_x * abs_x; 
    124129        w = polevl( z, RPJ1, 3 ) / p1evl( z, RQJ1, 8 ); 
    125         w = w * x * (z - Z1) * (z - Z2); 
    126         return( w ); 
     130        w = w * abs_x * (z - Z1) * (z - Z2); 
     131        return( sign_x * w ); 
    127132    } 
    128133 
    129     w = 5.0/x; 
     134    w = 5.0/abs_x; 
    130135    z = w * w; 
    131  
    132136    p = polevl( z, PPJ1, 6)/polevl( z, PQJ1, 6 ); 
    133137    q = polevl( z, QPJ1, 7)/p1evl( z, QQJ1, 7 ); 
    134  
    135     xn = x - THPIO4; 
     138    xn = abs_x - THPIO4; 
    136139 
    137140    double sn, cn; 
     
    139142    p = p * cn - w * q * sn; 
    140143 
    141     return( p * SQ2OPI / sqrt(x) ); 
     144    return( sign_x * p * SQ2OPI / sqrt(abs_x) ); 
    142145} 
    143146 
     
    179182    }; 
    180183 
    181 float cephes_j1f(float x) 
     184float cephes_j1f(float xx) 
    182185{ 
    183186 
    184     float xx, w, z, p, q, xn; 
     187    float x, w, z, p, q, xn; 
    185188 
    186189    const float Z1 = 1.46819706421238932572E1; 
     
    188191 
    189192 
    190     xx = x; 
    191     if( xx < 0 ) 
    192         xx = -x; 
    193  
    194     if( xx <= 2.0 ) { 
    195         z = xx * xx; 
    196         p = (z-Z1) * xx * polevl( z, JPJ1, 4 ); 
    197         return( p ); 
     193    // 2017-05-18 PAK - mathematica and mpmath use J1(-x) = -J1(x) 
     194    x = xx; 
     195    if( x < 0 ) 
     196        x = -xx; 
     197 
     198    if( x <= 2.0 ) { 
     199        z = x * x; 
     200        p = (z-Z1) * x * polevl( z, JPJ1, 4 ); 
     201        return( xx < 0. ? -p : p ); 
    198202    } 
    199203 
     
    204208    w = q*q; 
    205209    xn = q * polevl( w, PH1J1, 7) - THPIO4F; 
    206     p = p * cos(xn + xx); 
    207  
    208     return(p); 
     210    p = p * cos(xn + x); 
     211 
     212    return( xx < 0. ? -p : p ); 
    209213} 
    210214#endif 
Note: See TracChangeset for help on using the changeset viewer.