Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Basic Math Tests

Plus

Add numbers.

$ wo 'Plus[1, 2]'
3
$ wo 'Plus[1, 2, 3]'
6
$ wo 'Plus[-1, -2]'
-3

+

$ wo '1+2'
3
$ wo '8 + (-5)'
3

List arithmetic (threading)

Arithmetic operations are automatically threaded over lists:

$ wo '{1, 2, 3} + 2'
{3, 4, 5}
$ wo '2 + {1, 2, 3}'
{3, 4, 5}
$ wo '{1, 2, 3} + {4, 5, 6}'
{5, 7, 9}
$ wo '{1, 2, 3} - 1'
{0, 1, 2}
$ wo '{1, 2, 3} * 2'
{2, 4, 6}
$ wo '{2, 4, 6} / 2'
{1, 2, 3}
$ wo '{1, 2, 3} ^ 2'
{1, 4, 9}

Minus

Make numbers negative.

$ wo 'Minus[5]'
-5
$ wo 'Minus[5, 2]'

Minus::argx: Minus called with 2 arguments; 1 argument is expected.
5 − 2

Subtract

Subtract numbers.

$ wo 'Subtract[5, 2]'
3

-

$ wo '5-2'
3

Times

Multiply numbers.

$ wo 'Times[2, 3]'
6
$ wo 'Times[2, 3, 4]'
24
$ wo 'Times[-2, 3]'
-6

*

$ wo '2*2'
4
$ wo '2 * (-2)'
-4

Divide

Divide numbers.

$ wo 'Divide[6, 2]'
3
$ wo 'Divide[6, 2, 3]'

Divide::argrx: Divide called with 3 arguments; 2 arguments are expected.
Divide[6, 2, 3]

Sign

Returns the sign of a number.

$ wo 'Sign[5]'
1
$ wo 'Sign[0]'
0
$ wo 'Sign[-7]'
-1

Prime

Returns the nth prime number.

$ wo 'Prime[5]'
11

Abs

Returns the absolute value of a number.

$ wo 'Abs[-5]'
5
$ wo 'Abs[5]'
5
$ wo 'Abs[0]'
0

Floor

Rounds down to the nearest integer.

$ wo 'Floor[3.7]'
3
$ wo 'Floor[-3.7]'
-4
$ wo 'Floor[3.2]'
3
$ wo 'Floor[0]'
0
$ wo 'Floor[-0]'
0
$ wo 'Floor[0.5]'
0
$ wo 'Floor[-0.5]'
-1

Ceiling

Rounds up to the nearest integer.

$ wo 'Ceiling[3.2]'
4
$ wo 'Ceiling[-3.2]'
-3
$ wo 'Ceiling[3.7]'
4
$ wo 'Ceiling[0]'
0
$ wo 'Ceiling[-0]'
0
$ wo 'Ceiling[0.5]'
1
$ wo 'Ceiling[-0.5]'
0

Round

Rounds to the nearest integer.

$ wo 'Round[3.5]'
4
$ wo 'Round[3.4]'
3
$ wo 'Round[-3.5]'
-4
$ wo 'Round[-3.4]'
-3
$ wo 'Round[0.5]'
0
$ wo 'Round[-0.5]'
0
$ wo 'Round[0]'
0
$ wo 'Round[-0]'
0

Sqrt

Returns the square root of a number.

$ wo 'Sqrt[16]'
4
$ wo 'Sqrt[0]'
0

Mod

Returns the remainder when dividing the first argument by the second.

$ wo 'Mod[10, 3]'
1
$ wo 'Mod[7, 4]'
3
$ wo 'Mod[15, 5]'
0
$ wo 'Mod[8, 3]'
2
$ wo 'Mod[-1, 3]'
2
$ wo 'Mod[-5, 3]'
1
$ wo 'Mod[10, -3]'
-2
$ wo 'Mod[7.5, 2]'
1.5
$ wo 'Mod[0, 5]'
0

Max

Returns the maximum value from a set of arguments or a list.

Multiple arguments

$ wo 'Max[1, 5, 3]'
5
$ wo 'Max[1, 2]'
2
$ wo 'Max[-5, -2, -8]'
-2
$ wo 'Max[3.14, 2.71, 3.5]'
3.5
$ wo 'Max[1, 2.5, 3]'
3

Single list argument

