numeric-linalg

Educational material on the SciPy implementation of numerical linear algebra algorithms

NameSizeMode
..
lapack/LAPACKE/example/example_DGESV_rowmajor.c 3990B -rw-r--r--
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
   LAPACKE_dgesv Example
   =====================

   The program computes the solution to the system of linear
   equations with a square matrix A and multiple
   right-hand sides B, where A is the coefficient matrix
   and b is the right-hand side matrix:

   Description
   ===========

   The routine solves for X the system of linear equations A*X = B,
   where A is an n-by-n matrix, the columns of matrix B are individual
   right-hand sides, and the columns of X are the corresponding
   solutions.

   The LU decomposition with partial pivoting and row interchanges is
   used to factor A as A = P*L*U, where P is a permutation matrix, L
   is unit lower triangular, and U is upper triangular. The factored
   form of A is then used to solve the system of equations A*X = B.

   LAPACKE Interface
   =================

   LAPACKE_dgesv (row-major, high-level) Example Program Results

  -- LAPACKE Example routine --
  -- LAPACK is a software package provided by Univ. of Tennessee,    --
  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <lapacke.h>
#include "lapacke_example_aux.h"

/* 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"); free(A); exit(0); }
        ipiv = (lapack_int *)malloc(n*sizeof(lapack_int)) ;
        if (ipiv==NULL){ printf("error of memory allocation\n"); free(A); free(b); 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(%" LAPACK_IFMT ",%" LAPACK_IFMT ") is zero, so that A is singular;\n", info, info );
            printf( "the solution could not be computed.\n" );
            free(A);
            free(b);
            free(ipiv);
            exit( 1 );
        }
        if (info <0) {
            free(A);
            free(b);
            free(ipiv);
            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 );

        /* Free matrices and vectors */
        free(A);
        free(b);
        free(ipiv);
        exit( 0 );
} /* End of LAPACKE_dgesv Example */