Arithmetic/Complex: Difference between revisions
Content added Content deleted
(Added solution for Action!) |
|||
Line 60: | Line 60: | ||
<lang Action!>INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit |
<lang Action!>INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit |
||
DEFINE R_="+0" |
|||
TYPE Complex=[CARD r,i] ;REAL POINTER |
|||
DEFINE I_="+6" |
|||
TYPE Complex=[CARD cr1,cr2,cr3,ci1,ci2,ci3] |
|||
BYTE FUNC Positive(REAL POINTER x) |
BYTE FUNC Positive(REAL POINTER x) |
||
Line 72: | Line 74: | ||
PROC PrintComplex(Complex POINTER x) |
PROC PrintComplex(Complex POINTER x) |
||
PrintR(x |
PrintR(x R_) |
||
IF Positive(x |
IF Positive(x I_) THEN |
||
Put('+) |
Put('+) |
||
FI |
FI |
||
PrintR(x |
PrintR(x I_) Put('i) |
||
RETURN |
RETURN |
||
Line 95: | Line 97: | ||
PROC ComplexAdd(Complex POINTER x,y,res) |
PROC ComplexAdd(Complex POINTER x,y,res) |
||
RealAdd(x |
RealAdd(x R_,y R_,res R_) ;res.r=x.r+y.r |
||
RealAdd(x |
RealAdd(x I_,y I_,res I_) ;res.i=x.i+y.i |
||
RETURN |
RETURN |
||
PROC ComplexSub(Complex POINTER x,y,res) |
PROC ComplexSub(Complex POINTER x,y,res) |
||
RealSub(x |
RealSub(x R_,y R_,res R_) ;res.r=x.r-y.r |
||
RealSub(x |
RealSub(x I_,y I_,res I_) ;res.i=x.i-y.i |
||
RETURN |
RETURN |
||
Line 107: | Line 109: | ||
REAL tmp1,tmp2 |
REAL tmp1,tmp2 |
||
RealMult(x |
RealMult(x R_,y R_,tmp1) ;tmp1=x.r*y.r |
||
RealMult(x |
RealMult(x I_,y I_,tmp2) ;tmp2=x.i*y.i |
||
RealSub(tmp1,tmp2,res |
RealSub(tmp1,tmp2,res R_) ;res.r=x.r*y.r-x.i*y.i |
||
RealMult(x |
RealMult(x R_,y I_,tmp1) ;tmp1=x.r*y.i |
||
RealMult(x |
RealMult(x I_,y R_,tmp2) ;tmp2=x.i*y.r |
||
RealAdd(tmp1,tmp2,res |
RealAdd(tmp1,tmp2,res I_) ;res.i=x.r*y.i+x.i*y.r |
||
RETURN |
RETURN |
||
Line 119: | Line 121: | ||
REAL tmp1,tmp2,tmp3,tmp4 |
REAL tmp1,tmp2,tmp3,tmp4 |
||
RealMult(x |
RealMult(x R_,y R_,tmp1) ;tmp1=x.r*y.r |
||
RealMult(x |
RealMult(x I_,y I_,tmp2) ;tmp2=x.i*y.i |
||
RealAdd(tmp1,tmp2,tmp3) ;tmp3=x.r*y.r+x.i*y.i |
RealAdd(tmp1,tmp2,tmp3) ;tmp3=x.r*y.r+x.i*y.i |
||
RealMult(y |
RealMult(y R_,y R_,tmp1) ;tmp1=y.r^2 |
||
RealMult(y |
RealMult(y I_,y I_,tmp2) ;tmp2=y.i^2 |
||
RealAdd(tmp1,tmp2,tmp4) ;tmp4=y.r^2+y.i^2 |
RealAdd(tmp1,tmp2,tmp4) ;tmp4=y.r^2+y.i^2 |
||
RealDiv(tmp3,tmp4,res |
RealDiv(tmp3,tmp4,res R_) ;res.r=(x.r*y.r+x.i*y.i)/(y.r^2+y.i^2) |
||
RealMult(x |
RealMult(x I_,y R_,tmp1) ;tmp1=x.i*y.r |
||
RealMult(x |
RealMult(x R_,y I_,tmp2) ;tmp2=x.r*y.i |
||
RealSub(tmp1,tmp2,tmp3) ;tmp3=x.i*y.r-x.r*y.i |
RealSub(tmp1,tmp2,tmp3) ;tmp3=x.i*y.r-x.r*y.i |
||
RealDiv(tmp3,tmp4,res |
RealDiv(tmp3,tmp4,res I_) ;res.i=(x.i*y.r-x.r*y.i)/(y.r^2+y.i^2) |
||
RETURN |
RETURN |
||
Line 136: | Line 138: | ||
REAL neg |
REAL neg |
||
ValR("-1",neg) ;neg=-1 |
ValR("-1",neg) ;neg=-1 |
||
RealMult(x |
RealMult(x R_,neg,res R_) ;res.r=-x.r |
||
RealMult(x |
RealMult(x I_,neg,res I_) ;res.r=-x.r |
||
RETURN |
RETURN |
||
Line 144: | Line 146: | ||
REAL tmp1,tmp2,tmp3 |
REAL tmp1,tmp2,tmp3 |
||
RealMult(x |
RealMult(x R_,x R_,tmp1) ;tmp1=x.r^2 |
||
RealMult(x |
RealMult(x I_,x I_,tmp2) ;tmp2=x.i^2 |
||
RealAdd(tmp1,tmp2,tmp3) ;tmp3=x.r^2+x.i^2 |
RealAdd(tmp1,tmp2,tmp3) ;tmp3=x.r^2+x.i^2 |
||
RealDiv(x |
RealDiv(x R_,tmp3,res R_) ;res.r=x.r/(x.r^2+x.i^2) |
||
ValR("-1",tmp1) ;tmp1=-1 |
ValR("-1",tmp1) ;tmp1=-1 |
||
RealMult(x |
RealMult(x I_,tmp1,tmp2) ;tmp2=-x.i |
||
RealDiv(tmp2,tmp3,res |
RealDiv(tmp2,tmp3,res I_) ;res.i=-x.i/(x.r^2+x.i^2) |
||
RETURN |
RETURN |
||
Line 157: | Line 159: | ||
REAL neg |
REAL neg |
||
ValR("-1",neg) ;neg=-1 |
ValR("-1",neg) ;neg=-1 |
||
RealAssign(x |
RealAssign(x R_,res R_) ;res.r=x.r |
||
RealMult(x |
RealMult(x I_,neg,res I_) ;res.i=-x.i |
||
RETURN |
RETURN |
||
PROC Main() |
PROC Main() |
||
Complex x,y,res |
Complex x,y,res |
||
REAL xr,xi,yr,yi,resr,resi |
|||
⚫ | |||
x.r=xr x.i=xi |
|||
⚫ | |||
y.r=yr y.i=yi |
|||
res.r=resr res.i=resi |
|||
⚫ | |||
⚫ | |||
Put(125) PutE() ;clear screen |
Put(125) PutE() ;clear screen |