translation from matlab to C object containing matrices undefinedlength of object

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • adinda

    translation from matlab to C object containing matrices undefinedlength of object

    So what i need is this; (I'm very new at this,, programming in C I
    mean...)

    In matlab I had a while loop, and after each loop was done I added my
    resulting matrix to an object. Seeing the loop is conditional to my
    results I do not know in advance how many loops I need. The matrices
    are Nx * Ny* Nz*6.

    In C I put my matrices into 1D arrays. So I thought that if I use a
    counter for each time i do a loop and the calloc and realloc function
    that would work...But it doesnt seem to be working...Hope one of you
    can help me.

    This is a sample text which i use to test my programs...

    Vnulpoint is my result matrix for which i used pointers. So Varray is
    supposed to contain each of the Vnulpoint matrices...

    #include <math.h>
    #include <stdio.h>
    #include <stdlib.h>
    #define nx 2
    #define ny 2
    #define nz 2

    void max_array(doubl e *, int,int *,double *);
    int compare_doubles (double *, double *);
    void vindminnietnul( double *,double *,int,int* );
    void vindmaxnieteen( double *,double *,int ,int*);
    main()
    { int i,j,s,kl;
    int countergetal=6;
    double
    gradoud[nx*ny*nz*6]={5.569,6.9987, 0.00987,98.7743 ,1009.8876,0.00 09987};;
    double *gradpointeroud ,*gradpointerni euw;
    double gradnieuw[nx*ny*nz*6];
    double *Vnulpoint[nx*ny*nz];
    double *Vnulpoint2[nx*ny*nz];
    gradpointeroud= gradoud;
    gradpointernieu w=gradnieuw;
    int counter=1;
    double *Varray;
    Varray=calloc(1 *(nx*ny*nz*6),s izeof(double));
    while(counter<1 00)
    {
    for(i=0; i<nx; i++)
    { for(j=0; j<ny ; j++)
    { for(s=0; s<nz ; s++)
    {


    Vnulpoint[i+j*nx
    +s*nx*ny]=calloc(counter getal,sizeof(do uble));
    Vnulpoint2[i+j*nx
    +s*nx*ny]=calloc(counter getal,sizeof(do uble));
    for(kl=0;kl<cou ntergetal;kl++)
    {
    *(Vnulpoint[i+j*nx+s*nx*ny]+kl)=0.12369*kl ;
    *(Vnulpoint[i+j*nx+s*nx*ny]+0)=1;

    printf("%f\n ",*(Vnulpoi nt[i+j*nx+s*nx*ny]
    +kl));
    }
    int maxindex;
    double Vmax;
    max_array(Vnulp oint[i+j*nx+s*nx*ny],
    4,&maxindex,&Vm ax);
    printf("Het maximum volume is %f op locatie %d
    \n",Vmax,maxind ex);
    Vnulpoint2[i+j*nx+s*nx*ny]=Vnulpoint[i+j*nx
    +s*nx*ny];
    printf("Het toegewezen volume is %f \t %f \t %f
    \n",*(Vnulpoint 2[i+j*nx+s*nx*ny]),*(Vnulpoint2[i+j*nx+s*nx*ny]
    +1),*(Vnulpoint 2[i+j*nx+s*nx*ny]+2));


    int teller;
    double Vsort[countergetal];
    for(teller=0;te ller<counterget al;teller++)
    {
    Vsort[teller]=*(Vnulpoint[i+j*nx+s*nx*ny]+teller);
    }
    for(kl=0;kl<cou ntergetal;kl++)
    {
    printf("Vsort %d \t %f\n ",kl,Vsort[kl]);
    }

    qsort(Vsort,cou ntergetal,sizeo f(double),(int( *)(const void*,const
    void*))compare_ doubles);
    for(kl=0;kl<cou ntergetal;kl++)
    {
    printf("Vsort %d \t %f\n ",kl,Vsort[kl]);
    }

    int indexmaxnieteen ;
    vindmaxnieteen( Vsort,Vnulpoint[i+j*nx
    +s*nx*ny],countergetal,& indexmaxnieteen );


    printf("De index is %d\n", indexmaxnieteen );
    Varray=(double
    *)realloc(Varra y,counter*(nx*n y*nz*6)*sizeof( double));
    int tel;
    for (tel=0;tel<6;te l++)
    {
    Varray[(counter-1)*(i+j*nx+s*nx *ny+tel*nx*ny*n z)]=*(Vnulpoint[i
    +j*nx+s*nx*ny]+tel*nx*ny*nz);

    }
    printf("Varray is %f \t %f \t %f \n %f \t %f \t %f \n en counter
    is %d \n", Varray[(counter-1)*(i+j*nx+s*nx *ny)],Varray[(counter-1)*(i
    +j*nx+s*nx*ny+1 )],Varray[(counter-1)*(i+j*nx+s*nx *ny
    +2)],Varray[(counter-1)*(i+j*nx+s*nx *ny+3)],Varray[(counter-1)*(i+j*nx
    +s*nx*ny+4)],Varray[(counter-1)*(i+j*nx+s*nx *ny
    +5)],Varray[(counter-1)*(i+j*nx+s*nx *ny+6)],counter);


    gradpointernieu w=gradpointerou d;

    }
    }
    }
    counter++;
    }

    }

    void max_array(doubl e *a, int num_elements,in t *indexptmax,dou ble
    *maxpt)
    {
    int i;

    *indexptmax=0;
    *maxpt=a[0];



    for (i=1; i<num_elements ; i++)
    {
    if (a[i]>*maxpt)
    {
    *maxpt=a[i];
    *indexptmax=i;
    }
    }


    return;
    }

    int compare_doubles (double *x, double *y)
    {

    if (*x *y)
    {
    return 1;
    }
    else
    {
    if (*x < *y)
    {
    return -1;
    }
    else
    {
    return 0;
    }
    }
    }

    void vindminnietnul( double *V,double *Vl,int counternummer,i nt *index)
    { int nk,nn;

    for(nk=0;nk<cou nternummer;nk++ )
    { if ( *(V+nk)!=0)
    {for (nn=0;nn<counte rnummer;nn++)
    {
    if (*(Vl+nn-1)==*(V+nk))
    {
    *index=nn;
    }

    }
    break;
    }

    }

    }

    void vindmaxnieteen( double *V,double *Vl,int counternummer,i nt*index)
    { int nk,nn;

    for(nk=0;nk<cou nternummer;nk++ )
    { if ( *(V+(counternum mer-1)-nk)!=1)
    {for (nn=0;nn<counte rnummer;nn++)
    {
    if (*(Vl+nn)==*(V+ (counternummer-1)-nk))
    {
    *index=nn;
    }
    }
    break;
    }
    }
    return;
    }
  • dj3vande@csclub.uwaterloo.ca.invalid

    #2
    Re: translation from matlab to C object containing matrices undefined length of object

    In article <8eee42d6-1bb7-43fd-acbc-571dd5366d16@e3 9g2000hsf.googl egroups.com>,
    adinda <adinda.vandenb erg@gmail.comwr ote:
    >So what i need is this; (I'm very new at this,, programming in C I
    >mean...)
    >
    >In matlab I had a while loop, and after each loop was done I added my
    >resulting matrix to an object. Seeing the loop is conditional to my
    >results I do not know in advance how many loops I need. The matrices
    >are Nx * Ny* Nz*6.
    >
    >In C I put my matrices into 1D arrays. So I thought that if I use a
    >counter for each time i do a loop and the calloc and realloc function
    >that would work...But it doesnt seem to be working...Hope one of you
    >can help me.
    What is it doing, and what were you expecting?
    Reducing your code to the smallest example that demonstrates the
    problem will make it a lot easier for people to help you (and may
    also lead you to the problem without external help).


    dave

    --
    Dave Vandervies dj3vande at eskimo dot com
    [D]etermining whether [the DS9k2 is] a conforming implementation is
    equivalent to solving the halting problem. --Keith Thompson
    (Hmm, I wonder if that's true for most real-world implementations .) in CLC

    Comment

    • Ben Bacarisse

      #3
      Re: translation from matlab to C object containing matrices undefined length of object

      adinda <adinda.vandenb erg@gmail.comwr ites:
      So what i need is this; (I'm very new at this,, programming in C I
      mean...)
      >
      In matlab I had a while loop, and after each loop was done I added my
      resulting matrix to an object. Seeing the loop is conditional to my
      results I do not know in advance how many loops I need. The matrices
      are Nx * Ny* Nz*6.
      >
      In C I put my matrices into 1D arrays.
      That is an off choice. With 4D arrays it leads to code that makes my
      head hurt just reading it. If, for some reason, you must do it this
      way, at least provide an access function so the code is readable.
      So I thought that if I use a
      counter for each time i do a loop and the calloc and realloc function
      that would work...But it doesnt seem to be working...Hope one of you
      can help me.
      That is a bit vague. Since I have no idea what it should do, it is
      very unlikely that I can help fix it. At least say what is going
      wrong.
      This is a sample text which i use to test my programs...
      >
      Vnulpoint is my result matrix for which i used pointers. So Varray is
      supposed to contain each of the Vnulpoint matrices...
      >
      #include <math.h>
      #include <stdio.h>
      #include <stdlib.h>
      #define nx 2
      #define ny 2
      #define nz 2
      I'd add one for the magic number 6 that appears in a few places.
      void max_array(doubl e *, int,int *,double *);
      int compare_doubles (double *, double *);
      void vindminnietnul( double *,double *,int,int* );
      void vindmaxnieteen( double *,double *,int ,int*);
      main()
      { int i,j,s,kl;
      int countergetal=6;
      double
      gradoud[nx*ny*nz*6]={5.569,6.9987, 0.00987,98.7743 ,1009.8876,0.00 09987};;
      double *gradpointeroud ,*gradpointerni euw;
      double gradnieuw[nx*ny*nz*6];
      double *Vnulpoint[nx*ny*nz];
      double *Vnulpoint2[nx*ny*nz];
      gradpointeroud= gradoud;
      gradpointernieu w=gradnieuw;
      int counter=1;
      double *Varray;
      Varray=calloc(1 *(nx*ny*nz*6),s izeof(double));
      while(counter<1 00)
      Head starting to hurt. Add spaces, please!

      Where you have calloc and realloc, you should check the return. In
      this short of program, I'd write small array allocate (and
      re-allocate) functions that just exit if the allocation fails.
      {
      for(i=0; i<nx; i++)
      { for(j=0; j<ny ; j++)
      { for(s=0; s<nz ; s++)
      {
      >
      >
      Vnulpoint[i+j*nx
      +s*nx*ny]=calloc(counter getal,sizeof(do uble));
      Vnulpoint2[i+j*nx
      +s*nx*ny]=calloc(counter getal,sizeof(do uble));
      for(kl=0;kl<cou ntergetal;kl++)
      {
      Hmm... a for inside a for inside a for inside for inside a while. I
      would try to find some way to decompose these loops into functions.
      *(Vnulpoint[i+j*nx+s*nx*ny]+kl)=0.12369*kl ;
      *(Vnulpoint[i+j*nx+s*nx*ny]+0)=1;
      >
      printf("%f\n ",*(Vnulpoi nt[i+j*nx+s*nx*ny]
      +kl));
      }
      int maxindex;
      double Vmax;
      max_array(Vnulp oint[i+j*nx+s*nx*ny],
      4,&maxindex,&Vm ax);
      printf("Het maximum volume is %f op locatie %d
      \n",Vmax,maxind ex);
      Vnulpoint2[i+j*nx+s*nx*ny]=Vnulpoint[i+j*nx
      +s*nx*ny];
      printf("Het toegewezen volume is %f \t %f \t %f
      \n",*(Vnulpoint 2[i+j*nx+s*nx*ny]),*(Vnulpoint2[i+j*nx+s*nx*ny]
      +1),*(Vnulpoint 2[i+j*nx+s*nx*ny]+2));
      >
      >
      int teller;
      double Vsort[countergetal];
      for(teller=0;te ller<counterget al;teller++)
      {
      Vsort[teller]=*(Vnulpoint[i+j*nx+s*nx*ny]+teller);
      }
      for(kl=0;kl<cou ntergetal;kl++)
      {
      printf("Vsort %d \t %f\n ",kl,Vsort[kl]);
      }
      >
      qsort(Vsort,cou ntergetal,sizeo f(double),(int( *)(const void*,const
      void*))compare_ doubles);
      You can loose the cast if you write compare function differently. See
      below.
      for(kl=0;kl<cou ntergetal;kl++)
      {
      printf("Vsort %d \t %f\n ",kl,Vsort[kl]);
      }
      >
      int indexmaxnieteen ;
      vindmaxnieteen( Vsort,Vnulpoint[i+j*nx
      +s*nx*ny],countergetal,& indexmaxnieteen );
      >
      >
      printf("De index is %d\n", indexmaxnieteen );
      Varray=(double
      *)realloc(Varra y,counter*(nx*n y*nz*6)*sizeof( double));
      You don't need the cast. Also, do you really need to realloc? It
      looks to me that know the final size at the start and you could just
      allocate once. Of course, I am a bit lost by now, so I may have that
      wrong.
      int tel;
      for (tel=0;tel<6;te l++)
      {
      Varray[(counter-1)*(i+j*nx+s*nx *ny+tel*nx*ny*n z)]=*(Vnulpoint[i
      +j*nx+s*nx*ny]+tel*nx*ny*nz);
      >
      }
      printf("Varray is %f \t %f \t %f \n %f \t %f \t %f \n en counter
      is %d \n", Varray[(counter-1)*(i+j*nx+s*nx *ny)],Varray[(counter-1)*(i
      +j*nx+s*nx*ny+1 )],Varray[(counter-1)*(i+j*nx+s*nx *ny
      +2)],Varray[(counter-1)*(i+j*nx+s*nx *ny+3)],Varray[(counter-1)*(i+j*nx
      +s*nx*ny+4)],Varray[(counter-1)*(i+j*nx+s*nx *ny
      +5)],Varray[(counter-1)*(i+j*nx+s*nx *ny+6)],counter);
      Head really sore now!
      gradpointernieu w=gradpointerou d;
      >
      }
      }
      }
      counter++;
      }
      >
      }
      >
      void max_array(doubl e *a, int num_elements,in t *indexptmax,dou ble
      *maxpt)
      {
      int i;
      >
      *indexptmax=0;
      *maxpt=a[0];
      >
      >
      >
      for (i=1; i<num_elements ; i++)
      {
      if (a[i]>*maxpt)
      {
      *maxpt=a[i];
      *indexptmax=i;
      }
      }
      >
      >
      return;
      }
      >
      int compare_doubles (double *x, double *y)
      {
      >
      if (*x *y)
      {
      return 1;
      }
      else
      {
      if (*x < *y)
      {
      return -1;
      }
      else
      {
      return 0;
      }
      }
      }
      I'd write:

      int compare_doubles (const void *xvp, const void *yvp)
      {
      const double *xp = xvp;
      const double *yp = yvp;
      return (*xp < *yp) ? -1 : (*xp *yp);
      }
      void vindminnietnul( double *V,double *Vl,int counternummer,i nt *index)
      { int nk,nn;
      >
      for(nk=0;nk<cou nternummer;nk++ )
      { if ( *(V+nk)!=0)
      You do this a lot. I think V[nk] is much clearer than *(V+nk).
      {for (nn=0;nn<counte rnummer;nn++)
      {
      if (*(Vl+nn-1)==*(V+nk))
      {
      *index=nn;
      }
      >
      }
      break;
      }
      >
      }
      >
      }
      I think I see now the reason for 1D arrays. You'd like to process
      them as above with a single loop rather than 4 nested for loops. I
      would not worry about that. It is almost always better to write the
      clearest code you can, and then fiddle with it if you need t speed it
      up. [You would get away with treating a 4D array as a 1D one (if it
      comes to that) on all the compilers you are every likely to meet.]
      void vindmaxnieteen( double *V,double *Vl,int counternummer,i nt*index)
      { int nk,nn;
      >
      for(nk=0;nk<cou nternummer;nk++ )
      { if ( *(V+(counternum mer-1)-nk)!=1)
      {for (nn=0;nn<counte rnummer;nn++)
      {
      if (*(Vl+nn)==*(V+ (counternummer-1)-nk))
      {
      *index=nn;
      }
      }
      break;
      }
      }
      return;
      }
      Serious question: why re-write in C? It is not the obvious choice for
      this sort of thing.

      --
      Ben.

      Comment

      • adinda

        #4
        Re: translation from matlab to C object containing matrices undefinedlength of object

        Well actually this is just a test file. In my actual main file i don't
        know my count until i've completed the loop (because my magnetic
        energy needs to agree with my constraints, and i don't know how many
        times I have to do the loop)
        I put my 4D arrays in 1D because my collegues do that too and then we
        can interchange code.
        The reason why i'm translating in C is because matlab is slow. and C
        is supposed to be faster (I hope)

        Thanks for the tips,

        Adinda

        Comment

        • Ben Bacarisse

          #5
          Re: translation from matlab to C object containing matrices undefined length of object

          It helps if you quote a bit of the reply you are commenting on.

          adinda <adinda.vandenb erg@gmail.comwr ites:
          Well actually this is just a test file. In my actual main file i don't
          know my count until i've completed the loop (because my magnetic
          energy needs to agree with my constraints, and i don't know how many
          times I have to do the loop)
          OK, so you probably need realloc.
          I put my 4D arrays in 1D because my collegues do that too and then we
          can interchange code.
          An excellent reason, though they should reconsider as well!
          The reason why i'm translating in C is because matlab is slow. and C
          is supposed to be faster (I hope)
          OK. I meant why C specifically. Fortran used to be the language of
          choice for such things but if your colleagues have chosen it, you must
          follow.
          Thanks for the tips,
          There are unlikely to be any more unless you can say (in way that is
          not domain specific) what was wrong with the program you posted.

          --
          Ben.

          Comment

          • Flash Gordon

            #6
            Re: translation from matlab to C object containing matrices undefinedlength of object

            Ben Bacarisse wrote, On 25/04/08 15:28:
            adinda <adinda.vandenb erg@gmail.comwr ites:
            <snip>
            > printf("Varray is %f \t %f \t %f \n %f \t %f \t %f \n en counter
            >is %d \n", Varray[(counter-1)*(i+j*nx+s*nx *ny)],Varray[(counter-1)*(i
            >+j*nx+s*nx*ny+ 1)],Varray[(counter-1)*(i+j*nx+s*nx *ny
            >+2)],Varray[(counter-1)*(i+j*nx+s*nx *ny+3)],Varray[(counter-1)*(i+j*nx
            >+s*nx*ny+4)],Varray[(counter-1)*(i+j*nx+s*nx *ny
            >+5)],Varray[(counter-1)*(i+j*nx+s*nx *ny+6)],counter);
            >
            Head really sore now!
            <snip>

            I'm not surprised. However, when poked gcc gives the following warning
            for this line...
            t.c:94: warning: format ‘%d’ expects type ‘int’, but argument 8 has type
            ‘double’
            So I suspect the OP has either too many parameters or too fe format
            specifiers.

            It also pointed out that Vsort is a variable sized array. In the full
            code this may or may not be appropriate, but as far as I can see in the
            example countergetal (which specifies the size of this array amongst
            other things) is set to 6 and never changed.
            --
            Flash Gordon

            Comment

            Working...