# Defining a function with a sum

**URL:** <https://community.wolfram.com/t/defining-a-function-with-a-sum/4191>\
**Category:** Questions\
**Tags:** mathematica\
**Created:** [December 1, 2014, 9:17pm UTC](https://community.wolfram.com/t/defining-a-function-with-a-sum/4191 "2014-12-01T21:17:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gabrielledemicheli](https://avatars.discourse-cdn.com/v4/letter/g/e9c0ed/32.png) [@gabrielledemicheli](https://community.wolfram.com/u/gabrielledemicheli)\
**Post date:** [December 1, 2014, 9:17pm UTC](https://community.wolfram.com/t/defining-a-function-with-a-sum/4191/1 "2014-12-01T21:17:47Z")

</div>

I am a new user of mathematica and I am struggling to code the following.  
I am given a positive integer s and a series of points P\_n \in R^3 with n= 1, ..., s . I want to define the function U from R^3 \setminus {P\_n} to R given by :  
 U= \sum\_{n=1}^s \frac{1}{|x-P\_n|}.  
After that, I can define a metric given by g = \frac{1}{U}(d\tau +\phi\sin?d?)^2 +U(dr^2 +r^2(d?^2 +sin?^2d?^2)) U. Defining the metric should be ok but I am stuck with how to define U. I read some tutorials but that didn’t help much …  
thanks for any advice !

---

<div class="post-metadata">

**Author:** ![dreiss](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/dreiss/32/29094_2.png) [@dreiss](https://community.wolfram.com/u/dreiss)\
**Post date:** [December 1, 2014, 9:44pm UTC](https://community.wolfram.com/t/defining-a-function-with-a-sum/4191/2 "2014-12-01T21:44:06Z")

</div>

I assume that your x is a triple (i.e., a 3-vector) and that each of your points are themselves triples. If this is the case then your function (which I will call uFunction – you shold avoid giving functions single capital letter names in Mathematica) could be

```
uFunction[x : {_, _, _}, points : {{_, _, _} ..}] := Tr[1/(Sqrt[Tr[(x - #)^2]] & /@ points)]

```

Then try it out:

```
In[40]:= uFunction[{x, y, z}, {{a, b, c}, {d, e, f}}]

Out[40]= 1/Sqrt[(-a + x)^2 + (-b + y)^2 + (-c + z)^2] + 1/Sqrt[(-d + x)^2 + (-e + y)^2 + (-f + z)^2]

```

---

<div class="post-metadata">

**Author:** ![dreiss](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/dreiss/32/29094_2.png) [@dreiss](https://community.wolfram.com/u/dreiss)\
**Post date:** [December 1, 2014, 9:45pm UTC](https://community.wolfram.com/t/defining-a-function-with-a-sum/4191/3 "2014-12-01T21:45:05Z")

</div>

Note that I make use of Pure functions to make the code compact as well as the Map function in its shorthand form.

---

<div class="post-metadata">

**Author:** ![gabrielledemicheli](https://avatars.discourse-cdn.com/v4/letter/g/e9c0ed/32.png) [@gabrielledemicheli](https://community.wolfram.com/u/gabrielledemicheli)\
**Post date:** [December 1, 2014, 9:58pm UTC](https://community.wolfram.com/t/defining-a-function-with-a-sum/4191/4 "2014-12-01T21:58:17Z")

</div>

Thanks for the answer ! I just don’t understand why you define {a,b,c} and also {d,e,f}? Also, what does & /@ mean (in the definition of ufunction)? Thanks again

---

<div class="post-metadata">

**Author:** ![dreiss](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/dreiss/32/29094_2.png) [@dreiss](https://community.wolfram.com/u/dreiss)\
**Post date:** [December 1, 2014, 10:10pm UTC](https://community.wolfram.com/t/defining-a-function-with-a-sum/4191/5 "2014-12-01T22:10:58Z")

</div>

I gave an example where you have two points `{a,b,c}` and `{d,e,f}` in symbolic form so you could see the action of the function.

I mentioned that I make use of pure functions and the short form for `Map`. Here is the documentation for Pure functions:

> **[Function: A pure or anonymous construct—Wolfram Documentation](https://reference.wolfram.com/language/ref/Function.html)**
>
> body & or Function\[body\] is a pure (or anonymous) function. The formal parameters are # (or #1), #2, etc. x |-\> body or x |-\> body or Function\[x, body\] is a pure function with a single formal parameter x. {x1, x2, ...} |-\> body or {x1, x2, ...} |-\>...

and here is the documentation for `Map`:

> **[Map: Apply a function to elements in a list—Wolfram Documentation](https://reference.wolfram.com/language/ref/Map.html)**
>
> Map\[f, expr\] or f /@ expr applies f to each element on the first level in expr. Map\[f, expr, levelspec\] applies f to parts of expr specified by levelspec. Map\[f\] represents an operator form of Map that can be applied to an expression.

Here is a different, roughly equivalent, approach:

```
uFunction[x : {_, _, _}, points : {{_, _, _} ..}] :=
 Module[{nPoints},
  
  nPoints = Length[points];
  
  Sum[1/(Norm[x - points[[i]]]), {i, 1, nPoints}]
  
  ]

```

(it differs from the original one in that it ends up having explicit absolute value functions in the result because `Norm` makes no assumptions as to whether the vector supplied to it has real components).

---

<div class="post-metadata">

**Author:** ![gabrielledemicheli](https://avatars.discourse-cdn.com/v4/letter/g/e9c0ed/32.png) [@gabrielledemicheli](https://community.wolfram.com/u/gabrielledemicheli)\
**Post date:** [December 2, 2014, 8:23am UTC](https://community.wolfram.com/t/defining-a-function-with-a-sum/4191/6 "2014-12-02T08:23:29Z")

</div>

Hi,  
Thanks again ! Just to be sure, in your second definition of uFunction, i’d have to put in the coordinates of the points in the brackets right ?

---

<div class="post-metadata">

**Author:** ![dreiss](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/dreiss/32/29094_2.png) [@dreiss](https://community.wolfram.com/u/dreiss)\
**Post date:** [December 2, 2014, 6:19pm UTC](https://community.wolfram.com/t/defining-a-function-with-a-sum/4191/7 "2014-12-02T18:19:17Z")

</div>

Both definitions take the same arguments. The first argument is the list of the cartesian coordinates of your point `x`. The second argument is a list of points, each of which is a list of its three cartesian coordinates.

From your question (and what you said in your original post) I gather that you are very new to Mathematica. Given this, I’d recommend that you spend some time learning its syntax and programming methods before diving in to a project like this.

This is a very fast introduction for folks who have a reasonable amount of programming experience in other languages:

> **[Wolfram Language Tutorial: Fast Introduction for Programmers](https://www.wolfram.com/language/fast-introduction-for-programmers/en/)**
>
> Get started and up to speed on the basic principles and concepts of programming in the Wolfram Language. Introductory tutorial for programmers.

but it is certainly not sufficient to get up to speed. A good way to do that is to read and work though the following recent book:

> **[Programming with Mathematica®: An Introduction: Wellin, Paul: 9781107009462:...](https://www.amazon.com/Programming-Mathematica%C2%AE-Introduction-Paul-Wellin/dp/1107009464)**
>
> Programming with Mathematica®: An Introduction \[Wellin, Paul\] on Amazon.com. \*FREE\* shipping on qualifying offers. Programming with Mathematica®: An Introduction

An additional comment about your project. In your specification you use the expression |x-P\_i|. In this I assumed that all points were expressed in 3-dimensional cartesian coordinates and that your norm was the euclidian norm. But this is not consistent with the metric that you are designing which is generally non-euclidian and expressed in (for the spatial part) spherical polar coordinates. So what I gave you may not be what you actually want. But once you learn some more programming in Mathematica then you will be able to use the constructs that I gave as a starting point for creating the correct expressions for your project.
