Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include "matrix_io.h"
  4.  
  5. int call_dgels(matrix_t * A, vector_t * b);
  6.  
  7. int main(int argc, char *argv[]) {
  8.  
  9. /* Number of input arguments: /.lwork argv[1] argv[2] argv[3] */
  10. if (argc != 4) {
  11. fprintf(stderr,"Usage: %s A b x\n", argv[0]);
  12. return EXIT_FAILURE;
  13. }
  14.  
  15. /* Read the text files */
  16. matrix_t * A = read_matrix(argv[1]);
  17. vector_t * b = read_vector(argv[2]);
  18.  
  19. /* Input checks: If pointers is NULL */
  20. if((A == NULL) || (b == NULL)){
  21. if(A == NULL){
  22. FILE_ERR(argv[1]);
  23. free(b);
  24. }
  25. else{
  26. FILE_ERR(argv[2]);
  27. free(A);
  28. }
  29. return EXIT_FAILURE;
  30. }
  31.  
  32. /* Using dgels() to solve LU and get info */
  33. int info = call_dgels(A,b);
  34.  
  35. /* Input checks: For invalid info values */
  36. switch(info){
  37. case -12:
  38. INPUT_ERR;
  39. free(A);
  40. free(b);
  41. return EXIT_FAILURE;
  42.  
  43. case -13:
  44. fprintf(stderr,"Matrix A is not a tall matrix, so m<n");
  45. free(A);
  46. free(b);
  47. return EXIT_FAILURE;
  48.  
  49. case -14:
  50. fprintf(stderr, "The dimensions of A and b are incompatible");
  51. free(A);
  52. free(b);
  53. return EXIT_FAILURE;
  54.  
  55. case -15:
  56. MEM_ERR;
  57. free(A);
  58. free(b);
  59. return EXIT_FAILURE;
  60. }
  61.  
  62. /* Input checks taken from doc of dgels_() */
  63. if(info != 0){
  64. if(info < 0){
  65. fprintf(stderr,"INFO = %d, the %d-th argument had an illegal value\n", info, info);
  66. }
  67. else{
  68. fprintf(stderr,"INFO = %d and because of that, A does not have full rank\n", info);
  69. }
  70. free(A);
  71. free(b);
  72. return EXIT_FAILURE;
  73. }
  74.  
  75. /* Allocation of x */
  76. vector_t * x = malloc_vector(A->n);
  77. if(x == NULL){
  78. MEM_ERR;
  79. free(A);
  80. free(b);
  81. return EXIT_FAILURE;
  82. }
  83.  
  84. /* Write values of b into x. Vector x should be n long */
  85. for(int i=0; i<A->n; i++){
  86. x->v[i] = b->v[i];
  87. }
  88.  
  89. /* Making the output file */
  90. if(write_vector(argv[3], x) != 0){
  91. fprintf(stderr,"%s: failed to write file %s\n",__func__,argv[3]);
  92. free(A);
  93. free(b);
  94. free(x);
  95. return EXIT_FAILURE;
  96. }
  97.  
  98. /* Free the memory */
  99. free(A);
  100. free(b);
  101. free(x);
  102.  
  103. return EXIT_SUCCESS;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement