Message Boards Message Boards

6 Replies
0 Total Likes
View groups...
Share this post:

Defining a function with a sum

Posted 9 years ago

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 !

POSTED BY: Gabrielle DM
6 Replies

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:

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:

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.

POSTED BY: David Reiss

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:

and here is the documentation for Map:

Here is a different, roughly equivalent, approach:

uFunction[x : {_, _, _}, points : {{_, _, _} ..}] :=

  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).

POSTED BY: David Reiss
Posted 9 years ago

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 ?

POSTED BY: Gabrielle DM

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

POSTED BY: David Reiss

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]
POSTED BY: David Reiss
Posted 9 years ago

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

POSTED BY: Gabrielle DM
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract