File: yeti_cost.c

package info (click to toggle)
yorick-yeti 6.2.2-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,708 kB
  • ctags: 1,606
  • sloc: ansic: 17,054; makefile: 338; sh: 9
file content (439 lines) | stat: -rw-r--r-- 9,493 bytes parent folder | download
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
 * yeti_cost.c --
 *
 *	Implement various cost functions for solving inverse problems
 *	in Yorick.
 *
 *-----------------------------------------------------------------------------
 *
 *	Copyright (C) 1999-2006 Eric Thibaut.
 *
 *	This file is part of Yeti.
 *
 *	Yeti is  free software;  you can redistribute  it and/or  modify it
 *	under  the terms of  the GNU  General Public  License version  2 as
 *	published by the Free Software Foundation.
 *
 *	Yeti is distributed in the hope that it will be useful, but WITHOUT
 *	ANY WARRANTY; without even  the implied warranty of MERCHANTABILITY
 *	or FITNESS  FOR A PARTICULAR  PURPOSE.  See the GNU  General Public
 *	License for more details.
 *
 *	You should have  received a copy of the  GNU General Public License
 *	along with  Yeti (file "COPYING"  in the top source  directory); if
 *	not, write to  the Free Software Foundation, Inc.,  51 Franklin St,
 *	Fifth Floor, Boston, MA 02110-1301 USA
 *
 *-----------------------------------------------------------------------------
 *
 * History:
 *	$Id: yeti_cost.c,v 1.2 2006/12/21 09:34:30 eric Exp $
 *	$Log: yeti_cost.c,v $
 *	Revision 1.2  2006/12/21 09:34:30  eric
 *	 - Fix stupid way to get number of elements.
 *
 *	Revision 1.1  2006/07/19 14:43:01  eric
 *	Initial revision
 *
 */

#include <stdlib.h>
#include <math.h>
#include <ydata.h>

extern BuiltIn Y_cost_l2;
extern BuiltIn Y_cost_l2l1;
extern BuiltIn Y_cost_l2l0;

typedef double cost_worker_t(const double hyper[],
			     const double x[], double g[], size_t number,
			     int kase);
static cost_worker_t cost_l2;
static cost_worker_t cost_l2l1;
static cost_worker_t cost_l2l0;

static void cost_wrapper(int argc, const char *name,
			 cost_worker_t *worker);

void Y_cost_l2(int argc)
{
  cost_wrapper(argc, "l2", cost_l2);
}

void Y_cost_l2l1(int argc)
{
  cost_wrapper(argc, "l2-l1", cost_l2l1);
}

void Y_cost_l2l0(int argc)
{
  cost_wrapper(argc, "l2-l0", cost_l2l0);
}

