0
$\begingroup$

I have expression involving Cos of some parameters. I would like to replace those Cos by their infinite series each time they do appear.

I tried the following which doesn't work:

zz^2*Cos[3*zz + 8] + Cos[5 yy] /. 
 Cos[x_] ->  Series[Cos[x_], {x, 0, Infinity}]

Indeed I read in the documentation that x_ should stand for any pattern that we decide to name x. I am not sure to understand why this code is not valid.

How can I replace the Cos here by their infinite series each time they do appear (my real problem contains longer expressions).

$\endgroup$
5
  • 2
    $\begingroup$ In the example you provided you could do expr /. Cos[arg_] :> Series[Cos[arg], {zz, 0, Infinity}]. (Note that Pattern should not appear in the right-hand side of the Rule.) $\endgroup$
    – Natas
    Oct 30, 2020 at 17:56
  • $\begingroup$ @Natas thank you for your comment. But I would like to do the expansion with respect to anything that is inside the Cos, sometimes it is variables depending on zz but sometimes it could depend on other variable yy (I just modified for more clarity) $\endgroup$
    – StarBucK
    Oct 30, 2020 at 18:08
  • $\begingroup$ @Natas - With my version (12.1.1), you need to replace Infinity with a positive integer. $\endgroup$
    – Bob Hanlon
    Oct 30, 2020 at 18:09
  • $\begingroup$ @BobHanlon Yes, I am also not quite sure what the benefit of this replacement might be. $\endgroup$
    – Natas
    Oct 30, 2020 at 18:30
  • $\begingroup$ Will something like the following help? Clear[x, cos]; cos[arg_] := (Series[Cos[x], {x, 0, 6}] // Normal) /. x -> arg; zz^2*Cos[3*zz + 8] + Cos[5 yy] /. Cos[x_] :> (cos[x]) $\endgroup$ Oct 30, 2020 at 20:09

3 Answers 3

0
$\begingroup$

Series needs to know with respect to which variable it should expand.

One solution is that any Symbol will be considered a variable

expr = zz^2*Cos[3*zz + 8] + Cos[5 yy];
expr /. Cos[arg_] :> 
  With[{vars = Cases[arg, _Symbol, Infinity]}, 
   Series[Cos[arg], 
    Sequence @@ Table[{var, 0, Infinity}, {var, vars}]]]
(*Series[Cos[5 yy],{yy,0,\[Infinity]}]+zz^2 Series[Cos[8+3 zz],{zz,0,\
\[Infinity]}]*)
$\endgroup$
0
$\begingroup$
Clear["Global`*"]

cos[x_, n : _Integer?Positive : 10] := 
 Normal[Series[Cos[$var], {$var, 0, n}]] /. $var :> x

expr = zz^2*Cos[3*zz + 8] + Cos[5 zz];

Using the default order

expr2 = expr /. Cos -> cos

(* 1 - (25 zz^2)/2 + (625 zz^4)/24 - (3125 zz^6)/144 + (
 78125 zz^8)/8064 - (390625 zz^10)/145152 + 
 zz^2 (1 - 1/2 (8 + 3 zz)^2 + 1/24 (8 + 3 zz)^4 - 
    1/720 (8 + 3 zz)^6 + (8 + 3 zz)^8/40320 - (8 + 3 zz)^10/3628800) *)

Specifying the order

expr3 = expr /. Cos -> (cos[#, 15] &)

(* 1 - (25 zz^2)/2 + (625 zz^4)/24 - (3125 zz^6)/144 + (
 78125 zz^8)/8064 - (390625 zz^10)/145152 + (
 9765625 zz^12)/19160064 - (244140625 zz^14)/3487131648 + 
 zz^2 (1 - 1/2 (8 + 3 zz)^2 + 1/24 (8 + 3 zz)^4 - 
    1/720 (8 + 3 zz)^6 + (8 + 3 zz)^8/40320 - (8 + 3 zz)^10/
    3628800 + (8 + 3 zz)^12/479001600 - (8 + 3 zz)^14/87178291200) *)

EDIT: The order in Series cannot be Infinity. However, an infinite series can be an Inactive[Sum]

Clear[cos]

cos[x_] := Inactive[Sum][I^n (1 + (-1)^n)/(2 n!) x^n, {n, 0, Infinity}]

expr4 = expr /. Cos -> cos

enter image description here

expr == (expr4 // Activate)

(* True *)
$\endgroup$
0
$\begingroup$

Just another way (getting order 10 terms):

expr = zz^2*Cos[3*zz + 8] + Cos[5 yy];
rules = Thread[Cases[expr, Cos[a__] :> a, Infinity] :> {x, y}];
irules = Reverse /@ rules;
em = expr /. rules;
s = Series[em, {x, 0, 10}, {y, 0, 10}] // Normal;
Expand[s /. irules]

1-((25 yy^2)/2) + (625 yy^4)/24 - (3125 yy^6)/144 + ( 78125 yy^8)/8064 - (390625 yy^10)/145152 - (1477249 zz^2)/14175 - ( 423832 zz^3)/945 - (60379 zz^4)/70 - (34708 zz^5)/35 - ( 30297 zz^6)/40 - (9909 zz^7)/25 - (11313 zz^8)/80 - ( 2349 zz^9)/70 - (22599 zz^10)/4480 - (243 zz^11)/560 - ( 729 zz^12)/44800

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.