Overload = and [ ] to store complex value terms in class

Discussion in 'C++' started by jwest, Apr 6, 2008.

  1. jwest

    jwest Guest

    I am writing a class to wrap around a C numerical routine that processes
    the real and imaginary parts of complex data in two different arrays.
    The wrapper I have so far is

    class COMPLEX_DATA {
    std::valarray<std::complex<double> > real_data, imag_data;
    public:
    // (functions that call the C-code to process)
    }

    In order to make access to this class as transparent as possible within
    our C++ code, I would like to overload equal sign and [ ] operators so
    we can both read from and write to the internal valarrays directly from a
    complex variable. That is, I would like to be able to do the following

    COMPLEX_DATA d_c;
    std::complex<double> a, b;

    // Set a to the value std::complex<double>(d_c.real_data, d_c.imag_data)
    // using

    a = d_c;



    // Set d_c.real_data = real(b) and d_c.imag_data = imag(b) using

    d_c = b;


    The first one is easy, but the second one is unfortunately beyond my
    abilities (if it is even possible). Can anyone enlighten me?
    jwest, Apr 6, 2008
    #1
    1. Advertising

  2. jwest

    joe Guest


    > // Set a to the value std::complex<double>(d_c.real_data, d_c.imag_data)
    > // using
    >
    > a = d_c;
    >
    > // Set d_c.real_data = real(b) and d_c.imag_data = imag(b) using
    >
    > d_c = b;
    >
    > The first one is easy, but the second one is unfortunately beyond my
    > abilities (if it is even possible). Can anyone enlighten me?


    My first instinct would to expose the fact that you have separate R/I
    data by having member functions

    d_c.real(i) = b.real();
    d_c.imag(i) = b.imag();

    But I guess you could do something a lot more complicated like define
    the following in class COMPLEX_DATA:

    somethingElse& operator[](unsigned index)
    {
    SomethingElse* se = new SomethingElse; //use your favorite smart
    pointer
    se.index = index;
    se.parent = this;
    return *se;
    }

    and add
    friend SomethingElse;

    and add the following:

    class SomethingElse
    {
    public:
    std::complex<double>& operator==(std::complex<double>& num)
    {
    parent->real_data[index] = num.real();
    parent->imag_data[index] = num.imag();
    return num;
    }

    private:
    unsigned index;
    COMPLEX_DATA* parent;

    };
    joe, Apr 6, 2008
    #2
    1. Advertising

  3. jwest wrote:
    > I am writing a class to wrap around a C numerical routine that processes
    > the real and imaginary parts of complex data in two different arrays.
    > The wrapper I have so far is
    >
    > class COMPLEX_DATA {
    > std::valarray<std::complex<double> > real_data, imag_data;

    I assume you really mean
    std::valarray<double> real_data, imag_data;
    > public:
    > // (functions that call the C-code to process)
    > }
    >
    > In order to make access to this class as transparent as possible within
    > our C++ code, I would like to overload equal sign and [ ] operators so
    > we can both read from and write to the internal valarrays directly from a
    > complex variable. That is, I would like to be able to do the following
    >
    > COMPLEX_DATA d_c;
    > std::complex<double> a, b;
    >
    > // Set a to the value std::complex<double>(d_c.real_data, d_c.imag_data)
    > // using
    >
    > a = d_c;
    >
    >
    >
    > // Set d_c.real_data = real(b) and d_c.imag_data = imag(b) using
    >
    > d_c = b;
    >
    >
    > The first one is easy, but the second one is unfortunately beyond my
    > abilities (if it is even possible). Can anyone enlighten me?


    OK - here is an example using a proxy class.

    #include <complex>
    #include <valarray>

    class COMPLEX_DATA {
    std::valarray<double> real_data, imag_data;
    public:

    COMPLEX_DATA(std::size_t size)
    : real_data(size), imag_data(size)
    {}

    class adaptor {
    COMPLEX_DATA & v;
    const std::size_t index;

    public:
    adaptor( COMPLEX_DATA & v, std::size_t index )
    : v(v), index(index) {}

    adaptor & operator = ( const std::complex<double> & val ) const
    {
    v.real_data[index] = val.real();
    v.imag_data[index] = val.imag();
    }

    operator std::complex<double> () const
    {
    return std::complex<double>(
    v.real_data[index], v.imag_data[index] );
    }
    };

    const std::complex<double> operator[](std::size_t index) const
    {
    return std::complex<double>( real_data[index], imag_data[index]);
    }

    const adaptor operator[](std::size_t index)
    {
    return adaptor(*this, index);
    }
    };

    void f(COMPLEX_DATA & d)
    {
    std::complex<double> v(1,1);

    d[2] = v;

    v = d[2];
    }

    void y(const COMPLEX_DATA & d)
    {
    std::complex<double> v(1,1);

    // d[2] = v; -- error - can't assign to const

    v = d[2];
    }

    COMPLEX_DATA d(std::size_t(30));

    int main()
    {
    f(d);
    y(d);
    }
    Gianni Mariani, Apr 6, 2008
    #3
  4. jwest

    joe Guest


    > adaptor & operator = ( const std::complex<double> & val ) const
    > {
    > v.real_data[index] = val.real();
    > v.imag_data[index] = val.imag();


    ---> return *this;


    > }
    >


    This example is well more thought out than mine. (Disregard mine in
    favor of Gianni's version). Except for the above correction; although
    I think I prefer the adapter's operator just to return 'val'
    joe, Apr 6, 2008
    #4
  5. jwest

    joe Guest

    On Apr 5, 10:23 pm, joe <> wrote:
    > > adaptor & operator = ( const std::complex<double> & val ) const
    > > {
    > > v.real_data[index] = val.real();
    > > v.imag_data[index] = val.imag();

    >
    > ---> return *this;
    >
    > > }

    >
    > This example is well more thought out than mine. (Disregard mine in
    > favor of Gianni's version). Except for the above correction; although
    > I think I prefer the adapter's operator just to return 'val'


    ....And I believe that adapter will still need to be a friend of
    COMPLEX_DATA to access "real_data" in the above example, although I
    suspect you should/will/do have public member functions to access
    these individually.
    joe, Apr 6, 2008
    #5
  6. joe wrote:
    > On Apr 5, 10:23 pm, joe <> wrote:
    >>> adaptor & operator = ( const std::complex<double> & val ) const
    >>> {
    >>> v.real_data[index] = val.real();
    >>> v.imag_data[index] = val.imag();

    >> ---> return *this;
    >>
    >>> }

    >> This example is well more thought out than mine. (Disregard mine in
    >> favor of Gianni's version). Except for the above correction; although
    >> I think I prefer the adapter's operator just to return 'val'



    I compiled it ... honest - I prolly shoulda turned warnings on.

    >
    > ...And I believe that adapter will still need to be a friend of
    > COMPLEX_DATA to access "real_data" in the above example, although I
    > suspect you should/will/do have public member functions to access
    > these individually.


    An inner class has visibility into it's outer class.

    As for the return value of operator=, I'm thinking you don't want
    someone to expect code like this to break:

    (v = A) = C;

    ... yep, it's an edge case but it comes in hidden ways when you pass
    parameters - especially template based ones.
    Gianni Mariani, Apr 6, 2008
    #6
  7. jwest

    jwest Guest

    On 2008-04-06, joe <> wrote:
    >
    >> adaptor & operator = ( const std::complex<double> & val ) const
    >> {
    >> v.real_data[index] = val.real();
    >> v.imag_data[index] = val.imag();

    >
    > ---> return *this;
    >
    >
    >> }
    >>

    >
    > This example is well more thought out than mine. (Disregard mine in
    > favor of Gianni's version). Except for the above correction; although
    > I think I prefer the adapter's operator just to return 'val'


    I'll change a statement in my OP a bit. This wasn't beyond my ability,
    this was WAY beyond my ability :). Thank you both very much. I'll get it
    implemented in the code with a citation to your posts on Google. The
    class will primarily be used by engineering students with most of their
    programming experience in Matlab, so I'm pretty confident that making
    access look as much like access to a standard array/vector will ultimately
    save us a lot of time in the future.
    jwest, Apr 6, 2008
    #7
  8. jwest wrote:
    > On 2008-04-06, joe <> wrote:
    >>> adaptor & operator = ( const std::complex<double> & val ) const
    >>> {
    >>> v.real_data[index] = val.real();
    >>> v.imag_data[index] = val.imag();

    >> ---> return *this;
    >>
    >>
    >>> }
    >>>

    >> This example is well more thought out than mine. (Disregard mine in
    >> favor of Gianni's version). Except for the above correction; although
    >> I think I prefer the adapter's operator just to return 'val'

    >
    > I'll change a statement in my OP a bit. This wasn't beyond my ability,
    > this was WAY beyond my ability :). Thank you both very much. I'll get it
    > implemented in the code with a citation to your posts on Google. The
    > class will primarily be used by engineering students with most of their
    > programming experience in Matlab, so I'm pretty confident that making
    > access look as much like access to a standard array/vector will ultimately
    > save us a lot of time in the future.


    I just took another squiz at the code - I left out an explicit on the
    constructor as well.

    You can also add "iterator" support to this which would then make it
    work with many of the standard algorithms as well.

    #include <complex>
    #include <valarray>

    class COMPLEX_DATA {
    std::valarray<double> real_data, imag_data;
    public:

    explicit COMPLEX_DATA(std::size_t size)
    : real_data(size), imag_data(size)
    {}

    class adaptor {
    COMPLEX_DATA & v;
    const std::size_t index;

    public:
    adaptor( COMPLEX_DATA & v, std::size_t index )
    : v(v), index(index) {}

    const adaptor &operator=(const std::complex<double> & val) const
    {
    v.real_data[index] = val.real();
    v.imag_data[index] = val.imag();
    return * this;
    }

    operator std::complex<double> () const
    {
    return std::complex<double>(
    v.real_data[index], v.imag_data[index] );
    }
    };

    const std::complex<double> operator[](std::size_t index) const
    {
    return std::complex<double>(
    real_data[index], imag_data[index] );
    }

    const adaptor operator[](std::size_t index)
    {
    return adaptor(*this, index);
    }
    };

    // "compile" test code

    void f(COMPLEX_DATA & d)
    {
    std::complex<double> v(1,1);

    d[2] = v;

    v = d[2];
    }

    void y(const COMPLEX_DATA & d)
    {
    std::complex<double> v(1,1);

    // d[2] = v; -- error - can't assign to const

    v = d[2];
    }

    COMPLEX_DATA d(30);

    int main()
    {
    f(d);
    y(d);

    std::complex<double> v(1,1);
    std::complex<double> u(2,1);
    (d[3]=u)=v;
    }
    Gianni Mariani, Apr 6, 2008
    #8
    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. savvy
    Replies:
    1
    Views:
    399
    savvy
    Jan 16, 2006
  2. Piotre Ugrumov
    Replies:
    3
    Views:
    360
    Nick Hounsome
    Jan 25, 2004
  3. Replies:
    2
    Views:
    446
  4. jraul

    complex number overload

    jraul, Dec 15, 2006, in forum: C++
    Replies:
    1
    Views:
    334
    rossum
    Dec 15, 2006
  5. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    230
    Sherm Pendley
    Oct 11, 2004
Loading...

Share This Page