Ramp¶
Status: Stable
documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
Description¶
Ramp[x]
gives x for x >= 0 and 0 for x \< 0 -- the positive part of x, and the standard spelling of a rectified linear unit.
Notes
The zero returned for a negative argument carries the argument's own exactness: Ramp\[-1.\] is 0. and Ramp\[-3\] is the exact 0, so a Real list maps to a Real list and an integer list to an integer one. Non-real arguments, and symbolic ones whose sign cannot be decided, are left unevaluated. Ramp is Listable and a NumericFunction.Examples (10)¶
Every input below was run against the current Mathilda build and its output recorded.
Basic examples (5)¶
In[1]:= Ramp[{-1., 0., 2.5}]
Out[1]= {0.0, 0.0, 2.5}
In[2]:= Ramp[{-3, 0, 4}]
Out[2]= {0, 0, 4}
In[3]:= Ramp[{-1/2, 3/4}]
Out[3]= {0, 3/4}
In[4]:= Ramp[1 - Sqrt[2]]
Out[4]= 0
In[5]:= Ramp[1. + 2. I]
Out[5]= Ramp[1.0 + 2.0*I]
Applications (5)¶
The positive part passes a nonnegative argument through
A negative argument becomes zero
Zero carries the argument's exactness, so a Real stays Real
A rectified linear unit applied across a vector
An undecidable sign is left unevaluated
Implementation notes¶
Algorithm. builtin_ramp is the positive part max(x, 0), the standard
spelling of a rectified linear unit. It classifies the sign with ustep_class:
a non-negative argument is returned unchanged, a negative one becomes zero, and
an undecidable or non-real argument is left unevaluated. The zero returned for a
negative argument carries the argument's own exactness ā Ramp[-1.] is 0.,
Ramp[-3] is the exact 0, and an MPFR argument returns a zero at its precision ā
so a Real vector maps to a Real vector and an integer vector to an integer one
with no mixed-head result (this is why Ramp needs no gate on its output where
Clip does).
Data structures. One ustep_class call and at most one expr_copy; no
intermediate expressions, unlike UnitBox. The ND kernel (REG_U(Ramp)) maps a
packed or visible numeric NDArray element-wise and preserves the element type,
so the buffer is answered in place.
Complexity / limits. O(1) per element; Compile[] lowers it at scalar and
rank-1 shapes. Like UnitStep, Ramp threads over an Interval argument (it is
non-decreasing, so endpoint threading is a rigorous enclosure). A genuinely
complex argument, or one whose sign cannot be certified, stays symbolic.
Listable,NumericFunction,Protected.- The zero returned for a negative argument carries the argument's own
exactness:
Ramp[-1.]is0.andRamp[-3]is the exact0. AReallist therefore maps to aReallist and an integer list to an integer one, with no mixed-head result -- unlikeClip, which returns the bound at a clipped position and so can put an exactIntegerinto a machine-real answer. - Exact symbolic real arguments are resolved by the same numerical
certification
UnitStepuses, soRamp[Sqrt[2] - 1]gives-1 + Sqrt[2]andRamp[1 - Sqrt[2]]gives0. - Non-real arguments (a
Complexwith non-zero imaginary part) and symbolic arguments whose sign cannot be certified are left unevaluated. - A packed list of
Reals is handled by a threaded buffer kernel (seepacked-arrays.md); an integer buffer materialises, which changes speed and not the answer.
Attributes: Listable, NumericFunction, Protected.
References¶
See also: Real, Clip, UnitStep, Complex
- Source:
src/piecewise.c - Specification:
docs/spec/builtins/elementary-functions.md - Tests:
tests/test_interval.c - Tests:
tests/test_packed_list.c
Notes & additional examples¶
Notes¶
Ramp[x] is the positive part max(x, 0) ā the rectified linear unit (ReLU) of
machine learning, now a single pass where x UnitStep[x] once needed two and
produced a mixed Real/Integer product. The zero returned for a negative argument
carries that argument's own exactness, so Ramp over a Real vector gives a Real
vector and over an integer vector an integer one, with no mixed-head output.
Ramp is non-decreasing, so it threads rigorously over an Interval and lowers
under Compile[]; its NDArray kernel maps a packed buffer element-wise while
preserving the element type.