Complex numbers

Discussion in 'C Programming' started by Richard Harris, Nov 17, 2005.

  1. I understand that C will suport complex values under iso99c. Does
    anyone know of some examples that will show how to implement complex
    functions?
    Richard Harris, Nov 17, 2005
    #1
    1. Advertising

  2. Richard Harris

    Simon Biber Guest

    Richard Harris wrote:
    > I understand that C will suport complex values under iso99c. Does
    > anyone know of some examples that will show how to implement complex
    > functions?


    Here's an example of how to use the complex number support:

    #include <stdio.h>
    #include <complex.h>

    int main(void)
    {
    double minx = -1.7, maxx = 0.7, sx = (maxx - minx) / 50;
    double miny = -1.0, maxy = 1.0, sy = (maxy - miny) / 70;
    int i, maxi = 64;
    double bail = 4.0;
    for(double x = minx; x < maxx; x += sx)
    {
    for(double y = miny; y < maxy; y += sy)
    {
    complex double c = x + I * y;
    complex double z = c;
    for(i = 0; i < maxi && cabs(z) < bail; i++)
    {
    z = z * z + c;
    }
    putchar(i == maxi ? ' '
    : "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[i % 36]);
    }
    putchar('\n');
    }
    return 0;
    }

    Here is the output:

    22222222222223333333333344444455678 8765544444433333333333222222222222
    22222222222333333333333444444555678 8765554444443333333333332222222222
    22222222223333333333344444445556679 9766555444444433333333333222222222
    22222222233333333333444444455566679 9766655544444443333333333322222222
    2222222233333333333444444455566678A A876665554444444333333333332222222
    2222222333333333334444445555667789C C987766555544444433333333333222222
    222222333333333344444455555668HBACH HCABH86655555444444333333333322222
    2222233333333334444455556666789BJOL LOJB987666655554444433333333332222
    222233333333334444556667777789ADO ODA98777776665544443333333333222
    223333333333344444568BBB9889BFDGIR RIGDFB9889BBB86544444333333333332
    23333333333334444555689AGBBCE ECBBGA9865554444333333333333
    333333333333444445556789EMY T T YME98765554444433333333333
    333333333334444445556679C2 2C97665554444443333333333
    333333333344444445556789BHN NHB98765554444444333333333
    3333333334444444555668EEGLY YLGEE866555444444433333333
    333333334444444455566789DQ0 0QD98766555444444443333333
    333333334444444555566779TK1 1KT97766555544444443333333
    3333333444444455556667789BI2 2IB987766655554444444333333
    3333334444444555566667789ACP PCA987766665555444444433333
    3333344444455555666777899ABCEI7 7IECBA998777666555554444443333
    3333444444555566667889ABBFEHL 7 7 LHEFBBA988766665555444444333
    333444444555666778AGCBL L L LBCGA87766655544444433
    334444445567778889Q Q Q Q98887776554444443
    34444445567AHJCBBBFK KFBBBCJHA7655444444
    44444455567APEHHVI1 1IVHHEPA7655544444
    44444555667ABZ ZBA7665554444
    4444555666789CGO OGC987666555444
    445555666789DEW WED98766655554
    45555667778B B8777665555
    55566888899B X X B9988886655
    66679DDCHDCD DCDHCDD9766
    7778ADP MN NM PDA877
    A99BFH HFB99
    BOEJ JEO
    BNBCQHW U U WHQCBN
    K9889BF S VGR RGV S FB9889
    56667HBBAAABG GBAAABBH7666
    55556667778AD DA8777666555
    44455556677B B7766555544
    444445556678ACF FCA87665554444
    44444455566789BEM MEB9876655544444
    3344444555667AL B M B LA766555444443
    3334444455678AZ KJ A JK ZA876554444433
    33333444456CESQFCD99ECI 9 NF98889FN 9 ICE99DCFQSEC6544443333
    33333344445568B8777789E SABLHAD876666678DAHLBAS E9877778B8655444433333
    333333334444555555566AAC87777666555555566677778CAA66555555544443333333
    3333333333444444445555555555555555555555555555555555544444444333333333
    3333333333333444444444444444444444444444444444444444444444333333333333
    2233333333333333344444444444444444444444444444444444443333333333333332
    2222233333333333333333333333334444444444433333333333333333333333332222

    --
    Simon.
    Simon Biber, Nov 17, 2005
    #2
    1. Advertising

  3. Richard Harris

    Malcolm Guest

    "Richard Harris" <> wrote
    >
    >I understand that C will suport complex values under iso99c. Does
    > anyone know of some examples that will show how to implement complex
    > functions?
    >

    You have to understand the maths first.

    Probably the most important thing for the maths library is the exponential
    function, e^x. This can be expanded

    e^x = 1 + x + x^2/2! + x^3/3! + x^4/4! ...

    Now simply plug an imaginary value into the series, and you understand what
    it means to exponentiate by an imaginary power.

    Once you understand the maths, you also have to understand the limits of
    your floating point system, in order to produce results of known accuracy,
    and the limits of the series expansions - some are acceptable to the
    mathematician but in fact converge too slowly to be of practical use to the
    computer programmer.

    So to make a loose implementation is quite simple, but to code an industrial
    strength complex maths library is more difficult.
    Malcolm, Nov 22, 2005
    #3
  4. In article <dm03kg$ec2$-infra.bt.com> "Malcolm" <> writes:
    > Probably the most important thing for the maths library is the exponential
    > function, e^x. This can be expanded
    >
    > e^x = 1 + x + x^2/2! + x^3/3! + x^4/4! ...
    >
    > Now simply plug an imaginary value into the series, and you understand what
    > it means to exponentiate by an imaginary power.


    Oh. I would simply go for:
    exp(real_part(z)) * (cos(imag_part(z)) + i * sin(imag_part(z)))
    or something like that. Industrial strength and all that stuff.
    --
    dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
    home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
    Dik T. Winter, Nov 23, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. news.amnet.net.au
    Replies:
    1
    Views:
    554
    =?UTF-8?b?TMSByrtpZSBUZWNoaWU=?=
    Apr 13, 2004
  2. oulan bator
    Replies:
    29
    Views:
    1,148
    Chris Uppal
    Dec 4, 2005
  3. Stanimir Stamenkov
    Replies:
    2
    Views:
    716
    Stanimir Stamenkov
    Oct 25, 2005
  4. Robert Mark Bram
    Replies:
    0
    Views:
    668
    Robert Mark Bram
    Feb 4, 2007
  5. Kottiyath

    How complex is complex?

    Kottiyath, Mar 18, 2009, in forum: Python
    Replies:
    22
    Views:
    729
Loading...

Share This Page