hypot: Hypotenuse

hypotR Documentation

Hypotenuse

Description

hypot returns the “hypotenuse” of all the values present in its arguments.

phypot returns the parallel “hypotenuse” of the input values. It takes any number of vectors as arguments, recycle them to common length, and return a single vector giving the ‘parallel’ “hypotenuse” of the argument vectors.

Usage

hypot(..., na.rm = FALSE)
phypot(..., na.rm = FALSE)

Arguments

...

numeric or complex arguments.

na.rm

a logical indicating whether missing values should be removed.

Details

The hypotenuse is the longest side of a right-angled triangle, the side opposite the right angle. The length of the hypotenuse is defined as:

\sqrt(x^2 + y^2)

The 3-dimensional “hypotenuse” is defined as:

\sqrt(x^2 + y^2 + z^2)

The n-dimensional “hypotenuse” is defined as:

\sqrt{\sum_{k = 1}^n x_k^2}

Suppose we have a vector x and we want to know its “hypotenuse”.

If any of x is infinite, the “hypotenuse” is always Inf.

If na.rm = FALSE and any of x is NA or NaN, the “hypotenuse” is NaN.

Otherwise, the “hypotenuse” will be calculated using the above definition. If na.rm = TRUE all NA and NaN values are treated as 0.

Value

For hypot a numeric vector of length 1.

For phypot a numeric vector. If any of the input values is a zero-length vector the result has length zero. Otherwise, the result has length equal to the length of the longest vector. The rules for determining the attributes of the result are rather complicated. Attributes are only copied from input values whose lengths are equal to the length of the result. If any such input values have a dim attribute, the first dim attribute is copied to the result. dimnames are copied in a similar manner (but only after the result has a dim attribute). If any such input values have a conformable dimnames attribute, the first conformable dimnames attribute is copied to the result. If a dim attribute has not been assigned to the result, then finally names are copied in a similar manner. If any such input values have a names attribute, the first names attribute is copied to the result. A result can have a dim attribute, a names attribute, neither, but cannot have both. dim has priority over names (similar to Arithmetic operators).

Note

‘Numeric’ arguments are vectors of type integer and numeric, and logical (coerced to integer). NULL is accepted as equivalent to numeric(0).

Examples

## when a side is infinite, the hypotenuse is Inf
hypot(Inf, NaN)   # Inf
hypot(-Inf, NaN)  # Inf (applies to negative infinity too)

## when a side is NA or NaN, the hypotenuse is NaN
hypot(NaN, 0)     # NaN
hypot(NA , 0)     # NaN

## numbers whose squares would overflow normally are handled well
hypot(.Machine$double.xmax, 5     )
hypot(1e+300              , 1e+300)


## hypotenuse
hypot(3, 4)      # 5
hypot(3+4i)      # 5 (works for complex numbers as well)

## 3-dimensional "hypotenuse"
hypot(3, 4, 12)  # 13

## n-dimensional "hypotenuse"
hypot(1:100)


x <- seq.int(-3, 3, length.out = 101)
y <- 1
(h <- phypot(x, 1))  # parallel hypotenuse
graphics::plot(
    panel.first = graphics::grid(col = "gray69"),
    x = x, y = h, type = "l",
    main = "Distance from" ~ (list(0, 0)) ~ "to" ~ (list(x, 1))
)

ArcadeAntics/essentials documentation built on Nov. 7, 2024, 4:33 p.m.