Mojo math详解及相关说明

math

Defines math utilities.

You can import these APIs from the math package. For example:

from math import mul

mod

mod[type: DType, simd_width: Int](x: SIMD[type, simd_width], y: SIMD[type, simd_width]) -> SIMD[type, simd_width]

Performs elementwise modulo operation of two SIMD vectors.

Parameters:

  • type (DType): DType of the input SIMD vectors.
  • simd_width (Int): Width of the input SIMD vectors.

Args:

  • x (SIMD[type, simd_width]): The numerator of the operation.
  • y (SIMD[type, simd_width]): The denominator of the operation.

Returns:

The remainder of x divided by y.

mul

mul[type: DType, simd_width: Int](x: SIMD[type, simd_width], y: SIMD[type, simd_width]) -> SIMD[type, simd_width]

Performs elementwise multiplication of two SIMD vectors.

Parameters:

  • type (DType): DType of the input SIMD vectors.
  • simd_width (Int): Width of the input SIMD vectors.

Args:

  • x (SIMD[type, simd_width]): First SIMD vector to multiply.
  • y (SIMD[type, simd_width]): Second SIMD vector to multiply.

Returns:

Elementwise multiplication of x and y.

sub

sub[type: DType, simd_width: Int](x: SIMD[type, simd_width], y: SIMD[type, simd_width]) -> SIMD[type, simd_width]

Performs elementwise subtraction of two SIMD vectors.

Parameters:

  • type (DType): DType of the input SIMD vectors.
  • simd_width (Int): Width of the input SIMD vectors.

Args:

  • x (SIMD[type, simd_width]): SIMD vector which y will be subtracted from.
  • y (SIMD[type, simd_width]): SIMD vector to subtract from x.

Returns:

Elementwise subtraction of SIMD vector y x - y).

add

add[type: DType, simd_width: Int](x: SIMD[type, simd_width], y: SIMD[type, simd_width]) -> SIMD[type, simd_width]

Performs elementwise addition of two SIMD vectors.

Parameters:

  • type (DType): DType of the input SIMD vectors.
  • simd_width (Int): Width of the input SIMD vectors.

Args:

  • x (SIMD[type, simd_width]): First SIMD vector to add.
  • y (SIMD[type, simd_width]): Second SIMD vector to add.

Returns:

Elementwise addition of x and y.

div

div[type: DType, simd_width: Int](x: SIMD[type, simd_width], y: SIMD[type, simd_width]) -> SIMD[type, simd_width]

Performs elementwise division of two SIMD vectors.

Parameters:

  • type (DType): DType of the input SIMD vectors.
  • simd_width (Int): Width of the input SIMD vectors.

Args:

  • x (SIMD[type, simd_width]): SIMD vector containing the dividends.
  • y (SIMD[type, simd_width]): SIMD vector containing the quotients.

Returns:

Elementwise division of SIMD vector x by SIMD vector y (this is x / y).

clamp

clamp[type: DType, simd_width: Int](x: SIMD[type, simd_width], lower_bound: SIMD[type, simd_width], upper_bound: SIMD[type, simd_width]) -> SIMD[type, simd_width]

Clamps the values in a SIMD vector to be in a certain range.

Clamp cuts values in the input SIMD vector off at the upper bound and lower bound values. For example, SIMD vector [0, 1, 2, 3] clamped to a lower bound of 1 and an upper bound of 2 would return [1, 1, 2, 2].

Parameters:

  • type (DType): DType of the input SIMD vectors.
  • simd_width (Int): Width of the input SIMD vectors.

Args:

  • x (SIMD[type, simd_width]): SIMD vector to perform the clamp operation on.
  • lower_bound (SIMD[type, simd_width]): Minimum of the range to clamp to.
  • upper_bound (SIMD[type, simd_width]): Maximum of the range to clamp to.

Returns:

A new SIMD vector containing x clamped to be within lower_bound and upper_bound.

abs

abs(x: Int) -> Int

