Velocity Reviews > array of array of complex things

# array of array of complex things

ma740988@gmail.com
Guest
Posts: n/a

 08-29-2006

I'm looking for an equivalent approach to what I would do in C++ such
to create a vector of vector of complex. I would do :

typedef std::vector < std::complex < double > > complex_dvec;
typedef std::vector < complex_dvec > twod_complex_vec;

int main() {
twod_complex_vec tdv ( 3, 3 );
for ( size_t idx ( 0 ); idx < tdv.size(); ++idx )
{
for ( size_t jdx ( 0 ); jdx < tdv.front().size(); ++jdx )
tdv [ idx ] [ jdx ] = idx * jdx ;
}
}

I'm trying to come up with an equivalent C approach to this but I'm
going around in circles. Source snippet appreaciated.. Thanks in

dbtid
Guest
Posts: n/a

 08-29-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I'm looking for an equivalent approach to what I would do in C++ such
> to create a vector of vector of complex. I would do :
>
> typedef std::vector < std::complex < double > > complex_dvec;
> typedef std::vector < complex_dvec > twod_complex_vec;
>
> int main() {
> twod_complex_vec tdv ( 3, 3 );
> for ( size_t idx ( 0 ); idx < tdv.size(); ++idx )
> {
> for ( size_t jdx ( 0 ); jdx < tdv.front().size(); ++jdx )
> tdv [ idx ] [ jdx ] = idx * jdx ;
> }
> }
>
> I'm trying to come up with an equivalent C approach to this but I'm
> going around in circles. Source snippet appreaciated.. Thanks in
>

http://www.c-faq.com/aryptr/dynmuldimary.html

Bill Pursell
Guest
Posts: n/a

 08-30-2006
(E-Mail Removed) wrote:
> I'm looking for an equivalent approach to what I would do in C++ such
> to create a vector of vector of complex. I would do :

Assuming C89 with no complex types:

struct complex {
double r,i;
};

struct vector {
void *data;
size_t size;
};

struct vector a;

a.data = malloc(NUM_VEC);
if (a.data == NULL)
handle_error();

a.size = NUM_VEC;
a.data[0] = create_complex_vector();
.....
push_value ( 3.4, -1, a.data[0]);

You'll obviously want to have the ability
to reallocate space as the vector grows, and
you'll want a more generic "constructor"
than create_complex_vector, but the
above demonstrates some ideas. You
wouldn't want to call malloc directly on
want to write a function that takes perhaps
an initial size and an allocator function ptr
as parameters which will create the vector
for you. You might want to make the vector
an opaque type and hide the size_t variable
from the user....lots of ways to do this.