Qu

max

Builtin index · Core maths

max(x, [on_invalid=]) or max(a, b)

Input: x, on_invalid · Output: no output

What it is

One argument reduces x (a scalar, Vec, or Mat/Signal) to its largest/smallest element, a scalar Num, and accepts on_invalid= ("propagate"/"ignore"/"error", see this section's intro). Two arguments a, b (matching or broadcastable shapes) instead compare elementwise and keep the larger/smaller of each pair — the "clip" idiom, e.g. max(x, 0); on_invalid= does not apply to this two-argument form. In the one-argument form on a Mat, the optional axis= keyword (0 or 1) reduces along just that dimension instead, returning a length-cols/rows Vec rather than a single scalar.

Overloads

x can be scalar, Vec, Mat, Signal, or Num; a can be scalar, Vec, Mat, Signal, or Num.

2 forms. Each carries its own inputs, outputs and worked example — pick the tab for the one you are calling.

CodeInputOutputDescription
result = max(x)
x scalar, Vec, Mat, Signal, or Numnot documented yet
x scalar, Vec, Mat, or Signala scalar, Vec, or Mat/Signal, flattened
Largest element of x (a scalar, Vec, or Mat/Signal, flattened) — a single scalar Num.

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

print(max([1, 5, 3]))      # 5 -- one-argument reduction

print(max([1, 5, 3], 4))   # [4, 5, 4] -- two-argument elementwise clip

output
5
[4, 5, 4]

from core-math

scale = 100

function scaled(v)
    return v * scale        # reads the outer `scale`

end function

print("scaled max {max(scaled([0.1, 0.5])):.1f}")
output
scaled max 50.0

from from-zero

fs = 1000               # a plain number

t  = (0 to 999) / fs    # a range, divided elementwise -> a vector

print("{len(t)} samples covering {max(t):.3f} s")
output
1000 samples covering 0.999 s

from from-zero

signal = sin(2 * pi * 5 * t)
print("goes from {min(signal):.2f} to {max(signal):.2f}")
output
goes from -1.00 to 1.00

from start-here

CodeInputOutputDescription
result = max(a, b)
a scalar, Vec, Mat, Signal, or NumOne argument reduces x (a scalar, Vec, or Mat/Signal) to its largest/smallest element, a scalar Num, and accepts on_invalid= ("propagate"/"ignore"/"error", see this section's intro).
b —Two arguments a, b (matching or broadcastable shapes) instead compare elementwise and keep the larger/smaller of each pair — the "clip" idiom, e.g.
result scalar
Elementwise max of a and b (matching or broadcastable shapes) — the "clip" idiom, e.g. max(x, 0). Returns the broadcast shape, not a scalar.

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

print(max([1, 5, 3]))      # 5 -- one-argument reduction

print(max([1, 5, 3], 4))   # [4, 5, 4] -- two-argument elementwise clip

output
5
[4, 5, 4]

from core-math

clipped = min(max(x, -1), 1)   # same as clip(x, -1, 1)

from fundamentals

running_max(current) := max(current, 7)

m = mutex(0)
mutex_set(m, 3)                        # overwrite -- no read involved

mutex_update(m, "running_max")         # atomic read-modify-write via a user fn

print(mutex_get(m))                    # 7

output
7

from concurrency

How it works

one argument reduces; two arguments are elementwise (clip idiom).

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.

See also

min — documented in the same entry.