473,399 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Complex numbers


I can't seem to find an implementation of complex numbers in the C# standard
library. Is there one?

--
Dr Jon D Harrop, Flying Frog Consultancy
The F#.NET Journal
http://www.ffconsultancy.com/product...ournal/?usenet
May 3 '07 #1
17 4115
Not buit in AFAIK, but a quick google yields:

http://www.codeproject.com/dotnet/complex_math.asp

Marc

May 3 '07 #2
Jon,

No, there is not. You will have to implement it yourself, or use a
third-party implementation.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jon Harrop" <jo*@ffconsultancy.comwrote in message
news:46**********************@ptn-nntp-reader02.plus.net...
>
I can't seem to find an implementation of complex numbers in the C#
standard
library. Is there one?

--
Dr Jon D Harrop, Flying Frog Consultancy
The F#.NET Journal
http://www.ffconsultancy.com/product...ournal/?usenet

May 3 '07 #3
On May 3, 3:34 pm, Marc Gravell <marc.grav...@gmail.comwrote:
Not buit in AFAIK, but a quick google yields:

http://www.codeproject.com/dotnet/complex_math.asp

Marc
Or another one:

http://www.extremeoptimization.com/m...s/default.aspx

May 3 '07 #4
On May 3, 3:36 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
Jon,

No, there is not. You will have to implement it yourself, or use a
third-party implementation.
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.
>
--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

"Jon Harrop" <j...@ffconsultancy.comwrote in message

news:46**********************@ptn-nntp-reader02.plus.net...


I can't seem to find an implementation of complex numbers in the C#
standard
library. Is there one?
--
Dr Jon D Harrop, Flying Frog Consultancy
The F#.NET Journal
http://www.ffconsultancy.com/product...urnal/?usenet- Hide quoted text -

- Show quoted text -

May 3 '07 #5
All you need is a class with two properties, real part and imaginary part
IMO, this would be one of the (few) occasions when a struct
(immutable) would be in order. The painful part here is having to
choose a type for the 2 components (or duplicate the code) - since
unfortunately generics do not allow you to efficiently use operators
such as *, +, - (and hence you can't do Complex<Tfor T in {int,
decimal, etc...})

Marc

May 3 '07 #6
I would use a struct here, not a class.

Nonetheless, you are right: it wouldn't be hard to implement.

On May 3, 1:53 pm, z...@construction-imaging.com wrote:
On May 3, 3:36 pm, "Nicholas Paldino [.NET/C# MVP]"

<m...@spam.guard.caspershouse.comwrote:
Jon,
No, there is not. You will have to implement it yourself, or use a
third-party implementation.

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.
--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com
"Jon Harrop" <j...@ffconsultancy.comwrote in message
news:46**********************@ptn-nntp-reader02.plus.net...
I can't seem to find an implementation of complex numbers in the C#
standard
library. Is there one?
--
Dr Jon D Harrop, Flying Frog Consultancy
The F#.NET Journal
>http://www.ffconsultancy.com/product...ournal/?usenet
May 4 '07 #7
za***@construction-imaging.com wrote:
On May 3, 3:36 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
>Jon,

No, there is not. You will have to implement it yourself, or use a
third-party implementation.

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.
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

--
Dr Jon D Harrop, Flying Frog Consultancy
The F#.NET Journal
http://www.ffconsultancy.com/product...ournal/?usenet
May 4 '07 #8
Bruce Wood wrote:
I would use a struct here, not a class.
Yes. I've timed both on a simple all-n FFT implementation and using a struct
is >3x faster.

--
Dr Jon D Harrop, Flying Frog Consultancy
The F#.NET Journal
http://www.ffconsultancy.com/product...ournal/?usenet
May 4 '07 #9
Jon Harrop <jo*@ffconsultancy.comwrote:
Bruce Wood wrote:
I would use a struct here, not a class.

Yes. I've timed both on a simple all-n FFT implementation and using a struct
is >3x faster.
Unless performance is absolutely critical (which it may be of course) I
wouldn't use it as a reason to choose a class over a struct or vice
versa. The reason for making it a struct is that it's a natural value
type in the same way that int and double are.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 4 '07 #10
On Fri, 04 May 2007 02:39:45 +0100, Jon Harrop <jo*@ffconsultancy.com>
wrote:
>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...
That would be an unrealistic expectation. Microsoft isn't involved in
numerical computing, outside of MS Research. Also, neither C# nor
Visual Basic have any ancestry or relationship (C, C++, Java, Basic
dialects) with built-in complex numbers -- unless you count Fortran.
It's just not a requirement for the business users MS is targeting.
--
http://www.kynosarges.de
May 4 '07 #11
On May 3, 6:39 pm, Jon Harrop <j...@ffconsultancy.comwrote:
z...@construction-imaging.com wrote:
On May 3, 3:36 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
Jon,
No, there is not. You will have to implement it yourself, or use a
third-party implementation.
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.

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

--
Dr Jon D Harrop, Flying Frog Consultancy
The F#.NET Journalhttp://www.ffconsultancy.com/products/fsharp_journal/?usenet
OK... maybe a caveat is in order. :-)

It would be relatively easy to implement a basic complex number type
yourself.

To get all of the boundary cases right, though, including avoiding
overflows when very close to the limits of what various types can
hold, and handling other odd situations... well, that would require
some serious study of numerical computing. (I don't count myself as an
expert... let's just say that I've taken enough courses to know what
it is that I don't know....)

So, if you're not going to push the limits, then it's probably pretty
easy to cobble something together. If, on the other hand, you need to
do some heavy-duty calculations that could involve values within an
order of magnitude of what the base type can handle, well then you're
probably better off looking for a commercial package, or something
developed by someone with a deep knowledge of numerical analysis.

May 4 '07 #12
Jay
If I were to implement complex numbers myself, or via a third party, would it work with normal
operators (eg * / + -)?

"Jon Harrop" <jo*@ffconsultancy.comwrote in message
news:46**********************@ptn-nntp-reader02.plus.net...

I can't seem to find an implementation of complex numbers in the C# standard
library. Is there one?

--
Dr Jon D Harrop, Flying Frog Consultancy
The F#.NET Journal
http://www.ffconsultancy.com/product...ournal/?usenet
May 4 '07 #13
Jay
Doesn't C++ have complex numbers as standard? If so, C++ is sounding more attractive for a programme
I have to write soon.

"Chris Nahr" <di******@kynosarges.dewrote in message
news:jh********************************@4ax.com...
On Fri, 04 May 2007 02:39:45 +0100, Jon Harrop <jo*@ffconsultancy.com>
wrote:
>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...
That would be an unrealistic expectation. Microsoft isn't involved in
numerical computing, outside of MS Research. Also, neither C# nor
Visual Basic have any ancestry or relationship (C, C++, Java, Basic
dialects) with built-in complex numbers -- unless you count Fortran.
It's just not a requirement for the business users MS is targeting.
--
http://www.kynosarges.de
May 4 '07 #14
On Fri, 4 May 2007 12:33:40 +0100, "Jay" <nospamwrote:
>Doesn't C++ have complex numbers as standard? If so, C++ is sounding more attractive for a programme
I have to write soon.
Hey, you're right -- the STL defines a template class for complex
numbers. I wasn't aware of that.
--
http://www.kynosarges.de
May 4 '07 #15
"Jay" <nospamschrieb im Newsbeitrag
news:e7**************@TK2MSFTNGP05.phx.gbl...
If I were to implement complex numbers myself, or via a third party, would
it work with normal
operators (eg * / + -)?
If the type is implemented to do so, yes. C# supports operator overloading.
May 4 '07 #16
Chris Nahr wrote:
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.

--
Dr Jon D Harrop, Flying Frog Consultancy
The F#.NET Journal
http://www.ffconsultancy.com/product...ournal/?usenet
May 4 '07 #17
On Fri, 04 May 2007 15:35:46 +0100, Jon Harrop <jo*@ffconsultancy.com>
wrote:
>C also provides complex numbers, in C99.
Of course, Microsoft doesn't support that standard either. ;)
--
http://www.kynosarges.de
May 5 '07 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Blair | last post by:
could someone PLEASE tell me why this doesn't work... ----------------------------------------- #include <complex> using namespace std; typedef complex<long double> cld; void main() { cld...
34
by: Pmb | last post by:
I've been working on creating a Complex class for my own learning purpose (learn through doing etc.). I'm once again puzzled about something. I can't figure out how to overload the assignment...
5
by: Todd Steury | last post by:
Greetings Python'ers: I'm just an amature who occasionally uses Python for complex mathematical models. The current model I'm working with occasionally generates really large numbers that are...
17
by: Chris Travers | last post by:
Hi all; I just made an interesting discovery. Not sure if it is a good thing or not, and using it certainly breakes first normal form.... Not even sure if it really works. However, as I am...
6
by: gc | last post by:
Hi, Why didn't the committee propose a new type for complex numbers with integer components? thanks, gc
1
by: seia0106 | last post by:
Hello, I have an array X=, whose even and odd indices should represent real and imaginary parts of complex numbers. This I want to use in a routine that uses the typedef double Cx; for storing...
3
by: Russ | last post by:
I'd like to get output formatting for my own classes that mimics the built-in output formatting. For example, >>> x = 4.54 >>> print "%4.2f" % x 4.54 In other words, if I substitute a class...
7
by: schaefer.mp | last post by:
To compute the absolute value of a negative base raised to a fractional exponent such as: z = (-3)^4.5 you can compute the real and imaginary parts and then convert to the polar form to get...
25
by: jacob navia | last post by:
The C99 standard forgot to define the printf equivalent for complex numbers Since I am revising the lcc-win implementation of complex numbers I decided to fill this hole with "Z" for...
9
by: void main | last post by:
I'm rather new to complex numbers in C and was wondering, how do I initialize a complex variable properly if the imaginary part is 0. I tried -------- #include <complex.h> float complex c...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.