Arithmetic/Complex: Difference between revisions
Content added Content deleted
(→{{header|Smalltalk}}: me stupid) |
(→{{header|Phix}}: use builtin) |
||
Line 3,780: | Line 3,780: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<lang Phix> |
<lang Phix>-- demo\rosetta\ArithComplex.exw |
||
include complex.e |
|||
IMAG = 2 |
|||
complex a = complex_new(1,1), |
|||
b = complex_new(3.14159,1.25), |
|||
type complex(sequence s) |
|||
⚫ | |||
return length(s)=2 and atom(s[REAL]) and atom(s[IMAG]) |
|||
d = complex_new(0,1) |
|||
end type |
|||
⚫ | |||
⚫ | |||
function add(complex a, complex b) |
|||
⚫ | |||
return sq_add(a,b) |
|||
⚫ | |||
end function |
|||
⚫ | |||
⚫ | |||
function mult(complex a, complex b) |
|||
printf(1,"1/a = %s\n",{complex_sprint(complex_inv(a))}) |
|||
return {a[REAL] * b[REAL] - a[IMAG] * b[IMAG], |
|||
printf(1,"c/a = %s\n",{complex_sprint(complex_div(c,a))}) |
|||
a[REAL] * b[IMAG] + a[IMAG] * b[REAL]} |
|||
printf(1,"c-a = %s\n",{complex_sprint(complex_sub(c,a))}) |
|||
end function |
|||
printf(1,"d-a = %s\n",{complex_sprint(complex_sub(d,a))}) |
|||
printf(1,"-a = %s\n",{complex_sprint(complex_neg(a))}) |
|||
function inv(complex a) |
|||
printf(1,"conj a = %s\n",{complex_sprint(complex_conjugate(a))})</lang> |
|||
atom denom |
|||
denom = a[REAL] * a[REAL] + a[IMAG] * a[IMAG] |
|||
return {a[REAL] / denom, -a[IMAG] / denom} |
|||
end function |
|||
function neg(complex a) |
|||
return sq_uminus(a) |
|||
end function |
|||
function scomplex(complex a) |
|||
sequence s = "" |
|||
atom ar, ai |
|||
{ar, ai} = a |
|||
if ar!=0 then |
|||
⚫ | |||
end if |
|||
if ai!=0 then |
|||
if ai=1 then |
|||
s &= "+i" |
|||
elsif ai=-1 then |
|||
s &= "-i" |
|||
else |
|||
s &= sprintf("%+gi",ai) |
|||
end if |
|||
end if |
|||
if length(s)=0 then |
|||
return "0" |
|||
end if |
|||
return s |
|||
end function |
|||
complex a, b |
|||
a = { 1.0, 1.0 } |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
a = 1+i |
a = 1+i |
||
b = 3.14159+1. |
b = 3.14159+1.25i |
||
c = 1 |
|||
a+b = 4.14159+2.2i |
|||
d = i |
|||
⚫ | |||
⚫ | |||
⚫ | |||
1/a = 0.5-0.5i |
1/a = 0.5-0.5i |
||
c/a = 0.5-0.5i |
|||
c-a = -i |
|||
d-a = -1 |
|||
-a = -1-i |
-a = -1-i |
||
conj a = 1-i |
|||
</pre> |
</pre> |
||