static void cost_wrapper(int argc, const char *name,
			 cost_worker_t *worker)
{
  const double ZERO = 0.0;
  double result, mu, tpos, tneg, hyper[3];
  Operand op;
  size_t number;
  const double *x;
  double *g ;
  Symbol *s;
  long index;
  int kase, temporary;

  if (argc < 2 || argc > 3) YError("expecting 2 or 3 arguments");

  /* Get the hyper-parameters. */
  s = sp - argc + 1;
  if (s->ops && s->ops->FormOperand(s, &op)->ops->isArray) {
    number = op.type.number;
    if (number < 1 || number > 3) {
      YError("expecting 1, 2 or 3 hyper-parameters");
      return;
    }
    switch (op.ops->typeID) {
    case T_CHAR:
    case T_SHORT:
    case T_INT:
    case T_LONG:
    case T_FLOAT:
      op.ops->ToDouble(&op);
    case T_DOUBLE:
      x = (const double *)op.value;
      break;
    default:
      YError("bad data type for the hyper-parameters");
      return;
    }
  } else {
    YError("hyper-parameters must be an array");
    return;
  }
  if (number == 1) {
    mu = x[0];
    tneg = ZERO;
    tpos = ZERO;
  } else if (number == 2) {
    mu = x[0];
    tneg = -x[1];
    tpos = +x[1];
  } else {
    mu = x[0];
    tneg = x[1];
    tpos = x[2];
  }
  kase = 0;
  if (tneg < ZERO) kase |= 1;
  else if (tneg != ZERO) YError("lower threshold must be negative");
  if (tpos > ZERO) kase |= 2;
  else if (tpos != ZERO) YError("upper threshold must be positive");

  /* Get the parameters. */
  ++s;
  x = (double *)0;
  temporary = 0;
  if (s->ops && s->ops->FormOperand(s, &op)->ops->isArray) {
    switch (op.ops->typeID) {
    case T_CHAR:
    case T_SHORT:
    case T_INT:
    case T_LONG:
    case T_FLOAT:
      op.ops->ToDouble(&op);
    case T_DOUBLE:
      x = (const double *)op.value;
      temporary = (! op.references);
      number = op.type.number;
    }
  }
  if (! x) {
    YError("invalid input array");
    return;
  }

  if (argc == 3) {
    /* Get the symbol for the gradient. */
    /* If gradient is required and input array X is a temporary one, re-use
       X as the output gradient; otherwise, create a new array from scratch
       for G (see BuildResultU in ops0.c). */
    ++s;
    if (s->ops!=&referenceSym)
      YError("needs simple variable reference to store the gradient");
    index = s->index;
    Drop(1);
    if (temporary) {
      g = (double *)x;
    } else {
      g = ((Array *)PushDataBlock(NewArray(&doubleStruct,
					   op.type.dims)))->value.d;
    }
  } else {
    index = -1L;
    g = (double *)0;
  }

  hyper[0] = mu;
  hyper[1] = tneg;
  hyper[2] = tpos;
  result = worker(hyper, x, g, number, kase);
  if (index >= 0L) PopTo(&globTab[index]);
  PushDoubleValue(result);
}

static double cost_l2(const double hyper[],
		      const double x[], double g[], size_t number,
		      int kase)
{
  double mu, result, gscl, t;
  size_t i;

  result = 0.0;
  mu = hyper[0];
  gscl = mu + mu;
  if (g) {
    for (i = 0 ; i < number ; ++i) {
      t = x[i];
      g[i] = gscl*t;
      result += mu*t*t;
    }
  } else {
    for (i = 0 ; i < number ; ++i) {
      t = x[i];
      result += mu*t*t;
    }
  }
  return result;
}

static double cost_l2l1(const double hyper[],
			const double x[], double g[], size_t number,
			int kase)
{
  const double ZERO = 0.0;
  const double ONE = 1.0;
  double mu, result, qneg, qpos, fneg, fpos, gscl, t, q;
  size_t i;

  result = ZERO;
  mu = hyper[0];
  gscl = mu + mu;
  switch (kase) {
  case 0:
    /* L2 norm for all residuals. */
    if (g) {
      for (i = 0 ; i < number ; ++i) {
	t = x[i];
	g[i] = gscl*t;
	result += mu*t*t;
      }
    } else {
      for (i = 0 ; i < number ; ++i) {
	t = x[i];
	result += mu*t*t;
      }
    }
    break;

  case 1:
    /* L2-L1 norm for negative residuals, L2 norm for positive residuals. */
    qneg = ONE/hyper[1];
    fneg = gscl*hyper[1]*hyper[1];
    if (g) {
      for (i = 0 ; i < number ; ++i) {
	if ((t = x[i]) < ZERO) {
	  q = qneg*t;
	  g[i] = gscl*t/(ONE + q);
	  result += fneg*(q - log(ONE + q));
	} else {
	  g[i] = gscl*t;
	  result += mu*t*t;
	}
      }
    } else {
      for (i = 0 ; i < number ; ++i) {
	if ((t = x[i]) < ZERO) {
	  q = qneg*t;
	  result += fneg*(q - log(ONE + q));
	} else {
	  result += mu*t*t;
	}
      }
    }
    break;

  case 2:
    /* L2 norm for negative residuals, L2-L1 norm for positive residuals. */
    qpos = ONE/hyper[2];
    fpos = gscl*hyper[2]*hyper[2];
    if (g) {
      for (i = 0 ; i < number ; ++i) {
	if ((t = x[i]) > ZERO) {
	  q = qpos*t;
	  g[i] = gscl*t/(ONE + q);
	  result += fpos*(q - log(ONE + q));
	} else {
	  g[i] = gscl*t;
	  result += mu*t*t;
	}
      }
    } else {
      for (i = 0 ; i < number ; ++i) {
	if ((t = x[i]) > ZERO) {
	  q = qpos*t;
	  result += fpos*(q - log(ONE + q));
	} else {
	  result += mu*t*t;
	}
      }
    }
    break;

  case 3:
    /* L2-L1 norm for all residuals. */
    qneg = ONE/hyper[1];
    fneg = gscl*hyper[1]*hyper[1];
    qpos = ONE/hyper[2];
    fpos = gscl*hyper[2]*hyper[2];
    if (g) {
      for (i = 0 ; i < number ; ++i) {
	if ((t = x[i]) < ZERO) {
	  q = qneg*t;
	  g[i] = gscl*t/(ONE + q);
	  result += fneg*(q - log(ONE + q));
	} else {
	  q = qpos*t;
	  g[i] = gscl*t/(ONE + q);
	  result += fpos*(q - log(ONE + q));
	}
      }
    } else {
      for (i = 0 ; i < number ; ++i) {
	if ((t = x[i]) < ZERO) {
	  q = qneg*t;
	  result += fneg*(q - log(ONE + q));
	} else {
	  q = qpos*t;
	  result += fpos*(q - log(ONE + q));
	}
      }
    }
    break;
  }

  return result;
}

