home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / qb / toolbox / disk1 / complex.bas < prev    next >
Encoding:
BASIC Source File  |  1988-04-27  |  18.0 KB  |  512 lines

  1.   ' ************************************************
  2.   ' **  Name:          COMPLEX                    **
  3.   ' **  Type:          Toolbox                    **
  4.   ' **  Module:        COMPLEX.BAS                **
  5.   ' **  Language:      Microsoft QuickBASIC 4.00  **
  6.   ' ************************************************
  7.   '
  8.   ' Demonstrates a set of complex number functions and
  9.   ' subprograms.
  10.   '
  11.   ' USAGE:         No command line parameters
  12.   ' .MAK FILE:     COMPLEX.BAS
  13.   '                CARTESIA.BAS
  14.   ' PARAMETERS:    (none)
  15.   ' VARIABLES:     a          Variable of type Complex
  16.   '                b          Variable of type Complex
  17.   '                c          Variable of type Complex
  18.   '                x$         String representation of a complex number
  19.   '                y$         String representation of a complex number
  20.   '                z$         String representation of a complex number
  21.   
  22.     TYPE Complex
  23.         r AS SINGLE
  24.         i AS SINGLE
  25.     END TYPE
  26.   
  27.   ' Subprograms
  28.     DECLARE SUB ComplexSub (a AS Complex, b AS Complex, c AS Complex)
  29.     DECLARE SUB ComplexSqr (a AS Complex, c AS Complex)
  30.     DECLARE SUB ComplexRoot (a AS Complex, b AS Complex, c AS Complex)
  31.     DECLARE SUB ComplexReciprocal (a AS Complex, c AS Complex)
  32.     DECLARE SUB ComplexAdd (a AS Complex, b AS Complex, c AS Complex)
  33.     DECLARE SUB ComplexLog (a AS Complex, c AS Complex)
  34.     DECLARE SUB ComplexPower (a AS Complex, b AS Complex, c AS Complex)
  35.     DECLARE SUB Complex2String (a AS Complex, x$)
  36.     DECLARE SUB String2Complex (x$, a AS Complex)
  37.     DECLARE SUB ComplexDiv (a AS Complex, b AS Complex, c AS Complex)
  38.     DECLARE SUB ComplexExp (a AS Complex, c AS Complex)
  39.     DECLARE SUB ComplexMul (a AS Complex, b AS Complex, c AS Complex)
  40.     DECLARE SUB Rec2pol (x!, y!, r!, theta!)
  41.   
  42.     DIM a AS Complex, b AS Complex, c AS Complex
  43.   
  44.     CLS
  45.     INPUT "Enter first complex number  "; x$
  46.     String2Complex x$, a
  47.     Complex2String a, x$
  48.     PRINT x$
  49.     PRINT
  50.   
  51.     ComplexExp a, c
  52.     Complex2String c, z$
  53.     PRINT "ComplexExp", , z$
  54.   
  55.     ComplexLog a, c
  56.     Complex2String c, z$
  57.     PRINT "ComplexLog", , z$
  58.   
  59.     ComplexReciprocal a, c
  60.     Complex2String c, z$
  61.     PRINT "ComplexReciprocal", z$
  62.   
  63.     ComplexSqr a, c
  64.     Complex2String c, z$
  65.     PRINT "ComplexSqr", , z$
  66.   
  67.     PRINT
  68.     INPUT "Enter second complex number "; y$
  69.     String2Complex y$, b
  70.     Complex2String b, y$
  71.     PRINT y$
  72.     PRINT
  73.   
  74.     ComplexAdd a, b, c
  75.     Complex2String c, z$
  76.     PRINT "ComplexAdd", , z$
  77.   
  78.     ComplexSub a, b, c
  79.     Complex2String c, z$
  80.     PRINT "ComplexSub", , z$
  81.   
  82.     ComplexMul a, b, c
  83.     Complex2String c, z$
  84.     PRINT "ComplexMul", , z$
  85.   
  86.     ComplexDiv a, b, c
  87.     Complex2String c, z$
  88.     PRINT "ComplexDiv", , z$
  89.   
  90.     ComplexPower a, b, c
  91.     Complex2String c, z$
  92.     PRINT "ComplexPower", , z$
  93.   
  94.     ComplexRoot a, b, c
  95.     Complex2String c, z$
  96.     PRINT "ComplexRoot", , z$
  97.   
  98.  
  99.   ' ************************************************
  100.   ' **  Name:          Complex2String             **
  101.   ' **  Type:          Subprogram                 **
  102.   ' **  Module:        COMPLEX.BAS                **
  103.   ' **  Language:      Microsoft QuickBASIC 4.00  **
  104.   ' ************************************************
  105.   '
  106.   ' Makes a string representation of a complex number.
  107.   '
  108.   ' EXAMPLE OF USE:  Complex2String a, x$
  109.   ' PARAMETERS:      a          Complex number variable (type Complex)
  110.   '                  x$         String representation of the complex number
  111.   ' VARIABLES:       r$         Working string, real part
  112.   '                  i$         Working string, imaginary part
  113.   ' MODULE LEVEL
  114.   '   DECLARATIONS:  TYPE Complex
  115.   '                     r AS SINGLE
  116.   '                     i AS SINGLE
  117.   '                  END TYPE
  118.   '
  119.   '                  DECLARE SUB Complex2String (a AS Complex, x$)
  120.   '
  121.     SUB Complex2String (a AS Complex, x$) STATIC
  122.       
  123.       ' Form the left part of the string
  124.         IF a.r < 0 THEN
  125.             r$ = "(" + STR$(a.r)
  126.         ELSE
  127.             r$ = "(" + MID$(STR$(a.r), 2)
  128.         END IF
  129.       
  130.       ' Form the right part of the string
  131.         IF a.i < 0 THEN
  132.             i$ = STR$(a.i)
  133.         ELSE
  134.             i$ = "+" + MID$(STR$(a.i), 2)
  135.         END IF
  136.       
  137.       ' The whole is more complex than the sum of the parts
  138.         x$ = r$ + i$ + "i)"
  139.       
  140.     END SUB
  141.  
  142.   ' ************************************************
  143.   ' **  Name:          ComplexAdd                 **
  144.   ' **  Type:          Subprogram                 **
  145.   ' **  Module:        COMPLEX.BAS                **
  146.   ' **  Language:      Microsoft QuickBASIC 4.00  **
  147.   ' ************************************************
  148.   '
  149.   ' Adds two complex numbers.
  150.   '
  151.   ' EXAMPLE OF USE:  ComplexAdd a, b, c
  152.   ' PARAMETERS:      a          First complex number for the addition
  153.   '                  b          Second complex number for the addition
  154.   '                  c          Result of the complex number addition
  155.   ' VARIABLES:       (none)
  156.   ' MODULE LEVEL
  157.   '   DECLARATIONS:  TYPE Complex
  158.   '                     r AS SINGLE
  159.   '                     i AS SINGLE
  160.   '                  END TYPE
  161.   '
  162.   '         DECLARE SUB ComplexAdd (a AS Complex, b AS Complex, c AS Complex)
  163.   '
  164.     SUB ComplexAdd (a AS Complex, b AS Complex, c AS Complex) STATIC
  165.         c.r = a.r + b.r
  166.         c.i = a.i + b.i
  167.     END SUB
  168.  
  169.   ' ************************************************
  170.   ' **  Name:          ComplexDiv                 **
  171.   ' **  Type:          Subprogram                 **
  172.   ' **  Module:        COMPLEX.BAS                **
  173.   ' **  Language:      Microsoft QuickBASIC 4.00  **
  174.   ' ************************************************
  175.   '
  176.   ' Divides two complex numbers.
  177.   '
  178.   ' EXAMPLE OF USE:  ComplexDiv a, b, c
  179.   ' PARAMETERS:      a          First complex number for the division
  180.   '                  b          Second complex number for the division
  181.   '                  c          Result of the complex number division a/b
  182.   ' VARIABLES:       (none)
  183.   ' MODULE LEVEL
  184.   '   DECLARATIONS:  TYPE Complex
  185.   '                     r AS SINGLE
  186.   '                     i AS SINGLE
  187.   '                  END TYPE
  188.   '
  189.   '        DECLARE SUB ComplexDiv (a AS Complex, b AS Complex, c AS Complex)
  190.   '
  191.     SUB ComplexDiv (a AS Complex, b AS Complex, c AS Complex) STATIC
  192.         t! = b.r * b.r + b.i * b.i
  193.         c.r = (a.r * b.r + a.i * b.i) / t!
  194.         c.i = (a.i * b.r - a.r * b.i) / t!
  195.     END SUB
  196.  
  197.   ' ************************************************
  198.   ' **  Name:          ComplexExp                 **
  199.   ' **  Type:          Subprogram                 **
  200.   ' **  Module:        COMPLEX.BAS                **
  201.   ' **  Language:      Microsoft QuickBASIC 4.00  **
  202.   ' ************************************************
  203.   '
  204.   ' Calculates the exponential function of a complex number.
  205.   '
  206.   ' EXAMPLE OF USE:  ComplexExp a, c
  207.   ' PARAMETERS:      a          Complex number argument
  208.   '                  c          Complex result of the calculations
  209.   ' VARIABLES:       t!         Temporary working value
  210.   ' MODULE LEVEL
  211.   '   DECLARATIONS:  TYPE Complex
  212.   '                     r AS SINGLE
  213.   '                     i AS SINGLE
  214.   '                  END TYPE
  215.   '
  216.   '                  DECLARE SUB ComplexExp (a AS Complex, c AS Complex)
  217.   '
  218.     SUB ComplexExp (a AS Complex, c AS Complex) STATIC
  219.         t! = EXP(a.r)
  220.         c.r = t! * COS(a.i)
  221.         c.i = t! * SIN(a.i)
  222.     END SUB
  223.  
  224.   ' ************************************************
  225.   ' **  Name:          ComplexLog                 **
  226.   ' **  Type:          Subprogram                 **
  227.   ' **  Module:        COMPLEX.BAS                **
  228.   ' **  Language:      Microsoft QuickBASIC 4.00  **
  229.   ' ************************************************
  230.   '
  231.   ' Calculates the log of a complex number.
  232.   '
  233.   ' EXAMPLE OF USE:  ComplexLog a, c
  234.   ' PARAMETERS:      a          Complex number argument
  235.   '                  c          Complex result of the calculations
  236.   ' VARIABLES:       r!         Magnitude of complex number a
  237.   '                  theta!     Angle of complex number a
  238.   ' MODULE LEVEL
  239.   '   DECLARATIONS:  TYPE Complex
  240.   '                     r AS SINGLE
  241.   '                     i AS SINGLE
  242.   '                  END TYPE
  243.   '
  244.   '                  DECLARE SUB ComplexLog (a AS Complex, c AS Complex)
  245.   '                  DECLARE SUB Rec2pol (x!, y!, r!, theta!)
  246.   '
  247.     SUB ComplexLog (a AS Complex, c AS Complex) STATIC
  248.         CALL Rec2pol(a.r, a.i, r!, theta!)
  249.         IF r! <> 0! THEN
  250.             c.r = LOG(r!)
  251.             c.i = theta!
  252.         ELSE
  253.             ERROR 5
  254.         END IF
  255.     END SUB
  256.  
  257.   ' ************************************************
  258.   ' **  Name:          ComplexMul                 **
  259.   ' **  Type:          Subprogram                 **
  260.   ' **  Module:        COMPLEX.BAS                **
  261.   ' **  Language:      Microsoft QuickBASIC 4.00  **
  262.   ' ************************************************
  263.   '
  264.   ' Multiplies two complex numbers.
  265.   '
  266.   ' EXAMPLE OF USE:  ComplexMul a, b, c
  267.   ' PARAMETERS:      a          First complex number for the multiplication
  268.   '                  b          Second complex number for the multiplication
  269.   '                  c          Result of the complex number multiplication
  270.   ' VARIABLES:       (none)
  271.   ' MODULE LEVEL
  272.   '   DECLARATIONS:  TYPE Complex
  273.   '                     r AS SINGLE
  274.   '                     i AS SINGLE
  275.   '                  END TYPE
  276.   '
  277.   '         DECLARE SUB ComplexMul (a AS Complex, b AS Complex, c AS Complex)
  278.   '
  279.     SUB ComplexMul (a AS Complex, b AS Complex, c AS Complex) STATIC
  280.         c.r = a.r * b.r - a.i * b.i
  281.         c.i = a.r * b.i + a.i * b.r
  282.     END SUB
  283.  
  284.   ' ************************************************
  285.   ' **  Name:          ComplexPower               **
  286.   ' **  Type:          Subprogram                 **
  287.   ' **  Module:        COMPLEX.BAS                **
  288.   ' **  Language:      Microsoft QuickBASIC 4.00  **
  289.   ' ************************************************
  290.   '
  291.   ' Calculates a complex number raised to a complex number.
  292.   '
  293.   ' EXAMPLE OF USE:  ComplexPower a, b, c
  294.   ' PARAMETERS:      a          Complex number to be raised to a power
  295.   '                  b          Complex number to raise a to
  296.   '                  c          Result of a raised to the power of b
  297.   ' VARIABLES:       t1         Structure of type Complex
  298.   '                  t2         Structure of type Complex
  299.   ' MODULE LEVEL
  300.   '   DECLARATIONS:  TYPE Complex
  301.   '                     r AS SINGLE
  302.   '                     i AS SINGLE
  303.   '                  END TYPE
  304.   '
  305.   '      DECLARE SUB ComplexPower (a AS Complex, b AS Complex, c AS Complex)
  306.   '      DECLARE SUB ComplexExp (a AS Complex, c AS Complex)
  307.   '      DECLARE SUB ComplexLog (a AS Complex, c AS Complex)
  308.   '      DECLARE SUB ComplexMul (a AS Complex, b AS Complex, c AS Complex)
  309.   '
  310.     SUB ComplexPower (a AS Complex, b AS Complex, c AS Complex) STATIC
  311.         DIM t1 AS Complex, t2 AS Complex
  312.         IF a.r <> 0! OR a.i <> 0! THEN
  313.             CALL ComplexLog(a, t1)
  314.             CALL ComplexMul(t1, b, t2)
  315.             CALL ComplexExp(t2, c)
  316.         ELSE
  317.             ERROR 5
  318.         END IF
  319.     END SUB
  320.  
  321.   ' ************************************************
  322.   ' **  Name:          ComplexReciprocal          **
  323.   ' **  Type:          Subprogram                 **
  324.   ' **  Module:        COMPLEX.BAS                **
  325.   ' **  Language:      Microsoft QuickBASIC 4.00  **
  326.   ' ************************************************
  327.   '
  328.   ' Calculates the reciprocal of a complex number.
  329.   '
  330.   ' EXAMPLE OF USE:  ComplexReciprocal a, c
  331.   ' PARAMETERS:      a          Complex number to be processed
  332.   '                  c          Result of calculating 1/a
  333.   ' VARIABLES:       t          Structure of type Complex
  334.   ' MODULE LEVEL
  335.   '   DECLARATIONS:  TYPE Complex
  336.   '                     r AS SINGLE
  337.   '                     i AS SINGLE
  338.   '                  END TYPE
  339.   '
  340.   '         DECLARE SUB ComplexReciprocal (a AS Complex, c AS Complex)
  341.   '         DECLARE SUB ComplexDiv (a AS Complex, b AS Complex, c AS Complex)
  342.   '
  343.     SUB ComplexReciprocal (a AS Complex, c AS Complex) STATIC
  344.         DIM t AS Complex
  345.         t.r = 1!
  346.         t.i = 0
  347.         ComplexDiv t, a, c
  348.     END SUB
  349.  
  350.   ' ************************************************
  351.   ' **  Name:          ComplexRoot                **
  352.   ' **  Type:          Subprogram                 **
  353.   ' **  Module:        COMPLEX.BAS                **
  354.   ' **  Language:      Microsoft QuickBASIC 4.00  **
  355.   ' ************************************************
  356.   '
  357.   ' Calculates the complex root of a complex number.
  358.   '
  359.   ' EXAMPLE OF USE:  ComplexRoot a, b, c
  360.   ' PARAMETERS:      a          First complex number
  361.   '                  b          Complex number root
  362.   '                  c          Result of finding the bth root of a
  363.   ' VARIABLES:       t          Structure of type Complex
  364.   ' MODULE LEVEL
  365.   '   DECLARATIONS:  TYPE Complex
  366.   '                     r AS SINGLE
  367.   '                     i AS SINGLE
  368.   '                  END TYPE
  369.   '
  370.   '       DECLARE SUB ComplexRoot (a AS Complex, b AS Complex, c AS Complex)
  371.   '       DECLARE SUB ComplexReciprocal (a AS Complex, c AS Complex)
  372.   '       DECLARE SUB ComplexPower (a AS Complex, b AS Complex, c AS Complex)
  373.   '
  374.     SUB ComplexRoot (a AS Complex, b AS Complex, c AS Complex) STATIC
  375.         DIM t AS Complex
  376.         IF b.r <> 0! OR b.i <> 0! THEN
  377.             CALL ComplexReciprocal(b, t)
  378.             CALL ComplexPower(a, t, c)
  379.         ELSE
  380.             ERROR 5
  381.         END IF
  382.     END SUB
  383.  
  384.   ' ************************************************
  385.   ' **  Name:          ComplexSqr                 **
  386.   ' **  Type:          Subprogram                 **
  387.   ' **  Module:        COMPLEX.BAS                **
  388.   ' **  Language:      Microsoft QuickBASIC 4.00  **
  389.   ' ************************************************
  390.   '
  391.   ' Calculates the square root of a complex number.
  392.   '
  393.   'EXAMPLE OF USE:   ComplexSqr a, c
  394.   'PARAMETERS:       a          Complex number argument
  395.   '                  c          Result of finding the square root of a
  396.   'VARIABLES:        r!         Magnitude of complex number a
  397.   '                  theta!     Angle of complex number a
  398.   '                  rs!        Square root of r!
  399.   '                  h!         One half of theta!
  400.   '
  401.   'MODULE LEVEL
  402.   ' DECLARATIONS:    TYPE Complex
  403.   '                     r AS SINGLE
  404.   '                     i AS SINGLE
  405.   '                  END TYPE
  406.   '
  407.   '                  DECLARE SUB ComplexSqr (a AS Complex, c AS Complex)
  408.   '
  409.     SUB ComplexSqr (a AS Complex, c AS Complex) STATIC
  410.         CALL Rec2pol(a.r, a.i, r!, theta!)
  411.         rs! = SQR(r!)
  412.         h! = theta! / 2!
  413.         c.r = rs! * COS(h!)
  414.         c.i = rs! * SIN(h!)
  415.     END SUB
  416.  
  417.   ' ************************************************
  418.   ' **  Name:          ComplexSub                 **
  419.   ' **  Type:          Subprogram                 **
  420.   ' **  Module:        COMPLEX.BAS                **
  421.   ' **  Language:      Microsoft QuickBASIC 4.00  **
  422.   ' ************************************************
  423.   '
  424.   ' Subtracts two complex numbers.
  425.   '
  426.   ' EXAMPLE OF USE:  ComplexSub a, b, c
  427.   ' PARAMETERS:      a          First complex number
  428.   '                  b          Second Complex number
  429.   '                  c          Result of subtracting b from a
  430.   ' VARIABLES:       (none)
  431.   ' MODULE LEVEL
  432.   '   DECLARATIONS:  TYPE Complex
  433.   '                     r AS SINGLE
  434.   '                     i AS SINGLE
  435.   '                  END TYPE
  436.   '
  437.   '         DECLARE SUB ComplexSub (a AS Complex, b AS Complex, c AS Complex)
  438.   '
  439.     SUB ComplexSub (a AS Complex, b AS Complex, c AS Complex) STATIC
  440.         c.r = a.r - b.r
  441.         c.i = a.i - b.i
  442.     END SUB
  443.  
  444.   ' ************************************************
  445.   ' **  Name:          String2Complex             **
  446.   ' **  Type:          Subprogram                 **
  447.   ' **  Module:        COMPLEX.BAS                **
  448.   ' **  Language:      Microsoft QuickBASIC 4.00  **
  449.   ' ************************************************
  450.   '
  451.   ' Converts a string representation of a complex
  452.   ' number to a type Complex variable.
  453.   '
  454.   ' EXAMPLE OF USE:  String2Complex x$, a
  455.   ' PARAMETERS:      x$         String representation of a complex number
  456.   '                  a          Complex number structure of type Complex
  457.   ' VARIABLES:       j%         Index to first numerical character
  458.   '                  i%         Pointer to the "i" or "j" character
  459.   '                  k%         Pointer to start of imaginary part
  460.   ' MODULE LEVEL
  461.   '   DECLARATIONS:  TYPE Complex
  462.   '                     r AS SINGLE
  463.   '                     i AS SINGLE
  464.   '                  END TYPE
  465.   '
  466.   '                  DECLARE SUB Complex2String (a AS Complex, x$)
  467.   '
  468.     SUB String2Complex (x$, a AS Complex) STATIC
  469.       
  470.       ' Real part starts just after left parenthesis
  471.         j% = INSTR(x$, "(") + 1
  472.       
  473.       ' Step forward to find start of number
  474.         DO UNTIL INSTR("+-0123456789", MID$(x$, j%, 1)) OR j% > LEN(x$)
  475.             j% = j% + 1
  476.         LOOP
  477.       
  478.       ' Imaginary part ends at the "i" or "j"
  479.         i% = INSTR(LCASE$(x$), "i")
  480.         IF INSTR(LCASE$(x$), "j") > i% THEN
  481.             i% = INSTR(LCASE$(x$), "j")
  482.         END IF
  483.       
  484.       ' Step back to find start of imaginary part
  485.         FOR k% = i% TO 1 STEP -1
  486.             IF INSTR("+-", MID$(x$, k%, 1)) THEN
  487.                 EXIT FOR
  488.             END IF
  489.         NEXT k%
  490.       
  491.       ' Error if pointers don't make sense
  492.         IF j% = 0 OR j% > LEN(x$) THEN
  493.             PRINT "Error: String2Complex - unrecognizable string format"
  494.             SYSTEM
  495.         END IF
  496.       
  497.       ' Grab the real part
  498.         a.r = VAL(MID$(x$, j%))
  499.       
  500.       ' Grab the imaginary part
  501.         IF k% > j% THEN
  502.             a.i = VAL(MID$(x$, k%))
  503.         ELSEIF k% = j% THEN
  504.             a.r = 0
  505.             a.i = VAL(MID$(x$, j%))
  506.         ELSE
  507.             a.i = 0
  508.         END IF
  509.       
  510.     END SUB
  511.  
  512.