/usr/share/tcltk/tcllib1.14/math/qcomplex.tcl is in tcllib 1.14-dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | # qcomplex.tcl --
# Small module for dealing with complex numbers
# The design goal was to make the operations as fast
# as possible, not to offer a nice interface. So:
# - complex numbers are represented as lists of two elements
# - there is hardly any error checking, all arguments are assumed
# to be complex numbers already (with a few obvious exceptions)
# Missing:
# the inverse trigonometric functions and the hyperbolic functions
#
namespace eval ::math::complexnumbers {
namespace export + - / * conj exp sin cos tan real imag mod arg log pow sqrt tostring
}
# complex --
# Create a new complex number
# Arguments:
# real The real part
# imag The imaginary part
# Result:
# New complex number
#
proc ::math::complexnumbers::complex {real imag} {
return [list $real $imag]
}
# binary operations --
# Implement the basic binary operations
# Arguments:
# z1 First argument
# z2 Second argument
# Result:
# New complex number
#
proc ::math::complexnumbers::+ {z1 z2} {
set result {}
foreach c $z1 d $z2 {
lappend result [expr {$c+$d}]
}
return $result
}
proc ::math::complexnumbers::- {z1 {z2 {}}} {
if { $z2 == {} } {
set z2 $z1
set z1 {0.0 0.0}
}
set result {}
foreach c $z1 d $z2 {
lappend result [expr {$c-$d}]
}
return $result
}
proc ::math::complexnumbers::* {z1 z2} {
set result {}
foreach {c1 d1} $z1 {break}
foreach {c2 d2} $z2 {break}
return [list [expr {$c1*$c2-$d1*$d2}] [expr {$c1*$d2+$c2*$d1}]]
}
proc ::math::complexnumbers::/ {z1 z2} {
set result {}
foreach {c1 d1} $z1 {break}
foreach {c2 d2} $z2 {break}
set denom [expr {$c2*$c2+$d2*$d2}]
return [list [expr {($c1*$c2+$d1*$d2)/$denom}] \
[expr {(-$c1*$d2+$c2*$d1)/$denom}]]
}
# unary operations --
# Implement the basic unary operations
# Arguments:
# z1 Argument
# Result:
# New complex number
#
proc ::math::complexnumbers::conj {z1} {
foreach {c d} $z1 {break}
return [list $c [expr {-$d}]]
}
proc ::math::complexnumbers::real {z1} {
foreach {c d} $z1 {break}
return $c
}
proc ::math::complexnumbers::imag {z1} {
foreach {c d} $z1 {break}
return $d
}
proc ::math::complexnumbers::mod {z1} {
foreach {c d} $z1 {break}
return [expr {hypot($c,$d)}]
}
proc ::math::complexnumbers::arg {z1} {
foreach {c d} $z1 {break}
if { $c != 0.0 || $d != 0.0 } {
return [expr {atan2($d,$c)}]
} else {
return 0.0
}
}
# elementary functions --
# Implement the elementary functions
# Arguments:
# z1 Argument
# z2 Second argument (if any)
# Result:
# New complex number
#
proc ::math::complexnumbers::exp {z1} {
foreach {c d} $z1 {break}
return [list [expr {exp($c)*cos($d)}] [expr {exp($c)*sin($d)}]]
}
proc ::math::complexnumbers::cos {z1} {
foreach {c d} $z1 {break}
return [list [expr {cos($c)*cosh($d)}] [expr {-sin($c)*sinh($d)}]]
}
proc ::math::complexnumbers::sin {z1} {
foreach {c d} $z1 {break}
return [list [expr {sin($c)*cosh($d)}] [expr {cos($c)*sinh($d)}]]
}
proc ::math::complexnumbers::tan {z1} {
return [/ [sin $z1] [cos $z1]]
}
proc ::math::complexnumbers::log {z1} {
return [list [expr {log([mod $z1])}] [arg $z1]]
}
proc ::math::complexnumbers::sqrt {z1} {
set argz [expr {0.5*[arg $z1]}]
set modz [expr {sqrt([mod $z1])}]
return [list [expr {$modz*cos($argz)}] [expr {$modz*sin($argz)}]]
}
proc ::math::complexnumbers::pow {z1 z2} {
return [exp [* [log $z1] $z2]]
}
# transformational functions --
# Implement transformational functions
# Arguments:
# z1 Argument
# Result:
# String like 1+i
#
proc ::math::complexnumbers::tostring {z1} {
foreach {c d} $z1 {break}
if { $d == 0.0 } {
return "$c"
} else {
if { $c == 0.0 } {
if { $d == 1.0 } {
return "i"
} elseif { $d == -1.0 } {
return "-i"
} else {
return "${d}i"
}
} else {
if { $d > 0.0 } {
if { $d == 1.0 } {
return "$c+i"
} else {
return "$c+${d}i"
}
} else {
if { $d == -1.0 } {
return "$c-i"
} else {
return "$c${d}i"
}
}
}
}
}
#
# Announce our presence
#
package provide math::complexnumbers 1.0.2
|