1

I'm attempting to use my own modified version of the logsumexp() from here: https://github.com/scipy/scipy/blob/v0.14.0/scipy/misc/common.py#L18

On line 85, is this calculation:

out = log(sum(exp(a - a_max), axis=0))

But I have a threshold and I don't want a - a_max to exceed that threshold. Is there a way to do a conditional calculation, which would allow the subtraction to take place only if the difference isn't less than the threshold. So something like:

out = log(sum(exp( (a - a_max < threshold) ? threshold : a - a_max), axis = 0))
Nilesh
  • 20,521
  • 16
  • 92
  • 148
user961627
  • 12,379
  • 42
  • 136
  • 210

2 Answers2

1

How about

out = log(sum(exp( threshold if a - a_max < threshold else a - a_max), axis = 0))
qingbo
  • 2,130
  • 16
  • 19
1

There is a conditional inline statement in Python:

Value1 if Condition else Value2

Your formula transforms to:

out = log(sum(exp(threshold if a - a_max < threshold else a - a_max), axis = 0))
bogs
  • 2,286
  • 18
  • 22
  • I'm getting this error: ` out = log(sum(exp(threshold if a - a_max < threshold else a - a_max), axis=0)) ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()` – user961627 Sep 04 '14 at 09:00
  • Can you give me some sample data? What data types are each variable? – bogs Sep 04 '14 at 12:20
  • The thread is old, but for completeness sake: the problem is that the condition tries to evaluate the whole condition array, not per index. Meaning, for each index it does “if condition_array“, instead of “if condition_array[0]“, then “if condition_array[1]„ ..., see also: https://stackoverflow.com/questions/18397869/placing-a-condition-while-calculating-using-numpy-array – Michael S. Feb 16 '18 at 07:09