user961627
user961627

Reputation: 12747

Conditional calculation in python

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))

Upvotes: 1

Views: 3240

Answers (2)

bogs
bogs

Reputation: 2296

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))

Upvotes: 1

qingbo
qingbo

Reputation: 2160

How about

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

Upvotes: 1

Related Questions