Pooh program example 01-complex.p

Test 01-complex.p

Source of programm

# test complex number library - complex.inc
include 'complex.inc'


n = make_rect_complex( ~re 0 ~im 1 )
m = make_rect_complex( ~re 0 ~im 1 )

res = n . mult( ~num m )

println( ~msg 'square of i is' )
res . print()


res = res . add( ~num res )

println( ~msg '
twice i is' )
res . print()


println( ~msg '
absolute is [ res . absolute () ]' )




Included file: complex.inc



# make a complex number in polar coordinates

sub make_polar_complex( rad, theta )
   return {
	    # complex number mode - polar coordinates
	    'mode' : 'polar',

	    # radius
	    'rad' : rad,

	    # angular value
	    'thet' : theta,

	    'add'   : sub( num )
			r = this.torect()
			res = r.add( ~num num )
			return res.topolar()
		      end,

	    'sub'   : sub( num )
		        r = this.torect()
		        res = r.add( ~num num )
			return res.topolar()
		      end,		       

	     'mult' : sub( num )
		        r = this.torect()
		        res = r.mult( ~num num )
			return res.topolar()
		      end,

	     'div'  : sub( num )
		        r = this.torect()
		        res = r.div( ~num num )
			return res.topolar()
		      end,		       

	       # returns the absolute number of this value (length of vector)
             'absolute' : sub() 
			    r = this.torect()
	  		    return r.absolute()
			  end,


	     # convert to rectangular coordinate
	     'torect' : sub()
			 make_rect_complex( ~re this.rad * cos( ~num this.theta ) ~im this.rad * sin( ~num this.theta ) )
			end,
	    			
	     'topolar' : sub()
			    return this
			end,

    # print the number
	    'print' : sub() 
			print( ~msg 'rectangular-polar: radius [ this.rad ] theta [ this.theta ]' )
		      end


	  }
end		  


# make a complex number in rectangular coordinates
sub make_rect_complex( re, im )

    return { 
	     # complex numer mode - rectangular/Cartesian coordinates
	     'mode' : 'rectangular',

	     # real value
	     're' : re,

	     #imaginairy value; return new object that is the result  
	     'im' : im,

	      # add this to argument num and return the result; return new object that is the result 
             'add' : sub( num )
			if num.mode ne 'rectangular'
			    num = num.torect()
			end
			return make_rect_complex( ~re this.re + num.re  ~im this.im + num.im )
		     end,

	      # multiply this by argument num and return the result; return new object that is the result 
	      'mult' : sub( num )
			if num.mode ne 'rectangular'
			    num = num.torect()
			end
		        return make_rect_complex( ~re this.re * num.re - this.im * num.im ~im this.im * num.re + this.re * num.im )
		     end,

	      # print the number
	      'print' : sub() 
			    print( ~msg 'rectangular-complex: real [ this.re ] imaginary [this.im]' )
		      end,

	      # divide this by the argument num; return new object that is the result
	      'div'   : sub( num )
			    if num.mode ne 'rectangular'
				num = num.torect()
			    end

		            sq = this.re * num.re + this.im * num.im 
			    re = ( this.re * num.re + this.im * num.im) / sq
			    im = ( num.im * this.re - this.re * num.im) / sq

			    return make_rect_complex( ~re re ~im im )
			end,

	       # returns the absolute number of this value (length of vector)
	       'absolute' : sub() 
				return sqr( ~num 3 ) #this.re * this re + this.im * this im )
			    end,

	       # convert to rectangular coordinates - return this object.
	       'torect' : sub()
			    return this
	  		  end,					

	       # convert to polar coordinates
	       'topolar' : sub()
			     rad = sqr( ~num this.re * this.re + this.im * this.im )
			     thet = atan2( ~x this.im ~y this.re )

			     return make_polar_complex( ~rad rad ~theta thet )
			   end
	   }			    
end

Standard output for 01-complex.p

square of i is
rectangular-complex: real -1 imaginary 0
twice i is
rectangular-complex: real -2 imaginary 0
absolute is 1.732051e+00

Trace output for 01-complex.p

004|... = make_rect_complex( ~re 0 ~im 1 )...
069| return { 'mode' : 'rectangular' , 're' : re:0 , 'im' : im:1 , 'add' : sub (~num) , 'mult' : sub (~num) , 'print' : sub () , 'div' : sub (~num) , 'absolute' : sub () , 'torect' : sub () , 'topolar' : sub () }
004|n = make_rect_complex( ~re 0 ~im 1 ):{ 'absolute' : sub , 'torect' : sub , 'topolar' : sub , 'mode' : -> 'rectangular', 'print' : sub , 'mult' : sub , 'div' : sub , 'im' : -> 1, 'add' : sub , 're' : -> 0 }
005|... = make_rect_complex( ~re 0 ~im 1 )...
069| return { 'mode' : 'rectangular' , 're' : re:0 , 'im' : im:1 , 'add' : sub (~num) , 'mult' : sub (~num) , 'print' : sub () , 'div' : sub (~num) , 'absolute' : sub () , 'torect' : sub () , 'topolar' : sub () }
005|m = make_rect_complex( ~re 0 ~im 1 ):{ 'absolute' : sub , 'torect' : sub , 'topolar' : sub , 'mode' : -> 'rectangular', 'print' : sub , 'mult' : sub , 'div' : sub , 'im' : -> 1, 'add' : sub , 're' : -> 0 }
007|... = n{'mult'}:sub ( ~num m:{ 'absolute' : sub (), 'torect' : sub (), 'topolar' : sub (), 'mode' : -> 'rectangular', 'print' : sub (), 'mult' : sub (~num), 'div' : sub (~num), 'im' : -> 1, 'add' : sub (~num), 're' : -> 0 } )...
089| if (num{'mode'}:'rectangular' ne 'rectangular'):false
090| end # if
092| return make_rect_complex( ~re ((this{'re'}:0 * num{'re'}:0):0 - (this{'im'}:1 * num{'im'}:1):1):-1 ~im ((this{'im'}:1 * num{'re'}:0):0 + (this{'re'}:0 * num{'im'}:1):0):0 )...
069|  return { 'mode' : 'rectangular' , 're' : re:-1 , 'im' : im:0 , 'add' : sub (~num) , 'mult' : sub (~num) , 'print' : sub () , 'div' : sub (~num) , 'absolute' : sub () , 'torect' : sub () , 'topolar' : sub () }
092| return make_rect_complex( ~re ((this{'re'}:0 * num{'re'}:0):0 - (this{'im'}:1 * num{'im'}:1):1):-1 ~im ((this{'im'}:1 * num{'re'}:0):0 + (this{'re'}:0 * num{'im'}:1):0):0 ):{ 'absolute' : sub , 'torect' : sub , 'topolar' : sub , 'mode' : -> 'rectangular', 'print' : sub , 'mult' : sub , 'div' : sub , 'im' : -> 0, 'add' : sub , 're' : -> -1 }
007|res = n{'mult'}:sub ( ~num m:{ 'absolute' : sub (), 'torect' : sub (), 'topolar' : sub (), 'mode' : -> 'rectangular', 'print' : sub (), 'mult' : sub (~num), 'div' : sub (~num), 'im' : -> 1, 'add' : sub (~num), 're' : -> 0 } ):{ 'absolute' : sub , 'torect' : sub , 'topolar' : sub , 'mode' : -> 'rectangular', 'print' : sub , 'mult' : sub , 'div' : sub , 'im' : -> 0, 'add' : sub , 're' : -> -1 }
009|println( ~msg 'square of i is' )...
010|res{'print'}:sub (  )...
097| print( ~msg 'rectangular-complex: real ' .. this{'re'}:-1 .. ' imaginary ' .. this{'im'}:0 .. '' )...

013|... = res{'add'}:sub ( ~num res:{ 'absolute' : sub (), 'torect' : sub (), 'topolar' : sub (), 'mode' : -> 'rectangular', 'print' : sub (), 'mult' : sub (~num), 'div' : sub (~num), 'im' : -> 0, 'add' : sub (~num), 're' : -> -1 } )...
081| if (num{'mode'}:'rectangular' ne 'rectangular'):false
082| end # if
084| return make_rect_complex( ~re (this{'re'}:-1 + num{'re'}:-1):-2 ~im (this{'im'}:0 + num{'im'}:0):0 )...
069|  return { 'mode' : 'rectangular' , 're' : re:-2 , 'im' : im:0 , 'add' : sub (~num) , 'mult' : sub (~num) , 'print' : sub () , 'div' : sub (~num) , 'absolute' : sub () , 'torect' : sub () , 'topolar' : sub () }
084| return make_rect_complex( ~re (this{'re'}:-1 + num{'re'}:-1):-2 ~im (this{'im'}:0 + num{'im'}:0):0 ):{ 'absolute' : sub , 'torect' : sub , 'topolar' : sub , 'mode' : -> 'rectangular', 'print' : sub , 'mult' : sub , 'div' : sub , 'im' : -> 0, 'add' : sub , 're' : -> -2 }
013|res = res{'add'}:sub ( ~num res:{ 'absolute' : sub (), 'torect' : sub (), 'topolar' : sub (), 'mode' : -> 'rectangular', 'print' : sub (), 'mult' : sub (~num), 'div' : sub (~num), 'im' : -> 0, 'add' : sub (~num), 're' : -> -1 } ):{ 'absolute' : sub , 'torect' : sub , 'topolar' : sub , 'mode' : -> 'rectangular', 'print' : sub , 'mult' : sub , 'div' : sub , 'im' : -> 0, 'add' : sub , 're' : -> -2 }
015|println( ~msg '
twice i is' )...
018|res{'print'}:sub (  )...
097| print( ~msg 'rectangular-complex: real ' .. this{'re'}:-2 .. ' imaginary ' .. this{'im'}:0 .. '' )...

021|println( ~msg '
absolute is ' .. res{'absolute'}:sub (  )...
115| return sqr( ~num 3 )...
115| return sqr( ~num 3 ):1.732051e+00
021|println( ~msg '
absolute is ' .. res{'absolute'}:sub (  ):1.732051e+00 .. '' )...