home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / graf / fract4.zip / MPMATH_C.C < prev    next >
C/C++ Source or Header  |  1989-12-30  |  9KB  |  368 lines

  1. /* MPMath_c.c (C) 1989, Mark C. Peterson, CompuServe [70441,3353]
  2.      All rights reserved.
  3.  
  4.    Code may be used in any program provided the author is credited
  5.      either during program execution or in the documentation.  Source
  6.      code may be distributed only in combination with public domain or
  7.      shareware source code.  Source code may be modified provided the
  8.      copyright notice and this message is left unchanged and all
  9.      modifications are clearly documented.
  10.  
  11.      I would appreciate a copy of any work which incorporates this code,
  12.      however this is optional.
  13.  
  14.      Mark C. Peterson
  15.      128 Hamden Ave., F
  16.      Waterbury, CT 06704
  17.      (203) 754-1162
  18. */
  19.  
  20. #include "mpmath.h"
  21.  
  22. int MPaccuracy = 32;
  23.  
  24. struct MP MPsub(struct MP x, struct MP y) {
  25.    y.Exp ^= 0x8000;
  26.    return(MPadd(x, y));
  27. }
  28.  
  29. /* added by TW */
  30. struct MP MPsub086(struct MP x, struct MP y) {
  31.    y.Exp ^= 0x8000;
  32.    return(MPadd086(x, y));
  33. }
  34.  
  35. /* added by TW */
  36. struct MP MPsub386(struct MP x, struct MP y) {
  37.    y.Exp ^= 0x8000;
  38.    return(MPadd386(x, y));
  39. }
  40.  
  41. struct MP MPabs(struct MP x) {
  42.    x.Exp &= 0x7fff;
  43.    return(x);
  44. }
  45.  
  46. struct MPC MPCsqr(struct MPC x) {
  47.    struct MPC z;
  48.  
  49.    z.r = pMPsub(pMPmul(x.r, x.r), pMPmul(x.i, x.i));
  50.    z.i = pMPmul(x.r, x.i);
  51.    z.i.Exp++;
  52.    return(z);
  53. }
  54.  
  55. struct MP MPCmod(struct MPC x) {
  56.    return(pMPadd(pMPmul(x.r, x.r), pMPmul(x.i, x.i)));
  57. }
  58.  
  59. struct MPC MPCmul(struct MPC x, struct MPC y) {
  60.    struct MPC z, t;
  61.  
  62.    z.r = pMPsub(pMPmul(x.r, y.r), pMPmul(x.i, y.i));
  63.    z.i = pMPadd(pMPmul(x.r, y.i), pMPmul(x.i, y.r));
  64.    return(z);
  65. }
  66.  
  67. struct MPC MPCdiv(struct MPC x, struct MPC y) {
  68.    struct MP mod;
  69.  
  70.    mod = MPCmod(y);
  71.    y.i.Exp ^= 0x8000;
  72.    y.r = pMPdiv(y.r, mod);
  73.    y.i = pMPdiv(y.i, mod);
  74.    return(MPCmul(x, y));
  75. }
  76.  
  77. struct MPC MPCadd(struct MPC x, struct MPC y) {
  78.    struct MPC z;
  79.  
  80.    z.r = pMPadd(x.r, y.r);
  81.    z.i = pMPadd(x.i, y.i);
  82.    return(z);
  83. }
  84.  
  85. struct MPC MPCsub(struct MPC x, struct MPC y) {
  86.    struct MPC z;
  87.  
  88.    z.r = pMPsub(x.r, y.r);
  89.    z.i = pMPsub(x.i, y.i);
  90.    return(z);
  91. }
  92.  
  93. struct MPC MPCone = { 0x3fff, 0x80000000l, 0, 0l };
  94.  
  95. struct MPC MPCpow(struct MPC x, int exp) {
  96.    struct MPC z;
  97.    struct MPC zz;
  98.  
  99.    if(exp & 1)
  100.       z = x;
  101.    else
  102.       z = MPCone;
  103.    exp >>= 1;
  104.    while(exp) {
  105.       zz.r = pMPsub(MPmul(x.r, x.r), pMPmul(x.i, x.i));
  106.       zz.i = pMPmul(x.r, x.i);
  107.       zz.i.Exp++;
  108.       x = zz;
  109.       if(exp & 1) {
  110.          zz.r = pMPsub(pMPmul(z.r, x.r), pMPmul(z.i, x.i));
  111.          zz.i = pMPadd(pMPmul(z.r, x.i), pMPmul(z.i, x.r));
  112.          z = zz;
  113.       }
  114.       exp >>= 1;
  115.    }
  116.    return(z);
  117. }
  118.  
  119. int MPCcmp(struct MPC x, struct MPC y) {
  120.    struct MPC z;
  121.  
  122.    if(pMPcmp(x.r, y.r) || pMPcmp(x.i, y.i)) {
  123.       z.r = MPCmod(x);
  124.       z.i = MPCmod(y);
  125.       return(pMPcmp(z.r, z.i));
  126.    }
  127.    else
  128.       return(0);
  129. }
  130.  
  131. struct complex MPC2cmplx(struct MPC x) {
  132.    struct complex z;
  133.  
  134.    z.x = *pMP2d(x.r);
  135.    z.y = *pMP2d(x.i);
  136.    return(z);
  137. }
  138.  
  139. struct MPC cmplx2MPC(struct complex z) {
  140.    struct MPC x;
  141.  
  142.    x.r = pd2MP(z.x);
  143.    x.i = pd2MP(z.y);
  144.    return(x);
  145. }
  146.  
  147. /* function pointer versions added by Tim Wegner 12/07/89 */
  148. int        (*ppMPcmp)() = MPcmp086;
  149. int        (*pMPcmp)(struct MP x, struct MP y) = MPcmp086;
  150. struct MP  (*pMPmul)(struct MP x, struct MP y) = MPmul086;
  151. struct MP  (*pMPdiv)(struct MP x, struct MP y) = MPdiv086;
  152. struct MP  (*pMPadd)(struct MP x, struct MP y) = MPadd086;
  153. struct MP  (*pMPsub)(struct MP x, struct MP y) = MPsub086;
  154. struct MP  (*pd2MP)(double x)                  = d2MP086 ;
  155. double *   (*pMP2d)(struct MP m)               = MP2d086 ;
  156. struct MP  (*pfg2MP)(long x, int fg)           = fg2MP086;
  157.  
  158. void setMPfunctions(void) {
  159.    if(cpu == 386)
  160.    {
  161.       pMPmul = MPmul386;
  162.       pMPdiv = MPdiv386;
  163.       pMPadd = MPadd386;
  164.       pMPsub = MPsub386;
  165.       pMPcmp = MPcmp386;
  166.       pd2MP  = d2MP386 ;
  167.       pMP2d  = MP2d386 ;
  168.       pfg2MP = fg2MP386;
  169.    }
  170.    else
  171.    {
  172.       pMPmul = MPmul086;
  173.       pMPdiv = MPdiv086;
  174.       pMPadd = MPadd086;
  175.       pMPsub = MPsub086;
  176.       pMPcmp = MPcmp086;
  177.       pd2MP  = d2MP086 ;
  178.       pMP2d  = MP2d086 ;
  179.       pfg2MP = fg2MP086;
  180.    }
  181. }
  182.  
  183. #define sqr(x) ((x) * (x))
  184.  
  185. extern int debugflag, fpu;
  186.  
  187. struct complex ComplexPower(struct complex x, struct complex y) {
  188.    struct complex z, cLog, t;
  189.    double dist, e2x, siny, cosy;
  190.  
  191.    FPUcplxlog(&x, &cLog);
  192.    FPUcplxmul(&cLog, &y, &t);
  193.  
  194.    if(fpu == 387)
  195.       FPUcplxexp387(&t, &z);
  196.    else {
  197.       e2x = exp(t.x);
  198.       FPUsincos(&t.y, &siny, &cosy);
  199.       z.x = e2x * cosy;
  200.       z.y = e2x * siny;
  201.    }
  202.    return(z);
  203. }
  204.  
  205.  
  206. /***** FRACTINT specific routines and variables *****/
  207.  
  208. #ifndef TESTING_MATH
  209.  
  210. extern double param[];
  211. extern struct complex old, new, init;
  212. extern double threshold, roverd, d1overd, dx0[], dy0[];
  213. extern int periodicitycheck, row, col, debugflag;
  214.  
  215.  
  216. #include <float.h>
  217. #include <stdlib.h>
  218.  
  219. #include "fmath.h"
  220.  
  221. extern void (*plot)(int x, int y, int Color);
  222. extern int  xdots, ydots;     /* coordinates of dots on the screen  */
  223. extern int  colors;           /* maximum colors available */
  224. extern int  maxit;
  225. extern char far *farmemalloc(long bytestoalloc);
  226. extern void farmemfree(char far *farptr);
  227.  
  228. char far *LogTable = (char far *)0;
  229. extern int LogFlag;
  230. float fColors;
  231.  
  232. void ChkLogFlag(void) {
  233.    float l, f, c = (float)256.0, m;
  234.    unsigned n, x;
  235.  
  236.    if(!LogFlag) {
  237.       if(LogTable) {
  238.          farmemfree(LogTable);
  239.          LogTable = (char far *)0;
  240.       }
  241.    }
  242.    else {
  243.       if(!LogTable) {
  244.          if(LogTable = farmemalloc((long)maxit + 1)) {
  245.             Fg2Float((long)maxit, 0, m);
  246.             fLog14(m, m);
  247.             fDiv(c, m, c);
  248.             for(n = 1; n < maxit; n++) {
  249.                Fg2Float((long)n, 0, f);
  250.                fLog14(f, l);
  251.                fMul16(l, c, l);
  252.                LogTable[n] = Float2Fg(l, 0) + 1;
  253.             }
  254.             LogTable[0] = 0;
  255.          }
  256.          else
  257.             buzzer(2);
  258.       }
  259.    }
  260. }
  261.  
  262. long far ExpFloat14(long x) {
  263.    static float fLogTwo = (float)0.6931472;
  264.    int f;
  265.    long Ans;
  266.  
  267.    f = 23 - (int)RegFloat2Fg(RegDivFloat(x, *(long*)&fLogTwo), 0);
  268.    Ans = ExpFudged(RegFloat2Fg(x, 16), f);
  269.    return(RegFg2Float(Ans, (char)f));
  270. }
  271.  
  272. extern struct complex tmp;
  273. extern int color, colors;
  274. double X_TwoPi;
  275. struct complex temp, t2, BaseLog;
  276. struct complex cdegree = { 3.0, 0.0 },
  277.                croot   = { 1.0, 0.0 };
  278.  
  279. int ComplexNewtonSetup(void) {
  280.    threshold = .001;
  281.    periodicitycheck = 0;
  282.    if(param[0] != 0.0 || param[1] != 0.0 || param[2] != 0.0 || 
  283.       param[3] != 0.0) {
  284.       croot.x = param[2];
  285.       croot.y = param[3];
  286.       cdegree.x = param[0];
  287.       cdegree.y = param[1];
  288.       FPUcplxlog(&croot, &BaseLog);
  289.       X_TwoPi = asin(1.0) * 4;
  290.    }
  291.    return(1);
  292. }
  293.  
  294. int ComplexNewton(void) {
  295.    struct complex cd1;
  296.    double dist, mod, Angle, e2x;
  297.  
  298.    /* new = ((cdegree-1) * old**cdegree) + croot
  299.             ----------------------------------
  300.                  cdegree * old**(cdegree-1)         */
  301.  
  302.    cd1.x = cdegree.x - 1.0;
  303.    cd1.y = cdegree.y;
  304.  
  305.    temp = ComplexPower(old, cd1);
  306.    FPUcplxmul(&temp, &old, &new);
  307.  
  308.    tmp.x = new.x - croot.x;
  309.    tmp.y = new.y - croot.y;
  310.    if((sqr(tmp.x) + sqr(tmp.y)) < threshold)
  311.       return(1);
  312.  
  313.    FPUcplxmul(&new, &cd1, &tmp);
  314.    tmp.x += croot.x;
  315.    tmp.y += croot.y;
  316.  
  317.    FPUcplxmul(&temp, &cdegree, &t2);
  318.    FPUcplxdiv(&tmp, &t2, &old);
  319.    return(0);
  320. }
  321.  
  322. int ComplexBasin(void) {
  323.    struct complex cd1;
  324.    double dist, mod, Angle, e2x;
  325.  
  326.    /* new = ((cdegree-1) * old**cdegree) + croot
  327.             ----------------------------------
  328.                  cdegree * old**(cdegree-1)         */
  329.  
  330.    cd1.x = cdegree.x - 1.0;
  331.    cd1.y = cdegree.y;
  332.  
  333.    temp = ComplexPower(old, cd1);
  334.    FPUcplxmul(&temp, &old, &new);
  335.  
  336.    tmp.x = new.x - croot.x;
  337.    tmp.y = new.y - croot.y;
  338.    if((sqr(tmp.x) + sqr(tmp.y)) < threshold) {
  339.       if(fabs(old.y) < .01)
  340.          old.y = 0.0;
  341.       FPUcplxlog(&old, &temp);
  342.       FPUcplxmul(&temp, &cdegree, &tmp);
  343.       mod = tmp.y/X_TwoPi;
  344.       color = (int)mod;
  345.       if(fabs(mod - color) > 0.5) {
  346.          if(mod < 0.0)
  347.             color--;
  348.          else
  349.             color++;
  350.       }
  351.       color += 2;
  352.       if(color < 0)
  353.          color += 128;
  354.       return(1);
  355.    }
  356.  
  357.    FPUcplxmul(&new, &cd1, &tmp);
  358.    tmp.x += croot.x;
  359.    tmp.y += croot.y;
  360.  
  361.    FPUcplxmul(&temp, &cdegree, &t2);
  362.    FPUcplxdiv(&tmp, &t2, &old);
  363.    return(0);
  364. }
  365.  
  366. #endif
  367.  
  368.