Примеры подготовки пакетов расширений
Пакет символьных преобразований тригонометрических функций
Следующий пакет служит для демонстрации символьных преобразований тригонометрических функций синуса и косинуса.
(*:Title: TrigDefine *) (*:Context: ProgramminglnMathematica'TrigDefine' *) BeginPackage["ProgramminglnMathematica' TrigDefine'"] TrigDefine::usage = "TrigDefine.m defines global rules for putting products of trigonometric functions into normal form."Begin["'Private'"] (* set the private context *) (* unprotect any system functions for which rules will be defined *) protected = Unprotect[ Sin, Cos ] (* linearization *) Sin/: Sin[x_] Cos[y_]: = Sin[x+y]/2 + Sin[x-y]/2Sin/: Sin[x_] Sin[y_]: = Cos[x-y]/2 -Cos[x+y]/2 Cos/: Cos[x_] Cos[y_]: = Cos[x+y]/2 + Cos[x-y]/2Sin/: Sin[x_]An_Integer?Positive: =Expandt (1/2 -Cos[2x]/2) Sin [x]^(n-2) ] Cos/: Cos[x_]An_Integer?Positive: =Expand[(l/2 + Cos[2x]/2) Cos[x]^(n-2)] Protect[ Evaluate[protected]](* restore protection of system symbols *) End[] (* end the private context *) EndPackage[] (* end the package context *)Данный пакет задает преобразования для произведений sin(x) cos(x), sin(x) sin(y) и cos(x) cos(y), а также для sin(x)n и cos(x)n. Следующие примеры наглядно показывают работу с этим пакетом:
<< mypack\trigdefine.m ?Sin Sin[z] gives the sine of z. Sin[a]*Cos[b] 1/2Sin[a-b] + 1/2 Sin[a+b] Sin[a]*Sin[b] 1/2Cos[a-b] -1/2Cos[a+b] Cos[a]*Cos[b] 1/2 Costa-b] + 1/2Cos[a+b] Sin[x]^21/2-1/2 Cos[2x] Cos[x]^3Sec[x]/4 +1/2Cos[2x] Sec[x] + 1/4(1/2 + 1/2 Cos[4x]) Sec[x] Sin[x]^n Sin[x]nДанный пример – наглядная иллюстрация программирования символьных вычислений.
