home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume29 / persim / part01 / set.c < prev    next >
C/C++ Source or Header  |  1992-04-06  |  4KB  |  172 lines

  1. /*Copyright  (c)   1992  Adam  Stein.   All  Rights Reserved.   
  2.  
  3.   Permission to use,  copy,  modify  and  distribute  without
  4.   charge this software, documentation, images, etc. is grant-
  5.   ed, provided that this copyright and the author's  name  is  
  6.   retained.   
  7.   
  8.   A fee may be charged for this program ONLY to recover costs   
  9.   for distribution (i.e. media costs).  No profit can be made            
  10.   on this program.   
  11.    
  12.   The author assumes no responsibility for disasters (natural   
  13.   or otherwise) as a consequence of use of this software.      
  14.    
  15.   Adam Stein (stein.wbst129@xerox.com)         
  16. */ 
  17.  
  18. #include <stdio.h>
  19. #include <math.h>
  20. #include "persim.h"
  21.  
  22. extern NODE_ATTR *node_attr;
  23. extern STATE state;
  24.  
  25. /*This routine will set a variable to an integer number.
  26.  
  27.   Inputs:  number    - number to set variable to
  28.        var       - variable to set
  29.   Outputs: none
  30.   Locals:  loop      - loop through output nodes
  31.   Globals: input     - input values array
  32.        node_attr - node attributes (function, threshold)
  33.        output    - output values array
  34.        state     - system variables
  35.        INODES    - set number of input nodes variable
  36.        ONODES    - set number of output nodes variable
  37.        NULL      - 0
  38. */
  39. void iset(var,number)
  40. register int var,number;
  41. {
  42.     register int loop;
  43.     char *calloc();
  44.     extern double *input,*output;
  45.  
  46.     switch(var) {
  47.       case INODES:
  48.         if(number < 1) puts("illegal value for number of input nodes");
  49.         else {
  50.                if(input != (double *) NULL)
  51.              resize_io(&input,number);
  52.  
  53.                state.inodes = number;
  54.              }
  55.         break;
  56.       case ONODES:
  57.         if(number < 1) puts("illegal value for number of output nodes");
  58.         else {
  59.                if(output != (double *) NULL) resize_io(&output,number);
  60.                if(node_attr != (NODE_ATTR *) NULL)
  61.              resize_attr(&node_attr,number);
  62.                else if((node_attr = (NODE_ATTR *) calloc(number,
  63.                               sizeof(NODE_ATTR)))
  64.                               == NULL)
  65.                   error();
  66.  
  67.                for(loop = 0;loop < state.onodes;++loop)
  68.              node_attr[loop].func = NULL;
  69.  
  70.                state.onodes = number;
  71.              }
  72.         break;
  73.     }
  74. }
  75.  
  76. /*This routine will set a variable to a floating point number.
  77.  
  78.   Inputs:  number - number to set variable to
  79.        var      - variable to set
  80.   Outputs: none
  81.   Locals:  none
  82.   Globals: state  - system variables
  83.        ALPHA  - set alpha
  84. */
  85. void fpset(var,number)
  86. register int var;
  87. register double number;
  88. {
  89.     switch(var) {
  90.       case ALPHA:
  91.         if((number < 0.0) || (number > 1.0))
  92.           puts("illegal value for alpha");
  93.         else state.alpha = number;
  94.         break;
  95.     }
  96. }
  97.  
  98. /*This routine will set the threshold level of an output node.
  99.  
  100.   Inputs:  value      - threshold level
  101.        whichnnode - which node to set (-1 means ALL nodes)
  102.   Outputs: none
  103.   Locals:  loop          - loop through output nodes
  104.   Globals: node_attr  - node attributes (function, threshold)
  105.        state      - system variables
  106.        NULL       - 0
  107. */
  108. void set_threshold(whichnode,value)
  109. register int whichnode;
  110. register double value;
  111. {
  112.     register int loop;
  113.  
  114.     if(node_attr == (NODE_ATTR *) NULL) {
  115.       puts("the number of output nodes has not been set yet");
  116.       return;
  117.     }
  118.  
  119.     if(whichnode == -1)
  120.       for(loop = 0;loop < state.onodes;++loop)
  121.         node_attr[loop].threshold = value;
  122.     else node_attr[whichnode-1].threshold = value;
  123. }
  124.  
  125. /*This routine will set the function of an output node.
  126.  
  127.   Inputs:  string     - function to set
  128.        whichnnode - which node to set (-1 means ALL nodes)
  129.   Outputs: none
  130.   Locals:  loop          - loop through output nodes
  131.   Globals: node_attr  - node attributes (function, threshold)
  132.        state      - system variables
  133.        NULL       - 0
  134. */
  135. void set_func(whichnode,string)
  136. register int whichnode;
  137. register char *string;
  138. {
  139.     register int loop;
  140.  
  141.     if(node_attr == (NODE_ATTR *) NULL) {
  142.       puts("the number of output nodes has not been set yet");
  143.       return;
  144.     }
  145.  
  146.     if(whichnode == -1) {
  147.       for(loop = 0;loop < state.onodes;++loop)
  148.         if(!_set_func(string,&node_attr[loop]))
  149.           puts("illegal function");
  150.     } else if(!_set_func(string,&node_attr[whichnode-1]))
  151.          puts("illegal function");
  152. }
  153.  
  154. /*This routine will set the correct function pointer based on function name.
  155.   
  156.   Inputs:  node   - which output node to set
  157.        string - function to set it to
  158.   Outputs: none
  159.   Locals:  none
  160.   Globals: none
  161. */
  162. _set_func(string,node)
  163. register char *string;
  164. register NODE_ATTR *node;
  165. {
  166.     double step();
  167.  
  168.     if(!strcmp(string,"step")) node->func = step;
  169.     else if(!strcmp(string,"arctan")) node->func = atan;
  170. }
  171.  
  172.