This is a class fot complex numbers. Here is the export. We get power of complex, with various combinations. I use Atan2 from internal math library (atach on Math object). To prevent π to write as π (greek letter) and not p (windows subtitute π to p) we use Wide Output (UTF16LE).
Output:
(1+i)^5=(-4-4i)
(2+2i)^6=(-512i)
(1+i)^18=(512i)
(2+i)^(3-i)=(14.8073788312+9.8335865958i)
(221-4i)^(12+3i)=(-1.38398760508149E+28-3.82799320957046E+27i)
e^(πi)+1 = (0)
module check (filename as string=""){
Class Complex {
private:
double vr, vi
public:
function Arg {
declare m math
Method m, "Atan2", .vi, .vr as ret
=ret
}
final pidivby180=1.74532925199433E-02
final m_e=2.71828182845905
final maxval=1.7976931348623157E+308
property real {
value {link parent vr to vr : value=vr}
}
property imaginary {
value {link parent vi to vi : value=vi}
}
property toString {
value { clear 'so a new clear as string created after
link parent vr, vi to vr, vi
if vi then
if vr then
if vi>0 then
if vi==1 then value="("+vr+"+i)" else value="("+vr+"+"+vi+"i)"
else
if vi==-1 then value="("+vr+"-i)" else value="("+vr+""+vi+"i)"
end if
else
if vi=1 then value="(i)" else.if vi=-1 then value="(-i)" else value="("+vi+"i)"
end if
else
value="("+vr+")"
end if
}
}
function exp(rr=10) {
double exp = .m_e^.vr
c=this
th=.vi/.pidivby180
c.vr<=round(exp * cos(th), rr)
c.vi<=round(exp * sin(th),rr)
=c
}
function cabs() { ' ret type double
if abs(.vr)=infinity or abs(.vi)=infinity then =.maxval:Exit
double c=abs(.vr), d=abs(.vi)
if c>d then
r=d/c
=c*sqrt(1+r*r)
else.if d==0 then
=c
else
r=c/d
=d*sqrt(1+r*r)
end if
}
function clog {
c=this
c.vr<=ln(.cabs())
c.vi<=.Arg()
=c
}
function pow() {
if match("G") then
read p as Complex
else
read p1 as double
p=this:p.vr=p1:p.vi=0
end if
Read ? rr=10
exp=.clog()*p
c=exp.exp(rr)
c.vr=round(c.vr, rr)
c.vi=round(c.vi, rr)
=c
}
function conj {
c=this : c.vi-! : =c
}
function absc {
c=this*.conj() : =sqrt(c.vr)
}
operator "+" {
read k as Complex : .vr+= k.vr : .vi+= k.vi
}
operator "-" {
read k as Complex : .vr-= k.vr : .vi-= k.vi
}
operator "*" {
read k as Complex
double ivr = .vr*k.vr-.vi*k.vi
.vi <= .vi*k.vr+.vr*k.vi
.vr <= ivr
}
operator "/" {
read k as Complex
k1=k*k.conj()
acb = this*k.conj()
.vr <= acb.vr/k1.vr
.vi <= acb.vi/k1.vr
}
class:
module Complex (.vr, .vi) {}
}
open filename for wide output as #f
r=Complex(1,1)
rr=r.pow(5)
Print #f, r.toString+"^5="+rr.toString
r=Complex(2,2)
rr=r.pow(6)
Print #f, r.toString+"^6="+ rr.toString
r=Complex(1, 1)
rr=r.pow(18)
Print #f, r.toString+"^18="+rr.toString
z=complex(2,1)
b=complex(3, -1)
r=z.pow(b)
Print #f, z.toString+"^"+b.toString+"="+r.toString
z=complex(221, -4)
b=complex(12, 3)
r=z.pow(b)
Print #f, z.toString+"^"+b.tostring+"="+r.toString
r=Complex(2.71828182845905)
b=Complex(0, pi)
z=r.pow(b)+complex(1)
Print #f, "e^(πi)+1 = "+z.toString
close #f
}
check "out.txt"
win "notepad", dir$+"out.txt"
Δεν υπάρχουν σχόλια:
Δημοσίευση σχολίου
You can feel free to write any suggestion, or idea on the subject.