3 trigtools package for working with expressions with trigonometric and hyperbolic functions.
5 Copyright (C) A.Domarkas 2013
6 rigtools package is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License.
11 The function c2sin convert expression a*cos(x)+b*sin(x) to r*sin(x+phi).
14 c2sin(f):=block([x,a,b,r,phi],
16 a:coeff(f,cos(x)),b:coeff(f,sin(x)),
17 r:signum(b)*sqrt(a^2+b^2),
23 The function c2cos convert expression a*cos(x)+b*sin(x) to r*cos(x-phi).
26 c2cos(f):=block([x,a,b,r,phi],
28 a:coeff(f,sin(x)),b:coeff(f,cos(x)),
29 r:signum(b)*sqrt(a^2+b^2),
34 The function c2trig (convert to trigonometric) reduce expression with hyperbolic functions
35 sinh, cosh, tanh, coth to trigonometric expression with sin, cos, tan, cot.
38 c2trig(r):=block([x,i,sinv,cosv,mi,mii,%iargs:false],
41 sinv:compose_functions([mii,sin,mi]),
42 tanv:compose_functions([mii,tan,mi]),
43 cosv:compose_functions([cos,mi]),
44 cotv:compose_functions([mi,cot,mi]),
45 subst([sinh=sinv,cosh=cosv,tanh=tanv,coth=cotv],r),
53 The function c2hyp (convert to hyperbolic) convert expression with exp function
54 to expression with hyperbolic functions sinh, cosh.
57 c2hyp(expr):=block([pa,f,e1,e2,S],
58 pa(f):=if atom(f) then f else makelist(part(f,k),k,1,length(f)),
62 sublist(e2,lambda ([x], part(x,1)=%e)),
68 sublist(e2,lambda ([x], part(x,1)=%e)),
71 makelist(exp(S[k][2])=cosh(S[k][2])+sinh(S[k][2]),k,1,length(S)),
76 The function trigfactor factors expressions of form +-sin(x)+-cos(y)
79 trigfactor(f):=block([r,_x,_y,%piargs:false],
80 st(f):=block([f0,fun,a1],
82 if f0=sin or f0=cos then return([part(f,0),part(f,1)])
85 if f0=sin or f0=cos then
86 return([part(f,1,0),part(f,1,1)])
90 if nterms(f)=2 and op(f)="+" then
91 (s1:st(part(f,1))[1], s2:st(part(f,2))[1])
93 if s1#false and f2#false and s1#s2 then
94 (sinv:compose_functions([cos,cx]),f1:subst([sin=sinv],f))
96 _x:st(part(f1,1))[2],_y:st(part(f1,2))[2],
97 r:[2*sin((_x+_y)/2)*cos((_x-_y)/2),2*sin((_x-_y)/2)*cos((_x+_y)/2),
98 2*cos((_x+_y)/2)*cos((_x-_y)/2),2*sin((_x+_y)/2)*sin((_x-_y)/2)],
101 sublist(r,lambda([x],trigrat(f1-x)=0)),
103 if length(%%)>=1 then %%[1] else f )$
106 The function trigsolve find solutions of trigonometric equation from interval [a, b).
109 trigsolve(eq,a,b):=block([s,i,ats,algebraic],
111 to_poly_solve([eq], [x],'simpfuncs =
112 ['rootscontract,'expand,'radcan,'nicedummies]),
113 s:makelist(rhs(part(%%,k)[1]),k,1,length(%%)),
115 for i:1 thru length(s) do
116 (makelist(ev(s[i],%z0=k),k,-10,10),
118 sublist(ats,lambda([e],e>=a and e<b)),
124 The function trigvalue compute values of sin(m*pi/n), cos(m*pi/n), tan(m*pi/n), cot(m*pi/n) in radicals.
128 [f,x,sol,spr,spr1,solvetrigwarn,algebraic],
131 if freeof(%pi,r) then return(r),
133 if part(r,0)="-" then f:part(r,1,0),
135 sol:solve(x=r,%pi)[1],
141 if (length(spr)<=2 or not freeof(%i,%%)) then return(r),
142 spr1:sublist(spr,lambda([e],is(abs(rhs(e)-r)<ratepsilon))),
143 if %%=[] then return(r),
150 The function trigeval compute values of expressions with sin(m*pi/n), cos(m*pi/n), tan(m*pi/n), cot(m*pi/n)
154 trigeval(r):=block([sinv,cosv,tanv,cotv],
155 sinv:compose_functions([trigvalue,sin]),
156 cosv:compose_functions([trigvalue,cos]),
157 tanv:compose_functions([trigvalue,tan]),
158 cotv:compose_functions([trigvalue,cot]),
159 subst([sin=sinv,cos=cosv, tan=tanv,cot=cotv],r)
163 The function atan_contract contracts atan functions.
166 atan_contract(r):=block([],
167 if equal(r,%pi/2) then return(%pi/2)
168 elseif equal(r,-%pi/2) then return(-%pi/2),
181 compose_functions -- function from to_poly_solve package( used for trigeval, c2trig)
184 compose_functions(l):=block([z,f],
185 if listp(l) then (l:reverse(l),f:z,for lk in l do
186 f:funmake(lk,[f]),buildq([z,f],lambda([z],f))) else
187 error("The argument to 'compose_functions' must be a list."))$