Jump to content



Photo

Complex Numbers


  • Please log in to reply
12 replies to this topic

#1 Colombia

Colombia

    Casio Addict

  • Members
  • PipPipPip
  • 94 posts
  • Gender:Male
  • Location:Venezuela

  • Calculators:
    algebra FX 2.0 plus
    Classpad 300

Posted 29 October 2006 - 12:12 AM

require("cas")
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 vanhoa

vanhoa

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 854 posts
  • Gender:Male
  • Location:Vietnam

  • Calculators:
    AFX 2.0, CP 300, CP 330, nSpire, TI 89, FX 5800

Posted 29 October 2006 - 05: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 Colombia

Colombia

    Casio Addict

  • Members
  • PipPipPip
  • 94 posts
  • Gender:Male
  • Location:Venezuela

  • Calculators:
    algebra FX 2.0 plus
    Classpad 300

Posted 29 October 2006 - 05:41 AM

CpLua have the function re( or im(, which means real part of the complex and the no-real part of the complex?


Is there something like SetDegree or SetRadian on CpLua?

#4 vanhoa

vanhoa

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 854 posts
  • Gender:Male
  • Location:Vietnam

  • Calculators:
    AFX 2.0, CP 300, CP 330, nSpire, TI 89, FX 5800

Posted 29 October 2006 - 05:47 AM

The functions math.deg and math.rad convert between radians and degrees. print(math.deg(4*math.atan(1))) return 180 for example.

#5 Kilburn

Kilburn

    Casio Technician

  • Members
  • PipPipPipPipPipPip
  • 491 posts
  • Gender:Male
  • Location:France
  • Interests:Blah

  • Calculators:
    FX-7500 G
    ClassPad 300

Posted 29 October 2006 - 09:33 PM

I made a simple lib that handles complex numbers and allows to make simple operations on them (+ - * / ^) :

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 Colombia

Colombia

    Casio Addict

  • Members
  • PipPipPip
  • 94 posts
  • Gender:Male
  • Location:Venezuela

  • Calculators:
    algebra FX 2.0 plus
    Classpad 300

Posted 30 October 2006 - 04:35 AM

Killburn I tried your code but it didn?t run well.

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 Kilburn

Kilburn

    Casio Technician

  • Members
  • PipPipPipPipPipPip
  • 491 posts
  • Gender:Male
  • Location:France
  • Interests:Blah

  • Calculators:
    FX-7500 G
    ClassPad 300

Posted 30 October 2006 - 09:56 AM

Ah, yes I don't know why I put "input(m)", it's totally useless. :blush: And you can't actually input a complex number using input(), I'm supposing you are entering a real number for a...

#8 Colombia

Colombia

    Casio Addict

  • Members
  • PipPipPip
  • 94 posts
  • Gender:Male
  • Location:Venezuela

  • Calculators:
    algebra FX 2.0 plus
    Classpad 300

Posted 30 October 2006 - 10:26 PM

I have CpLua 0.9 and I wrote this

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 vanhoa

vanhoa

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 854 posts
  • Gender:Male
  • Location:Vietnam

  • Calculators:
    AFX 2.0, CP 300, CP 330, nSpire, TI 89, FX 5800

Posted 31 October 2006 - 02:02 AM

require("cas")
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 Colombia

Colombia

    Casio Addict

  • Members
  • PipPipPip
  • 94 posts
  • Gender:Male
  • Location:Venezuela

  • Calculators:
    algebra FX 2.0 plus
    Classpad 300

Posted 31 October 2006 - 10:21 PM

vanhoa

I writed u=m(), but CpLua show me

"Runtime Error:
main:7: attempt to concatenate global 'u' (a nil value)"

#11 vanhoa

vanhoa

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 854 posts
  • Gender:Male
  • Location:Vietnam

  • Calculators:
    AFX 2.0, CP 300, CP 330, nSpire, TI 89, FX 5800

Posted 01 November 2006 - 01:02 PM

What??? Nothing with me...

#12 vanhoa

vanhoa

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 854 posts
  • Gender:Male
  • Location:Vietnam

  • Calculators:
    AFX 2.0, CP 300, CP 330, nSpire, TI 89, FX 5800

Posted 03 November 2006 - 04:14 AM

Sorry, Im wrong... In place of compToPol(x+y),

a,b=cas.abs(x+y),cas.arg(x+y)

can solve that problem.

#13 Colombia

Colombia

    Casio Addict

  • Members
  • PipPipPip
  • 94 posts
  • Gender:Male
  • Location:Venezuela

  • Calculators:
    algebra FX 2.0 plus
    Classpad 300

Posted 03 November 2006 - 10:19 PM

Gracias, this time it works




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users