## 5 Algebra with Xcas

### 5.1 Integer arithmetic

Integers
irem | remainder
iquo | quotient
iquorem | quotient and remainder
ifactor | prime factorization
ifactors | list of prime factors
idivis | list of divisors
gcd | greatest common divisor
lcm | least common multiple
iegcd | Bezout’s identity
isprime | primality test
nextprime | next prime number
previousprime | previous prime number
a%p | a modulo p
powmod(a,n,p) | an modulo p

Xcas has the usual number theoretic functions. The iquo command will find the integer quotient of two integers and irem will find the remainder. The iquorem command will return a list of both the quotient and remainder; if you enter

> iquorem(30,7)

you will get

[4,2]

since 30 divided by 7 is 4 with a remainder of 2.

The gcd and lcm commands will find the greatest common divisor and least common multiple of two integers. If you enter

> gcd(72,120)

you will get

24

The greatest common divisor d of two integers a and b can always be written in the form a*u + b*v = d for integers u and v. (This is known as Bézout’s Identity.) The iegcd will return the coefficients u and v as well as the greatest commond divisor. If you enter

> iegcd(72,120)

you will get

[2,−1,24]

since

72· 2 + 120·(−1) = 24

The ifactor command will give the prime factorization of an integer; if you enter

> ifactor(250)

you will get

2*53

You can use ifactors to get a list of the prime factors of an integer, where in the list each factor is followed by its multiplicity. If you enter

> ifactors(250)

you will get

[2,1,5,3]

since 250 has a prime factor of 2 (it has 1 factor of 2) and a prime factor of 5 (it has 3 factors of 5). The idivis command will return a complete list of factors;

> idivis(250)

will return

[1,2,5,10,25,50,125,250]

The subject of primes is a difficult one, and you should see the manual for a discussion of how Xcas checks for primes. But the command isprime will return true or false depending on whether or not you enter a prime. If you enter

> isprime(37)

you will get

true

since 37 is a prime number. The commands nextprime and previousprime will find the first prime after (or before) the number that you give it; if you enter

> nextprime(37)

you will get

41

since the first prime after 37 is 41.

Integers modulo p are defined by putting % p after them. Once an integer modulo p is defined, then any calculations done with it are done in ℤ/pℤ. For example, if you define

> a := 3 % 5

then

> a*2

will return

1 % 5

(since 6 mod 5 is reduced to 1 mod 5);

> 1/a

will return

2 % 5

etc. The powermod or powmod functions can be used to efficiently calculate powers modulo a number.

### 5.2 Polynomials and rational functions

Polynomials
normal | normal form (expanded and reduced)
expand | expanded form
ptayl | Taylor form
peval or horner | evaluation using Horner’s method
canonical_form | canonical form for a trinomial
coeff | list of coefficients
poly2symb | transform an algebraic polynomial to list form
symb2poly | transform the list form of a polynomial to algebraic form
pcoeff | return the polynomial (list form) given a list of zeroes
degree | degree
lcoeff | the coefficient of the leading term
valuation | the lowest degree of the terms
tcoeff | the coefficient of the term with the lowest degree
factor | prime factorization
factors | list of prime factors
divis | list of divisors
froot | roots with multiplicities
proot | approximate values of the roots
sturmab | the number of roots in an interval
getNum | the numerator of a rational function
getDenom | the denominator of a rational function
propfrac | writes a rational expression as a whole part and a proper rational part
partfrac | partial fraction decomposition
quo | quotient
rem | remainder
gcd | greatest common divisor
lcm | least common multiple
egcd | Bezout’s identity
divpc | Taylor polynomial for a rational expression
randpoly | random polynomial
cyclotomic | cyclotomic polynomial
lagrange | Lagrange polynomials
hermite | Hermite polynomials
laguerre | Laguerre polynomials
tchebyshev1 | Tchebyshev polynomials
tchebyshev2 | Tchebyshev polynomials

Various polynomial operations are available in the Polynomials submenu of the Cmds menu.

The expand and normal operators will distribute multiplication across addition, and so expand a polynomial completely out. If you enter

> expand((x+1)*(x+2)^2)

you will get

x3+5*x2+8*x+4

Additionally, normal will reduce a rational expression to lowest terms; if you enter

> normal((x-1)^2/(x^2-1))

you will get

x−1
x+1

The factor operator will factor a polynomial. If you enter