static double cost_l2l0(const double hyper[],
			const double x[], double g[], size_t number,
			int kase)
{
  const double ZERO = 0.0;
  const double ONE = 1.0;
  double mu, result, tneg, tpos, qneg, qpos, r, s, t;
  size_t i;

  result = ZERO;
  mu = hyper[0];
  s = mu + mu;
  switch (kase) {
  case 0:
    /* L2 norm for all residuals. */
    if (g) {
      for (i = 0 ; i < number ; ++i) {
	r = x[i];
	g[i] = s*r;
	result += r*r;
      }
    } else {
      for (i = 0 ; i < number ; ++i) {
	r = x[i];
	result += r*r;
      }
    }
    break;

  case 1:
    /* L2-L0 norm for negative residuals, L2 norm for positive residuals. */
    tneg = hyper[1];
    qneg = ONE/tneg;
    if (g) {
      for (i = 0 ; i < number ; ++i) {
	if ((r = x[i]) < ZERO) {
	  t = qneg*r;
	  r = tneg*atan(t);
	  g[i] = s*r/(ONE + t*t);
	  result += r*r;
	} else {
	  g[i] = s*r;
	  result += r*r;
	}
      }
    } else {
      for (i = 0 ; i < number ; ++i) {
	if ((r = x[i]) < ZERO) {
	  r = tneg*atan(qneg*r);
	  result += r*r;
	} else {
	  result += r*r;
	}
      }
    }
    break;

  case 2:
    /* L2 norm for negative residuals, L2-L0 norm for positive residuals. */
    tpos = hyper[2];
    qpos = ONE/tpos;
    if (g) {
      for (i = 0 ; i < number ; ++i) {
	if ((r = x[i]) > ZERO) {
	  t = qpos*r;
	  r = tpos*atan(t);
	  g[i] = s*r/(ONE + t*t);
	  result += r*r;
	} else {
	  g[i] = s*r;
	  result += r*r;
	}
      }
    } else {
    }
    break;

  case 3:
    /* L2-L0 norm for all residuals. */
    tneg = hyper[1];
    qneg = ONE/tneg;
    tpos = hyper[2];
    qpos = ONE/tpos;
    if (g) {
      for (i = 0 ; i < number ; ++i) {
	if ((r = x[i]) < ZERO) {
	  t = qneg*r;
	  r = tneg*atan(t);
	} else {
	  t = qpos*r;
	  r = tpos*atan(t);
	}
	g[i] = s*r/(ONE + t*t);
	result += r*r;
      }
    } else {
      for (i = 0 ; i < number ; ++i) {
	if ((r = x[i]) < ZERO) {
	  r = tneg*atan(qneg*r);
	} else {
	  r = tpos*atan(qpos*r);
	}
	result += r*r;
      }
    }
    break;
  }

  return mu*result;
}

/*---------------------------------------------------------------------------*/