Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include "matrix_io.h"
- int call_dgels(matrix_t * A, vector_t * b);
- int main(int argc, char *argv[]) {
- /* Number of input arguments: /.lwork argv[1] argv[2] argv[3] */
- if (argc != 4) {
- fprintf(stderr,"Usage: %s A b x\n", argv[0]);
- return EXIT_FAILURE;
- }
- /* Read the text files */
- matrix_t * A = read_matrix(argv[1]);
- vector_t * b = read_vector(argv[2]);
- /* Input checks: If pointers is NULL */
- if((A == NULL) || (b == NULL)){
- if(A == NULL){
- FILE_ERR(argv[1]);
- free(b);
- }
- else{
- FILE_ERR(argv[2]);
- free(A);
- }
- return EXIT_FAILURE;
- }
- /* Using dgels() to solve LU and get info */
- int info = call_dgels(A,b);
- /* Input checks: For invalid info values */
- switch(info){
- case -12:
- INPUT_ERR;
- free(A);
- free(b);
- return EXIT_FAILURE;
- case -13:
- fprintf(stderr,"Matrix A is not a tall matrix, so m<n");
- free(A);
- free(b);
- return EXIT_FAILURE;
- case -14:
- fprintf(stderr, "The dimensions of A and b are incompatible");
- free(A);
- free(b);
- return EXIT_FAILURE;
- case -15:
- MEM_ERR;
- free(A);
- free(b);
- return EXIT_FAILURE;
- }
- /* Input checks taken from doc of dgels_() */
- if(info != 0){
- if(info < 0){
- fprintf(stderr,"INFO = %d, the %d-th argument had an illegal value\n", info, info);
- }
- else{
- fprintf(stderr,"INFO = %d and because of that, A does not have full rank\n", info);
- }
- free(A);
- free(b);
- return EXIT_FAILURE;
- }
- /* Allocation of x */
- vector_t * x = malloc_vector(A->n);
- if(x == NULL){
- MEM_ERR;
- free(A);
- free(b);
- return EXIT_FAILURE;
- }
- /* Write values of b into x. Vector x should be n long */
- for(int i=0; i<A->n; i++){
- x->v[i] = b->v[i];
- }
- /* Making the output file */
- if(write_vector(argv[3], x) != 0){
- fprintf(stderr,"%s: failed to write file %s\n",__func__,argv[3]);
- free(A);
- free(b);
- free(x);
- return EXIT_FAILURE;
- }
- /* Free the memory */
- free(A);
- free(b);
- free(x);
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement