41 lines
1.2 KiB
Plaintext
41 lines
1.2 KiB
Plaintext
* # Define complex datatype
|
|
data('complex(r,i)')
|
|
|
|
* # Addition
|
|
define('addx(x1,x2)a,b,c,d') :(addx_end)
|
|
addx a = r(x1); b = i(x1); c = r(x2); d = i(x2)
|
|
addx = complex(a + c, b + d) :(return)
|
|
addx_end
|
|
|
|
* # Multiplication
|
|
define('multx(x1,x2)a,b,c,d') :(multx_end)
|
|
multx a = r(x1); b = i(x1); c = r(x2); d = i(x2)
|
|
multx = complex(a * c - b * d, b * c + a * d) :(return)
|
|
multx_end
|
|
|
|
* # Negation
|
|
define('negx(x)') :(negx_end)
|
|
negx negx = complex(-r(x), -i(x)) :(return)
|
|
negx_end
|
|
|
|
* # Inverse
|
|
define('invx(x)d') :(invx_end)
|
|
invx d = (r(x) * r(x)) + (i(x) * i(x))
|
|
invx = complex(1.0 * r(x) / d, 1.0 * -i(x) / d) :(return)
|
|
invx_end
|
|
|
|
* # Print compex number: a+bi / a-bi
|
|
define('printx(x)sign') :(printx_end)
|
|
printx sign = ge(i(x),0) '+'
|
|
printx = r(x) sign i(x) 'i' :(return)
|
|
printx_end
|
|
|
|
* # Test and display
|
|
a = complex(1,1)
|
|
b = complex(3.14159, 1.2)
|
|
output = printx( addx(a,b) )
|
|
output = printx( multx(a,b) )
|
|
output = printx( negx(a) ) ', ' printx( negx(b) )
|
|
output = printx( invx(a) ) ', ' printx( invx(b) )
|
|
end
|