Group Abstract Group Abstract

Message Boards Message Boards

0
|
4.2K Views
|
6 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Defining a function with a sum

Posted 12 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
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:

http://reference.wolfram.com/language/ref/Function.html

and here is the documentation for Map:

http://reference.wolfram.com/language/ref/Map.html

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

POSTED BY: David Reiss
Posted 12 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 12 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
Attachments
Remove
or Discard