[Python-ideas] Consider adding clip or clamp function to math

Stephen J. Turnbull turnbull.stephen.fw at u.tsukuba.ac.jp
Fri Aug 5 10:30:35 EDT 2016


Steven D'Aprano writes:

 > >     x = [clamp(x[t], f(t), g(t)) for t in range(1_000_000)]
 > > 
 > > is perfectly plausible code.

 > I have my doubts. Sure, you can write it, but what would you use it for? 
 > What's your use-case?

Any varying optimal control might also be subject to bounds that vary.
These problems arise rather frequently in economic theory.  Often the
cheapest way to compute them is to compute an unconstrained problem
and clamp.

I can even think of a case where clamp could be used with a constant
control and a varying bound: S-s inventory control facing occasional
large orders in an otherwise continuous, stationary demand process.



More information about the Python-ideas mailing list