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))
1
  • You could check a against threshold + a_max, i.e. a modified threshold.
    – fuesika
    Commented Sep 4, 2014 at 8:36

2 Answers 2

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))
3
  • 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
    Commented Sep 4, 2014 at 9:00
  • Can you give me some sample data? What data types are each variable?
    – bogs
    Commented Sep 4, 2014 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: stackoverflow.com/questions/18397869/…
    – Michael S.
    Commented Feb 16, 2018 at 7:09
1

How about

out = log(sum(exp( threshold if a - a_max < threshold else a - a_max), axis = 0))

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.