Complex conjugate: Difference between revisions

From Rosetta Code
Content added Content deleted
(Start with Factor and Ruby.)
 
(add Tcl)
Line 20: Line 20:
# Numeric#conj or Numeric#conjugate returns self.
# Numeric#conj or Numeric#conjugate returns self.
puts 67.conjugate # 67</lang>
puts 67.conjugate # 67</lang>

=={{header|Tcl}}==
{{tcllib|math::complexnumbers}}
<lang tcl>package require math::complexnumbers
namespace import math::complexnumbers::*
foreach x {{2 3} {4 -5} {67 0}} {
set c [complex {*}$x]
puts "[tostring $c] conjucate is [tostring [conj $c]]"
}</lang>
Produces the output
<pre>
2+3i conjucate is 2-3i
4-5i conjucate is 4+5i
67 conjucate is 67</pre>

Revision as of 17:12, 27 January 2012

Complex conjugate is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Given a complex number, find its complex conjugate. By definition, the complex conjugate of is (where and are real numbers, and is the square root of -1).

Some languages have complex number libraries available. If possible, use your library's operation for complex conjugate.

Factor

<lang factor>USING: math.functions prettyprint ;

C{ 2 3 } conjugate .  ! prints C{ 2 -3 } C{ 4 -5 } conjugate .  ! prints C{ 4 5 }</lang>

Ruby

<lang ruby>require 'complex' # With Ruby 1.9, this line is optional.

  1. Complex#conj or Complex#conjugate finds the conjugate.

i = Complex::I puts (2 + 3*i).conj # 2-3i puts (4 - 5*i).conjugate # 4+5i

  1. Numeric#conj or Numeric#conjugate returns self.

puts 67.conjugate # 67</lang>

Tcl

Library: Tcllib (Package: math::complexnumbers)

<lang tcl>package require math::complexnumbers namespace import math::complexnumbers::* foreach x {{2 3} {4 -5} {67 0}} {

   set c [complex {*}$x]
   puts "[tostring $c] conjucate is [tostring [conj $c]]"

}</lang> Produces the output

2+3i conjucate is 2-3i
4-5i conjucate is 4+5i
67 conjucate is 67