home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Snippets / NewDynamic 1.0.1 / original source / Original app. / DYNAMIC.TEXT < prev    next >
Encoding:
Text File  |  1995-12-01  |  11.4 KB  |  551 lines  |  [TEXT/EDIT]

  1. #include <stdio.h>
  2. #include <packages.h>
  3. #include <math.h>
  4.  
  5. #define lastMenu                3     /* number of menus */
  6. #define appleMenu             1     /* menu ID for desk accessory menu */
  7. #define fileMenu                256     /* menu ID for File menu */
  8. #define editMenu                257     /* menu ID for Edit menu */
  9.  
  10. #define maxvalue                512
  11. #define maxvectsize         8
  12. short vscaling = 1;
  13. #define hscaling                1
  14.  
  15.  
  16. typedef double matrixtype[maxvectsize][maxvectsize];
  17. typedef double vectortype[maxvectsize];
  18. typedef char titletype[21];
  19.  
  20.  
  21. typedef struct optiontype
  22.     {
  23.     Boolean start, quit, edit, newfile, debug;
  24. } optiontype;
  25.  
  26. Rect Trect, dragRect;
  27. EventRecord myEvent;
  28. WindowPtr myWindow, whichWindow;
  29. WindowRecord wRecord;
  30. short code;
  31. MenuHandle myMenus[lastMenu];     /*Handles to all of the menus*/
  32. DialogPtr theDialog;
  33. optiontype options;
  34.  
  35. vectortype need, deltaneed, response, deltaresponse, alphavector,
  36.             stimulation, consumvector, tempvct1, tempvct2;
  37. matrixtype generalization, consumation, excitation, inhibition,
  38.             tempmatrix, ident;
  39. short vectSize, loop, stimuli;
  40. double alpha, personality;
  41. char answer;
  42.  
  43.  
  44. /* procedure InitScreen;*/
  45. /* begin*/
  46. /*    HideAll;*/
  47. /*    GetPort(dPort);*/
  48. /*    GetDrawingRect(dRect);*/
  49. /*    GetTextRect(TRect);*/
  50. /*    SetRect(TextRect, 250, 30, 530, 250);*/
  51. /*    SetRect(DrawRect, 5, 40, 700, 380);*/
  52. /*    SetTextRect(TextRect);*/
  53. /*    SetDrawingRect(DrawRect);*/
  54. /*    ShowDrawing;*/
  55. /*    ShowText;*/
  56. /* end;*/
  57.  
  58. void SetUpMenus(void)
  59. {
  60.     short i;
  61.  
  62.     InitMenus ();
  63.     myMenus[0] = GetMenu(appleMenu);
  64.     AddResMenu(myMenus[0], 'DRVR');  /*add desk accessories*/
  65.     myMenus[1] = GetMenu(fileMenu);
  66.     myMenus[2] = GetMenu(editMenu);   /*not really used,  but just for practice*/
  67.     for (i = 0; i <= lastMenu - 1; i++)
  68.         InsertMenu(myMenus[i], 0);
  69.     DrawMenuBar ();
  70. }
  71.  
  72. void Startup(void)
  73. {     /*start initialization sequence*/
  74.     /*basic initialization of MAC tools*/
  75.     InitGraf(&thePort);        /*start initialization sequence*/
  76.     InitFonts ();
  77.     InitWindows ();
  78.     TEInit ();
  79.     InitDialogs(0L);
  80.  
  81.     SetUpMenus ();  /*throw menu bar up on the screen and insert menu items*/
  82.     InitCursor ();  /*make cursor an arrow*/
  83.     FlushEvents(everyEvent, 0);
  84.  
  85.     myWindow = GetNewWindow(256, &wRecord, (WindowPtr)-1L);/*show this one*/
  86.     SetPort(myWindow);   /*graphics will go into my window*/
  87.  
  88.     /*Writelns and Readlns text will go to my window*/
  89.  
  90.     /*     WWINIT;*/
  91.     /*initialize the WritelnWindow unit*/
  92.     /*not used anymore*/
  93.     /*with Trect do*/
  94.     /*begin*/
  95.     /*top := 40;*/
  96.     /*right := 480;*/
  97.     /*bottom := 330;*/
  98.     /*left := 40;*/
  99.     /*end;*/
  100.     /*GetFNum('Geneva', familyID);*/
  101.     /*WWNew(Trect, 'Animals', false, true, 80, FamilyID, 9);*/
  102. }
  103.  
  104. void matrixXvector(double *a, double (*b)[maxvectsize], double *c,
  105.                 short n, short m)
  106. {
  107.     /*A:= B C                                    A is a m dimensional vector*/
  108.     /*B is a m x n dimensional matrix*/
  109.     /*C is a n dimensional vector*/
  110.     short i, j;
  111.     double aij;
  112.  
  113.     for (i = 0; i <= n - 1; i++)
  114.     {     /*rows*/
  115.         aij = 0.0;
  116.         for (j = 0; j <= m - 1; j++)     /*columns*/
  117.             aij += b[i][j] * c[j];
  118.         a[i] = aij;
  119.     }
  120. }    /*matrixXvector*/
  121.  
  122.  
  123. void vectoradd(double *a, double *b, double *c, short n)
  124. {
  125.     /*A:=B+ C                     A,B,C are n dimensional vectors*/
  126.     short i;
  127.  
  128.     for (i = 0; i <= n - 1; i++)
  129.         a[i] = b[i] + c[i];
  130. }
  131.  
  132.  
  133. void vectorsubtract(double *a, double *b, double *c, short n)
  134. {
  135.     short i;
  136.  
  137.     for (i = 0; i <= n - 1; i++)
  138.         a[i] = b[i] - c[i];
  139. }
  140.  
  141.  
  142. void vectorzero(double *a, short n)
  143. {
  144.     short i;
  145.  
  146.     for (i = 0; i <= n - 1; i++)
  147.     {
  148.         if (a[i] < 0.0)
  149.             a[i] = 0.0;
  150.     }
  151. }
  152.  
  153.  
  154. void VectorInit(double *a, short n)
  155. {
  156.     short i;
  157.  
  158.     for (i = 0; i <= n - 1; i++)
  159.         a[i] = 0.0;
  160. }
  161.  
  162.  
  163. void vectordraw(double *a, double *b, short n, short loop,
  164.                      short offset, short vscaling)
  165. {
  166.     short i, v, dv;
  167.  
  168.     for (i = 1; i <= n; i++)
  169.     {
  170.         if (a[i-1] == 0.0)
  171.     {     /*look out for zeroed out responses*/
  172.             v = offset;
  173.             dv = 0;
  174.         } else
  175.     {
  176.             v = offset - ceil(a[i-1] * vscaling);
  177.             dv = - ceil(b[i-1] * vscaling);
  178.         }
  179.         /* normal condition*/
  180.         MoveTo(loop * (long) hscaling, v);
  181.         PenSize(i, i);  /*change pen size to indicate which activity is occuring*/
  182.         Line((long) hscaling, dv);
  183.     }
  184. }
  185.  
  186.  
  187. void vectorread(double *a, short n, char *title)
  188. {
  189.     short i;
  190.  
  191.     printf("enter the %s vector\n", title);
  192.     for (i = 0; i <= n - 1; i++)
  193.         scanf("%lg", &a[i]);
  194.     putchar('\n');
  195. }
  196.  
  197.  
  198. void scalarXvector(double *a, double s, double *b, short n)
  199. {
  200.     short i;
  201.  
  202.     for (i = 0; i <= n - 1; i++)
  203.         a[i] = s * b[i];
  204. }
  205.  
  206.  
  207. void scalarXmatrix(double (*a)[maxvectsize], double s,
  208.                 double (*b)[maxvectsize], short n)
  209. {
  210.     short i, j;
  211.  
  212.     for (i = 0; i <= n - 1; i++)
  213.     {
  214.         for (j = 0; j <= n - 1; j++)
  215.             a[i][j] = s * b[i][j];
  216.     }
  217. }
  218.  
  219.  
  220. void matrixread(double (*a)[maxvectsize], short n, char *title)
  221. {
  222.     short i, j;
  223.  
  224.     printf("enter the %s matrix\n", title);
  225.     for (i = 0; i <= n - 1; i++)
  226.     {
  227.         for (j = 0; j <= n - 1; j++)
  228.             scanf("%lg", &a[i][j]);
  229.         putchar('\n');
  230.     }
  231. }
  232.  
  233.  
  234. void matrixwrite(double (*a)[maxvectsize], short n)
  235. {
  236.     short i, j;
  237.  
  238.     for (i = 0; i <= n - 1; i++)
  239.     {
  240.         for (j = 0; j <= n - 1; j++)
  241.             printf("%5.2f", a[i][j]);
  242.         putchar('\n');
  243.     }
  244. }
  245.  
  246.  
  247. void identity(double (*temp)[maxvectsize], short vectSize)
  248. {
  249.     short i, j;
  250.  
  251.     for (i = 1; i <= vectSize; i++)
  252.     {
  253.         if (i > 1)
  254.     {
  255.             for (j = 0; j <= i - 2; j++)
  256.             {
  257.                 temp[i-1][j] = 0.0;
  258.                 temp[j][i-1] = 0.0;
  259.             }
  260.         }
  261.         temp[i-1][i-1] = 1.0;
  262.     }
  263. }
  264.  
  265.  
  266. void DynamicSimulation(void)
  267. {    /*dynamic*/
  268.     /*the old program dynamic*/
  269.     short stimuli, loop;
  270.     char STR1[256];
  271.     short FORLIM, FORLIM1;
  272.  
  273.  
  274.     /*InitScreen;*/
  275.     printf(" a model of reciprocal inhibition \n");
  276.     /*write('enter alpha, personality ');*/
  277.     /*readln(alpha, personality);*/
  278.     alpha = 0.2;
  279.     personality = 1.0;
  280.  
  281.     printf("do you want to specify the problem size? [no] \n");
  282.     scanf("%c%*[^\n]", &answer);
  283.     getchar();
  284.     if (answer == '\n')
  285.         answer = ' ';
  286.     if (answer == 'y')
  287.     {
  288.         printf("enter the problem size    ");
  289.         scanf("%hd%*[^\n]", &vectSize);
  290.         getchar();
  291.     } else
  292.         vectSize = 3;
  293.     identity(ident, vectSize);
  294.     printf("do you want to specify the stimulation vector? [no] \n");
  295.     scanf("%c%*[^\n]", &answer);
  296.     getchar();
  297.     if (answer == '\n')
  298.         answer = ' ';
  299.     if (answer == 'y')
  300.         vectorread(stimulation, vectSize, "stimulation");
  301.     else
  302.     {
  303.         FORLIM = vectSize;
  304.         for (stimuli = 1; stimuli <= vectSize; stimuli++)
  305.             stimulation[stimuli-1] = stimuli * 0.5;
  306.     }
  307.     printf("do you want to specify the consummation matrix [no] \n");
  308.     scanf("%c%*[^\n]", &answer);
  309.     getchar();
  310.     if (answer == '\n')
  311.         answer = ' ';
  312.     if (answer == 'y')
  313.         matrixread(consumation, vectSize, "consummation");
  314.     else
  315.         scalarXmatrix(consumation, 0.2, ident, vectSize);
  316.     printf("do you want to specify the excitation matrix [no] \n");
  317.     scanf("%c%*[^\n]", &answer);
  318.     getchar();
  319.     if (answer == '\n')
  320.         answer = ' ';
  321.     if (answer == 'y')
  322.         matrixread(excitation, vectSize, "excitation");
  323.     else
  324.         scalarXmatrix(excitation, alpha, ident, vectSize);
  325.     printf("do you want to specify the inhibition matrix? [no] \n");
  326.     scanf("%c%*[^\n]", &answer);
  327.     getchar();
  328.     if (answer == '\n')
  329.         answer = ' ';
  330.     if (answer == 'y')
  331.         matrixread(inhibition, vectSize, "inhibition");
  332.     else
  333.     {
  334.         for (loop = 0; loop <= vectSize - 1; loop++)
  335.         {
  336.             for (stimuli = 0; stimuli <= vectSize - 1; stimuli++)
  337.                 inhibition[loop][stimuli] = 1.0;
  338.             inhibition[loop][loop] = 0.1;
  339.         }
  340.     }
  341.     VectorInit(need, vectSize);
  342.             /*zero out initial values of need and response*/
  343.     VectorInit(response, vectSize);
  344.     scalarXmatrix(generalization, personality, ident, vectSize);
  345.  
  346.     for (loop = 1; loop <= 25; loop++)     /*clear the screen*/
  347.         putchar('\n');
  348.     ShowWindow (myWindow);
  349.     SetPort (myWindow);
  350.     SelectWindow (myWindow);
  351.     MoveTo(0, 140);
  352.     DrawString("\pNeeds");
  353.     MoveTo(0, 250);
  354.     DrawString("\pResponses");
  355.     MoveTo(100, 30);
  356.     DrawString("\pTime -->");
  357.  
  358.     /*begin the main loop*/
  359.     for (loop = 1; loop <= maxvalue; loop++)
  360.     {
  361.         /*if loop = 250 then*/
  362.         /*        ShowDrawing;*/
  363.         matrixXvector(tempvct1, generalization, stimulation, vectSize, vectSize);
  364.         matrixXvector(consumvector, consumation, response, vectSize, vectSize);
  365.  
  366. //         if (! Button ())
  367. //             vectorread(stimulation, vectSize, "new stimulation");
  368.  
  369.         vectorsubtract(deltaneed, tempvct1, consumvector, vectSize);
  370.  
  371.         matrixXvector(tempvct1, inhibition, response, vectSize, vectSize);
  372.         matrixXvector(tempvct2, excitation, need, vectSize, vectSize);
  373.  
  374.         vectorsubtract(deltaresponse, tempvct2, tempvct1, vectSize);
  375.  
  376.         vectordraw(need, deltaneed, vectSize, loop, 125, vscaling);
  377.  
  378.         vectoradd(need, need, deltaneed, vectSize);
  379.  
  380.         vectordraw(response, deltaresponse, vectSize, loop, 300, vscaling);
  381.  
  382.         vectoradd(response, response, deltaresponse, vectSize);
  383.  
  384.         vectorzero(response, vectSize);
  385.     }
  386. }    /*dynamic*/
  387.  
  388.  
  389. /*the remaining code was added to allow for windows, menus, etc*/
  390.  
  391. void Message(short dialogNumber)
  392. {     /*save the current status of the screen display*/
  393.     /*Throw up a Dialog Box and wait for keypress*/
  394.     DialogPtr theDialog;
  395.     EventRecord myEvent;
  396.     short itemHit;
  397.     GrafPtr tempPort;
  398.  
  399.     GetPort(tempPort);       /*save the current status of the screen display*/
  400.     theDialog = GetNewDialog(dialogNumber, 0L, (WindowPtr)-1L); /*display dialog # */
  401.     ModalDialog(0L, &itemHit); /*show the dialog and wait for a button*/
  402.     DisposDialog(theDialog);  /*get rid of the dialog and show the screen*/
  403.     SetPort(tempPort);        /*put back the original display*/
  404. }
  405.  
  406.  
  407.  
  408.  
  409. void DoCommand(long mResult, optiontype *options)
  410. {
  411.     /*process menu selection*/
  412.     Str255 name;
  413.     short theMenu, theItem, refNum;
  414.     short AlertId = 1;
  415.  
  416.     theMenu = HiWord(mResult);
  417.     theItem = LoWord(mResult);
  418.     switch (theMenu)
  419.     {
  420.  
  421.     case appleMenu:
  422.         if (theItem == 1)     /*throw up message window*/
  423.             Message(1);
  424.         else
  425.             {
  426.                 GetItem(myMenus[0], theItem, &name);
  427.                 refNum = OpenDeskAcc(&name);
  428.         }
  429.         break;
  430.         /*applemenu*/
  431.  
  432.     /*do a desk accessory*/
  433. /* p2c: DYNAMIC.pas, line 396:
  434.  * Warning: Too many arguments for built-in routine [225] */
  435.     case fileMenu:
  436.         switch (theItem)
  437.         {
  438.  
  439.             case 1:
  440.                 DynamicSimulation ();
  441.             break;
  442.  
  443.         case 2:
  444.             options->debug = true;
  445.             break;
  446.  
  447.         case 3:
  448.             /* blank case */
  449.             break;
  450.  
  451.         /* AlertNum:=CautionAlert(AlertId,nil);*/
  452.         /*                                                                If AlertNum=1 then*/
  453.         /*                                                                             initialize;*/
  454.         case 4:
  455.             options->quit = true;
  456.             break;
  457.         }
  458.         break;
  459.  
  460.     case editMenu:
  461.         options->edit = true;
  462.         break;
  463.  
  464.  
  465.     }/*case theMenu*/
  466. }    /*DoCommand*/
  467.  
  468.  
  469. void WaitForCommand(void)
  470. {
  471.     /*get options from menu bar until start*/
  472.     char theChar;
  473.  
  474.     options.start = false;
  475.     options.debug = false;
  476.     options.edit = false;
  477.     options.newfile = false;
  478.     options.quit = false;
  479.     do
  480.     {
  481.         GetNextEvent(everyEvent, &myEvent);
  482.         switch (myEvent.what)
  483.         {
  484.         case mouseDown:
  485.             code = FindWindow(myEvent.where, &whichWindow);
  486.             switch (code)
  487.             {
  488.                 case inMenuBar:
  489.                     DoCommand(MenuSelect(myEvent.where), &options);
  490.                 break;
  491.  
  492.                 case inSysWindow:
  493.                     SystemClick(&myEvent, whichWindow);
  494.     break;
  495.  
  496.             case inDrag:
  497.                         DragWindow(whichWindow, myEvent.where, &dragRect);
  498.     break;
  499.             }/*case code*/
  500.             break;
  501.             /*mouse down*/
  502.  
  503.         case keyDown:
  504.         case autoKey:    /*try processing a menu-key equivalent*/
  505.             theChar = (myEvent.message & charCodeMask);     /*the last byte*/
  506.             if ((myEvent.modifiers & cmdKey) != 0)
  507.     DoCommand(MenuKey(theChar), &options);
  508.             break;
  509.         }/*case myEvent.what*/
  510.  
  511.         if (options.start)
  512.     {;
  513. //             DynamicSimulation();
  514. //             options.start = false;
  515.         }
  516.         if (options.edit)
  517.     {     /*repeat loop*/
  518.             /* editfile;*/
  519.             options.edit = false;
  520.         }
  521.     } while (!(options.quit || options.edit));     /*set in DoCommand*/
  522. }    /*waitfor command*/
  523.  
  524.  
  525.  
  526. void PutUpWindow(void)
  527. {
  528.     WindowPtr drawWindPtr;
  529.     Point offSet;
  530.     char STR[256];
  531.  
  532.     drawWindPtr = NewCWindow(0L, &screenBits.bounds, "\p", true, plainDBox, (WindowPtr)-1L,
  533.                  false, 0L);
  534.     SetPort (drawWindPtr);
  535. }
  536.  
  537.  
  538. main()
  539. {
  540.     Startup();     /*various MAC type initializations*/
  541.     /*PutUpWindow;*/
  542.     putchar('\n');
  543.             /*the screen initialization seems to start 1 line too high*/
  544.  
  545.     WaitForCommand();     /*the Main Event Loop*/
  546. }
  547.  
  548.  
  549.  
  550. /* End. */
  551.