> factor(x^3+6*x^2+3*x-10

you will get

(x−1)*(x+2)*(x+5)

The result often depends on the number field being used. For example, over the rational numbers the polynomial x4 − 1 factors as (x−1)(x+1)(x2 + 1), while over the complex numbers it factors as (x−1)(x+1)(x−i)(x+i). If the coefficients of a polynomial are exact fractions, then the factoring will be over the rationals. To factor over the complex numbers, you can configure Xcas to do complex factorization (see section 2.3, “Configuration”) or use the cfactor command. If the coefficients are in ℤ/pℤ then the polynomial will be factored over ℤ/pℤ.

### 5.3 Trigonometry

Trigonométrie
tlin | linearize
tcollect | linearize and regroup
texpand | expand
trig2exp | trigonometric to exponential
exp2trig | exponential to trigonometric
hyp2exp | hyperbolic to exponential

Xcas has the usual trigonometic functions, both circular and hyperbolic, as well as their inverses. It also has commands for manipulating trigonometric expressions; these are in the Trigo submenus of the Expression menu.

One example is the tlin command will write products and powers of sines and cosines as linear combinations of sin(n x)s and cos(n x)s. If you enter

> tlin(2*sin(x)^2*cos(3*x))

you will get

−

cos(x)
2

+ cos(3*x) −

cos(5*x)
2

The texpand command will take expressions involving sin(n x) and cos(n x) and write them in terms of powers if sin(x) and cos(x). If you enter

> texpand(sin(2*x)^2*cos(3*x))

you will get

16*cos(x)5*sin(x)2−12*cos(x)3*sin(x)2

### 5.4 Vectors and matrices

Vectors and matrices
v*w | scalar product
cross(v,w) | cross product
A*B | matrix product
A.*B | term by term product
1/A | inverse
tran | transpose
rank | rank
det | determinant
ker | basis for the kernel
image | base for the image
idn | identity matrix
ranm | matrix with random coefficients
makematrix | make a matrix from a function
matrix | make a matrix from a function
blockmatrix | combine matrices

A vector is a list of numbers, such as [2,3,5], and a matrix is a list of vectors all of the same length, such as [[1,2,3],[4,5,6]].

The usual matrix operations (addition, scalar multiplication, matrix multiplication) are done with the usual operators + and *. If you define

> A := [[1,2,3],[4,5,6],[7,8,9]]

> B := [[1,1,1],[2,2,2]]

then

> 3*A

will give you

⎛
⎜
⎜
⎝

3 | 6 | 9
12 | 15 | 18
21 | 24 | 27

⎞
⎟
⎟
⎠

and

> B*A

will give you

⎛
⎜
⎝

12 | 15 | 18
24 | 30 | 36

⎞
⎟
⎠

A vector can be regarded as a matrix with one row, except that if a matrix is multiplied on the right by a vector, the vector will be regarded as a column. In particular, if v and w are vectors of the same length, then v*w will return the scalar product.

The idn command will create an identity matrix;

> idn(2)

will return

⎛
⎜
⎝

1 | 0
0 | 1

⎞
⎟
⎠

You can also use makemat or matrix commands to build a matrix. They both require a real-valued function of two variables, the number of rows and the number of columns. The indices start at 0, and with the makemat the function comes first, with matrix the function comes last. Both

> makemat((j,k)->j+k,3,2)

and

> matrix(3,2,(j,k)->j+k)

produce

⎛
⎜
⎜
⎝

0 | 1
1 | 2
2 | 3

⎞
⎟
⎟
⎠

Several matrices can be combined into a larger matrix with the blockmatrix command. To arrange m * n matrices into m rows and n columns, you give blockmatrix the values m, n and a list of the matrices. If you enter

> A := [[1,2,3],[4,5,6]]

> B := [[1,2],[2,3]]

then

> blockmatrix(2,2,[A,B,B,A])

will give you

⎛
⎜
⎜
⎜
⎝

1 | 2 | 3 | 1 | 2
4 | 5 | 6 | 2 | 3
1 | 2 | 1 | 2 | 3
2 | 3 | 4 | 5 | 5

⎞
⎟
⎟
⎟
⎠

You can get the elements from a matrix by following the matrix with the indices in brackets, separated by commas. For A as above,

> A[1,2]

will return

6

You can extract a submatrix by using intervals of indices (the beginning and end index separated by two periods);

> A[0..1,1..2]

returns

⎛
⎜
⎝

2 | 3
5 | 6

⎞
⎟
⎠

Note that if you change one value of a matrix in Xcas, the entire matrix will be copied. If a program modifies parts of a large matrix one element at a time, this time can add up.

### 5.5 Linear systems

Linear systems
linsolve | solution of a linear system
simult | solutions of many linear systems
rref | Gauss-Jordan reduction

The linsolve command will solve a system of linear equations; its syntax is the same as that of solve (see section 4.4, “Solving equations”). If you enter

> linsolve([2*x + 3*y = 4, 5*x + 4*y = 3],[x,y])

you will get

[−1,2]

The simult command can also solve a system of linear equations; more generally, it can solve several systems with the same coefficient matrix. To solve the systems

A x=b1,…,A x=bk

you can enter

> simult(A,B)

where

B= | ⎛ ⎝ | b1⋯bk | ⎞ ⎠

The result will be a matrix whose jth column is the solution of A x=bj. For example, if you want to solve the systems

⎧
⎪
⎨
⎪
⎩

x | + | y | − | z | = | 1
x | − | y | + | z | = | 1
−x | + | y | + | z | = | −2

⎧
⎪
⎨
⎪
⎩

x | + | y | − | z | = | −2
x | − | y | + | z | = | 1
−x | + | y | + | z | = | 1

which both have the same matrix of coefficients

⎛
⎜
⎜
⎝

1 | 1 | −1
1 | −1 | 1
−1 | 1 | 1

⎞
⎟
⎟
⎠

you can create the matrix which has one column for each system

⎛
⎜
⎜
⎝

1 | −2
1 | 1
−2 | 1

⎞
⎟
⎟
⎠

If you enter

> simult([[1,1,-1],[1,-1,1],[-1,1,1]],[[1,-2],[1,1],[-2,1]])

you will get

⎛
⎜
⎜
⎝

1 | −1/2
−1/2 | −1/2
−1/2 | 1

⎞
⎟
⎟
⎠

The solution to the first system is the first column, x=1,y=−1/2,z=−1/2, and the solution to the second system is the second column, x=−1/2,y=−1/2,z=1.

When there are no solutions, linsolve will return the empty list while simult will return an error. When there are infinitely many solutions, linsolve will return formulas for all solutions while simult will return one solution.

### 5.6 Matrix reduction

Matrix reduction
jordan | diagonalization or Jordan reduction
pcar | characteristic polynomial (list form)
pmin | minimal polynomial (list form)
eigenvals | eigenvalues
eigenvects | eigenvectors

The jordan command will take a matrix A and returns a transition matrix P and a matrix J in Jordan canonical form, so that P−1 A P = J. In particular, if A is diagonalizable, then J will be diagonal with the eigenvalues of A on the diagonal and the columns of P will be the corresponding eigenvectors. If you enter

> jordan([[4,1],[-8,-5]])

you will get

⎛
⎜
⎝

⎛
⎜
⎝

1 | 1
−1 | −8

⎞
⎟
⎠

,

⎛
⎜
⎝

3 | 0
0 | −4

⎞
⎟
⎠

⎞
⎟
⎠

This means that 3 and −4 (the diagonal elements of the second matrix) are the eigenvalues of (

4 | 1
−8 | −5

) and the corresponding eigenvectors are (

1
−1

) and (

1
−8

) (the columns of the first matrix). For diagonalizable matrices you can also get this information with the eigenvals and eigenvects commands;

> eigenvals([[4,1],[-8,-5]])

will return

(3,−4)

and

> eigenvects([[4,1],[-8,-5]])

will return

⎛
⎜
⎝

1 | 1
−1 | −8

⎞
⎟
⎠

For matrices with exact and symbolic values, the only eigenvalues used are those computable with solve; for matrices with floating point numbers, a numerical algorithm is used to find the eigenvalues. This algorithm may fail in some cases where there are very close eigenvalues or eigenvalues with multiplicity greater than one.

If a function is defined by a polynomial, you can evaluate it with an argument of a square matrix. If a function is given by a series, the Jordan form of the matrix can be used to define the value of the function at a matrix. For example, you can find the exponential of a square matrix;

> exp([[0,-1],[1,2]])

will result in

⎛
⎜
⎝

0 | −exp(1)
exp(1) | 2*exp(1)

⎞
⎟
⎠