Group Abstract Group Abstract

Message Boards Message Boards

0
|
4.6K Views
|
1 Reply
|
2 Total Likes
View groups...
Share
Share this post:

Redefine a sum function that factors out 'constants' ?

Posted 8 years ago

code:

FreeQ[\[Lambda]^i, i]
! FreeQ[2 \[Lambda]^i, i]
output:
False
True
reset Sum :
a3 = Attributes[Sum];
Unprotect[Sum];
ClearAttributes[Sum, a3];
Attributes[Sum];
Sum[f_ g_, i_] := f Sum[g, i] /; FreeQ[f, i];
SetAttributes[Sum, a3];
Protect[Sum];
test:
\!\(
\*UnderoverscriptBox[\(\[Sum]\), \(i\), \(n\)]\(2\
\*SuperscriptBox[\(\[Lambda]\), \(k\)]\
\*SuperscriptBox[\(x[i]\), \(j\)]\)\)
\!\(
\*UnderoverscriptBox[\(\[Sum]\), \(i\), \(n\)]\(2\
\*SuperscriptBox[\(\[Lambda]\), \(i\)]\
\*SuperscriptBox[\(x[i]\), \(j\)]\)\)
output:

2 \[Lambda]^k \!\(
\*UnderoverscriptBox[\(\[Sum]\), \(i\), \(n\)]
\*SuperscriptBox[\(x[i]\), \(j\)]\)
2 \[Lambda]^i \!\(
\*UnderoverscriptBox[\(\[Sum]\), \(i\), \(n\)]
\*SuperscriptBox[\(x[i]\), \(j\)]\)

I found that the lambda^i in Sum was factor out, but it has i index. By the way the software version I use is mathematica 7.0. Thank you in advance!

POSTED BY: zhu xiaoming

The 'i' in your pattern is actually not only the variable:

Sum[f_ g_, i_] := (Print[i]; (f Sum[g, i] /; FreeQ[f, i]))

As you can see, 'i' in your case is {i,n}, so if you restart the kernel and make the following definition:

Sum[f_ g_, i_] := f Sum[g, i] /; FreeQ[f, i[[1]]]

it works.

I would, however, advice you to make your own helper function:

ClearAll[MySum]
MySum[f_ g_,spec:{i_,___}] := f Sum[g, spec] /; FreeQ[f, i]
MySum[f_ ,spec___]:=Sum[f, spec]

MySum[2\[Lambda]^k x[i]^j,{i,n}]
MySum[2\[Lambda]^i x[i]^j,{i,n}]

So that it becomes much easier to edit, and you don't have to protect and unprotect each time, and restart kernel and so on, and you can still harness all the 'power' of Sum, because if it does not match it goes back to Sum...

POSTED BY: Sander Huisman
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard