summaryrefslogtreecommitdiff
path: root/nnls_solver.c
blob: a8e67980931eaa80ecbd93e0b97ac8e097be4ddf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <ctype.h>
#include <errno.h>
#include <getopt.h>
#include <math.h>
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "nnls.h"

#define USAGE "." 

int load_a_matrix(char *filename) {

  long x = 0;
  long y = 0;
  char *line = NULL;
  size_t len = 0;

  fh = fopen(filename, "r");
  if(fh == NULL) {
    fprintf(stderr, "Could not open %s\n", filename);
    exit(EXIT_FAILURE);
  }
    
  if(getline(&line, &len, fp) != -1) {

  }

  while ((read = getline(&line, &len, fp)) != -1) {

  }


}
int main(int argc, char **argv) {


  int c;

  char *a_matrix_filename = NULL;
  char *b_matrix_filename = NULL;
  char *output_filename = NULL;
  char *format = NULL;

  long x = 0;
  long y = 0;

  int verbose = 0;

  long width = 0;
  long height = 0;

  while (1) {
    static struct option long_options[] = {
      {"a", required_argument, 0, 'a'},
      {"b", required_argument, 0, 'b'},
      {"output", required_argument, 0, 'o'},
      {"verbose", no_argument, 0, 'v'},
      {"format", required_argument, 0, 'f'},
      {"version", no_argument, 0, 'V'},
      {0, 0, 0, 0}
    };

    int option_index = 0;

    c = getopt_long (argc, argv, "i:o:hdvV", long_options, &option_index);

    if (c == -1)
      break;

    switch (c) {
      case 0:
      case 'a':
        a_matrix_filename = optarg;
        break;
      case 'b':
        b_matrix_filename = optarg;
        break;
      case 'o':
        output_filename = optarg;
        break;
      case 'f':
        format = optarg;
        break;
      case 'v':
        verbose = 1;
        break;
      case 'V':
        printf("%s\n", VERSION);
        exit(EXIT_SUCCESS);
        break;
      case 'h':
        printf("%s\n", USAGE);
        exit(EXIT_SUCCESS);
        break;
      default:
        break;
    }
  }


  if(a_matrix_filename == NULL) {
    fprintf(stderr, "Error: a matrix file (-a) must be specified\n\n");
    fprintf(stderr, "%s\n", USAGE);
    exit(EXIT_FAILURE);
  }

  if(b_matrix_filename == NULL) {
    fprintf(stderr, "Error: b matrix file (-b) must be specified\n\n");
    fprintf(stderr, "%s\n", USAGE);
    exit(EXIT_FAILURE);
  }

  if(output_filename == NULL) {
    fprintf(stderr, "Error: output filename (-o) must be specified\n\n");
    fprintf(stderr, "%s\n", USAGE);
    exit(EXIT_FAILURE);
  }

  if(format == NULL) {
    format = "%10lf";
  }

  if(verbose) { 
    printf("a matrix: %s\n", a_matrix_filename);
    printf("b matrix: %s\n", a_matrix_filename);
    printf("output file: %s\n", output_filename);
  }

  if(access (a_matrix_filename, F_OK) == -1) {
    fprintf(stderr, "Error: could not open %s\n", a_matrix_filename);
    exit(EXIT_FAILURE);
  }

  if(access (b_matrix_filename, F_OK) == -1) {
    fprintf(stderr, "Error: could not open %s\n", b_matrix_filename);
    exit(EXIT_FAILURE);
  }

  // load our matricies
  double *a_matrix = load_a_matrix(a_matrix_filename);
  double *b_matrix = load_b_matrix(b_matrix_filename);

  // run NNLS
  double *solution = nnls(a_matrix, b_matrix, height, width);

  // print to stdout if the user supplies "-" as the output argument
  FILE *output_fh = NULL;
  if(strcmp(output_filename, "-") == 0) {
    output_fh = stdout;
  }
  else {
    output_fh = fopen(output_filename, "w");
  }

  if(output_fh == NULL) { 
    fprintf(stderr, "Could not open %s for writing\n", output_filename);
    exit(EXIT_FAILURE);
  }
  for(x = 0; x < height; x++) {
      fprintf(output_fh, format, solution[x]);
  }
  fclose(output_fh);

  return EXIT_SUCCESS;
}