Gets the absolute value of an integer.

Args:

  • x (Int): Value to take the absolute value of.

Returns:

The absolute value of x.

abs[type: DType, simd_width: Int](x: ComplexSIMD[type, simd_width]) -> SIMD[type, simd_width]

Performs elementwise abs (norm) on each element of the complex value.

Parameters:

  • type (DType): The dtype of the input and output SIMD vector.
  • simd_width (Int): The width of the input and output SIMD vector.

Args:

  • x (ComplexSIMD[type, simd_width]): The complex vector to perform absolute value on.

Returns:

The elementwise abs of x.

abs[type: DType, simd_width: Int](x: SIMD[type, simd_width]) -> SIMD[type, simd_width]

Performs elementwise absolute value on the elements of a SIMD vector.

Parameters:

  • type (DType): The dtype of the input and output SIMD vector.
  • simd_width (Int): The width of the input and output SIMD vector.

Args:

  • x (SIMD[type, simd_width]): SIMD vector to perform absolute value on.

Returns:

The elementwise absolute value of x.

rotate_bits_left

rotate_bits_left[shift: Int](x: Int) -> Int

Shifts the bits of a input to the left by shift bits (with wrap-around).

Constraints:

-size <= shift < size

Parameters:

  • shift (Int): The number of bit positions by which to rotate the bits of the integer to the left (with wrap-around).

Args:

  • x (Int): The input value.

Returns:

The input rotated to the left by shift elements (with wrap-around).

rotate_bits_left[shift: Int, type: DType, width: Int](x: SIMD[type, width]) -> SIMD[type, width]

Shifts bits to the left by shift positions (with wrap-around) for each element of a SIMD vector.

Constraints:

0 <= shift < size Only unsigned types can be rotated.

Parameters:

  • shift (Int): The number of positions by which to shift left the bits for each element of a SIMD vector to the left (with wrap-around).
  • type (DType): The dtype of the input and output SIMD vector.
  • width (Int): The width of the input and output SIMD vector.

Args:

  • x (SIMD[type, width]): SIMD vector to perform the operation on.

Returns:

The SIMD vector with each element’s bits shifted to the left by shift bits (with wrap-around).

rotate_bits_right

rotate_bits_right[shift: Int](x: Int) -> Int

Shifts the bits of a input to the left by shift bits (with wrap-around).

Constraints:

-size <= shift < size

Parameters:

  • shift (Int): The number of bit positions by which to rotate the bits of the integer to the left (with wrap-around).

Args:

  • x (Int): The input value.

Returns:

The input rotated to the left by shift elements (with wrap-around).

rotate_bits_right[shift: Int, type: DType, width: Int](x: SIMD[type, width]) -> SIMD[type, width]

Shifts bits to the right by shift positions (with wrap-around) for each element of a SIMD vector.

Constraints:

0 <= shift < size Only unsigned types can be rotated.

Parameters:

  • shift (Int): The number of positions by which to shift right the bits for each element of a SIMD vector to the left (with wrap-around).
  • type (DType): The dtype of the input and output SIMD vector.
  • width (Int): The width of the input and output SIMD vector.

Args:

  • x (SIMD[type, width]): SIMD vector to perform the operation on.

Returns:

The SIMD vector with each element’s bits shifted to the right by shift bits (with wrap-around).

rotate_left

rotate_left[shift: Int](x: Int) -> Int

Shifts the bits of a input to the left by shift bits (with wrap-around).

Constraints:

-size <= shift < size

Parameters:

  • shift (Int): The number of bit positions by which to rotate the bits of the integer to the left (with wrap-around).

Args:

  • x (Int): The input value.

Returns:

The input rotated to the left by shift elements (with wrap-around).

rotate_left[shift: Int, type: DType, size: Int](x: SIMD[type, size]) -> SIMD[type, size]

Shifts the elements of a SIMD vector to the left by shift elements (with wrap-around).

Constraints:

-size <= shift < size

