예제 #1
0
/* Main program */
int main (int argc, const char * argv[])
{
   /* Locals */
   double A[5][3] = {1,1,1,2,3,4,3,5,2,4,2,5,5,4,3};
   double b[5][2] = {-10,-3,12,14,14,12,16,16,18,16};
   lapack_int info,m,n,lda,ldb,nrhs;
   int i,j;

   /* Initialization */
   m = 5;
   n = 3;
   nrhs = 2;
   lda = 3;
   ldb = 2;

   /* Print Entry Matrix */
   print_matrix_rowmajor( "Entry Matrix A", m, n, *A, lda );
   /* Print Right Rand Side */
   print_matrix_rowmajor( "Right Hand Side b", n, nrhs, *b, ldb );
   printf( "\n" );
   
   /* Executable statements */
   printf( "LAPACKE_dgels (row-major, high-level) Example Program Results\n" );
   /* Solve least squares problem*/
   info = LAPACKE_dgels(LAPACK_ROW_MAJOR,'N',m,n,nrhs,*A,lda,*b,ldb);

   /* Print Solution */
   print_matrix_rowmajor( "Solution", n, nrhs, *b, ldb );
   printf( "\n" );
   exit( 0 );
} /* End of LAPACKE_dgels Example */
예제 #2
0
/* Main program */
int main(int argc, char **argv) {

        /* Locals */
        lapack_int n, nrhs, lda, ldb, info;
		int i, j;
        /* Local arrays */
		double *A, *b;
		lapack_int *ipiv;
		
        /* Default Value */
	    n = 5; nrhs = 1;

        /* Arguments */
	    for( i = 1; i < argc; i++ ) {
	    	if( strcmp( argv[i], "-n" ) == 0 ) { 
		    	n  = atoi(argv[i+1]);
			    i++;
		    }
			if( strcmp( argv[i], "-nrhs" ) == 0 ) { 
				nrhs  = atoi(argv[i+1]);
				i++;
			} 
		}
		
        /* Initialization */
        lda=n, ldb=nrhs;
		A = (double *)malloc(n*n*sizeof(double)) ;
		if (A==NULL){ printf("error of memory allocation\n"); exit(0); }
		b = (double *)malloc(n*nrhs*sizeof(double)) ;
		if (b==NULL){ printf("error of memory allocation\n"); exit(0); }
		ipiv = (lapack_int *)malloc(n*sizeof(lapack_int)) ;
		if (ipiv==NULL){ printf("error of memory allocation\n"); exit(0); }

        for( i = 0; i < n; i++ ) {
                for( j = 0; j < n; j++ ) A[i*lda+j] = ((double) rand()) / ((double) RAND_MAX) - 0.5;
		}

		for(i=0;i<n*nrhs;i++)
			b[i] = ((double) rand()) / ((double) RAND_MAX) - 0.5;

        /* Print Entry Matrix */
        print_matrix_rowmajor( "Entry Matrix A", n, n, A, lda );
        /* Print Right Rand Side */
        print_matrix_rowmajor( "Right Rand Side b", n, nrhs, b, ldb );
        printf( "\n" );
        /* Executable statements */
        printf( "LAPACKE_dgesv (row-major, high-level) Example Program Results\n" );
        /* Solve the equations A*X = B */
        info = LAPACKE_dgesv( LAPACK_ROW_MAJOR, n, nrhs, A, lda, ipiv,
                        b, ldb );
        /* Check for the exact singularity */
        if( info > 0 ) {
                printf( "The diagonal element of the triangular factor of A,\n" );
                printf( "U(%i,%i) is zero, so that A is singular;\n", info, info );
                printf( "the solution could not be computed.\n" );
                exit( 1 );
        }
        if (info <0) exit( 1 );
        /* Print solution */
        print_matrix_rowmajor( "Solution", n, nrhs, b, ldb );
        /* Print details of LU factorization */
        print_matrix_rowmajor( "Details of LU factorization", n, n, A, lda );
        /* Print pivot indices */
        print_vector( "Pivot indices", n, ipiv );
        exit( 0 );
} /* End of LAPACKE_dgesv Example */