6
Information Processing Letters 29 (1988) 241-246 North-Holland 24 November 1988 A CANONICAL SIMPLIFIER FOR TRIGONOMETRIC EXPRESSIONS IN THE KINEMATIC EQUATION * Peter SCHORN Department of Computer Science, Unruersity of North Carolina, Chapel Hill, NC 27599.3175, lJ.S.A Communicated by J. Nievergelt Received 20 December 1987 Revised 15 February 1988 and 1 May 1988 We present a new canonical simplifier for a class of trigonometric expressions that occur in the kinematic equation. The expressions consist of rational numbers and the function symbols + , -, * , /, sin, and cos. Variables are not allowed and the arguments of sin and cos are limited to rational multiples of IT. We present an efficient algorithm which computes a simple normal form in the real domain, in contrast to the known methods which either produce unnecessarily complicated normal forms or are inefficient due to large integer arithmetic. The simplifier has successfully been applied in the field of robot kinematics. Keywords: Computer algebra, normal form, simplification, trigonometric expression, algebraic number, cyclotomic polynomial, kinematic equation 1. Motivation and definitions Our interest in simplifying trigonometric expressions arose from the problem of finding a closed solution to the kinematic equation for robots. Solving this equation means finding values for the joints of the robot so that a given position and orientation is reached. The kinematic equation is a matrix equation, whose left-hand side is a product of up to 24 4 X 4 matrices which describe movements and rotations in three-dimensional space. The right-hand side is a single matrix describing the position and orientation to be reached. It is important to know whether two or more adjacent joints are parallel. A rotational joint is described by a rotation around the z-axis. Therefore, two rotational joints are said to be parallel iff the remaining (constant) rotation between the (variable) joints is also a rotation around the z-axis. We seek an algorithm that decides whether the product of some rotational matrices yields a rotation around the z-axis. This is equivalent to the question whether the absolute value of element [3, 31 of the product matrix is 1. For example, lRot(x, 90”)Rot(z, 45”)Rot(x, 90”)Rot(z, 90”)Rot(x, 135”)[3, 31 1 = 1. Therefore, we are concerned with the class T of trigonometric expressions that consists of all rational expressions composed of cos, sin, and the integers where the arguments of cos and sin are rational multiples of 71. In the following we first give the necessary definitions. Then we state some results about cyclotomic polynomials and cosine-polynomials. The next section describes our simplification procedure. A compari- son with related results concludes the paper. * This work was partly supported by the National Science Foundation under Grant No. DCR-8518796. 0020-0190/88/$3.50 0 1988, Elsevier Science Publishers B.V. (North-Holland) 241

A canonical simplifier for trigonometric expressions in the kinematic equation

Embed Size (px)

Citation preview

Page 1: A canonical simplifier for trigonometric expressions in the kinematic equation

Information Processing Letters 29 (1988) 241-246

North-Holland 24 November 1988

A CANONICAL SIMPLIFIER FOR TRIGONOMETRIC EXPRESSIONS IN THE KINEMATIC EQUATION *

Peter SCHORN

Department of Computer Science, Unruersity of North Carolina, Chapel Hill, NC 27599.3175, lJ.S.A

Communicated by J. Nievergelt Received 20 December 1987

Revised 15 February 1988 and 1 May 1988

We present a new canonical simplifier for a class of trigonometric expressions that occur in the kinematic equation. The

expressions consist of rational numbers and the function symbols + , -, * , /, sin, and cos. Variables are not allowed and the arguments of sin and cos are limited to rational multiples of IT. We present an efficient algorithm which computes a simple

normal form in the real domain, in contrast to the known methods which either produce unnecessarily complicated normal

forms or are inefficient due to large integer arithmetic. The simplifier has successfully been applied in the field of robot

kinematics.

Keywords: Computer algebra, normal form, simplification, trigonometric expression, algebraic number, cyclotomic polynomial, kinematic equation

1. Motivation and definitions

Our interest in simplifying trigonometric expressions arose from the problem of finding a closed solution to the kinematic equation for robots. Solving this equation means finding values for the joints of the robot so that a given position and orientation is reached. The kinematic equation is a matrix equation, whose left-hand side is a product of up to 24 4 X 4 matrices which describe movements and rotations in three-dimensional space. The right-hand side is a single matrix describing the position and orientation to be reached. It is important to know whether two or more adjacent joints are parallel. A rotational joint is described by a rotation around the z-axis. Therefore, two rotational joints are said to be parallel iff the remaining (constant) rotation between the (variable) joints is also a rotation around the z-axis. We seek an algorithm that decides whether the product of some rotational matrices yields a rotation around the z-axis. This is equivalent to the question whether the absolute value of element [3, 31 of the product matrix is 1. For example,

lRot(x, 90”)Rot(z, 45”)Rot(x, 90”)Rot(z, 90”)Rot(x, 135”)[3, 31 1 = 1.

Therefore, we are concerned with the class T of trigonometric expressions that consists of all rational expressions composed of cos, sin, and the integers where the arguments of cos and sin are rational multiples of 71.

In the following we first give the necessary definitions. Then we state some results about cyclotomic polynomials and cosine-polynomials. The next section describes our simplification procedure. A compari- son with related results concludes the paper.

* This work was partly supported by the National Science Foundation under Grant No. DCR-8518796.

0020-0190/88/$3.50 0 1988, Elsevier Science Publishers B.V. (North-Holland) 241

Page 2: A canonical simplifier for trigonometric expressions in the kinematic equation

Volume 29, Number 5 INFORMATION PROCESSING LETTERS 24 November 1988

Definition. Let T be a class of expressions and = an equivalence relation on T. A canonical simplifier S on T is an effective procedure which maps T into T and has the following properties:

(1) V’t E T: S(t) = t.

(2) V’s, t E T: s = t ==, S(s) = S(t).

Definition. Euler’s +-function is defined by

$(n):=({j:gcd(j,n)=landl<j<n}l.

For n E N, let F, = { zl,. . . , z+,(,)} be th e p rimitive roots of unity in C, i.e., e2V’J/n, gcd(j, n) = 1. Then the polynomial

Q*(Z):= I-I (z-z) z t Iy;,

is called the n th cyclotomic polynomial,

Definition

is a cosine-polynomial with degree m. 0 is also a cosine-polynomial and has no degree.

2. Results about cyclotomic polynomials and cosine-polynomials

E a, cos(jX), U,EQ, mEN,, a,#O, J=o

This section summarizes the necessary mathematical results. Detailed proofs can be found in [8].

2.1. Lemma (Properties of the cyclotomic polynomials). Let

na3, [:=cos(2a/n), m:=:+(n), and QR(Z)= C c,ZJ. J=o

Then,

(a) PM( X) is the minimal polynomial of {, where

J=l /=I k=O

where T,(X) is the j-th Chebysheu polynomial defined as T,( X) := cos( j arccos( X)).

@I e a,=b, for OGj<m.

cc> cDn(Z)=Z” cm+ E c,_,(z’+z-‘) i J=l

Proof. All assertions follow from the well-known properties of the cyclotomic polynomials. q

242

Page 3: A canonical simplifier for trigonometric expressions in the kinematic equation

Volume 29, Number 5 INFORMATION PROCESSING LETTERS 24 November 1988

2.2. Lemma (Calculation of the nth cyclotomic polynomial). For n E N, let n = nJ=, 4,” be the canonical prime factor decomposition of n. CD,,(X) is calculated in three steps:

(1) P(X) := C$,’ X’. (2) For j = 2, 3,. . . , r, P(X) := P( Xp/)/P( X) (diuision of polynomiuls).

(3) cD,~( X) := P( X’), where s = fl;= 1 P,~J-‘.

For the proof, see [6, p. 611.

2.3. Lemma (Division with remainder for cosine-polynomials). Let f and g be cosine-polynomials with g f 0. The procedure divrem computes the pair (q, r) of cosine-polynomials such that f = qg + r and r = 0 or

deg r -C deg g.

procflure divrem( f, g);,

f= C a, cos(jX), g = C 4 cos(jX); j=o I=0

if (f = 0) or (n < m) then return (0, f)

else if (n > m) and (m > 0) then Q:= (2a,/b,) cos((n - m)X) elsif (r-17 = 0) then Q := (a,/ b,) cos(nX) else (* n=m *) Q:= (a,/&,) endif; return (Q, 0) + divrem(f - Q * g, g)

(* Note that the expression f - Q * g is also a cosine-polynomial since the identity

COS(CU) cos(p) = :COS((Y + j3) + :cos(a - p) guarantees that the product of two

cosine-polynomials can be written as a cosine-polynomial *)

endif;

The proof of Lemma 2.3 is by induction on deg f.

3. The simplification algorithm

Let T’ be the subclass of T (see Section 1) consisting of all expressions 1’ E T such that the operator / does not occur in t’. The problem of finding a normal form for an expression in T is solved if we can map an expression of the form l/t’. t’ f 0, t’ E T’ to an equivalent expression in T’ and if we can reduce an

expression in T’ to normal form. Section 3.1 describes a routine common to both subproblems whereas Sections 3.2 and 3.3 explain the solution of the subproblems.

3. I. Associating cosine-polynomials with an expression

We rewrite a given t’ E T’ using the rule sin(a) + cos($~ - a) which replaces all occurrences of sin by cos. Then we eliminate products using cos( a) cos( p) + t cos(a + p) t i cos(a - /3) and write t’ as

(.o$,

243

Page 4: A canonical simplifier for trigonometric expressions in the kinematic equation

Volume 29, Number 5 INFORMATION PROCESSING LETTERS 24 November 1988

We assume n > 3 since otherwise the simplification is trivial, and we associate with t’ the cosine-poly- nomials t ‘( X) and tL( X) defined as follows:

k

t’(X) := c a, cos /=o

@ (elx) m &(X):= n

e imX = c, + c 2c,_,Re(e’jX) = c, + 2 2 c,,_, cos(jX) by Lemma 2.1(c),

j=l J=l

where m = i+(n). The calculation of tL( X) requires the n th cyclotomic polynomial which is computed by the algorithm given in Lemma 2.2. The definitions directly show that t’(21r/n) = t’, th(2T/n) = 0, and

deg th( X) = i+(n).

3.2. Calculating an inverse

Let t = l/t ‘, t ’ # 0, t ’ E T’, be an expression in T and let t’(X) and t,&( X) be the cosine-polynomials associated with t’ according to Section 3.1. We invoke the extended Euclidean algorithm [5, p. 3251 based on our division with remainder for cosine-polynomials (Lemma 2.3) and calculate cosine-polynomials t-‘(X) and r;‘(X) such that t-‘( X)t’( X) + t;‘( X)th( X) = gcd(t’( X), th( X)) = 1. The relation gcd(t’( X), th( X)) = 1 holds because th( X) is irreducible which follows from the same property of PM(X) in Lemma 2.1 (a). The fact that

t = l/t’ = t-‘(27r/n)/( t-‘(2n/n)t’(2a/n))

= t-‘(2T/n)/(l - t,‘(2T/n)tM(27r/n)) = t-‘(2v/n)

shows that t-‘(2q/n) is an expression in T’ equivalent to t.

3.3. Reducing an expression

Let t ’ be an expression in T’ and let t ‘( X) and t L( X) be the cosine-polynomials associated with t ’ according to Section 3.1. Lemma 2.3 guarantees the existence of cosine-polynomials q(X) and r(X) such that t’(X) = q( X)tk( X) + r(X) and deg r(X) -C deg tb( X) or r(X) = 0. Substituting 27/n for X leaves us with the following:

t’ = t’(2a/n) = q(2n/n)th(2T/n) + r(2T/n) = r(2T/n).

r(2a/n) is the normal form of t’ because r(X) = 0 or deg r(X) < deg tb( X) = i+(n) and this represen- tation is unique by Lemma 2.1(b).

3.4. Example

Find the normal form of the expression

f= cos(23”) sin(29”) - i cos(38”) + $ sin(6”) - sin(66”) + cos(36”).

Using

sin(a) -+ cos(&r - CY) and cos( CY) cos( p) --) : cos( (Y + p> + ; cos( (Y - p>

we rewrite f to

cos(84”) + cos(36”) - cos(24”) = cos(7(2a/30)) + cos(3(2~,‘30)) - cos(2(2~/30)).

244

Page 5: A canonical simplifier for trigonometric expressions in the kinematic equation

Volume 29, Number 5 INFORMATION PROCESSING LETTERS 24 November 1988

Since 30 is the lowest common denominator of the fractions that occur as arguments and @30(X) = X8 + X7 - X5 - X4 - X3 + X + 1, we find that f(X) = cos(7X) + cos(3X) - cos(2 X) and fM( X) = 2 cos(4X) + 2 cos(3X) - 2 cos( X) - 1. The division with remainder of f(X) by fM( X) yields a quotient q(X) = cos(3X) - cos(2 X) + cos( X) and a remainder r(X) = 0 which implies f = 0.

4. Comparison with related work

4.1. Comparison with the ordinary approach

Since expressions of the form cos(Tr) and sin(ar), r E Q, are clearly algebraic numbers we can adapt the standard method for canonical representation of algebraic numbers [3, p. 1731 which proceeds for an expression in T as follows:

(1) Replace sin(a) by cos(i, - a). (2) Consider cos(f~p,/n), where n = lcm{ p, }. Take 5 to be the n th primitive root of unity (6 = e2ni/n). (3) Replace cos(2ak/n) by f(t” + tPk). (4) Calculate in Q(t) which is isomorphic to Q[E]/@,,(t). The resulting normal form is a polynomial in [ with a degree of at most @I(H) - 1. Since the imaginary

part is known to be 0, it can be omitted. The ordinary method has the advantage that division with remainder for polynomials suffices. But it is

less efficient than ours because the degree of the polynomial used for the reduction is twice as high as in our method and both division procedures are of comparable complexity. Another related disadvantage is that sometimes counterintuitive normal forms are obtained. For example, the ordinary method maps cos(84”) to cos(84“) + i(-cos(60”) - cos(48”) - cos(36”) + cos(l2”) + l), whereas our algorithm pro- duces the normal form - cos(36 “) + cos(24”). Lemma 2.1(b) proves that our algorithm computes the normal form with the smallest possible argument values for the cos function which is in some sense the ‘simplest’ one. In addition Lemma 2.1(b) proves that our simplifier S has the useful property that if

S(t) = t, + t,, then S(t,) = t, and S(t,)= t,, a property not shared by the ordinary method as the preceding example shows.

We could achieve the same normal form as our method by directly performing arithmetic in Q(l), where [ = cos(2a/n). This approach has the disadvantage that the coefficients of the minimal polynomial of { have an enormous size because their calculation is based on rapidly growing binomial coefficients (see Lemma 2.1(a) for a formula of the minimal polynomial of 5). For example, in the case n = 360 the corresponding cosine-polynomial is 2 cos(48X) + 2 cos(36X) - 2 cos(l2X) - 1 whereas the minimal poly- nomial of cos(2a/n) is

1 - 3456X2 + 579456X4 - 39 625 728X6 + I497 954 816X’ - 35 782 47168OX’o

+ 583 456 329 728X12 - 6832 518 856 704X14 + 59 570 604933 120X16 - 397 107 008 634 880X18

+2O64791O7293l84OX2o - 8500299631 16544OX22 + 28011 51O45OO94O8OX24

- 74448 984 852 135 936X26 + 160 303 703 377 575 936X28 - 280058 255 978 266 624X3’

+ 396 366 279 591591 936X32 - 452 180272 956 309 5O4X34 + 411985 976 135 516 16OX36

- 295 364007 592 722 432X3’ + 162 828 875 980603 392X40 - 66 568 831992 070 144X42

+ 18 999 560 927 969 280X+’ - 3377 699 720 527 872 X46 + 281474 976 710 656X48.

Lemma 2.1(a) also shows that the minimal polynomial of 2 cos(2n/n) has smaller but still quite large integer coefficients.

In summary our method is superior to the ordinary approach because it is more efficient and produces the normal form with the smallest possible argument values for the cos function.

245

Page 6: A canonical simplifier for trigonometric expressions in the kinematic equation

Volume 29, Number 5 INFORMATION PROCESSING LETTERS 24 November 1988

4.2. Trigonometric expressions with variables

Computer algebra systems such as MACSYMA [7] and SCHOONSCHIP [9] or special purpose systems such as TRIGMAN [4] and CAMAL [l] contain routines to deal with canonical forms like

which arise in celestial mechanics from (Fourier or) Poisson series calculations. In general, these systems are not capable of performing a complete simplification in the case where all X, are equal to n; for example, MACSYMA maps the expression cos(84”) + cos(36” ) - cos(24”) to itself whereas our algorithm reduces it to 0.

The simplification of trigonometric expressions containing variables is an intricate matter because it can lead to undecidable problems like in the class of trigonometric expressions generated by the rationals, the number 7, one variable X and the function symbols + , *, sin, and abs where zero equivalence is undecidable [3, p. 231. The difficulties in simplifying trigonometric expressions are also demonstrated the REX [2] (Rational Exponential expressions) algorithm which is based on an unproved conjecture.

Acknowledgment

I am grateful to R. Fateman, L. Geissinger, J. Nievergelt and an anonymous referee for helpful comments regarding the presentation of this paper.

References

PI

121

(31

141

[51

D. Barton, S.R. Bourne and J.P. Fitch, An algebra system, [61 Compuf. J. 13 (1970) 32-39.

W.S. Brown, Rational exponential expressions and a con-

jecture concerning Pi and I, Amer. Math. Monthly 76

(1969) 28-34.

B. Buchberger, G.E. Collins and R. Loos. Computer Alge-

bra, Symbolic and Algebraic Computation (Springer,

Berlin/New York, 1983). W.H. Jefferys, FORTRAN-based listprocessor for Poisson-

series, Celestial Mechanics 2 (1970) 474-480.

D.E. Knuth, The Art of Computer Programming, Vol. 2

(Addison-Wesley, Reading, MA, 1981).

[71

181

[91

H. Liineburg. Galoisfelder, Kreisteilungskijrper und Schie-

beregisterfolgen (Bibliographisches Institut, Mannheim/ Wien/Ziitich, 1979).

MATHLAB Group, MACSYMA Reference Manual. M.I.T., Cambridge, MA, 1977.

P. Schorn. Ein Expertensystem zur Liisung der Kinema-

tischen Gleichung, Dipl. Thesis, Inst. fur Informatik, Tech-

nische Univ. Miinchen, 1985.

H. Strubbe, Presentation of the SCHOONSCHIP system, EUROSAM (1974) 55-60.

246