Previous Up Next

5  Algebra with Xcas

5.1  Integer arithmetic

Integers
iremremainder
iquoquotient
iquoremquotient and remainder
ifactorprime factorization
ifactorslist of prime factors
idivislist of divisors
gcdgreatest common divisor
lcmleast common multiple
iegcdBezout’s identity
isprimeprimality test
nextprimenext prime number
previousprimeprevious prime number
a%pa 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
normalnormal form (expanded and reduced)
expandexpanded form
ptaylTaylor form
peval or hornerevaluation using Horner’s method
canonical_formcanonical form for a trinomial
coefflist of coefficients
poly2symbtransform an algebraic polynomial to list form
symb2polytransform the list form of a polynomial to algebraic form
pcoeffreturn the polynomial (list form) given a list of zeroes
degreedegree
lcoeffthe coefficient of the leading term
valuationthe lowest degree of the terms
tcoeffthe coefficient of the term with the lowest degree
factorprime factorization
factorslist of prime factors
divislist of divisors
frootroots with multiplicities
prootapproximate values of the roots
sturmabthe number of roots in an interval
getNumthe numerator of a rational function
getDenomthe denominator of a rational function
propfracwrites a rational expression as a whole part and a proper rational part
partfracpartial fraction decomposition
quoquotient
remremainder
gcdgreatest common divisor
lcmleast common multiple
egcdBezout’s identity
divpcTaylor polynomial for a rational expression
randpolyrandom polynomial
cyclotomiccyclotomic polynomial
lagrangeLagrange polynomials
hermiteHermite polynomials
laguerreLaguerre polynomials
tchebyshev1Tchebyshev polynomials
tchebyshev2Tchebyshev 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)(xi)(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
tlinlinearize
tcollectlinearize and regroup
texpandexpand
trig2exptrigonometric to exponential
exp2trigexponential to trigonometric
hyp2exphyperbolic 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*wscalar product
cross(v,w)cross product
A*Bmatrix product
A.*Bterm by term product
1/Ainverse
trantranspose
rankrank
detdeterminant
kerbasis for the kernel
imagebase for the image
idnidentity matrix
ranmmatrix with random coefficients
makematrixmake a matrix from a function
matrixmake a matrix from a function
blockmatrixcombine 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




369
121518
212427



and

B*A

will give you



121518
243036


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



10
01


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




01
12
23



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





12312
45623
12123
23455




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



23
56


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
linsolvesolution of a linear system
simultsolutions of many linear systems
rrefGauss-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

Ax = b1,…,Ax=bk

you can enter

simult(A,B)

where

B = 
b1 ⋯ bk

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





 x+yz=1
 xy+z=
 −x+y+z=−2 




 x+yz=−2
 xy+z=
 −x+y+z=

which both have the same matrix of coefficients




  11−1
  1−11
  −111



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




  1−2
  11
  −21



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/21



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
jordandiagonalization or Jordan reduction
pcarcharacteristic polynomial (list form)
pminminimal polynomial (list form)
eigenvalseigenvalues
eigenvectseigenvectors

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





11
−1−8 


,


30
0−4 




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

41
−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



11
−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)



Previous Up Next