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. 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))
Sign up to request clarification or add additional context in comments.

3 Comments

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()`
Can you give me some sample data? What data types are each variable?
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/…
1

How about

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.