Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1997
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1997

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Overloading mathematica built in functions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg5750] Re: [mg5716] Overloading mathematica built in functions
  • From: BobHanlon at aol.com
  • Date: Tue, 14 Jan 1997 10:42:28 -0500
  • Sender: owner-wri-mathgroup at wolfram.com

Playing with built-in definitions is risky.  There may be unintentional side
effects.  Recommend defining a new function.

Bob Hanlon
_______________

mySum[x_List] := Plus @@ x;
mySum[expr_, {iterator__}] := Sum[expr, {iterator}];

theList = Array[x, {3}]

{x[1], x[2], x[3]}

mySum[theList]

x[1] + x[2] + x[3]

mySum[n^2, {5}]

   2
5 n

mySum[n^2, {n, 5}]

55

mySum[n^2, {n, 1, 5}]

55

mySum[n^2, {n, 1, 5, 1}]

55

---------------------
Forwarded message:
From:	seanross at worldnet.att.net
To: mathgroup at smc.vnet.net
To:	mathgroup at smc.vnet.net

I want to add a capability to the Sum function.  If I give it a 
single argument that is a 1-D list, I want it to sum the list. For 
example:

In[1]:=
w={1,1,1,3};
Sum[w]
Out[1]:=
5

The function
sum[f_List]:=Sum[f[[i]],{i,1,Length[f]}];
performs this function, However

Unprotect[Sum];
Sum[f_List]:=Sum[f[[i]],{i,1,Length[f]}];
Protect[Sum];

does not work.  Does anyone know why?  I am guessing it has something to 
do with the attribute HoldAll on the built-in function Sum.  Is there a 
way to overload a function so that the attributes are different for 
different argument lists?  

Thanks




  • Prev by Date: Joining matrices
  • Next by Date: Re: Mathematica on Daystar
  • Previous by thread: Overloading mathematica built in functions
  • Next by thread: Re: Overloading mathematica built in functions