ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rmtxop.c
(Generate patch)

Comparing ray/src/util/rmtxop.c (file contents):
Revision 2.12 by greg, Mon Aug 27 23:03:05 2018 UTC vs.
Revision 2.14 by greg, Mon Aug 12 02:26:46 2019 UTC

# Line 13 | Line 13 | static const char RCSid[] = "$Id$";
13   #include "rmatrix.h"
14   #include "platform.h"
15  
16 < #define MAXCOMP         50              /* #components we support */
16 > #define MAXCOMP         16              /* #components we support */
17  
18 + static const char       stdin_name[] = "<stdin>";
19 +
20 + /* unary matrix operation(s) */
21   typedef struct {
22          double          sca[MAXCOMP];           /* scalar coefficients */
20        int             nsf;                    /* number of scalars */
23          double          cmat[MAXCOMP*MAXCOMP];  /* component transformation */
24 <        int             clen;                   /* number of coefficients */
25 <        int             transpose;              /* do transpose? */
26 <        int             op;                     /* '*' or '+' */
27 < } ROPERAT;                              /* matrix operation */
24 >        short           nsf;                    /* number of scalars */
25 >        short           clen;                   /* number of coefficients */
26 >        short           transpose;              /* do transpose? */
27 > } RUNARYOP;
28  
29 + /* matrix input source and requested operation(s) */
30 + typedef struct {
31 +        const char      *inspec;                /* input specification */
32 +        RUNARYOP        preop;                  /* unary operation(s) */
33 +        RMATRIX         *mtx;                   /* original matrix if loaded */
34 +        int             binop;                  /* binary op with next (or 0) */
35 + } ROPMAT;
36 +
37   int     verbose = 0;                    /* verbose reporting? */
38  
39 < static void
40 < op_default(ROPERAT *op)
39 > /* Load matrix */
40 > static int
41 > loadmatrix(ROPMAT *rop)
42   {
43 <        memset(op, 0, sizeof(ROPERAT));
44 <        op->op = '.';
43 >        if (rop->mtx != NULL)
44 >                return(0);
45 >
46 >        rop->mtx = rmx_load(rop->inspec == stdin_name ?
47 >                                (const char *)NULL : rop->inspec);
48 >        if (rop->mtx == NULL) {
49 >                fputs(rop->inspec, stderr);
50 >                fputs(": cannot load matrix\n", stderr);
51 >                return(-1);
52 >        }
53 >        return(1);
54   }
55  
56 + /* Get matrix and perform unary operations */
57   static RMATRIX *
58 < operate(RMATRIX *mleft, ROPERAT *op, const char *fname)
58 > loadop(ROPMAT *rop)
59   {
60 <        RMATRIX *mright = rmx_load(fname);
40 <        RMATRIX *mtmp;
60 >        RMATRIX *mres;
61          int     i;
62  
63 <        if (fname == NULL)
44 <                fname = "<stdin>";
45 <        if (mright == NULL) {
46 <                fputs(fname, stderr);
47 <                fputs(": cannot load matrix\n", stderr);
63 >        if (loadmatrix(rop) < 0)                /* make sure we're loaded */
64                  return(NULL);
65 <        }
66 <        if (op->nsf > 0) {              /* apply scalar(s) */
67 <                if (op->clen > 0) {
65 >
66 >        if (rop->preop.nsf > 0) {               /* apply scalar(s) */
67 >                if (rop->preop.clen > 0) {
68                          fputs("Options -s and -c are exclusive\n", stderr);
69 <                        rmx_free(mright);
54 <                        return(NULL);
69 >                        goto failure;
70                  }
71 <                if (op->nsf == 1) {
72 <                        for (i = mright->ncomp; --i; )
73 <                                op->sca[i] = op->sca[0];
74 <                } else if (op->nsf != mright->ncomp) {
71 >                if (rop->preop.nsf == 1) {
72 >                        for (i = rop->mtx->ncomp; --i; )
73 >                                rop->preop.sca[i] = rop->preop.sca[0];
74 >                } else if (rop->preop.nsf != rop->mtx->ncomp) {
75                          fprintf(stderr, "%s: -s must have one or %d factors\n",
76 <                                        fname, mright->ncomp);
77 <                        rmx_free(mright);
63 <                        return(NULL);
76 >                                        rop->inspec, rop->mtx->ncomp);
77 >                        goto failure;
78                  }
79 <                if ((mleft == NULL) | (op->op != '+') &&
80 <                                !rmx_scale(mright, op->sca)) {
67 <                        fputs(fname, stderr);
79 >                if (!rmx_scale(rop->mtx, rop->preop.sca)) {
80 >                        fputs(rop->inspec, stderr);
81                          fputs(": scalar operation failed\n", stderr);
82 <                        rmx_free(mright);
70 <                        return(NULL);
82 >                        goto failure;
83                  }
84                  if (verbose) {
85 <                        fputs(fname, stderr);
85 >                        fputs(rop->inspec, stderr);
86                          fputs(": applied scalar (", stderr);
87 <                        for (i = 0; i < op->nsf; i++)
88 <                                fprintf(stderr, " %f", op->sca[i]);
87 >                        for (i = 0; i < rop->preop.nsf; i++)
88 >                                fprintf(stderr, " %f", rop->preop.sca[i]);
89                          fputs(" )\n", stderr);
90                  }
91          }
92 <        if (op->clen > 0) {             /* apply transform */
93 <                if (op->clen % mright->ncomp) {
92 >        if (rop->preop.clen > 0) {      /* apply transform */
93 >                if (rop->preop.clen % rop->mtx->ncomp) {
94                          fprintf(stderr, "%s: -c must have N x %d coefficients\n",
95 <                                        fname, mright->ncomp);
96 <                        rmx_free(mright);
85 <                        return(NULL);
95 >                                        rop->inspec, rop->mtx->ncomp);
96 >                        goto failure;
97                  }
98 <                mtmp = rmx_transform(mright, op->clen/mright->ncomp, op->cmat);
99 <                if (mtmp == NULL) {
100 <                        fprintf(stderr, "%s: matrix transform failed\n", fname);
101 <                        rmx_free(mright);
102 <                        return(NULL);
98 >                mres = rmx_transform(rop->mtx, rop->preop.clen/rop->mtx->ncomp,
99 >                                        rop->preop.cmat);
100 >                if (mres == NULL) {
101 >                        fprintf(stderr, "%s: matrix transform failed\n",
102 >                                                rop->inspec);
103 >                        goto failure;
104                  }
105                  if (verbose)
106                          fprintf(stderr, "%s: applied %d x %d transform\n",
107 <                                        fname, mtmp->ncomp, mright->ncomp);
108 <                rmx_free(mright);
109 <                mright = mtmp;
107 >                                        rop->inspec, mres->ncomp,
108 >                                        rop->mtx->ncomp);
109 >                rmx_free(rop->mtx);
110 >                rop->mtx = mres;
111          }
112 <        if (op->transpose) {            /* transpose matrix? */
113 <                mtmp = rmx_transpose(mright);
114 <                if (mtmp == NULL) {
115 <                        fputs(fname, stderr);
112 >        if (rop->preop.transpose) {     /* transpose matrix? */
113 >                mres = rmx_transpose(rop->mtx);
114 >                if (mres == NULL) {
115 >                        fputs(rop->inspec, stderr);
116                          fputs(": transpose failed\n", stderr);
117 <                        rmx_free(mright);
105 <                        return(NULL);
117 >                        goto failure;
118                  }
119                  if (verbose) {
120 <                        fputs(fname, stderr);
120 >                        fputs(rop->inspec, stderr);
121                          fputs(": transposed rows and columns\n", stderr);
122                  }
123 <                rmx_free(mright);
124 <                mright = mtmp;
123 >                rmx_free(rop->mtx);
124 >                rop->mtx = mres;
125          }
126 <        if (mleft == NULL)              /* just one matrix */
127 <                return(mright);
128 <        if (op->op == '.') {            /* concatenate */
129 <                RMATRIX *mres = rmx_multiply(mleft, mright);
126 >        mres = rop->mtx;
127 >        rop->mtx = NULL;
128 >        return(mres);
129 > failure:
130 >        rmx_free(rop->mtx);
131 >        return(rop->mtx = NULL);
132 > }
133 >
134 > /* Execute binary operation, free matrix arguments and return new result */
135 > static RMATRIX *
136 > binaryop(const char *inspec, RMATRIX *mleft, int op, RMATRIX *mright)
137 > {
138 >        RMATRIX *mres = NULL;
139 >        int     i;
140 >
141 >        if ((mleft == NULL) | (mright == NULL))
142 >                return(NULL);
143 >
144 >        switch (op) {
145 >        case '.':                       /* concatenate */
146 >                mres = rmx_multiply(mleft, mright);
147 >                rmx_free(mleft);
148 >                rmx_free(mright);
149                  if (mres == NULL) {
150 <                        fputs(fname, stderr);
150 >                        fputs(inspec, stderr);
151                          if (mleft->ncols != mright->nrows)
152                                  fputs(": mismatched dimensions for multiply\n",
153                                                  stderr);
154                          else
155                                  fputs(": concatenation failed\n", stderr);
125                        rmx_free(mright);
156                          return(NULL);
157                  }
158                  if (verbose) {
159 <                        fputs(fname, stderr);
159 >                        fputs(inspec, stderr);
160                          fputs(": concatenated matrix\n", stderr);
161                  }
162 <                rmx_free(mright);
163 <                rmx_free(mleft);
164 <                mleft = mres;
165 <        } else if (op->op == '+') {
136 <                if (!rmx_sum(mleft, mright, op->nsf ? op->sca : (double *)NULL)) {
137 <                        fputs(fname, stderr);
162 >                break;
163 >        case '+':
164 >                if (!rmx_sum(mleft, mright, NULL)) {
165 >                        fputs(inspec, stderr);
166                          fputs(": matrix sum failed\n", stderr);
167 +                        rmx_free(mleft);
168                          rmx_free(mright);
169                          return(NULL);
170                  }
171                  if (verbose) {
172 <                        fputs(fname, stderr);
172 >                        fputs(inspec, stderr);
173                          fputs(": added in matrix\n", stderr);
174                  }
175                  rmx_free(mright);
176 <        } else if ((op->op == '*') | (op->op == '/')) {
177 <                const char *    tnam = (op->op == '/') ?
176 >                mres = mleft;
177 >                break;
178 >        case '*':
179 >        case '/': {
180 >                const char *    tnam = (op == '/') ?
181                                          "division" : "multiplication";
182                  errno = 0;
183 <                if (!rmx_elemult(mleft, mright, (op->op == '/'))) {
183 >                if (!rmx_elemult(mleft, mright, (op == '/'))) {
184                          fprintf(stderr, "%s: element-wise %s failed\n",
185 <                                        fname, tnam);
185 >                                        inspec, tnam);
186 >                        rmx_free(mleft);
187                          rmx_free(mright);
188                          return(NULL);
189                  }
190                  if (errno)
191                          fprintf(stderr,
192                                  "%s: warning - error during element-wise %s\n",
193 <                                        fname, tnam);
193 >                                        inspec, tnam);
194                  else if (verbose)
195 <                        fprintf(stderr, "%s: element-wise %s\n", fname, tnam);
195 >                        fprintf(stderr, "%s: element-wise %s\n", inspec, tnam);
196                  rmx_free(mright);
197 <        } else {
198 <                fprintf(stderr, "%s: unknown operation '%c'\n", fname, op->op);
197 >                mres = mleft;
198 >                } break;
199 >        default:
200 >                fprintf(stderr, "%s: unknown operation '%c'\n", inspec, op);
201 >                rmx_free(mleft);
202                  rmx_free(mright);
203                  return(NULL);
204          }
205 +        return(mres);
206 + }
207 +
208 + /* Perform matrix operations from left to right */
209 + static RMATRIX *
210 + op_left2right(ROPMAT *mop)
211 + {
212 +        RMATRIX *mleft = loadop(mop);
213 +
214 +        while (mop->binop) {
215 +                if (mleft == NULL)
216 +                        break;
217 +                mleft = binaryop(mop[1].inspec,
218 +                                mleft, mop->binop, loadop(mop+1));
219 +                mop++;
220 +        }
221          return(mleft);
222   }
223  
224 + /* Perform matrix operations from right to left */
225 + static RMATRIX *
226 + op_right2left(ROPMAT *mop)
227 + {
228 +        RMATRIX *mright;
229 +        int     rpos = 0;
230 +                                        /* find end of list */
231 +        while (mop[rpos].binop)
232 +                if (mop[rpos++].binop != '.') {
233 +                        fputs(
234 +                "Right-to-left evaluation only for matrix multiplication!\n",
235 +                                        stderr);
236 +                        return(NULL);
237 +                }
238 +        mright = loadop(mop+rpos);
239 +        while (rpos-- > 0) {
240 +                if (mright == NULL)
241 +                        break;
242 +                mright = binaryop(mop[rpos].inspec,
243 +                                loadop(mop+rpos), mop[rpos].binop, mright);
244 +        }
245 +        return(mright);
246 + }
247 +
248 + #define t_nrows(mop)    ((mop)->preop.transpose ? (mop)->mtx->ncols \
249 +                                                : (mop)->mtx->nrows)
250 + #define t_ncols(mop)    ((mop)->preop.transpose ? (mop)->mtx->nrows \
251 +                                                : (mop)->mtx->ncols)
252 +
253 + /* Should we prefer concatenating from rightmost matrix towards left? */
254   static int
255 + prefer_right2left(ROPMAT *mop)
256 + {
257 +        int     mri = 0;
258 +
259 +        while (mop[mri].binop)          /* find rightmost matrix */
260 +                if (mop[mri++].binop != '.')
261 +                        return(0);      /* pre-empt reversal for other ops */
262 +
263 +        if (mri <= 1)
264 +                return(0);              /* won't matter */
265 +
266 +        if (loadmatrix(mop+mri) < 0)    /* load rightmost cat */
267 +                return(1);              /* fail will bail in a moment */
268 +
269 +        if (t_ncols(mop+mri) == 1)
270 +                return(1);              /* definitely better R->L */
271 +
272 +        if (t_ncols(mop+mri) >= t_nrows(mop+mri))
273 +                return(0);              /* ...probably worse */
274 +
275 +        if (loadmatrix(mop) < 0)        /* load leftmost */
276 +                return(0);              /* fail will bail in a moment */
277 +
278 +        return(t_ncols(mop+mri) < t_nrows(mop));
279 + }
280 +
281 + static int
282   get_factors(double da[], int n, char *av[])
283   {
284          int     ac;
# Line 179 | Line 288 | get_factors(double da[], int n, char *av[])
288          return(ac);
289   }
290  
291 + static ROPMAT *
292 + grow_moparray(ROPMAT *mop, int n2alloc)
293 + {
294 +        int     nmats = 0;
295 +
296 +        while (mop[nmats++].binop)
297 +                ;
298 +        mop = (ROPMAT *)realloc(mop, n2alloc*sizeof(ROPMAT));
299 +        if (mop == NULL) {
300 +                fputs("Out of memory in grow_moparray()\n", stderr);
301 +                exit(1);
302 +        }
303 +        if (n2alloc > nmats)
304 +                memset(mop+nmats, 0, (n2alloc-nmats)*sizeof(ROPMAT));
305 +        return(mop);
306 + }
307 +
308   /* Load one or more matrices and operate on them, sending results to stdout */
309   int
310   main(int argc, char *argv[])
311   {
312          int     outfmt = DTfromHeader;
313 +        int     nall = 2;
314 +        ROPMAT  *mop = (ROPMAT *)calloc(nall, sizeof(ROPMAT));
315 +        int     nmats = 0;
316          RMATRIX *mres = NULL;
317 <        ROPERAT op;
317 >        int     stdin_used = 0;
318          int     i;
190                                        /* initialize */
191        op_default(&op);
319                                          /* get options and arguments */
320 <        for (i = 1; i < argc; i++)
320 >        for (i = 1; i < argc; i++) {
321                  if (argv[i][0] && !argv[i][1] &&
322 <                                strchr("+*/", argv[i][0]) != NULL) {
323 <                        op.op = argv[i][0];
324 <                } else if (argv[i][0] != '-' || !argv[i][1]) {
325 <                        char    *fname = NULL;  /* load matrix */
326 <                        if (argv[i][0] != '-')
327 <                                fname = argv[i];
201 <                        mres = operate(mres, &op, fname);
202 <                        if (mres == NULL) {
203 <                                fprintf(stderr, "%s: operation failed on '%s'\n",
204 <                                                argv[0], argv[i]);
205 <                                return(0);
322 >                                strchr(".+*/", argv[i][0]) != NULL) {
323 >                        if (mop[nmats].inspec == NULL || mop[nmats].binop) {
324 >                                fprintf(stderr,
325 >                        "%s: missing matrix argument for '%c' operation\n",
326 >                                                argv[0], argv[i][0]);
327 >                                return(1);
328                          }
329 <                        op_default(&op);        /* reset operator */
329 >                        mop[nmats++].binop = argv[i][0];
330 >                } else if (argv[i][0] != '-' || !argv[i][1]) {
331 >                        if (argv[i][0] == '-') {
332 >                                if (stdin_used++) {
333 >                                        fprintf(stderr,
334 >                        "%s: standard input used for more than one matrix\n",
335 >                                                argv[0]);
336 >                                        return(1);
337 >                                }
338 >                                mop[nmats].inspec = stdin_name;
339 >                        } else
340 >                                mop[nmats].inspec = argv[i];
341 >                        if (nmats > 0 && !mop[nmats-1].binop)
342 >                                mop[nmats-1].binop = '.';
343 >                        nmats++;
344                  } else {
345                          int     n = argc-1 - i;
346                          switch (argv[i][1]) {   /* get option */
347                          case 'v':
348 <                                verbose = !verbose;
348 >                                verbose++;
349                                  break;
350                          case 'f':
351                                  switch (argv[i][2]) {
# Line 230 | Line 366 | main(int argc, char *argv[])
366                                  }
367                                  break;
368                          case 't':
369 <                                op.transpose = 1;
369 >                                mop[nmats].preop.transpose = 1;
370                                  break;
371                          case 's':
372                                  if (n > MAXCOMP) n = MAXCOMP;
373 <                                op.nsf = get_factors(op.sca, n, argv+i+1);
374 <                                i += op.nsf;
373 >                                i += mop[nmats].preop.nsf =
374 >                                        get_factors(mop[nmats].preop.sca,
375 >                                                        n, argv+i+1);
376                                  break;
377                          case 'c':
378                                  if (n > MAXCOMP*MAXCOMP) n = MAXCOMP*MAXCOMP;
379 <                                op.clen = get_factors(op.cmat, n, argv+i+1);
380 <                                i += op.clen;
379 >                                i += mop[nmats].preop.clen =
380 >                                        get_factors(mop[nmats].preop.cmat,
381 >                                                        n, argv+i+1);
382                                  break;
383                          default:
384                                  fprintf(stderr, "%s: unknown operation '%s'\n",
# Line 248 | Line 386 | main(int argc, char *argv[])
386                                  goto userr;
387                          }
388                  }
389 <        if (mres == NULL)               /* check that we got something */
389 >                if (nmats >= nall)
390 >                        mop = grow_moparray(mop, nall += 2);
391 >        }
392 >        if (mop[0].inspec == NULL)      /* nothing to do? */
393                  goto userr;
394 +                                        /* favor quicker concatenation */
395 +        mop[nmats].mtx = prefer_right2left(mop) ? op_right2left(mop)
396 +                                                : op_left2right(mop);
397 +        if (mop[nmats].mtx == NULL)
398 +                return(1);
399 +                                        /* apply trailing unary operations */
400 +        mop[nmats].inspec = "trailing_ops";
401 +        mres = loadop(mop+nmats);
402 +        if (mres == NULL)
403 +                return(1);
404                                          /* write result to stdout */
405          if (outfmt == DTfromHeader)
406                  outfmt = mres->dtype;
# Line 261 | Line 412 | main(int argc, char *argv[])
412                  fprintf(stderr, "%s: error writing result matrix\n", argv[0]);
413                  return(1);
414          }
415 <        /* rmx_free(mres); mres = NULL; */
415 >        /* rmx_free(mres); free(mop); */
416          return(0);
417   userr:
418          fprintf(stderr,
419 <        "Usage: %s [-v][-f[adfc][-t][-s sf .. | -c ce ..] m1 [+*/] .. > mres\n",
419 >        "Usage: %s [-v][-f[adfc][-t][-s sf .. | -c ce ..] m1 [.+*/] .. > mres\n",
420                          argv[0]);
421          return(1);
422   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines