Results 1 to 11 of 11

Thread: Complex Numbers Class?

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    54

    Complex Numbers Class?

    hello everyone,
    I have been working for my Data structures class for a week now and have been attempting to understand this whole complex numbers concept. My textbook has very little description on the operations for complex numbers, so I was hoping someone here would be better able to explain it to me. I am trying to write the code to overload the - symbol for a complex number for my complex class. I was struggling mostly with understanding how does one negate a complex number. Heres the basic outline for the code I have come up with. Any and all help would really be appreciated.

    heres the definition within the class.

    complex operator- () const;
    //negation of a complex number



    and my function implementation would be...

    complex::complex operator-() const
    {

    }

  2. #2
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    first off, C99 has all of this as does MSVC++ (Complex class)
    Seondly, complex numbers do messy stuff for relatively simple operations.

    Here is a simplified version of some really old complex functions written in C for students learning pointers & structs. This is NOT production code, it's too slow.

    Code:
    #include <math.h>
    #include <stdio.h>
    #ifndef __neg 
    #define __neg 0
    #define neg(c) c*(-1)
    #endif
    typedef struct complex{
         double r;
         double i;
    };
    
    
    static struct complex cmplx_working;
    static struct complex cmplx_perm;
    static double cmplx_double;
    
    /* prototypes */
    void complex_print(struct complex *,int);
    void complex_fprint(struct complex *,int,FILE *);
    void complex_cpy(struct complex *,struct complex *);
    
    struct complex *complex_sqrt(struct complex *);
    struct complex *complex_init(void);
    struct complex *complex_multiply(struct complex *,struct complex *);
    struct complex *complex_divide(struct complex *, struct complex *);
    struct complex *complex_conjugate(struct complex *);
    struct complex *complex_add(struct complex *,struct complex *);
    struct complex *complex_subtract(struct complex *,struct complex *);
    
    double *complex_real(struct complex *);
    double *complex_imag(struct complex *);
    double *complex_abs(struct complex *);
    double *complex_modulus(struct complex *);
    double *complex_log(struct complex *);
    double *complex_phase(struct complex *);
    
    double *complex_phase(struct complex *src){
           cmplx_double=M_PI;
           if(src->r!=0)
               cmplx_double=atan2(src->i/src->r);
           return &cmplx_double;
    }
    
    double *complex_real(struct complex *src){
    	return src->r;
    }
    
    double *complex_imag(struct complex *src){
    	return src->i;
    }
    
    double *complex_log(struct complex *src){/* limited definition +/- pi/2 */
    	double x=M_PI;
             /* default value for r = 0 */
    	if(src->r !=0){
    	     x=*complex_abs(src);
    	     x*=*complex_phase(src);
            }
            cmplx_double=x;
            return &cmplx_double;       
    }        
    
    
    struct complex *complex_sqrt(struct complex *src){ /* NB: conjugate is other root*/
           double r,x,y;
           r=*complex_abs(src);
           y=sqrt(abs((r-src->r))/2);
           x=src->i/(2*y);
           cmplx_perm.r=x;
           cmplx_perm.i=y;
           return &cmplx_perm;
    }
    
    double *complex_modulus(struct complex *src){
           return complex_abs(src);
    }
    
    double *complex_abs(struct complex *src){
            cmplx_double=sqrt( (src->r*src->r) + (src->i*src->i) );
            return &cmplx_double;
    }	
    void complex_print(struct complex *src,int i){
    	if(!i)printf("(%f + %f)",src->r,src->i);
    	if(i)printf("(%f + %f)\n",src->r,src->i);
            return;
    }
    void complex_fprint(struct complex *src,int i,FILE *f){
    	if(!i)fprintf(f,"(%f + %f)",src->r,src->i);
    	if(i)fprintf(f,"(%f + %f)\n",src->r,src->i);
            return;
    }
    
    void complex_cpy(struct complex *dest,struct complex *src){
           dest->i=src->i;
           dest->r=src->r;	
    }
    
    struct complex *complex_add(struct complex *first,struct complex *second){       
           cmplx_perm.r=first->r+second->r;
           cmplx_perm.i=first->i+second->i;
           return &cmplx_perm;
           
    }	
    struct complex *complex_subtract(struct complex *first,struct complex *second){
           cmplx_perm.r=first->r - second->r;
           cmplx_perm.i=first->i - second->i;
           return &cmplx_perm;	
    }
    struct complex *complex_init(void){
    	cmplx_working.i=0;
    	cmplx_working.r=0;
    	return &cmplx_working;
    }
    struct complex *complex_conjugate(struct complex *src){
    	complex_cpy(&cmplx_perm,src);
            cmplx_perm.i=neg(src->i);
     	return &cmplx_perm;	
    }
    struct complex *complex_multiply(struct complex *first,struct complex *second){
    	double a,b,c,d;
            a=first->r;
            b=first->i;
            c=second->r;
            d=second->i;
            cmplx_perm.r=(a*c) - (b*d);
            cmplx_perm.i=(a*d) + (b*c);
            return &cmplx_perm;        
    }
    struct complex *complex_divide(struct complex *numerator, struct complex *denominator){
    	struct complex conj,tmp1,tmp2;
    	complex_cpy(&conj,complex_conjugate(denominator));
    	complex_cpy(&tmp1,complex_multiply(numerator,&conj));
    	complex_cpy(&tmp2,complex_multiply(denominator,&conj));
    	cmplx_perm.r= tmp1.r/tmp2.r;
    	cmplx_perm.i= tmp1.i/tmp2.r;
    	return &cmplx_perm;
    }
    This is production code (part of Fractint):
    Code:
    /* old way of inlining code */
    
    #define CMPLXrecip(arg,out)    \
       { double denom; denom = sqr((arg).x) + sqr((arg).y);\
         if(denom==0.0) {(out).x = 1.0e10;(out).y = 1.0e10;}else\
        { (out).x =  (arg).x/denom;\
         (out).y = -(arg).y/denom;}}
    
    
    
    /*take a cmplx to an integer power */
    
    void cpower(_CMPLX *base, int exp, _CMPLX *result)
    {
        if (exp<0) {
    	cpower(base,-exp,result);
    	CMPLXrecip(*result,*result);
    	return;
        }
    
        xt = base->x;   yt = base->y;
    
        if (exp & 1)
        {
           result->x = xt;
           result->y = yt;
        }
        else
        {
           result->x = 1.0;
           result->y = 0.0;
        }
    
        exp >>= 1;
        while (exp)
        {
    	t2 = xt * xt - yt * yt;
    	yt = 2 * xt * yt;
    	xt = t2;
    
    	if (exp & 1)
    	{
    	    t2 = xt * result->x - yt * result->y;
    	    result->y = result->y * xt + yt * result->x;
    	    result->x = t2;
    	}
    	exp >>= 1;
        }
    }

  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Isn't there the std::complex<T> template in the Standard C++ Library?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  4. #4
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    Yes there is. I think I tried to say that - he wanted examples.
    I'm not sure he/she undertands complex arithmetic - hence the awful examples.

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I'm sure you can find a good description of complex numbers and it's operations on the internet. Don't search for code, but rather for pure math. Or ask in the Math forum here, but I'm not sure if anyone still goes there.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    54

    a better explanation

    Hello again,
    The problem I was given gives me a class description, and left the class implementation for me to finish. I have attached the header file and the source file. Everything works except for when I attempt to negate a complex number. if you compile the code and run it it wont come up with any errors because I have commented the three lines that are giving me problems. Ultimately I am trying to prove that -i * i = 1.
    I cant quite figure out how to negate a complex number. I have been searching on the internet, but most of the complex number information does not include negating a complex number.
    Attached Files Attached Files

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    From some checking, it looks like...
    Code:
    #include <iostream>
    #include <complex>
    
    using namespace std;
    
    int main() {
        complex<double> c(5, -2);
        
        cout << c << endl;
        cout << -c << endl;
    }
    ...which gives:
    Code:
    (5,-2)
    (-5,2)
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  8. #8

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    54

    my bad

    thats such an obvious way to check that problem. Thanks alot for the help.

  9. #9
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    PHP Code:
    complex complex::operator- () const
    {
        
    complex temp(-real,-imag);
        return 
    temp;

    That should work.
    [edit: added the class scope complex::]
    Last edited by MoMad; Sep 24th, 2002 at 07:35 PM.
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    return complex(-real, -imag);


    even shorter, and you should inline this function.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  11. #11
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    Originally posted by CornedBee
    return complex(-real, -imag);


    even shorter, and you should inline this function.
    I was thinking about that but wasnt so sure it would work and since i didnt have time to check, i just did what i thought was the safest, shortest way! lol.

    Thanks for the confirmation CB.
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width