Basic Math Functions
General
- abs(x)
Return the absolute value of
x, commonly written as |x|. When given a real number, it returns a non-negative real value. When given a complex number, it returns the modulus of the number.The argument can have a dimension.
Example:
abs(-3 meter) = 3 meter abs(4 + 3j) = 5
- sqrt(x)
Return the square root of
x. Any complex number may be specified, yielding the complex root in the upper half plane.Alias:
√(x).New in version 1.0.
The argument may have a dimension.
- cbrt(x)
Compute the third (cubic) root of
x. Negative real numbers yield a negative real cubic root:cbrt(-27) = -3
This function accepts any complex input. The result will generally be the first complex root, i.e. the one with a phase between 0 and π/3. Real negative arguments however will still yield a real (negative) result. Use
x^(1/3)to get the first complex root.Alias:
∛(x).New in version 1.0.
- exp(x)
Compute the natural exponential function.
The argument must be dimensionless.
See also
ln()(natural logarithm)
- ln(x)
Compute the natural logarithm.
Any non-zero number may be given. The result will be the principal value. The branch cut runs across the negative real axis. Nevertheless, in SpeedCrunch
ln()is defined for negative real numbers as ln(-x) = ln(|x|)) + πj, extending the branch from the upper half-plane.
- lb(x)
Compute the binary logarithm. The same complex-number rules apply as for
ln().
- lg(x)
Compute the decimal logarithm. The same complex-number rules apply as for
ln().
- log(n; x)
Compute the logarithm of base
n. The same complex-number rules apply as forln().
- numval(x)
Return the numerical value of quantity
x.For unitless values, this returns the value unchanged. For quantities with units, it returns the numerical value in SI/base units by default.
If
xincludes an explicit conversion target (-> [unit]), it returns the numerical value in that target unit.Example:
numval(3) = 3 numval(3 [m/s]) = 3 numval(3 [km/s]) = 3000 numval(3 [km/s] -> [cm/s]) = 300000
- datetime(unix_timestamp[; offset])
New in version 1.0.
Convert a Unix timestamp (seconds since 1970-01-01 00:00:00 UTC) into a numeric date/time value formatted as
YYYYMMDD.HHMMSS.If
offsetis omitted, the timestamp is converted using the local system timezone. Ifoffsetis provided, it is interpreted as hours offset to GMT/UTC (fractional offsets are supported), and conversion is performed in UTC after applying that offset.This function is the inverse of
epoch()when using the sameoffset.Example:
datetime(1514761200; 1) = 20180101.000000 datetime(1551464695; -3.5) = 20190301.145455
- epoch(yyyymmdd.hhmmss[; offset])
New in version 1.0.
Convert a numeric date/time value in
YYYYMMDD.HHMMSSformat to a Unix timestamp (seconds since 1970-01-01 00:00:00 UTC).If
offsetis omitted, the value is interpreted in the local system timezone. Ifoffsetis provided, it is interpreted as hours offset to GMT/UTC (fractional offsets are supported).This function is the inverse of
datetime()when using the sameoffset.Example:
epoch(20180101.000000; 1) = 1514761200 epoch(20190301.145455; -3.5) = 1551464695
- molmass(formula)
Compute the molar mass of a chemical formula and return the result with dimension
g/mol.The
formulaargument is case-sensitive and supports either plain digits (for exampleC6H12O6) or subscript digits (for exampleC₆H₁₂O₆). Parsing fails for invalid formulas.Advanced chemical notation is not supported yet. For example, grouped or hydrated formulas such as
K4[Fe(CN)6]·3H2Ocurrently fail to parse.The element table follows the official IUPAC CIAAW 2021 abridged standard atomic weights.
Example:
molmass(C6H12O6) = 180.156 g/mol
- mass(mol; formula)
Compute the mass of a substance amount and return the result with dimension
g.This function reuses
molmass()internally and is equivalent to:mol * molmass(formula).The
formulaargument follows the same parsing rules asmolmass().Example:
mass(1; C6H12O6) = 180.156 g
- molarity(n; V)
Compute molarity as amount of substance divided by solution volume and return the result with dimension
mol/L.This function is equivalent to:
n / V.If
norVis dimensionless, SpeedCrunch interprets them asmolandL, respectively.Example:
molarity(0.50; 1.00) = 0.5 mol/L
Lists and Matrices
New in version 1.0.
Lists are written with braces and semicolon-separated elements. Matrices are written as a list of row lists. Only one-dimensional lists and two-dimensional matrices are supported:
mylist = {1; 2; 3; 4; 5}
mat = {{1; 2; 3}; {4; 5; 6}}
|
The function-style notation list(...) is also accepted on input, for
example list(1; 2; 3) is equivalent to {1; 2; 3}.
List and matrix results use the same spacing, for example {1; 2; 3} and
{{1; 2}; {3; 4}}.
List and matrix elements must be dimensionless. Units cannot be attached to individual elements or to a list or matrix as a whole.
Supported arithmetic operations are addition and subtraction between same-shaped lists or matrices, multiplication by a scalar on either side, division by a scalar on the right-hand side, and matrix multiplication for compatible matrices. In matrix multiplication, a list can be used as a row vector on the left when the right operand is a matrix. A list can also be used as a row vector on the right when the left operand is a one-column matrix. List-by-list multiplication is not supported. Matrix products with a single result element return that scalar directly. Scalar addition/subtraction, scalar divided by a list or matrix, elementwise multiplication, and elementwise division are not supported:
{1; 2; 3} + {4; 5; 6}
= {5; 7; 9}
2 * {{1; 2}; {3; 4}}
= {{2; 4}; {6; 8}}
{1; 2; 3} / 2
= {0.5; 1; 1.5}
{{1; 2}; {3; 4}} * {{5; 6}; {7; 8}}
= {{19; 22}; {43; 50}}
{1; 2; 3} * {{1}; {2}; {3}}
= 14
{{1}; {2}; {3}} * {4; 5; 6}
= {{4; 5; 6}; {8; 10; 12}; {12; 15; 18}}
|
The aggregation and statistics functions count(), sum(),
min(), max(), average(), mean(), median(),
varp(), vars(), stdevp(), and stdevs() accept a list or
matrix as a single argument. Matrix inputs are flattened for these scalar
statistics. The p variants use population normalization (n); the s
variants use sample normalization (n-1).
covp(), covs(), corrp(), and corrs() treat matrices
column-wise: rows are observations and columns are variables.
- dot(list1; list2)
New in version 1.0.
Return the dot product of two equal-length lists.
- cross(list1; list2)
New in version 1.0.
Return the cross product of two three-element lists.
- norm(list-or-matrix)
New in version 1.0.
Return the Euclidean norm of a list, or the Frobenius norm of a matrix.
- transpose(matrix)
New in version 1.0.
Return the transposed matrix.
- det(matrix)
New in version 1.0.
Return the determinant of a square matrix.
- inv(matrix)
New in version 1.0.
Return the inverse of a square matrix.
- trace(matrix)
New in version 1.0.
Return the sum of the diagonal of a square matrix.
- rank(matrix)
New in version 1.0.
Return the matrix rank.
- covp(matrix)
New in version 1.0.
Return the population covariance matrix. Rows are observations and columns are variables. This uses population normalization (
n).
- covs(matrix)
New in version 1.0.
Return the sample covariance matrix. Rows are observations and columns are variables. This uses sample normalization (
n-1).
- corrp(matrix)
New in version 1.0.
Return the population Pearson correlation matrix. Rows are observations and columns are variables. This is derived from
covp().
- corrs(matrix)
New in version 1.0.
Return the sample Pearson correlation matrix. Rows are observations and columns are variables. This is derived from
covs().
- flatten(matrix)
New in version 1.0.
Return all matrix elements as a single list in row-major order.
- rows(matrix)
New in version 1.0.
Return the row count.
- cols(matrix)
New in version 1.0.
Return the column count.
- shape(list-or-matrix)
New in version 1.0.
Return
{n}for a list and{rows; cols}for a matrix.
Trigonometric & Inverse Trigonometric
For direct trigonometric input (sin(), cos(), tan(),
cot(), sec(), csc()), explicit angle units
(rad, degree, gradian/grad/gon, turn, arcminute,
arcsecond) override the global angle mode. Unitless values follow the
current angle mode.
- sin(x)
Returns the sine of
x. The behavior depends on both the angle mode setting and on whether complex numbers are enabled.In degrees, gradians, or turns modes, the argument is assumed to be expressed in such that
sin()is periodic with a period of 360 degrees, 400 gradians, or 1 turn, respectively: sin(x) = sin(x+360), sin(x) = sin(x+400), or sin(x) = sin(x+1). Complex arguments are allowed only in radians mode, regardless of the corresponding setting.When radians are set as the angle mode,
sin()will be 2π-periodic. The argument may be complex.For real arguments beyond approx. |x|>1077, SpeedCrunch no longer recognizes the periodicity of the function and issues an error.
The argument of
sin()must be dimensionless.The inverse function is
arcsin().
- cos(x)
Returns the cosine of
x. The behavior depends on both the angle mode setting and on whether complex numbers are enabled.In degrees, gradians, or turns modes, the argument is assumed to be expressed in such that
cos()is periodic with a period of 360 degrees, 400 gradians, or 1 turn, respectively: cos(x) = cos(x+360), cos(x) = cos(x+400), or cos(x) = cos(x+1). Complex arguments are allowed only in radians mode, regardless of the corresponding setting.When radians are set as the angle mode,
cos()will be 2π-periodic. The argument may be complex.For real arguments beyond approx. |x|>1077, SpeedCrunch no longer recognizes the periodicity of the function and issues an error.
The argument of
cos()must be dimensionless.The inverse function is
arccos().
- cis(x)
Return
cos(x) + i·sin(x).Like
sin()andcos(), explicit angle units (rad,degree,gradian/grad/gon,turn,arcminute,arcsecond) override the global angle mode. Unitless values follow the current angle mode.Complex arguments are allowed only in radians mode. The argument must be dimensionless.
- tan(x)
Returns the tangent of
x. The behavior depends on both the angle mode setting and on whether complex numbers are enabled.In degrees, gradians, or turns modes, the argument is assumed to be expressed in such that
tan()is periodic with a period of 360 degrees, 400 gradians, or 1 turn, respectively: tan(x) = tan(x+360), tan(x) = tan(x+400), or tan(x) = tan(x+1). Complex arguments are allowed only in radians mode, regardless of the corresponding setting.When radians are set as the angle mode,
tan()will be π-periodic. The argument may be complex.The argument of
tan()must be dimensionless.The inverse function is
arctan().
- cot(x)
Returns the cotangent of
x. The behavior depends on both the angle mode setting and on whether complex numbers are enabled.In degrees, gradians, or turns modes, the argument is assumed to be expressed in such that
cot()is periodic with a period of 360 degrees, 400 gradians, or 1 turn, respectively: cot(x) = cot(x+360), cot(x) = cot(x+400), or cot(x) = cot(x+1). Complex arguments are allowed only in radians mode, regardless of the corresponding setting.When radians are set as the angle mode,
cot()will be π-periodic. The argument may be complex.The argument of
cot()must be dimensionless.
- sec(x)
Returns the secant of
x, defined as the reciprocal cosine ofx: sec(x) = 1/cos(x). The behavior depends on both the angle mode setting and on whether complex numbers are enabled.In degrees, gradians, or turns modes, the argument is assumed to be expressed in such that
sec()is periodic with a period of 360 degrees, 400 gradians, or 1 turn, respectively: sec(x) = sec(x+360), sec(x) = sec(x+400), or sec(x) = sec(x+1). Complex arguments are allowed only in radians mode, regardless of the corresponding setting.When radians are set as the angle mode,
sec()will be 2π-periodic. The argument may be complex.For real arguments beyond approx. |x|>1077, SpeedCrunch no longer recognizes the periodicity of the function and issues an error.
The argument of
sec()must be dimensionless.
- csc(x)
Returns the cosecant of
x, defined as the reciprocal sine ofx: csc(x) = 1/sin(x). The behavior depends on both the angle mode setting and on whether complex numbers are enabled.In degrees, gradians, or turns modes, the argument is assumed to be expressed in such that
csc()is periodic with a period of 360 degrees, 400 gradians, or 1 turn, respectively: csc(x) = csc(x+360), csc(x) = csc(x+400), or csc(x) = csc(x+1). Complex arguments are allowed only in radians mode, regardless of the corresponding setting.When radians are set as the angle mode,
csc()will be 2π-periodic. The argument may be complex.For real arguments beyond approx. |x|>1077, SpeedCrunch no longer recognizes the periodicity of the function and issues an error.
The argument of
csc()must be dimensionless.
- arccos(x)
Returns the inverse cosine of
x, such that cos(arccos(x)) = x. The behavior of the function depends on the angle mode setting.In degrees, gradians, or turns modes,
arccos()takes a real argument from [-1, 1], and the return value is in the range [0, 180], [0, 200], or [0, 0.5], respectively. Real arguments outside [-1, 1] and complex numbers are allowed only in radians mode.When radians are set as the angle mode,
arccos()maps an element from [-1, 1] to a value in [0, π] and may take any argument from the complex plane. arccos(-1) = π and arccos(1) = 0 match the real-valued results.The argument of
arccos()must be dimensionless.The inverse function is
cos().
- arcsin(x)
Returns the inverse sine of
x, such that sin(arcsin(x)) = x. The behavior of the function depends on the angle mode setting.In degrees, gradians, or turns modes,
arcsin()takes a real argument from [-1, 1], and the return value is in the range [-90, 90], [-100, 100], or [-0.25, 0.25], respectively. Real arguments outside [-1, 1] and complex numbers are allowed only in radians mode.When radians are set as the angle mode,
arcsin()maps an element from [-1, 1] to a value in [-π/2, π/2] and may take any argument from the complex plane. arcsin(-1) = π/2 and arcsin(1) = π/2 match the real-valued results.The argument of
arccos()must be dimensionless.The inverse function is
sin().
- arctan(x)
Returns the inverse tangent of
x, such that tan(arctan(x)) = x. The behavior of the function depends on the angle mode setting.In degrees, gradians, or turns modes,
arctan()takes a real argument from [-1, 1], and the return value is in the range [-90, 90], [-100, 100], or [-0.25, 0.25], respectively. Real arguments outside [-1, 1] and complex numbers are allowed only in radians mode.When radians are set as the angle mode,
arctan()maps a real number to a value in [-π/2, π/2] and may take any argument from the complex plane, except for +j and -j.The argument of
arctan()must be dimensionless.The inverse function is
tan().
- arctan2(x, y)
Returns the angle formed by the vector (x, y) and the X axis. If the point (x, y) lies in the first quadrant (i.e. both x > 0 and y > 0 are true), it is given by arctan(y/x). However, the function handles vectors in other quadrants as well.
The behavior of the function depends on the angle mode setting. In degrees, gradians, or turns modes, this function returns a value in the range ]-180, 180], ]-200, 200], or ]-0.5, 0.5], respectively. When radians are set as the angle mode, the return value lies in the range ]-π, π].
Unlike
arctan()this function only accepts real arguments.The argument values must be dimensionless.
Hyperbolic & Inverse Hyperbolic
- sinh(x)
Return the hyperbolic sine of
x. Any complex number may be used as the argument.The argument must be dimensionless.
The inverse function is
arsinh().
- cosh(x)
Return the hyperbolic cosine of
x. Any complex number may be used as the argument.The argument must be dimensionless.
The inverse function is
arcosh().
- tanh(x)
Return the hyperbolic tangent of
x. Any complex number may be used as the argument.The argument must be dimensionless.
For sufficiently large positive or negative real arguments, the result rounds to
1or-1, respectively, without a range error.The inverse function is
artanh().
- arsinh(x)
Compute the area hyperbolic sine of
x, the inverse function tosinh(). arsinh(x) is the only solution to cosh(y) = x.The function is defined for any complex
zas arsinh(z) = ln[z + (z 2 +1) 1/2 ].The function only accepts dimensionless arguments.
- arcosh(x)
Compute the area hyperbolic cosine of
x, the inverse function tocosh(). arcosh(x) is the positive solution to cosh(y) = x. Except for x=1, the second solution to this equation will be given by -arcosh(x).The function is defined for any complex
zas arcosh(z) = ln[z + (z 2 -1) 2 ].The function only accepts dimensionless arguments.
- artanh(x)
Compute the area hyperbolic tangent of
x, the inverse function totanh(). artanh(x) is the only solution to tanh(y) = x.This function accepts any argument except for -1 and +1. In the complex plane, it is defined as artanh(z) = 1/2 * ln[(z+1)/(z-1)].
The function only accepts dimensionless arguments.
Special
- erf(x)
Compute the error function, evaluated in
x. The error function is closely related to the Gaussian cumulative density function.Note that currently only real arguments are allowed. Furthermore, the function only accepts dimensionless arguments.
- erfc(x)
Compute the complementary error function, evaluated in
x. The complementary error function is defined byerfc(x) = 1 - erf(x).Note that currently only real arguments are allowed. Furthermore, the function only accepts dimensionless arguments.
- gamma(x)
Evaluates the gamma function (frequently denoted by the Greek letter Γ). The gamma function is an analytic extension to the factorial operation which is defined on real numbers as well. The relation between factorial and the gamma function is given by Γ(n) = (n - 1)!.
Note that currently only real arguments are allowed. Furthermore, the function only accepts dimensionless arguments.
The computation of the factorial operation is in fact implemented via
gamma(). This means that in SpeedCrunch, factorials of non-integer numbers are allowed.
- lngamma(x)
Computes
ln(abs(gamma(x))). As the gamma function grows extremely quickly, it is sometimes easier to work with its logarithm instead.lngamma()allows much larger arguments that would otherwise overflowgamma().Note that currently only real arguments are allowed. Furthermore, the function only accepts dimensionless arguments.
Complex Numbers
The complex-form functions in this section format one result in a specific complex representation. They override the global setting for that result only.
Complex numbers can also be entered in phasor notation as r ∠ θ. This is
equivalent to r * cis(θ): the left operand is the magnitude, and the right
operand is the phase angle. Unitless phases follow the global angle mode, while
explicit angle units override it.
When the angle mode is radians, complex forms display simple phase multiples
of pi symbolically, for example cis(pi / 2), exp(i · pi / 2), or
1 ∠ (pi / 2). Other angle modes display numeric phase values; exponential
form continues to use radians.
- real(x)
Return the real part of a complex number
x.The argument may have a dimension.
- imag(x)
Return the imaginary part of a complex number
x.The argument may have a dimension.
- conj(x)
New in version 1.0.
Return the complex conjugate of a complex number
x.This function accepts any real or complex input.
- phase(x)
Returns the phase (angle) of a complex number
x. The unit of the angle corresponds to the current angle mode.The argument may have a dimension.
See also
abs()(absolute value)
- rectform(x)
Format the complex number
xin rectangular form, i.e. the form a + b u, whereuis the currently selected imaginary-unit symbol (iorj).This corresponds to .
- trigform(x)
Format the complex number
xin trigonometric form, shown as r(cos ɸ + u sin ɸ), whereuis the selected imaginary-unit symbol (iorj). The angle ɸ follows the global angle mode.This corresponds to .
- expform(x)
Format the complex number
xin exponential form, shown as r e uɸ, whereuis the selected imaginary-unit symbol (iorj), and ɸ is in radians.This corresponds to .
- cisform(x)
Format the complex number
xin cis form, shown as r cis(ɸ). The angle ɸ follows the global angle mode.This corresponds to .
- phasorform(x)
Format the complex number
xin phasor form, shown as r ∠ ɸ. The angle ɸ follows the global angle mode.This output format is related to phasor input notation:
r ∠ θenters a complex number from magnituderand phaseθ.This corresponds to .
Various
- sgn(x)
For x >= 0, return +1. For x < 0, return -1.
- radians(x)
Convert the angle
xinto radians. Independently of the angle mode setting, this function will assume thatxis given in degrees and returnpi*x/180.The function accepts real arguments that are either dimensionless (interpreted as degrees) or explicitly tagged with an angle unit in
[](for example[rad],[degree],[gradian],[turn],[arcminute],[arcsecond]).With explicit unit blocks, the equivalent conversion is
x[deg] -> [rad].
- degrees(x)
Convert the angle
xinto degrees. Independently of the angle mode setting, this function will assume thatxis given in radians and return180*x/pi.The function accepts real arguments that are either dimensionless (interpreted as radians) or explicitly tagged with an angle unit in
[].With explicit unit blocks, the equivalent conversion is
x[rad] -> [deg].
- gradians(x)
New in version 1.0.
Convert the angle
xinto gradians. Independently of the angle mode setting, this function will assume thatxis given in radians and return200*x/pi.The function accepts real arguments that are either dimensionless (interpreted as radians) or explicitly tagged with an angle unit in
[].With explicit unit blocks, the equivalent conversion is
x[rad] -> [grad].
- turns(x)
New in version 1.0.
Convert the angle
xinto turns. Independently of the angle mode setting, this function will assume thatxis given in radians and returnx/(2*pi).The function accepts real arguments that are either dimensionless (interpreted as radians) or explicitly tagged with an angle unit in
[].With explicit unit blocks, the equivalent conversion is
x[rad] -> [turn].
- int(x)
Returns the integer part of
x, effectively rounding it towards zero.The function only accepts real, dimensionless arguments.
- frac(x)
Returns the fractional (non-integer) part of
x, given byfrac(x) = x - int(x).The function only accepts real, dimensionless arguments.