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.13 by greg, Mon Aug 12 01:20:26 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 +                rpos++;
233 +        mright = loadop(mop+rpos);
234 +        while (rpos-- > 0) {
235 +                if (mright == NULL)
236 +                        break;
237 +                mright = binaryop(mop[rpos].inspec,
238 +                                loadop(mop+rpos), mop[rpos].binop, mright);
239 +        }
240 +        return(mright);
241 + }
242 +
243 + #define t_nrows(mop)    ((mop)->preop.transpose ? (mop)->mtx->ncols \
244 +                                                : (mop)->mtx->nrows)
245 + #define t_ncols(mop)    ((mop)->preop.transpose ? (mop)->mtx->nrows \
246 +                                                : (mop)->mtx->ncols)
247 +
248 + /* Should we prefer concatenating from rightmost matrix towards left? */
249   static int
250 + prefer_right2left(ROPMAT *mop)
251 + {
252 +        int     mri = 0;
253 +
254 +        while (mop[mri].binop)          /* find rightmost matrix */
255 +                if (mop[mri++].binop != '.')
256 +                        return(0);      /* pre-empt reversal for other ops */
257 +
258 +        if (mri <= 1)
259 +                return(0);              /* won't matter */
260 +
261 +        if (loadmatrix(mop+mri) < 0)    /* load rightmost cat */
262 +                return(1);              /* fail will bail in a moment */
263 +
264 +        if (t_ncols(mop+mri) == 1)
265 +                return(1);              /* definitely better R->L */
266 +
267 +        if (t_ncols(mop+mri) >= t_nrows(mop+mri))
268 +                return(0);              /* ...probably worse */
269 +
270 +        if (loadmatrix(mop) < 0)        /* load leftmost */
271 +                return(0);              /* fail will bail in a moment */
272 +
273 +        return(t_ncols(mop+mri) < t_nrows(mop));
274 + }
275 +
276 + static int
277   get_factors(double da[], int n, char *av[])
278   {
279          int     ac;
# Line 179 | Line 283 | get_factors(double da[], int n, char *av[])
283          return(ac);
284   }
285  
286 + static ROPMAT *
287 + grow_moparray(ROPMAT *mop, int n2alloc)
288 + {
289 +        int     nmats = 0;
290 +
291 +        while (mop[nmats++].binop)
292 +                ;
293 +        mop = (ROPMAT *)realloc(mop, n2alloc*sizeof(ROPMAT));
294 +        if (mop == NULL) {
295 +                fputs("Out of memory in grow_moparray()\n", stderr);
296 +                exit(1);
297 +        }
298 +        if (n2alloc > nmats)
299 +                memset(mop+nmats, 0, (n2alloc-nmats)*sizeof(ROPMAT));
300 +        return(mop);
301 + }
302 +
303   /* Load one or more matrices and operate on them, sending results to stdout */
304   int
305   main(int argc, char *argv[])
306   {
307          int     outfmt = DTfromHeader;
308 +        int     nall = 2;
309 +        ROPMAT  *mop = (ROPMAT *)calloc(nall, sizeof(ROPMAT));
310 +        int     nmats = 0;
311          RMATRIX *mres = NULL;
312 <        ROPERAT op;
312 >        int     stdin_used = 0;
313          int     i;
190                                        /* initialize */
191        op_default(&op);
314                                          /* get options and arguments */
315 <        for (i = 1; i < argc; i++)
315 >        for (i = 1; i < argc; i++) {
316 >                if (nmats >= nall)
317 >                        mop = grow_moparray(mop, nall += 2);
318                  if (argv[i][0] && !argv[i][1] &&
319 <                                strchr("+*/", argv[i][0]) != NULL) {
320 <                        op.op = argv[i][0];
321 <                } else if (argv[i][0] != '-' || !argv[i][1]) {
322 <                        char    *fname = NULL;  /* load matrix */
323 <                        if (argv[i][0] != '-')
200 <                                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);
319 >                                strchr(".+*/", argv[i][0]) != NULL) {
320 >                        if (mop[nmats].inspec == NULL || mop[nmats].binop) {
321 >                                fprintf(stderr, "%s: missing matrix argument\n",
322 >                                                argv[0]);
323 >                                return(1);
324                          }
325 <                        op_default(&op);        /* reset operator */
325 >                        mop[nmats++].binop = argv[i][0];
326 >                } else if (argv[i][0] != '-' || !argv[i][1]) {
327 >                        if (argv[i][0] == '-') {
328 >                                if (stdin_used++) {
329 >                                        fprintf(stderr,
330 >                        "%s: standard input used for more than one matrix\n",
331 >                                                argv[0]);
332 >                                        return(1);
333 >                                }
334 >                                mop[nmats].inspec = stdin_name;
335 >                        } else
336 >                                mop[nmats].inspec = argv[i];
337 >                        if (nmats > 0 && !mop[nmats-1].binop)
338 >                                mop[nmats-1].binop = '.';
339 >                        nmats++;
340                  } else {
341                          int     n = argc-1 - i;
342                          switch (argv[i][1]) {   /* get option */
# Line 230 | Line 362 | main(int argc, char *argv[])
362                                  }
363                                  break;
364                          case 't':
365 <                                op.transpose = 1;
365 >                                mop[nmats].preop.transpose = 1;
366                                  break;
367                          case 's':
368                                  if (n > MAXCOMP) n = MAXCOMP;
369 <                                op.nsf = get_factors(op.sca, n, argv+i+1);
370 <                                i += op.nsf;
369 >                                i += mop[nmats].preop.nsf =
370 >                                        get_factors(mop[nmats].preop.sca,
371 >                                                        n, argv+i+1);
372                                  break;
373                          case 'c':
374                                  if (n > MAXCOMP*MAXCOMP) n = MAXCOMP*MAXCOMP;
375 <                                op.clen = get_factors(op.cmat, n, argv+i+1);
376 <                                i += op.clen;
375 >                                i += mop[nmats].preop.clen =
376 >                                        get_factors(mop[nmats].preop.cmat,
377 >                                                        n, argv+i+1);
378                                  break;
379                          default:
380                                  fprintf(stderr, "%s: unknown operation '%s'\n",
# Line 248 | Line 382 | main(int argc, char *argv[])
382                                  goto userr;
383                          }
384                  }
385 <        if (mres == NULL)               /* check that we got something */
385 >        }
386 >        if (mop[0].inspec == NULL)      /* nothing to do? */
387                  goto userr;
388 +                                        /* favor quicker concatenation */
389 +        mres = prefer_right2left(mop) ? op_right2left(mop) : op_left2right(mop);
390 +        if (!mres)
391 +                return(1);
392                                          /* write result to stdout */
393          if (outfmt == DTfromHeader)
394                  outfmt = mres->dtype;
# Line 261 | Line 400 | main(int argc, char *argv[])
400                  fprintf(stderr, "%s: error writing result matrix\n", argv[0]);
401                  return(1);
402          }
403 <        /* rmx_free(mres); mres = NULL; */
403 >        /* rmx_free(mres); free(mop); */
404          return(0);
405   userr:
406          fprintf(stderr,
407 <        "Usage: %s [-v][-f[adfc][-t][-s sf .. | -c ce ..] m1 [+*/] .. > mres\n",
407 >        "Usage: %s [-v][-f[adfc][-t][-s sf .. | -c ce ..] m1 [.+*/] .. > mres\n",
408                          argv[0]);
409          return(1);
410   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines