Posted 22 July 2007 - 01:19 PM
yeah this is some useful, only 2 things:
- I think that you have to mutiplicate the imaginary part by (-1) in the division complex function.
- Why pwrC(c1,0.5) =~ sqrtC(c1)??
Ok if the autor of the program (Hobart) lets me I have modified the program:
I have corrected the first bug (*-1), I have added the arg, conj and comptoexp functions, I have joined all the functions in 2 functions:
Comp2(c1,'+',c2)
Comp2(c1,'-',c2)
Comp2(c1,'*',c2)
Comp2(c1,'/',c2)
Comp2(c1,'^',n)
Comp1(c1,'sqrt')
Comp1(c1,'abs')
Comp1(c1,'arg')
Comp1(c1,'conj')
Comp1(c1,'comptotri') {a,b} -> a+bi
Comp1(c1,'comptoexp') {a,b} -> abs({a,b})*e^(arg({a,b})*i)
this is the code
[codebox]
function Comp2(c1,op,c2)
if #c1~=2 or #c2~=2 then
return print('Invalid dimension')
end
if op=='+' then
return {c11+c21,c12+c22}
elseif op=='-' then
return {c11-c21,c12-c22}
elseif op=='*' then
return {c11*c21-c12*c22,c11*c22+c12*c21}
elseif op=='/' then
return {(c11*c21+c12*c22)/(c21^2+c22^2),-(c11*c22-c12*c21)/(c21^2+c22^2)}
elseif op=='^' then
local ctmp = {1,0}
for i = 1,c2 do
ctmp = Comp2(ctmp,'*',c1)
end
return ctmp
else
return print('Invalid operation')
end
end
function Comp1(c1,op)
if #c1~=2 then
return print('Invalid dimension')
end
if op=='sqrt' then
return {.5*(2*(c11+(c11^2+c12^2)^.5))^.5,.5*(-2*(c11-(c11^2+c12^2)^.5))^.5}
elseif op=='abs' then
return (c11^2+c12^2)^.5
elseif op=='arg' then
if c11==0 and c12==0 then return 0 end
local a
a=c12/c11
if c11>=0 then
return
math.atan(a)
elseif c12>=0 then
return
math.atan(a)+math.pi
else return
math.atan(a)-math.pi
end
elseif op=='conj' then
return
{c11,-c12}
elseif op=='comptotri' then
if c12==0 then
return printf('%.4f \n',c11)
else
if c12<0 then
return printf('%.4f%.4f \238\1 \n',c11,c12)
else
return printf('%.4f+%.4f \238\1 \n',c11,c12)
end
end
elseif op=='comptoexp' then
if c12==0 and c11>0 then return
printf('%.4f \n',tostring(Comp1(c1,'abs')))
else
return printf('%.4f\238\2^(%.4f\238\1) \n'
,tostring(Comp1(c1,'abs'))
,tostring(Comp1(c1,'arg')))
end
else
return print('Invalid operation')
end
end
export{Comp1=Comp1,Comp2=Comp2}
[/codebox]
EDIT: I have edited a bug in my code.