J
Jon Harrop
I can't seem to find an implementation of complex numbers in the C# standard
library. Is there one?
library. Is there one?
Not buit in AFAIK, but a quick google yields:
http://www.codeproject.com/dotnet/complex_math.asp
Marc
Jon,
No, there is not. You will have to implement it yourself, or use a
third-party implementation.
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
I can't seem to find an implementation of complex numbers in the C#
standard
library. Is there one?
- Show quoted text -
And I don't think it would be all that hard to implement. All you need
is a class with two properties, real part and imaginary part, and then
a series of methods to do things like add, suntract, etc.
Bruce said:I would use a struct here, not a class.
Jon Harrop said:Yes. I've timed both on a simple all-n FFT implementation and using a struct
is >3x faster.
Ugh. That really sucks. I appreciate that it is faily easy to implement
yourself but I'd really expect to see complex numbers in a modern
language...
Ugh. That really sucks. I appreciate that it is faily easy to implement
yourself but I'd really expect to see complex numbers in a modern
language...
If you want to be accurate, the implementation isn't actually that
straightforward either. Check out these definitions from the OCaml standard
library, for example:
let div x y =
if abs_float y.re >= abs_float y.im then
let r = y.im /. y.re in
let d = y.re +. r *. y.im in
{ re = (x.re +. r *. x.im) /. d;
im = (x.im -. r *. x.re) /. d }
else
let r = y.re /. y.im in
let d = y.im +. r *. y.re in
{ re = (r *. x.re +. x.im) /. d;
im = (r *. x.im -. x.re) /. d }
let norm x =
(* Watch out for overflow in computing re^2 + im^2 *)
let r = abs_float x.re and i = abs_float x.im in
if r = 0.0 then i
else if i = 0.0 then r
else if r >= i then
let q = i /. r in r *. sqrt(1.0 +. q *. q)
else
let q = r /. i in i *. sqrt(1.0 +. q *. q)
let sqrt x =
if x.re = 0.0 && x.im = 0.0 then { re = 0.0; im = 0.0 }
else begin
let r = abs_float x.re and i = abs_float x.im in
let w =
if r >= i then begin
let q = i /. r in
sqrt(r) *. sqrt(0.5 *. (1.0 +. sqrt(1.0 +. q *. q)))
end else begin
let q = r /. i in
sqrt(i) *. sqrt(0.5 *. (q +. sqrt(1.0 +. q *. q)))
end in
if x.re >= 0.0
then { re = w; im = 0.5 *. x.im /. w }
else { re = 0.5 *. i /. w; im = if x.im >= 0.0 then w else -. w }
end
Ugh. That really sucks. I appreciate that it is faily easy to implement
yourself but I'd really expect to see complex numbers in a modern
language...
Doesn't C++ have complex numbers as standard? If so, C++ is sounding more attractive for a programme
I have to write soon.
If the type is implemented to do so, yes. C# supports operator overloading.Jay said:If I were to implement complex numbers myself, or via a third party, would
it work with normal
operators (eg * / + -)?
Chris said:Hey, you're right -- the STL defines a template class for complex
numbers. I wasn't aware of that.
C also provides complex numbers, in C99.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.