User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 456,556 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,441 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 1564 | Replies: 4
Reply
Join Date: Oct 2007
Posts: 6
Reputation: phylon is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
phylon phylon is offline Offline
Newbie Poster

reading in from vector to a two dimensional array

  #1  
Oct 18th, 2007
this is a small example from the gsl example online
I need to get the value from the "&evec_i.vector " into a 4x4 dimensional array
such that
abc[4][4]= vector elements

how can I do this ? those 16 values will create the 4x4 matrix
#include <stdio.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_eigen.h>

int main (void)
{
  double data[] = { 1.0  , 1/2.0, 1/3.0, 1/4.0,
                    1/2.0, 1/3.0, 1/4.0, 1/5.0,
                    1/3.0, 1/4.0, 1/5.0, 1/6.0,
                    1/4.0, 1/5.0, 1/6.0, 1/7.0 };

  gsl_matrix_view m  = gsl_matrix_view_array(data, 4, 4);

  gsl_vector *eval = gsl_vector_alloc (4);
  gsl_matrix *evec = gsl_matrix_alloc (4, 4);

  gsl_eigen_symmv_workspace * w =    gsl_eigen_symmv_alloc (4);
  
  gsl_eigen_symmv (&m.matrix, eval, evec, w);

  gsl_eigen_symmv_free(w);

  gsl_eigen_symmv_sort (eval, evec, GSL_EIGEN_SORT_ABS_ASC);
  
  {
    int i;

    for (i = 0; i < 4; i++)
      {
        double eval_i   = gsl_vector_get(eval, i);
        gsl_vector_view evec_i   = gsl_matrix_column(evec, i);

        printf("eigenvalue = %g\n", eval_i);
        printf("eigenvector = \n");
        gsl_vector_fprintf(stdout, &evec_i.vector, "%g");
      }
  }

  return 0;
}
Last edited by Ancient Dragon : Oct 18th, 2007 at 8:31 am. Reason: add code tags
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: reading in from vector to a two dimensional array

  #2  
Oct 18th, 2007
I don't really know, but did you try the obvious ?
for(i = 0; i < 4; ++i)
{
   for(j = 0; j < 4; ++j)
   {
        data[i][j] = gsl_matrix_column(i, j);
   }
}
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Oct 2007
Posts: 6
Reputation: phylon is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
phylon phylon is offline Offline
Newbie Poster

Re: reading in from vector to a two dimensional array

  #3  
Oct 18th, 2007
Originally Posted by Ancient Dragon View Post
I don't really know, but did you try the obvious ?
for(i = 0; i < 4; ++i)
{
   for(j = 0; j < 4; ++j)
   {
        data[i][j] = gsl_matrix_column(i, j);
   }
}


It doesnt work, since the function takes argument as below
gsl_matrix_column (gsl_matrix * m, size_t j)
http://www.gnu.org/software/gsl/manu...005fcolumn-947

I got the value of eigenvalue pretty easily but I am having trouble with the eigenvector
Reply With Quote  
Join Date: Oct 2007
Posts: 6
Reputation: phylon is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
phylon phylon is offline Offline
Newbie Poster

Re: reading in from vector to a two dimensional array

  #4  
Oct 18th, 2007
Ancient Dragon:

I did a workaround the problem by printing the output of the vector to a file and then reading them into an array
sure doesnt look impressive :<

#include <stdio.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_eigen.h>

// trying to take the values of eigenvector and eigenvalues and put it in a array
     
     int main (void)
     {


  FILE *data3;

		data3 = fopen("data3.txt", "w");

		   if (data3 == NULL) 
		   {
		      printf("File doesn't exist\n");
		      exit (EXIT_FAILURE);
		   } 	

       double data[] = { 1.0  , 1/2.0, 1/3.0, 1/4.0,
                         1/2.0, 1/3.0, 1/4.0, 1/5.0,
                         1/3.0, 1/4.0, 1/5.0, 1/6.0,
                         1/4.0, 1/5.0, 1/6.0, 1/7.0 };
	double datax[4][4];
        double datay[16];
 

       gsl_matrix_view m  = gsl_matrix_view_array (data, 4, 4);
     
       gsl_vector *eval = gsl_vector_alloc (4);
       gsl_matrix *evec = gsl_matrix_alloc (4, 4);
     
       gsl_eigen_symmv_workspace * w =  gsl_eigen_symmv_alloc (4);
       
       gsl_eigen_symmv (&m.matrix, eval, evec, w);
     
       gsl_eigen_symmv_free (w);
     
//             gsl_vector_fprintf (stdout, evec.vector, "%g");

         int i,j,p=0;
     
         for (i = 0; i < 4; i++)
           {
             double eval_i = gsl_vector_get (eval, i);
             gsl_vector_view evec_i = gsl_matrix_column (evec, i);
     
             printf ("eigenvalue = %g\n", eval_i);
             printf ("eigenvector = \n");
		
             gsl_vector_fprintf (data3, &evec_i.vector, "%g");
           }

	
       gsl_vector_free (eval);
       gsl_matrix_free (evec);
            fclose(data3);

FILE *fr;           

   int n;
   char line[80];
   float abc[10][10];
 //  int i=0,j=0;
  
   fr = fopen ("data3.txt", "r");  
   
for(i = 0; i < 4; ++i)
{
   for(j = 0; j < 4; ++j)
   {
	fgets(line, 80, fr);
         sscanf (line, "%f", &abc[i][j]);
	printf ("\n%f", abc[i][j]);	
   }
}
    printf("\n\n"); 
	
for(i = 0; i < 4; ++i)
{
   for(j = 0; j < 4; ++j)
   {
	printf ("%f ", abc[i][j]);	
   }
        printf("\n"); 
}


   fclose(fr); 

     
       return 0;
     }

Reply With Quote  
Join Date: Nov 2005
Location: Canada
Posts: 236
Reputation: dwks will become famous soon enough dwks will become famous soon enough 
Rep Power: 4
Solved Threads: 21
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

Re: reading in from vector to a two dimensional array

  #5  
Oct 20th, 2007
You're looking in the wrong section of the manual. You don't want to create a vector view. Check out this section: http://www.gnu.org/software/gsl/manu...-elements.html

I think that this is what you seek:
double gsl_matrix_get (const gsl_matrix * m, size_t i, size_t j)

This function returns the (i,j)-th element of a matrix m. If i or j lie outside the allowed range of 0 to n1-1 and 0 to n2-1 then the error handler is invoked and 0 is returned. An inline version of this function is used when HAVE_INLINE is defined.
dwk

Seek and ye shall find.

"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.

"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison

"The only real mistake is the one from which we learn nothing."
-- John Powell
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 5:34 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC