Announcement

Collapse
1 of 2 < >

New Sub-Forum

In an effort to help make sure there are appropriate categories for topics of discussion that are happening, there is now a sub-forum for databases and database programming under Special Interest groups. Please direct questions, etc., about this topic to that sub-forum moving forward. Thank you.
2 of 2 < >

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Simple math with complex numbers

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Simple math with complex numbers

    according to thread:
    http://www.powerbasic.com/support/pb...t=34906&page=2

    Code:
    #COMPILE EXE
    #DIM ALL
    '
    TYPE ComplexType
       r AS SINGLE
       i AS SINGLE
    END TYPE
    '
    UNION uComplex
       Complex AS STRING*8
       ComplexType
    END UNION
    '
    FUNCTION  Complex(A AS uComplex, Operator AS STRING ,OPT  B AS uComplex, OPT BYVAL  x AS SINGLE) AS STRING
      LOCAL c AS uComplex
      LOCAL real, im, den, w, r AS SINGLE
      '
      SELECT CASE UCASE$(Operator)
          CASE "+"
              c.r = a.r + b.r
              c.i = a.i + b.i
          CASE "-"
              c.r = a.r - b.r
              c.i = a.i - b.i
          CASE "*"
              c.r = a.r * b.r - a.i * b.i
              c.i = a.i * b.r + a.r * b.i
          CASE "/"
              IF (ABS(b.r) >= ABS(b.i)) THEN
                  r = b.i / b.r
                  den = b.r + r*b.i
                  c.r = (a.r + r*a.i)/den
                  c.i = (a.i - r*a.r)/den
              ELSE
                  r = b.r / b.i
                  den = b.i + r*b.r
                  c.r = (a.r*r + a.i)/den
                  c.i = (a.i*r - a.r)/den
              END IF
          CASE "CCONJ"
              c.r =  a.r
              c.i = -a.i
          CASE "SQR"
              IF ((A.r = 0.0!) AND (A.i = 0.0!)) THEN
                  c.r = 0.0!
                  c.i = 0.0!
              ELSE
                  real = ABS(A.r)
                  im = ABS(A.i)
                  IF (real >= im) THEN
                      r = im/real
                      w = SQR(real) * SQR(0.5! * (1.0! + SQR(1.0! + r*r)))
                  ELSE
                      r = real/im
                      w = SQR(im) * SQR(0.5! * (r + SQR(1.0! + r*r)))
                  END IF
                  IF (A.r >= 0.0!) THEN
                      c.r = w
                      c.i = A.i / (2.0! * w)
                  ELSE
                      c.i = IIF(A.i >= 0,  w,  -w )
                      c.r = A.i / (2.0! * c.i)
                  END IF
              END IF
          CASE "RCMUL"
              c.r = x * a.r
              c.i = x * a.i
      END SELECT
      '
      FUNCTION = c.Complex
    END FUNCTION
    '
    FUNCTION MakeComplex(real AS SINGLE, im AS SINGLE) AS STRING
        LOCAL c AS uComplex
        c.r = real
        c.i = im
        FUNCTION = c.Complex
    END FUNCTION
    '
    FUNCTION Cabs(BYVAL z AS uComplex) AS SINGLE
      LOCAL real, im, result, temp AS SINGLE
        '
        real = ABS(z.r)
        im   = ABS(z.i)
        '
        IF (real = 0.0!) THEN
            result = im
        ELSEIF (im = 0.0!) THEN
            result = real
        ELSEIF (real > im) THEN
            temp = im/real
            result = real * SQR(1.0! + temp*temp)
        ELSE
            temp = real/im
            result = im * SQR(1.0! + temp*temp)
        END IF
        FUNCTION = result
    END FUNCTION
    '
    '=====================  using  =======================
    FUNCTION PBMAIN () AS LONG
      LOCAL a , b, c AS uComplex
      LOCAL ComplexABS AS SINGLE
      LOCAL s AS STRING
      '
      a.Complex = MakeComplex(1,2) ' make complex number. The same as: a.r=1 : a.i=2
      b.Complex = MakeComplex(3,4)
      '
      c.Complex = Complex(a,"+",b)   : s = "(1+2i) + (3+4i) = "+FORMAT$(c.r,"#,.0##")+"+"+FORMAT$(c.i,"#,.0##")+"i"+$CRLF
      c.Complex = Complex(a,"-",b)   : s=s+"(1+2i) - (3+4i) = "+FORMAT$(c.r,"#,.0##")+"+"+FORMAT$(c.i,"#,.0##")+"i"+$CRLF
      c.Complex = Complex(a,"*",b)   : s=s+"(1+2i) * (3+4i) = "+FORMAT$(c.r,"#,.0##")+"+"+FORMAT$(c.i,"#,.0##")+"i"+$CRLF
      c.Complex = Complex(a,"/",b)   : s=s+"(1+2i) / (3+4i) = "+FORMAT$(c.r,"#,.0##")+"+"+FORMAT$(c.i,"#,.0##")+"i"+$CRLF
      c.Complex = Complex(a,"Cconj") : s=s+"Conj(1+2i) = "+FORMAT$(c.r,"#,.0##")+"+"+FORMAT$(c.i,"#,.0##")+"i"+$CRLF
      c.Complex = Complex(a,"SQR")   : s=s+"SQR(1+2i) = "+FORMAT$(c.r,"#,.0##")+"+"+FORMAT$(c.i,"#,.0##")+"i"+$CRLF
      c.Complex = Complex(a,"RCmul",BYVAL 0,5) :s=s+"(1+2i) * 5 = "+FORMAT$(c.r,"#,.0##")+"+"+FORMAT$(c.i,"#,.0##")+"i"+$CRLF
      ComplexABS= Cabs(c.Complex)    : s=s+"Absolut value of (1+2i) = "+STR$(ComplexABS)
      '
      ? s,,"Complex number math:"
    END FUNCTION
    -=Alex=-
Working...
X