Complex Numbers
#1
Posted 28 October 2006 - 11:12 PM
m=input()
a=input()
t=m*cas.cos(a)+m*cas.sin(a)*i
--the "i" is the representacion of complex number
print(t)
how can I work on complex number and how can I setdegree or setradian??
Thanks.
#2
Posted 29 October 2006 - 04:02 AM
require("cas") m=input() a=input() --cas("SetComplex"): Orwell: add this! t=m*cas.cos(a)+m*cas.sin(a)*cas("i") print(t)
You must set mode to complex.
#3
Posted 29 October 2006 - 04:41 AM
Is there something like SetDegree or SetRadian on CpLua?
#4
Posted 29 October 2006 - 04:47 AM
#5
Posted 29 October 2006 - 08:33 PM
cplx={ _mt={ __index={ re=0, im=0, set=function(u,r,i) u.re,u.im=r,i end, __type="cplx", }, __add=function(u1,u2) a,b=u1,u2 if type(a)=="table" and type(b)=="table" then return cplx(a.re+b.re,a.im+b.im) else if type(u1)=="number" then return cplx(u2.re+u1,u2.im) elseif type(u2)=="number" then return cplx(u1.re+u2,u1.im) end end end, __sub=function(u1,u2) a,b=u1,u2 if type(a)=="table" and type(b)=="table" then return cplx(a.re-b.re,a.im-b.im) else if type(u1)=="number" then return cplx(u1-u2.re,u2.im) elseif type(u2)=="number" then return cplx(u1.re-u2,u1.im) end end end, __mul=function(u1,u2) a,b=u1,u2 if type(a)=="table" and type(b)=="table" then return cplx(-u1.im*u2.im+u1.re*u2.re,u1.im*u2.re+u1.re*u2.im) else if type(u1)=="number" then return cplx(u2.re*u1,u2.im*u1) elseif type(u2)=="number" then return cplx(u1.re*u2,u1.im*u2) end end end, __div=function(u1,u2) a,b=u1,u2 if type(a)=="table" and type(b)=="table" then return cplx((u1.im*u2.im)/(u2.im^2+u2.re^2)+(u1.re*u2.re)/(u2.im^2+u2.re^2),(u1.im*u2.re)/(u2.im^2+u2.re^2)-(u1.re*u2.im)/(u2.im^2+u2.re^2)) else if type(u1)=="number" then return cplx((u2.re*u1)/(u2.im^2+u2.re^2),(-u1*u2.im)/(u2.im^2+u2.re^2)) elseif type(u2)=="number" then return cplx(u1.re/u2,u1.im/u2) end end end, __pow=function(u1,u2) a,b=u1,u2 if type(a)=="table" and type(b)=="table" then return cplx(math.exp(u2.im*(math.atan(u1.re*(u1.im)^(-1))-((math.pi*math.sign(u1.im))/(2))))*((u1.im)^(2)+(u1.re)^(2))^(((u2.re)/(2)))*math.cos(((u2.im*math.log((u1.im)^(2)+(u1.re)^(2)))/(2))-u2.re*(math.atan(((u1.re)/(u1.im)))-((math.pi*math.sign(u1.im))/(2)))),math.exp(u2.im*(math.atan(u1.re*(u1.im)^(-1))-((math.pi*math.sign(u1.im))/(2))))*((u1.im)^(2)+(u1.re)^(2))^(((u2.re)/(2)))*math.sin(((u2.im*math.log((u1.im)^(2)+(u1.re)^(2)))/(2))-u2.re*(math.atan(((u1.re)/(u1.im)))-((math.pi*math.sign(u1.im))/(2))))) else if type(u1)=="number" then return cplx((u1)^(u2.re)*math.cos(u2.im*math.log(u1)),(u1)^(u2.re)*math.sin(u2.im*math.log(u1))) elseif type(u2)=="number" then return cplx(((u1.im)^(2)+(u1.re)^(2))^(((u2)/(2)))*math.cos(u2*(math.atan(((u1.re)/(u1.im)))-((math.pi*math.sign(u1.im))/(2)))),-((u1.im)^(2)+(u1.re)^(2))^(((u2)/(2)))*math.sin(u2*(math.atan(((u1.re)/(u1.im)))-((math.pi*math.sign(u1.im))/(2))))) end end end, __unm=function(u) return cplx(-u.re,-u.im) end, __tostring=function(u) local r,i="","" if math.abs(u.re)~=1 then r=u.re elseif u.re==-1 then r="-" end if math.abs(u.im)~=1 then i=u.im elseif u.im==-1 then i="-" end if u.re==0 then return i.."i" elseif u.im>0 then return r.."+"..i.."i" elseif u.im<0 then return r..i.."i" else return r end end, __concat=function(u1,u2) if type(u1)==type(u2) then return cplx._mt.__tostring(u1)..cplx._mt.__tostring(u2) elseif type(u1)=="table" then return cplx._mt.__tostring(u1)..u2 else return u1..cplx._mt.__tostring(u2) end end } } setmetatable(cplx,{ __call=function(u,r,i) o={} setmetatable(o,cplx._mt) o:set(r or 0,i or 0) return o end }) export{cplx=cplx}
It's very simple to use:
require("cplx") -- supposing you saved the lib in "cplx" m=input() a=input() t=cplx(math.cos(a),math.sin(a)) -- or t=cplx(math.cos(math.rad(a)),math.sin(math.rad(a))) if a is in degrees print(t) print("Real part:"..t.re) print("Imaginary part:"..t.im) --Operations on t print("t+8="..t+8) print("t+3i="..t+cplx(0,3)) print("t+6-2i="..t+cplx(6,-2)) print("t^2="..t^2)
#6
Posted 30 October 2006 - 03:35 AM
My question is why do you store something in the variable m
You write this:
require("cplx") -- supposing you saved the lib in "cplx"
m=input()
a=input()
t=cplx(math.cos(a),math.sin(a))
......
if the user put the i while he/she runs the program how can the program knows that the i is the representation of the complex numbers??????
On Basic i can write
SetComplex
Input x,"input complex number:"
print(x+(2+2i))
Basic already know if the user uses the i on the keyboard of the CP300
Thanks
#7
Posted 30 October 2006 - 08:56 AM
#8
Posted 30 October 2006 - 09:26 PM
require("cas")
x=cas(input("COMPLEX NUMBER 1:"))
y=cas(input("COMPLEX NUMBER 2:"))
m=cas.compToPol(x+y)
print("x:".. cas.approx(x) .. "y:" .. cas.approx(y) .. "\n\nTotal:\n" .. (m))
My question about this code is: why CpLua show compToPol as string and then show the number as polar.?
I need just the number as polar not compToPol as string.?
#9
Posted 31 October 2006 - 01:02 AM
x=cas(input("COMPLEX NUMBER 1:"))
y=cas(input("COMPLEX NUMBER 2:"))
m=cas.compToPol(x+y)
u=m()
print("x:".. cas.approx(x) .. "y:" .. cas.approx(y) .. "\n\nTotal:\n" .. (u))
#10
Posted 31 October 2006 - 09:21 PM
I writed u=m(), but CpLua show me
"Runtime Error:
main:7: attempt to concatenate global 'u' (a nil value)"
#11
Posted 01 November 2006 - 12:02 PM
#12
Posted 03 November 2006 - 03:14 AM
a,b=cas.abs(x+y),cas.arg(x+y)
can solve that problem.
#13
Posted 03 November 2006 - 09:19 PM
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users