Arithmetic/Complex: Difference between revisions

Content added Content deleted
Line 2,793:
=={{header|Oforth}}==
 
<lang Oforth>100 Number 100 Class newPriority: Complex(re, im)
Complex method: re { @re };
Complex method: im { @im };
Complex method: initialize { := reim := imre };
Complex method: << { '(' <<c @re << ',' <<c @im << ')' <<c } ;
Integer method: asComplex { Complex new(self, 0) }Complex new ;
Float method: asComplex { Complex new(self, 0) }Complex new ;
 
Complex new(0, 1) Constant new: I
 
Complex method: ==(c) { c re @re == c im @im == and };
Complex method: norm { @re sq @im sq + sqrt };
Complex method: conj { Complex new(@re, @im neg) };
Complex method: +(c) { Complex new(c re @re +, c im @im +) };
Complex method: -(c) { Complex new(c re @re -, c im @im -) };
Complex method: *(c) { Complex new(c re @re * c im @im * -, c re @im * @re c im * + ) };
Complex method: inv
{
| n |
@re sq @im sq + asFloat ->n
Complex new(@re n /, @im neg n / ) ;
 
}
Complex method: /(c) { c self inv * };</lang>
 
Usage :
 
<lang Oforth>2 3.2 I * + println.cr
Complex new(2, 3) 1.2 + println.cr
Complex new(2, 3) 1.2 * println.cr
2 Complex new(2, 3) / println.cr</lang>
 
{{out}}