Parameters:

  • shift (Int): The number of positions by which to rotate the elements of SIMD vector to the left (with wrap-around).
  • type (DType): The DType of the input and output SIMD vector.
  • size (Int): The width of the input and output SIMD vector.

Args:

  • x (SIMD[type, size]): The input value.

Returns:

The SIMD vector rotated to the left by shift elements (with wrap-around).

rotate_right

rotate_right[shift: Int](x: Int) -> Int

Shifts the bits of a input to the right by shift bits (with wrap-around).

Constraints:

-size <= shift < size

Parameters:

  • shift (Int): The number of bit positions by which to rotate the bits of the integer to the right (with wrap-around).

Args:

  • x (Int): The input value.

Returns:

The input rotated to the right by shift elements (with wrap-around).

rotate_right[shift: Int, type: DType, size: Int](x: SIMD[type, size]) -> SIMD[type, size]

Shifts the elements of a SIMD vector to the right by shift elements (with wrap-around).

Constraints:

-size < shift <= size

Parameters:

  • shift (Int): The number of positions by which to rotate the elements of SIMD vector to the right (with wrap-around).
  • type (DType): The DType of the input and output SIMD vector.
  • size (Int): The width of the input and output SIMD vector.

Args:

  • x (SIMD[type, size]): The input value.

Returns:

The SIMD vector rotated to the right by shift elements (with wrap-around).

floor

floor[type: DType, simd_width: Int](x: SIMD[type, simd_width]) -> SIMD[type, simd_width]

Performs elementwise floor on the elements of a SIMD vector.

Parameters:

  • type (DType): The dtype of the input and output SIMD vector.
  • simd_width (Int): The width of the input and output SIMD vector.

Args:

  • x (SIMD[type, simd_width]): SIMD vector to perform floor on.

Returns:

The elementwise floor of x.

ceil

ceil[type: DType, simd_width: Int](x: SIMD[type, simd_width]) -> SIMD[type, simd_width]

Performs elementwise ceiling on the elements of a SIMD vector.

Parameters:

  • type (DType): The dtype of the input and output SIMD vector.
  • simd_width (Int): The width of the input and output SIMD vector.

Args:

  • x (SIMD[type, simd_width]): SIMD vector to perform ceiling on.

Returns:

The elementwise ceiling of x.

ceildiv

ceildiv(x: Int, y: Int) -> Int

Return the rounded-up result of dividing x by y.

Args:

  • x (Int): The numerator.
  • y (Int): The denominator.

Returns:

The ceiling of dividing x by y.

trunc

trunc[type: DType, simd_width: Int](x: SIMD[type, simd_width]) -> SIMD[type, simd_width]

Performs elementwise truncation on the elements of a SIMD vector.

Parameters:

  • type (DType): The dtype of the input and output SIMD vector.
  • simd_width (Int): The width of the input and output SIMD vector.

Args:

  • x (SIMD[type, simd_width]): SIMD vector to perform trunc on.

Returns:

The elementwise truncation of x.

round

round[type: DType, simd_width: Int](x: SIMD[type, simd_width]) -> SIMD[type, simd_width]

Performs elementwise rounding on the elements of a SIMD vector.

This rounding goes to the nearest integer with ties away from zero.

Parameters:

  • type (DType): The dtype of the input and output SIMD vector.
  • simd_width (Int): The width of the input and output SIMD vector.

Args:

  • x (SIMD[type, simd_width]): SIMD vector to perform rounding on.

Returns:

The elementwise rounding of x.

roundeven

roundeven[type: DType, simd_width: Int](x: SIMD[type, simd_width]) -> SIMD[type, simd_width]

Performs elementwise banker’s rounding on the elements of a SIMD vector.

This rounding goes to the nearest integer with ties toward the nearest even integer.

Parameters:

  • type (DType): The dtype of the input and output SIMD vector.
  • simd_width (Int): The width of the input and output SIMD vector.

Args:

  • x
  • 24
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

启航学途

您的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值