$ wo 'Max[{1, 5, 3}]'
5
$ wo 'Max[{-10, -5, -20}]'
-5
$ wo 'Max[{3.14, 2.71, 3.5}]'
3.5

Single value

$ wo 'Max[42]'
42
$ wo 'Max[-7]'
-7

Empty list

$ wo 'Max[{}]'
-Infinity

With expressions

$ wo 'Max[2 + 3, 4 * 2, 10 - 1]'
9
$ wo 'Max[{1 + 1, 2 * 2, 3 - 1}]'
4

Min

Returns the minimum value from a set of arguments or a list.

Multiple arguments

$ wo 'Min[1, 5, 3]'
1
$ wo 'Min[1, 2]'
1
$ wo 'Min[-5, -2, -8]'
-8
$ wo 'Min[3.14, 2.71, 3.5]'
2.71
$ wo 'Min[1, 2.5, 3]'
1

Single list argument

$ wo 'Min[{1, 5, 3}]'
1
$ wo 'Min[{-10, -5, -20}]'
-20
$ wo 'Min[{3.14, 2.71, 3.5}]'
2.71

Single value

$ wo 'Min[42]'
42
$ wo 'Min[-7]'
-7

Empty list

$ wo 'Min[{}]'
Infinity

With expressions

$ wo 'Min[2 + 3, 4 * 2, 10 - 1]'
5
$ wo 'Min[{1 + 1, 2 * 2, 3 - 1}]'
2

Sin

Returns the sine of an angle in radians.

$ wo 'Sin[Pi/2]'
1

NumberQ

Returns True if expr is a number, and False otherwise.

$ wo 'NumberQ[2]'
True

MemberQ

Checks if an element is in a list.

$ wo 'MemberQ[{1, 2}, 2]'
True
$ wo 'MemberQ[{1, 2}, 3]'
False

RandomInteger

RandomInteger[]

Randomly gives 0 or 1.

$ wo 'MemberQ[{0, 1}, RandomInteger[]]'
True

RandomInteger[{1, 6}]

Randomly gives a number between 1 and 6.

$ wo 'MemberQ[{1, 2, 3, 4, 5, 6}, RandomInteger[{1, 6}]]'
True

RandomInteger[{1, 6}, 50]

Randomly gives 50 numbers between 1 and 6.

$ wo 'AllTrue[RandomInteger[{1, 6}, 50], 1 <= # <= 6 &]'
True

Power

Power[2, 3]

2 raised to the power of 3 equals 8.

$ wo 'Power[2, 3]'
8

Power[5, 0]

Any number raised to the power of 0 equals 1.

$ wo 'Power[5, 0]'
1

0^0

0 raised to 0 is Indeterminate.

$ wo '0^0'

                                        0
Power::indet: Indeterminate expression 0  encountered.
Indeterminate
$ wo 'Power[0, 0]'

                                        0
Power::indet: Indeterminate expression 0  encountered.
Indeterminate
$ wo '0.0^0'

                                         0
Power::indet: Indeterminate expression 0.  encountered.
Indeterminate

Power[2, -1]

2 raised to the power of -1 equals 0.5 (1/2).

$ wo 'Power[2, -1]'
1/2

Power[4, 0.5]

4 raised to the power of 0.5 equals 2 (square root).

$ wo 'Power[4, 0.5]'
2.

Power[10, 2]

10 raised to the power of 2 equals 100.

$ wo 'Power[10, 2]'
100

Power[-2, 3]

-2 raised to the power of 3 equals -8.

$ wo 'Power[-2, 3]'
-8

Power[-2, 2]

-2 raised to the power of 2 equals 4.

$ wo 'Power[-2, 2]'
4

Power[27, 1/3]

27 raised to the power of 1/3 equals approximately 3 (cube root).

$ wo 'Power[27, 1/3]'
3

Power[1.5, 2.5]

1.5 raised to the power of 2.5 equals approximately 2.756.

$ wo 'Power[1.5, 2.5]'
2.7556759606310752

Factorial

Factorial[0]

The factorial of 0 is 1 by definition.

$ wo 'Factorial[0]'
1

Factorial[1]

The factorial of 1 is 1.

$ wo 'Factorial[1]'
1

Factorial[5]

The factorial of 5 is 120 (5! = 5 × 4 × 3 × 2 × 1).

$ wo 'Factorial[5]'
120

Factorial[10]

The factorial of 10 is 3628800.

$ wo 'Factorial[10]'
3628800

Factorial[3]

The factorial of 3 is 6 (3! = 3 × 2 × 1).

$ wo 'Factorial[3]'
6

Factorial[7]

The factorial of 7 is 5040.

$ wo 'Factorial[7]'
5040

Factorial[12]

The factorial of 12 is 479001600.

$ wo 'Factorial[12]'
479001600

Factorial[2]

The factorial of 2 is 2.

$ wo 'Factorial[2]'
2

Factorial[4]

The factorial of 4 is 24 (4! = 4 × 3 × 2 × 1).

$ wo 'Factorial[4]'
24

GCD

GCD[12, 8]

The GCD of 12 and 8 is 4.

$ wo 'GCD[12, 8]'
4

GCD[48, 18]

The GCD of 48 and 18 is 6.

$ wo 'GCD[48, 18]'
6

GCD[100, 50]

The GCD of 100 and 50 is 50.

$ wo 'GCD[100, 50]'
50

GCD[17, 19]

The GCD of 17 and 19 is 1 (coprime numbers).

$ wo 'GCD[17, 19]'
1

GCD[0, 5]

The GCD of 0 and any number n is |n|.

$ wo 'GCD[0, 5]'
5

GCD[15, 25, 35]

The GCD of 15, 25, and 35 is 5.

$ wo 'GCD[15, 25, 35]'
5

GCD[24, 36, 60]

The GCD of 24, 36, and 60 is 12.

$ wo 'GCD[24, 36, 60]'
12

GCD[-12, 8]

The GCD works with negative numbers (GCD of -12 and 8 is 4).

$ wo 'GCD[-12, 8]'
4

GCD[21, 14]

The GCD of 21 and 14 is 7.

$ wo 'GCD[21, 14]'
7

Surd

Real-valued nth root.

$ wo 'Surd[8, 3]'
2
$ wo 'Surd[27, 3]'
3
$ wo 'Surd[16, 4]'
2
$ wo 'Surd[-8, 3]'
-2
$ wo 'Surd[x, 3]'
Surd[x, 3]

Gamma

Gamma function. For positive integers, Gamma[n] = (n-1)!.

$ wo 'Gamma[1]'
1
$ wo 'Gamma[5]'
24
$ wo 'Gamma[6]'
120
$ wo 'Gamma[0]'
ComplexInfinity
$ wo 'Gamma[-1]'
ComplexInfinity
$ wo 'Gamma[x]'
Gamma[x]

RealDigits

Extracts the decimal digits and exponent of a real number.

$ wo 'RealDigits[Pi, 10, 20]'
{{3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2, 3, 8, 4}, 1}

Extract just the digit list with [[1]]:

$ wo 'RealDigits[Pi, 10, 10][[1]]'
{3, 1, 4, 1, 5, 9, 2, 6, 5, 3}

Works with rationals:

$ wo 'RealDigits[1/7, 10, 12]'
{{1, 4, 2, 8, 5, 7, 1, 4, 2, 8, 5, 7}, 0}

IntegerDigits

Returns the list of digits of an integer.

$ wo 'IntegerDigits[12345]'
{1, 2, 3, 4, 5}

With base 2 (binary):

$ wo 'IntegerDigits[255, 2]'
{1, 1, 1, 1, 1, 1, 1, 1}

FromDigits

Constructs an integer from its digits.

$ wo 'FromDigits[{1, 2, 3, 4, 5}]'
12345

With base 2 (binary):

$ wo 'FromDigits[{1, 1, 1, 1, 1, 1, 1, 1}, 2]'
255

FactorInteger

Returns the prime factorization as {prime, exponent} pairs.

$ wo 'FactorInteger[60]'
{{2, 2}, {3, 1}, {5, 1}}
$ wo 'FactorInteger[100]'
{{2, 2}, {5, 2}}
$ wo 'FactorInteger[17]'
{{17, 1}}
$ wo 'FactorInteger[2^128 - 1]'
{{3, 1}, {5, 1}, {17, 1}, {257, 1}, {641, 1}, {65537, 1}, {274177, 1}, {6700417, 1}, {67280421310721, 1}}

NumericQ

Tests if an expression has a numeric value.

$ wo 'NumericQ[42]'
True
$ wo 'NumericQ["hello"]'
False
$ wo 'NumericQ[3.14]'
True

Re

Returns the real part of a number. For real numbers, returns the number itself.

$ wo 'Re[5]'
5
$ wo 'Re[3.14]'
3.14
$ wo 'Re[3 + 4*I]'
3

Im

Returns the imaginary part of a number. For real numbers, returns 0.

$ wo 'Im[5]'
0
$ wo 'Im[3.14]'
0
$ wo 'Im[3 + 4*I]'
4

Conjugate

Returns the complex conjugate. For real numbers, returns the number itself.

$ wo 'Conjugate[5]'
5

Rationalize

Converts a decimal number to a rational approximation.

$ wo 'Rationalize[0.5]'
1/2
$ wo 'Rationalize[0.333333]'
0.333333
$ wo 'Rationalize[0.25]'
1/4
$ wo 'Rationalize[3]'
3

With a tolerance argument, finds a rational within that tolerance:

$ wo 'Rationalize[0.333333, 0.0001]'
1/3
$ wo 'Rationalize[0.333333, 0.00001]'
1/3

Numbers with up to 5 decimal places are rationalized:

$ wo 'Rationalize[0.33333]'
33333/100000

Arg

Returns the argument (phase angle) of a complex number in radians.

$ wo 'Arg[1]'
0
$ wo 'Arg[-1]'
Pi
$ wo 'Arg[0]'
0

Divisors

Returns all divisors of an integer.

$ wo 'Divisors[12]'
{1, 2, 3, 4, 6, 12}
$ wo 'Divisors[1]'
{1}
$ wo 'Divisors[17]'
{1, 17}

DivisorSigma

Returns the sum of the k-th powers of divisors.

$ wo 'DivisorSigma[0, 12]'
6
$ wo 'DivisorSigma[1, 12]'
28
$ wo 'DivisorSigma[1, 6]'
12

MoebiusMu

Returns the Möbius function value.

$ wo 'MoebiusMu[1]'
1
$ wo 'MoebiusMu[2]'
-1
$ wo 'MoebiusMu[6]'
1
$ wo 'MoebiusMu[4]'
0
$ wo 'MoebiusMu[30]'
-1

EulerPhi

Returns Euler’s totient function (count of integers up to n that are coprime to n).

$ wo 'EulerPhi[1]'
1
$ wo 'EulerPhi[10]'
4
$ wo 'EulerPhi[12]'
4
$ wo 'EulerPhi[7]'
6

CoprimeQ

Tests if two integers are coprime.

$ wo 'CoprimeQ[3, 5]'
True
$ wo 'CoprimeQ[6, 9]'
False
$ wo 'CoprimeQ[14, 15]'
True

Linear Algebra

Dot

$ wo 'Dot[{1, 2, 3}, {4, 5, 6}]'
32
$ wo 'Dot[{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}]'
{{19, 22}, {43, 50}}

Det

$ wo 'Det[{{1, 2}, {3, 4}}]'
-2

Inverse

$ wo 'Inverse[{{1, 2}, {3, 4}}]'
{{-2, 1}, {3/2, -1/2}}

Tr

$ wo 'Tr[{{1, 2}, {3, 4}}]'
5

IdentityMatrix

$ wo 'IdentityMatrix[3]'
{{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}

DiagonalMatrix

$ wo 'DiagonalMatrix[{1, 2, 3}]'
{{1, 0, 0}, {0, 2, 0}, {0, 0, 3}}

Cross

$ wo 'Cross[{1, 2, 3}, {4, 5, 6}]'
{-3, 6, -3}

Utility Functions

Unitize

$ wo 'Unitize[{0, 1, -3, 0, 5}]'
{0, 1, 1, 0, 1}

Ramp

$ wo 'Ramp[{-2, -1, 0, 1, 2}]'
{0, 0, 0, 1, 2}

KroneckerDelta

$ wo 'KroneckerDelta[1, 1]'
1
$ wo 'KroneckerDelta[1, 2]'
0

UnitStep

$ wo 'UnitStep[{-1, 0, 1}]'
{0, 1, 1}

Reap / Sow

$ wo 'Reap[Sow[1]; Sow[2]; 42]'
{42, {{1, 2}}}
$ wo 'Reap[42]'
{42, {}}