Иллюстрированный самоучитель по Mathematica 3/4

Примеры подготовки пакетов расширений

Пакет вычисления функций комплексного переменного

Еще один пакет расширений для вычисления функций комплексного переменного (блок пакетов ALGEBRA) представлен распечаткой, приведенной ниже.

(*:Title: Relm *)
(*:Authors: Roman Maeder and Martin Buchholz *) BeginPackage [ "Algebra 'RelrrT "]
RealValued::usage = "RealValued[f] declares f to be a real-valued function
(for real-valued arguments)."
SBegin["'Private'"]
protected = Unprotect[Re, Im, Abs, Conjugate, Arg] (* test for "reality", excluding numbers *)
realQ[x_] /;!NumberQ[x]: = Im[x] == 0 imagQ[x_] /;!NumberQ[x]: = Re[x] == 0
(* fundamental rules *)
Re[x_]: = x /; realQ[x] Arg[x_]: = 0 /; Positive[x] Arg[x_J: = Pi /; Negative[x]
Conjugate[x_]: = x /; realQ[x] Conjugate[x_]: = - x /; imagQ[x]
(* there must not be a rule for Im[x] in terms of Re[x]!! *) (* things known to be real *)
Im[Re[_]]: = 0 Im[Im[_]]: = 0 Im[Abs[_]]: = 0 Im[Arg[_]]: = 0 Im[x_?Positive] = 0
Im[x_?Negative] = 0
Im[x_ ^ y_]: = 0,/; Positive[x] && Im[y] == 0 Im[Log[r?Positive]]: = 0
(*' arithmetic *)
Re[x_Plus]: = Re /@ x Im[x_Plus]: = Im /@ x
Re[x_ y_Plus]: = Re[Expand[x y]] Im[x_ y_Plus]: = Im[Expand[x y]]
Re[x_ y_]: = Re[x] Re[y]- Im[x] Im[y] Im[x_ y_]: = Re[x] Im[y] + Im[x] Re[y]
(* products *)
Re[(x_?Positive y_) ^k_]: = Re[x^k y^k] Im[(x_?Positive y_)^k_]: = Im[x^k yAk]
(* nested powers *)
Re[(x_?Positive ^ y_ /; Im[x]==0)^k_]: = Re[x^(y k)] Im[(x_?Positive ^ y_ /;
Im[x]==0kj: = Im[xn(y k)]
Re[ l/x_ ]: = Re[x] / (Re[x]^2 + Im[x]^2) Im[ l/x_ ]: = -Im[x] / (Re[x]"2 + Im[x]A2)
Im[x_^2]: = 2 Re[x] Im[x]
Re[ x_^n_Integer ]: = Block[{a, b},
a = Round[n/2]; b = n-a;
Re[x^a] Re[x^b] -Im[x^a] 1^[x^b] ]
Im[ x_^n_Integer ]: = Block[{a, b}, a = Round[n/2]; b = n-a; Re[x^a] Im[x^b] + Im[x^a] Re[x^b] ]
Re[x_IntegerAn_Rational]: = 0 /; IntegerQ[2n] && Negative[x]
Im[x_IntegerAn_Rational]: =
(-x)^n (-1)^((Numerator[n]-l)/2 /; IntegerQ[2n] && Negative[x]
(* functions *)
Re[Log[r_?Negative]]: = Log[-r] Im[Log[r_?Negative]]: = Pi Re[Log[z_]]: = Log[Abs[z]] /;
 realQ[z] Re[Log[z_]]: = (1/2) Log[Re[z]^2 + Im[z]^2] Im[Log[z_]]: = Arg[z]
Re[Log[a_ b_]]: = Re[Log[a] + Log[b]]
Im[Log[a_ b_]]: = Im[Log[a] + Log[b]]
Re[Log[a_^c_]]: = Re[c Log[a]]
Im[Log[a_^c_]]: = Im[c Log[a]]
Ke[E^x_]: = Cos[Im[x]] Exp[Re[x]] Im[E^x_]: = Sin[Im[x]] Exp[Re[x]]
Re[Sin[x_]]: = Sin[Re[x]] Cosh[Im[x]] Im[Sin[x_]]: = Cos[Re[x]] Sinh[Im[x]]
Re[Cos[x_]]: = Cos[Re[x]] Cosh[Im[x]] Im[Cos[x_]]: = - Sin[Re[x]] Sinh[Im[x]]
Re[Sinh[x_]]: = Sinh[Re[x]] Cos[Im[x]] Im[Sinh[x_J]: = Cosh[Re[x]] Sin[Im[x]]
Re[Cosh[x_]]: = Cosh[Re[x]] Cos[Im[x]] Im[Cosh[x_]]: = Sinh[Re[x]] Sin[Im[x]]
(* conjugates *)
Re[Conjugate[z_]]: = Re[z] Im[Conjugate[z_]]: =
Conjugate[x_Plus]: = Conjugate /@ x Conjugate[x_Times]
: = Conjugate /@ x Conjugate[x_^n_Integer]: = Conjugate[x]An Conjugate[Conjugate[x_]]: = x
(* real-valued rules *)
Attributes[RealValued] = {Listable, HoldAll} Attributes[RealValuedQ] = {HoldFirst}
RealValued[f_Symbol]: = (f/: RealValuedQ[f] = True; f) RealValued[f ]: = RealValued /@ {f}
Im[ (_?RealValuedQ) [_? (Im[#J ==0&)...] ]: = 0
(* define built-in function to be real-valued *)
DoRules[flist_]: = Block[{protected},
protected = Unprotect[flist];
RealValued[flist];
Protect[Evaluate[protected]]
]
DoRules[{Sin, Cos, Tan, ArcSin, ArcCos, ArcTan, ArcCot, Sinh, Cosh, Tanh,
ArcSinh, ArcCosh, ArcTanh, Floor, Ceiling, Round, Sign, Factorial}]
Protect[Evaluate[protected] ]
End[ ]
Protect[RealValued]
EndPackage[ ]

Как нетрудно заметить, в этом пакете задано вычисление действительной и мнимой частей для ряда тригонометрических, гиперболических и числовых функций.

Если Вы заметили ошибку, выделите, пожалуйста, необходимый текст и нажмите CTRL + Enter, чтобы сообщить об этом редактору.