In this chapter, some facilities for doing calculus are described. These include functions implementing differentiation, integration, standard mathematical functions, and solving of equations.
Sin(x)
trigonometric sine function
- param x
argument to the function, in radians
This function represents the trigonometric function sine. Yacas leaves expressions alone even if x is a number, trying to keep the result as exact as possible. The floating point approximations of these functions can be forced by using the {N} function. Yacas knows some trigonometric identities, so it can simplify to exact results even if {N} is not used. This is the case, for instance, when the argument is a multiple of
Sin
is threaded <threaded function>
.
- Example
In> Sin(1)
Out> Sin(1);
In> N(Sin(1),20)
Out> 0.84147098480789650665;
In> Sin(Pi/4)
Out> Sqrt(2)/2;
Cos
, Tan
, ArcSin
, ArcCos
, ArcTan
, N
, Pi
Cos(x)
trigonometric cosine function
- param x
argument to the function, in radians
This function represents the trigonometric function cosine. Yacas leaves expressions alone even if x is a number, trying to keep the result as exact as possible. The floating point approximations of these functions can be forced by using the {N} function. Yacas knows some trigonometric identities, so it can simplify to exact results even if {N} is not used. This is the case, for instance, when the argument is a multiple of
Cos
isthreaded <threaded function>
.
- Example
In> Cos(1)
Out> Cos(1);
In> N(Cos(1),20)
Out> 0.5403023058681397174;
In> Cos(Pi/4)
Out> Sqrt(1/2);
Sin
, Tan
, ArcSin
, ArcCos
, ArcTan
, N
, Pi
Tan(x)
trigonometric tangent function
- param x
argument to the function, in radians
This function represents the trigonometric function tangent. Yacas leaves expressions alone even if x is a number, trying to keep the result as exact as possible. The floating point approximations of these functions can be forced by using the {N} function. Yacas knows some trigonometric identities, so it can simplify to exact results even if {N} is not used. This is the case, for instance, when the argument is a multiple of
- Example
In> Tan(1)
Out> Tan(1);
In> N(Tan(1),20)
Out> 1.5574077246549022305;
In> Tan(Pi/4)
Out> 1;
Sin
, Cos
, ArcSin
, ArcCos
, ArcTan
, N
, Pi
ArcSin(x)
inverse trigonometric function arc-sine
- param x
argument to the function
This function represents the inverse trigonometric function arcsine. For instance, the value of
- Example
In> ArcSin(1)
Out> Pi/2;
In> ArcSin(1/3)
Out> ArcSin(1/3);
In> Sin(ArcSin(1/3))
Out> 1/3;
In> x:=N(ArcSin(0.75))
Out> 0.848062;
In> N(Sin(x))
Out> 0.7499999477;
Sin
, Cos
, Tan
, N
, Pi
, Ln
, ArcCos
, ArcTan
ArcCos(x)
inverse trigonometric function arc-cosine
- param x
argument to the function
This function represents the inverse trigonometric function arc-cosine. For instance, the value of
- Example
In> ArcCos(0)
Out> Pi/2
In> ArcCos(1/3)
Out> ArcCos(1/3)
In> Cos(ArcCos(1/3))
Out> 1/3
In> x:=N(ArcCos(0.75))
Out> 0.7227342478
In> N(Cos(x))
Out> 0.75
Sin
, Cos
, Tan
, N
, Pi
, Ln
, ArcSin
, ArcTan
ArcTan(x)
inverse trigonometric function arc-tangent
- param x
argument to the function
This function represents the inverse trigonometric function arctangent. For instance, the value of
- Example
In> ArcTan(1)
Out> Pi/4
In> ArcTan(1/3)
Out> ArcTan(1/3)
In> Tan(ArcTan(1/3))
Out> 1/3
In> x:=N(ArcTan(0.75))
Out> 0.643501108793285592213351264945231378078460693359375
In> N(Tan(x))
Out> 0.75
Sin
, Cos
, Tan
, N
, Pi
, Ln
, ArcSin
, ArcCos
Exp(x)
exponential function
- param x
argument to the function
This function calculates ex where e is the mathematic constant 2.71828... One can use Exp(1)
to represent e. This function is threaded function
, meaning that if the argument x
is a list, the function is applied to all entries in the list.
- Example
In> Exp(0)
Out> 1;
In> Exp(I*Pi)
Out> -1;
In> N(Exp(1))
Out> 2.7182818284;
Ln
, Sin
, Cos
, Tan
, N
Ln(x)
natural logarithm
- param x
argument to the function
This function calculates the natural logarithm of "x". This is the inverse function of the exponential function, {Exp}, i.e.
- Example
In> Ln(1)
Out> 0;
In> Ln(Exp(x))
Out> x;
In> D(x) Ln(x)
Out> 1/x;
Exp
, Arg
Sqrt(x)
square root
- param x
argument to the function
This function calculates the square root of "x". If the result is not rational, the call is returned unevaluated unless a numerical approximation is forced with the {N} function. This function can also handle negative and complex arguments. This function is threaded, meaning that if the argument {x} is a list, the function is applied to all entries in the list.
- Example
In> Sqrt(16)
Out> 4;
In> Sqrt(15)
Out> Sqrt(15);
In> N(Sqrt(15))
Out> 3.8729833462;
In> Sqrt(4/9)
Out> 2/3;
In> Sqrt(-1)
Out> Complex(0,1);
Exp
, ^
, N
Abs(x)
absolute value or modulus of complex number
- param x
argument to the function
This function returns the absolute value (also called the modulus) of "x". If "x" is positive, the absolute value is "x" itself; if "x" is negative, the absolute value is "-x". For complex "x", the modulus is the "r" in the polar decomposition x = reıϕ. This function is connected to the {Sign} function by the identity Abs(x) * Sign(x) = x
for real "x". This function is threaded, meaning that if the argument {x} is a list, the function is applied to all entries in the list.
- Example
In> Abs(2);
Out> 2;
In> Abs(-1/2);
Out> 1/2;
In> Abs(3+4*I);
Out> 5;
Sign
, Arg
Sign(x)
sign of a number
- param x
argument to the function
This function returns the sign of the real number
- Example
In> Sign(2)
Out> 1;
In> Sign(-3)
Out> -1;
In> Sign(0)
Out> 1;
In> Sign(-3) * Abs(-3)
Out> -3;
Arg
, Abs
bodied D(expression, variable[,n=1])
derivative
- param variable
variable
- param expression
expression to take derivatives of
- param n
order
- returns
n
-th derivative ofexpression
with respect tovariable
bodied D(expression, variable)
derivative
- param variable
variable
- param list
a list of variables
- param expression
expression to take derivatives of
- param n
order of derivative
- returns
derivative of
expression
with respect tovariable
This function calculates the derivative of the expression {expr} with respect to the variable {var} and returns it. If the third calling format is used, the {n}-th derivative is determined. Yacas knows how to differentiate standard functions such as {Ln} and {Sin}. The {D} operator is threaded in both {var} and {expr}. This means that if either of them is a list, the function is applied to each entry in the list. The results are collected in another list which is returned. If both {var} and {expr} are a list, their lengths should be equal. In this case, the first entry in the list {expr} is differentiated with respect to the first entry in the list {var}, the second entry in {expr} is differentiated with respect to the second entry in {var}, and so on. The {D} operator returns the original function if
- Example
In> D(x)Sin(x*y)
Out> y*Cos(x*y);
In> D({x,y,z})Sin(x*y)
Out> {y*Cos(x*y),x*Cos(x*y),0};
In> D(x,2)Sin(x*y)
Out> -Sin(x*y)*y^2;
In> D(x){Sin(x),Cos(x)}
Out> {Cos(x),-Sin(x)};
Integrate
, Taylor
, Diverge
, Curl
Curl(vector, basis)
curl of a vector field
- param vector
vector field to take the curl of
- param basis
list of variables forming the basis
This function takes the curl of the vector field "vector" with respect to the variables "basis". The curl is defined in the usual way, Curl(f,x) = { D(x[2]) f[3] - D(x[3]) f[2], D(x[3]) f[1] - D(x[1]) f[3], D(x[1]) f[2] - D(x[2]) f[1] } Both "vector" and "basis" should be lists of length 3.
Diverge(vector, basis)
divergence of a vector field
- param vector
vector field to calculate the divergence of
- param basis
list of variables forming the basis
This function calculates the divergence of the vector field "vector" with respect to the variables "basis". The divergence is defined as Diverge(f,x) = D(x[1]) f[1] + ... + D(x[n]) f[n], where {n} is the length of the lists "vector" and "basis". These lists should have equal length.
bodied Integrate(expr, var) bodied Integrate(expr, var, x1, x2)
integral
- param expr
expression to integrate
- param var
atom, variable to integrate over
- param x1
first point of definite integration
- param x2
second point of definite integration
This function integrates the expression {expr} with respect to the variable {var}. In the case of definite integral, the integration is carried out from
- Example
In> Integrate(x,a,b) Cos(x)
Out> Sin(b)-Sin(a);
In> Integrate(x) Cos(x)
Out> Sin(x);
D
, UniqueConstant
bodied Limit(expr, var, val)
limit of an expression
- param var
variable
- param val
number or
Infinity
- param dir
direction (
Left
orRight
)- param expr
an expression
This command tries to determine the value that the expression "expr" converges to when the variable "var" approaches "val". One may use {Infinity} or {-Infinity} for "val". The result of {Limit} may be one of the symbols {Undefined} (meaning that the limit does not exist), {Infinity}, or {-Infinity}. The second calling sequence is used for unidirectional limits. If one gives "dir" the value {Left}, the limit is taken as "var" approaches "val" from the positive infinity; and {Right} will take the limit from the negative infinity.
- Example
In> Limit(x,0) Sin(x)/x
Out> 1;
In> Limit(x,0) (Sin(x)-Tan(x))/(x^3)
Out> -1/2;
In> Limit(x,0) 1/x
Out> Undefined;
In> Limit(x,0,Left) 1/x
Out> -Infinity;
In> Limit(x,0,Right) 1/x
Out> Infinity;
Random numbers
Random()
(pseudo-) random number generator
- param init
integer, initial seed value
- param option
atom, option name
- param value
atom, option value
- param r
a list, RNG object
These commands are an object-oriented interface to (pseudo-)random number generators (RNGs). {RngCreate} returns a list which is a well-formed RNG object. Its value should be saved in a variable and used to call {Rng} and {RngSeed}. {Rng(r)} returns a floating-point random number between 0 and 1 and updates the RNG object {r}. (Currently, the Gaussian option makes a RNG return a <i>complex</i> random number instead of a real random number.) {RngSeed(r,init)} re-initializes the RNG object {r} with the seed value {init}. The seed value should be a positive integer. The {RngCreate} function accepts several options as arguments. Currently the following options are available:
RandomIntegerMatrix(rows,cols,from,to)
generate a matrix of random integers
- param rows
number of rows in matrix
- param cols
number of cols in matrix
- param from
lower bound
- param to
upper bound
This function generates a {rows x cols} matrix of random integers. All entries lie between "from" and "to", including the boundaries, and are uniformly distributed in this interval.
- Example
In> PrettyForm( RandomIntegerMatrix(5,5,-2^10,2^10) )
/ \
| ( -506 ) ( 749 ) ( -574 ) ( -674 ) ( -106 ) |
| |
| ( 301 ) ( 151 ) ( -326 ) ( -56 ) ( -277 ) |
| |
| ( 777 ) ( -761 ) ( -161 ) ( -918 ) ( -417 ) |
| |
| ( -518 ) ( 127 ) ( 136 ) ( 797 ) ( -406 ) |
| |
| ( 679 ) ( 854 ) ( -78 ) ( 503 ) ( 772 ) |
\ /
RandomIntegerVector
, RandomPoly
RandomIntegerVector(nr, from, to)
generate a vector of random integers
- param nr
number of integers to generate
- param from
lower bound
- param to
upper bound
This function generates a list with "nr" random integers. All entries lie between "from" and "to", including the boundaries, and are uniformly distributed in this interval.
- Example
In> RandomIntegerVector(4,-3,3)
Out> {0,3,2,-2};
Random
, RandomPoly
RandomPoly(var,deg,coefmin,coefmax)
construct a random polynomial
- param var
free variable for resulting univariate polynomial
- param deg
degree of resulting univariate polynomial
- param coefmin
minimum value for coefficients
- param coefmax
maximum value for coefficients
RandomPoly generates a random polynomial in variable "var", of degree "deg", with integer coefficients ranging from "coefmin" to "coefmax" (inclusive). The coefficients are uniformly distributed in this interval, and are independent of each other.
- Example
In> RandomPoly(x,3,-10,10)
Out> 3*x^3+10*x^2-4*x-6;
In> RandomPoly(x,3,-10,10)
Out> -2*x^3-8*x^2+8;
Random
, RandomIntegerVector
Add(val1, val2, ...)
find sum of a list of values
- param val1}, {val2
expressions
- param {list}
list of expressions to add
This function adds all its arguments and returns their sum. It accepts any number of arguments. The arguments can be also passed as a list.
- Example
In> Add(1,4,9);
Out> 14;
In> Add(1 .. 10);
Out> 55;
Sum(var, from, to, body)
find sum of a sequence
- param var
variable to iterate over
- param from
integer value to iterate from
- param to
integer value to iterate up to
- param body
expression to evaluate for each iteration
The command finds the sum of the sequence generated by an iterative formula. The expression "body" is evaluated while the variable "var" ranges over all integers from "from" up to "to", and the sum of all the results is returned. Obviously, "to" should be greater than or equal to "from". Warning: {Sum} does not evaluate its arguments {var} and {body} until the actual loop is run.
- Example
In> Sum(i, 1, 3, i^2);
Out> 14;
Factorize
Factorize(list)
product of a list of values
- param list
list of values to multiply
- param var
variable to iterate over
- param from
integer value to iterate from
- param to
integer value to iterate up to
- param body
expression to evaluate for each iteration
The first form of the {Factorize} command simply multiplies all the entries in "list" and returns their product. If the second calling sequence is used, the expression "body" is evaluated while the variable "var" ranges over all integers from "from" up to "to", and the product of all the results is returned. Obviously, "to" should be greater than or equal to "from".
- Example
In> Factorize({1,2,3,4});
Out> 24;
In> Factorize(i, 1, 4, i);
Out> 24;
Sum
, Apply
Taylor(var, at, order) expr
univariate Taylor series expansion
- param var
variable
- param at
point to get Taylor series around
- param order
order of approximation
- param expr
expression to get Taylor series for
This function returns the Taylor series expansion of the expression "expr" with respect to the variable "var" around "at" up to order "order". This is a polynomial which agrees with "expr" at the point "var = at", and furthermore the first "order" derivatives of the polynomial at this point agree with "expr". Taylor expansions around removable singularities are correctly handled by taking the limit as "var" approaches "at".
- Example
In> PrettyForm(Taylor(x,0,9) Sin(x))
3 5 7 9
x x x x
x - -- + --- - ---- + ------
6 120 5040 362880
Out> True;
D
, InverseTaylor
, ReversePoly
, BigOh
InverseTaylor(var, at, order) expr
Taylor expansion of inverse
- param var
variable
- param at
point to get inverse Taylor series around
- param order
order of approximation
- param expr
expression to get inverse Taylor series for
This function builds the Taylor series expansion of the inverse of the expression "expr" with respect to the variable "var" around "at" up to order "order". It uses the function {ReversePoly} to perform the task.
- Example
In> PrettyPrinter'Set("PrettyForm")
True
In> exp1 := Taylor(x,0,7) Sin(x)
3 5 7
x x x
x - -- + --- - ----
6 120 5040
In> exp2 := InverseTaylor(x,0,7) ArcSin(x)
5 7 3
x x x
--- - ---- - -- + x
120 5040 6
In> Simplify(exp1-exp2)
0
ReversePoly
, Taylor
, BigOh
ReversePoly(f, g, var, newvar, degree)
solve
- param f
function of
var
- param g
function of
var
- param var
a variable
- param newvar
a new variable to express the result in
- param degree
the degree of the required solution
This function returns a polynomial in "newvar", say "h(newvar)", with the property that "h(f(var))" equals "g(var)" up to order "degree". The degree of the result will be at most "degree-1". The only requirement is that the first derivative of "f" should not be zero. This function is used to determine the Taylor series expansion of the inverse of a function "f": if we take "g(var)=var", then "h(f(var))=var" (up to order "degree"), so "h" will be the inverse of "f".
- Example
In> f(x):=Eval(Expand((1+x)^4))
Out> True;
In> g(x) := x^2
Out> True;
In> h(y):=Eval(ReversePoly(f(x),g(x),x,y,8))
Out> True;
In> BigOh(h(f(x)),x,8)
Out> x^2;
In> h(x)
Out> (-2695*(x-1)^7)/131072+(791*(x-1)^6)/32768 +(-119*(x-1)^5)/4096+(37*(x-1)^4)/1024+(-3*(x-1)^3)/64+(x-1)^2/16;
InverseTaylor
, Taylor
, BigOh
BigOh(poly, var, degree)
drop all terms of a certain order in a polynomial
- param poly
a univariate polynomial
- param var
a free variable
- param degree
positive integer
This function drops all terms of order "degree" or higher in "poly", which is a polynomial in the variable "var".
- Example
In> BigOh(1+x+x^2+x^3,x,2)
Out> x+1;
Taylor
, InverseTaylor
LagrangeInterpolant(xlist, ylist, var)
polynomial interpolation
- param xlist
list of argument values
- param ylist
list of function values
- param var
free variable for resulting polynomial
This function returns a polynomial in the variable "var" which interpolates the points "(xlist, ylist)". Specifically, the value of the resulting polynomial at "xlist[1]" is "ylist[1]", the value at "xlist[2]" is "ylist[2]", etc. The degree of the polynomial is not greater than the length of "xlist". The lists "xlist" and "ylist" should be of equal length. Furthermore, the entries of "xlist" should be all distinct to ensure that there is one and only one solution. This routine uses the Lagrange interpolant formula to build up the polynomial.
- Example
In> f := LagrangeInterpolant({0,1,2}, \
{0,1,1}, x);
Out> (x*(x-1))/2-x*(x-2);
In> Eval(Subst(x,0) f);
Out> 0;
In> Eval(Subst(x,1) f);
Out> 1;
In> Eval(Subst(x,2) f);
Out> 1;
In> PrettyPrinter'Set("PrettyForm");
True
In> LagrangeInterpolant({x1,x2,x3}, {y1,y2,y3}, x)
y1 * ( x - x2 ) * ( x - x3 )
----------------------------
( x1 - x2 ) * ( x1 - x3 )
y2 * ( x - x1 ) * ( x - x3 )
+ ----------------------------
( x2 - x1 ) * ( x2 - x3 )
y3 * ( x - x1 ) * ( x - x2 )
+ ----------------------------
( x3 - x1 ) * ( x3 - x2 )
Subst
postfix !(n)
factorial
- param m
integer
- param n
integer, half-integer, or list
- param a}, {b
numbers
The factorial function {n!} calculates the factorial of integer or half-integer numbers. For nonnegative integers, $n! := n*(n-1)(n-2)...1$. The factorial of half-integers is defined via Euler's Gamma function, $z! := Gamma(z+1)$. If $n=0$ the function returns $1$. The "double factorial" function {n!!} calculates $n(n-2)(n-4)...$. This product terminates either with
- Example
In> 5!
Out> 120;
In> 1 * 2 * 3 * 4 * 5
Out> 120;
In> (1/2)!
Out> Sqrt(Pi)/2;
In> 7!!;
Out> 105;
In> 1/3 *** 10;
Out> 17041024000/59049;
In> Subfactorial(10)
Out> 1334961;
Bin
, Factorize
, Gamma
, !!
, ***
, Subfactorial
postfix !!(n)
double factorial
infix ***(x,y)
whatever
Bin(n, m)
binomial coefficients
- param n}, {m
integers
This function calculates the binomial coefficient "n" above "m", which equals
- Example
In> Bin(10, 4)
Out> 210;
In> 10! / (4! * 6!)
Out> 210;
!
, Eulerian
Eulerian(n,m)
Eulerian numbers
- param n}, {m
integers
The Eulerian numbers can be viewed as a generalization of the binomial coefficients, and are given explicitly by $$ Sum(j,0,k+1,(-1)^j*Bin(n+1,j)*(k-j+1)^n) $$ .
- Example
In> Eulerian(6,2)
Out> 302;
In> Eulerian(10,9)
Out> 1;
Bin
LeviCivita(list)
totally anti-symmetric Levi-Civita symbol
- param list
a list of integers 1 .. n in some order
{LeviCivita} implements the Levi-Civita symbol. This is generally useful for tensor calculus. {list} should be a list of integers, and this function returns 1 if the integers are in successive order, eg. {LeviCivita( {1,2,3,...} )} would return 1. Swapping two elements of this list would return -1. So, {LeviCivita( {2,1,3} )} would evaluate to -1.
- Example
In> LeviCivita({1,2,3})
Out> 1;
In> LeviCivita({2,1,3})
Out> -1;
In> LeviCivita({2,2,3})
Out> 0;
Permutations
Permutations(list)
get all permutations of a list
- param list
a list of elements
Permutations returns a list with all the permutations of the original list.
- Example
In> Permutations({a,b,c})
Out> {{a,b,c},{a,c,b},{c,a,b},{b,a,c},
{b,c,a},{c,b,a}};
LeviCivita
Gamma(x)
Euler's Gamma function
- param x
expression
- param number
expression that can be evaluated to a number
{Gamma(x)} is an interface to Euler's Gamma function
- Example
In> Gamma(1.3)
Out> Gamma(1.3);
In> N(Gamma(1.3),30)
Out> 0.897470696306277188493754954771;
In> Gamma(1.5)
Out> Sqrt(Pi)/2;
In> N(Gamma(1.5),30);
Out> 0.88622692545275801364908374167;
!
, N
, gamma
Zeta(x)
Riemann's Zeta function
- param x
expression
- param number
expression that can be evaluated to a number
{Zeta(x)} is an interface to Riemann's Zeta function
- Example
In> Precision(30)
Out> True;
In> Zeta(1)
Out> Infinity;
In> Zeta(1.3)
Out> Zeta(1.3);
In> N(Zeta(1.3))
Out> 3.93194921180954422697490751058798;
In> Zeta(2)
Out> Pi^2/6;
In> N(Zeta(2));
Out> 1.64493406684822643647241516664602;
!
, N
Bernoulli(index)
Bernoulli numbers and polynomials
- param x
expression that will be the variable in the polynomial
- param index
expression that can be evaluated to an integer
{Bernoulli(n)} evaluates the
Euler(index)
Euler numbers and polynomials
- param x
expression that will be the variable in the polynomial
- param index
expression that can be evaluated to an integer
{Euler(n)} evaluates the
- Example
In> Euler(6)
Out> -61;
In> A:=Euler(5,x)
Out> (x-1/2)^5+(-10*(x-1/2)^3)/4+(25*(x-1/2))/16;
In> Simplify(A)
Out> (2*x^5-5*x^4+5*x^2-1)/2;
Bin
LambertW(x)
Lambert's W function
- param x
expression, argument of the function
Lambert's W function is (a multiple-valued, complex function) defined for any (complex) z by
W(z)eW(z) = z
The W function is sometimes useful to represent solutions of transcendental equations. For example, the equation
- Example
In> LambertW(0)
Out> 0;
In> N(LambertW(-0.24/Sqrt(3*Pi)))
Out> -0.0851224014;
Exp