- #1

- 526

- 151

Code:

```
In[7]:= fnSum[x_] := Sum[k, {k, 1, x}]
In[8]:= fnSum[x]
Out[8]= 1/2 x (1 + x)
In[9]:= fnSum[3.5]
Out[9]= 6
```

Of course, this is just an example -- I would like to know how to do this in general.

- Mathematica
- Thread starter Swamp Thing
- Start date

- #1

- 526

- 151

Code:

```
In[7]:= fnSum[x_] := Sum[k, {k, 1, x}]
In[8]:= fnSum[x]
Out[8]= 1/2 x (1 + x)
In[9]:= fnSum[3.5]
Out[9]= 6
```

Of course, this is just an example -- I would like to know how to do this in general.

- #2

Dale

Mentor

- 30,340

- 6,830

In your function definition use Evaluate on the right hand side

- #3

- 3,233

- 1,194

where the symbols [x] denote the integer part of x.

Did i get this right?

- #4

- 526

- 151

But I tried Dale's suggestion about "evaluate" and it works. I want it to create the expression x/2(x+1) from the summation formula, but then forget that it is about integers -- it should take any argument like, say, 3.5 and return 7.875

In case you're curious, this is related to my other post here:

https://www.physicsforums.com/threa...sus-integral-from-1-to-0.995310/#post-6409241

- #5

- 3,233

- 1,194

- #6

- 526

- 151

Using the Evaluate suggestion, I just do:

fnSum3[x_] = Evaluate[Sum[k*k*k, {k, 1, x}]]

which I might even generalize with a variable power.

- #7

- #8

- 526

- 151

- #9

Dale

Mentor

- 30,340

- 6,830

Yes, it is very powerful. I have used it for everything from learning physics to helping my wife design a good patchwork quilt.

Do you understand why using Evaluate had the result that it did?

- #10

- 526

- 151

Not really.Do you understand why using Evaluate had the result that it did?

I looked at the Wolfram page on Evaluate, and it says "causes expr to be evaluated even if it appears as the argument of a function whose attributes specify that it should be held unevaluated."

If I type in Attributes[Sum], it includes "HoldFirst" in the list, i.e. the first argument is to be held unevaluated.

But if I define my own function with OR without evaluate, it doesn't list any attributes, e.g. Attributes[MyFunction] is empty.

In any case, it's not clear how deferring evaluation (or not) would affect whether the argument is to be rounded off or not before calculation.

- #11

Dale

Mentor

- 30,340

- 6,830

When Sum is evaluated, if x is a number then it evaluates the Sum numerically, and if x is a symbol it evaluates the Sum symbolically.

So, without Evaluate the Sum is held unevaluated until the left hand side is called and then it is evaluated either way depending on if the argument is numeric or symbolic.

But with Evaluate the Sum is evaluated immediately, while x is still symbolic. This returns the algebraic expression which is then evaluated when the left hand side is called.

- Last Post

- Replies
- 1

- Views
- 3K

- Replies
- 1

- Views
- 2K

- Replies
- 0

- Views
- 2K

- Replies
- 4

- Views
- 886

- Last Post

- Replies
- 7

- Views
- 3K

- Last Post

- Replies
- 2

- Views
- 17K

- Last Post

- Replies
- 4

- Views
- 2K

- Last Post

- Replies
- 2

- Views
- 3K

- Replies
- 8

- Views
- 9K

- Replies
- 2

- Views
- 8K