Finding the location of a point in a given list

Clash Royale CLAN TAG#URR8PPP
$begingroup$
Say I have a list:
Line[-Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), 0, 1],
Line[Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), 0,1],
Line[-Sqrt[5/8 + Sqrt[5]/8],1/4 (-1 + Sqrt[5]), Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5])],
Line[Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), 0, 1]
So that each sublist of that list consist of, in this case, two lines. All the points that tell us about the position of the line appear in another list, call this list points. Now, I want to extract the position of all the points from the above list in that points list. I'm aware of Position function but I'm not sure how to effectively apply it to my big list above in order to get the list of positions. AI'd very much appreciate some help.
list-manipulation
$endgroup$
add a comment |
$begingroup$
Say I have a list:
Line[-Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), 0, 1],
Line[Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), 0,1],
Line[-Sqrt[5/8 + Sqrt[5]/8],1/4 (-1 + Sqrt[5]), Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5])],
Line[Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), 0, 1]
So that each sublist of that list consist of, in this case, two lines. All the points that tell us about the position of the line appear in another list, call this list points. Now, I want to extract the position of all the points from the above list in that points list. I'm aware of Position function but I'm not sure how to effectively apply it to my big list above in order to get the list of positions. AI'd very much appreciate some help.
list-manipulation
$endgroup$
$begingroup$
If you already call the listpointswhy not use Mathematica's expressiveness, give it the headPointand really call it that? Readability often beats shortness (and maybe also performance).
$endgroup$
– gwr
Feb 14 at 15:33
add a comment |
$begingroup$
Say I have a list:
Line[-Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), 0, 1],
Line[Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), 0,1],
Line[-Sqrt[5/8 + Sqrt[5]/8],1/4 (-1 + Sqrt[5]), Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5])],
Line[Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), 0, 1]
So that each sublist of that list consist of, in this case, two lines. All the points that tell us about the position of the line appear in another list, call this list points. Now, I want to extract the position of all the points from the above list in that points list. I'm aware of Position function but I'm not sure how to effectively apply it to my big list above in order to get the list of positions. AI'd very much appreciate some help.
list-manipulation
$endgroup$
Say I have a list:
Line[-Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), 0, 1],
Line[Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), 0,1],
Line[-Sqrt[5/8 + Sqrt[5]/8],1/4 (-1 + Sqrt[5]), Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5])],
Line[Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), 0, 1]
So that each sublist of that list consist of, in this case, two lines. All the points that tell us about the position of the line appear in another list, call this list points. Now, I want to extract the position of all the points from the above list in that points list. I'm aware of Position function but I'm not sure how to effectively apply it to my big list above in order to get the list of positions. AI'd very much appreciate some help.
list-manipulation
list-manipulation
asked Feb 14 at 11:52
amator2357amator2357
997
997
$begingroup$
If you already call the listpointswhy not use Mathematica's expressiveness, give it the headPointand really call it that? Readability often beats shortness (and maybe also performance).
$endgroup$
– gwr
Feb 14 at 15:33
add a comment |
$begingroup$
If you already call the listpointswhy not use Mathematica's expressiveness, give it the headPointand really call it that? Readability often beats shortness (and maybe also performance).
$endgroup$
– gwr
Feb 14 at 15:33
$begingroup$
If you already call the list
points why not use Mathematica's expressiveness, give it the head Point and really call it that? Readability often beats shortness (and maybe also performance).$endgroup$
– gwr
Feb 14 at 15:33
$begingroup$
If you already call the list
points why not use Mathematica's expressiveness, give it the head Point and really call it that? Readability often beats shortness (and maybe also performance).$endgroup$
– gwr
Feb 14 at 15:33
add a comment |
5 Answers
5
active
oldest
votes
$begingroup$
You can use Cases for this:
lines = Line[-Sqrt[5/8 - Sqrt[5]/8],
1/4 (-1 - Sqrt[5]), 0, 1],
Line[Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), 0,
1], Line[-Sqrt[5/8 + Sqrt[5]/8],
1/4 (-1 + Sqrt[5]), Sqrt[5/8 - Sqrt[5]/8],
1/4 (-1 - Sqrt[5])],
Line[Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), 0, 1];
Catenate @ Cases[lines, Line[pts : _, _ ..] :> pts, Infinity]
Out[5]= -Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), 0, 1, Sqrt[
5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), 0,
1, -Sqrt[5/8 + Sqrt[5]/8], 1/4 (-1 + Sqrt[5]), Sqrt[
5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), Sqrt[5/8 - Sqrt[5]/8],
1/4 (-1 - Sqrt[5]), 0, 1
$endgroup$
$begingroup$
Thank you @SjoerdSmit! Tbh, for my case, it makes more sense to not Catenate them and keep them within the sublists as initially. When I removeCatenateit gives a list of lists of points representing each line, as expected. But then it again looses the sublist's structure.
$endgroup$
– amator2357
Feb 14 at 12:21
add a comment |
$begingroup$
What about /.Line->List
lines /. Line -> (Flatten[ List[#], 1] &)
$endgroup$
2
$begingroup$
+1. Or perhaps evenlines /. Line -> Sequence.
$endgroup$
– WReach
Feb 14 at 15:20
add a comment |
$begingroup$
Answer to what you want
lines =
Line[-Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), 0,1]
, Line[Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), 0,1]
, Line[-Sqrt[5/8 + Sqrt[5]/8], 1/4 (-1 + Sqrt[5]), Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5])]
, Line[Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), 0, 1]
;
lines /. Line[ l : _, _ .. ] :> l
(* or as WReach has pointed out: lines /. Line -> Sequence *)
Simple and expressive - no remembering whether its Flatten[ ... , 1 ] or Flatten[ ... , 1 ], or Flatten[ ... , -1 ] and what have you.
Different Approach Making Use of Datatypes
While having lists is very compact we can easily get lost in different levels. Why not simply give each single point the head Point (Point can also contain a list of x,y tuples like Line, but giving each point a head of its own simplifies counting and identification of duplicates imo). You can then use pattern matching quite easily, count points, and immediately visualize the data:
Here I turn a list of lines into a list of points, thus not adding another level for each line:
points = lines /. Line[ l : _, _ .. ] :> ( Sequence @@ Point /@ l )
(* if you do not want this you can simply do: lines /. Line -> Point *)
Point[-Sqrt[5/8-Sqrt[5]/8],1/4 (-1-Sqrt[5])],Point[0,1],Point[Sqrt[5/8-Sqrt[5]/8],1/4 (-1-Sqrt[5])],Point[0,1],Point[-Sqrt[5/8+Sqrt[5]/8],1/4 (-1+Sqrt[5])],Point[Sqrt[5/8-Sqrt[5]/8],1/4 (-1-Sqrt[5])],Point[Sqrt[5/8-Sqrt[5]/8],1/4 (-1-Sqrt[5])],Point[0,1]
Now it is easy to count points:
Count[ points, Point[ _ ], Infinity ]
8
It is easy to discover that there are only four unique points:
Flatten @ points // Union
Point[0, 1], Point[-Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5])],
Point[Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5])],
Point[-Sqrt[5/8 + Sqrt[5]/8], 1/4 (-1 + Sqrt[5])]
And we can immediately visualize the results:
Graphics[ Red, PointSize -> Large, points ]

Don't care for Point anymore? Get back to lists:
points /. Point[ pos : _, _ ] :> pos
$endgroup$
add a comment |
$begingroup$
To make things easier to read, let's use these lines (with the same nested structure as in the question):
lines =
Line[60, 50, 60, 70]
, Line[40, 30, 40, 50]
, Line[20, 10, 20, 30]
;
Furthermore, let's define the list points as this:
points = 20, 10, 20, 30, 40, 30, 40, 50, 60, 50, 60, 70;
We can get the positions of each point in this list as follows:
pointPosition = points // PositionIndex // Map[First]
(* <|20, 10 -> 1, 20, 30 -> 2, 40, 30 -> 3, 40, 50 -> 4, 60, 50 -> 5, 60, 70 -> 6|> *)
This association can tell us, for example, that the point 40, 30 appears at position 3 in points.
pointPosition[40, 30]
(* 3 *)
We can now convert the line list into a similarly structured list where each point has been replaced by a pair of corresponding positions:
lines /. Line[pts__] :> pointPosition /@ pts
(* 5, 6, 3, 4, 1, 2 *)
Or, should we desire, we could retain the List heads in the result:
lines /. Line[pts__] :> Line[pointPosition /@ pts]
(* Line[5, 6], Line[3, 4], Line[1, 2] *)
This latter form has the advantage that we can still draw the resultant lines by means of GraphicsComplex:
newLines = lines /. Line[pts__] :> Line[pointPosition /@ pts];
Graphics[GraphicsComplex[points, newLines]]

$endgroup$
add a comment |
$begingroup$
As bad as it may look, this seems to be working fine:
Partition[Partition[Flatten[Position[points, #] & /@ Catenate @ Cases[lines, Line[pts : _, _ ..] :> pts, Infinity]],2],Length[l]]
$endgroup$
$begingroup$
Working code is one thing - readable and debuggable code another (try understanding that code three months from now). I tend to go for the union of these. :)
$endgroup$
– gwr
Feb 14 at 14:48
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
);
);
, "mathjax-editing");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "387"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f191548%2ffinding-the-location-of-a-point-in-a-given-list%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
You can use Cases for this:
lines = Line[-Sqrt[5/8 - Sqrt[5]/8],
1/4 (-1 - Sqrt[5]), 0, 1],
Line[Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), 0,
1], Line[-Sqrt[5/8 + Sqrt[5]/8],
1/4 (-1 + Sqrt[5]), Sqrt[5/8 - Sqrt[5]/8],
1/4 (-1 - Sqrt[5])],
Line[Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), 0, 1];
Catenate @ Cases[lines, Line[pts : _, _ ..] :> pts, Infinity]
Out[5]= -Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), 0, 1, Sqrt[
5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), 0,
1, -Sqrt[5/8 + Sqrt[5]/8], 1/4 (-1 + Sqrt[5]), Sqrt[
5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), Sqrt[5/8 - Sqrt[5]/8],
1/4 (-1 - Sqrt[5]), 0, 1
$endgroup$
$begingroup$
Thank you @SjoerdSmit! Tbh, for my case, it makes more sense to not Catenate them and keep them within the sublists as initially. When I removeCatenateit gives a list of lists of points representing each line, as expected. But then it again looses the sublist's structure.
$endgroup$
– amator2357
Feb 14 at 12:21
add a comment |
$begingroup$
You can use Cases for this:
lines = Line[-Sqrt[5/8 - Sqrt[5]/8],
1/4 (-1 - Sqrt[5]), 0, 1],
Line[Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), 0,
1], Line[-Sqrt[5/8 + Sqrt[5]/8],
1/4 (-1 + Sqrt[5]), Sqrt[5/8 - Sqrt[5]/8],
1/4 (-1 - Sqrt[5])],
Line[Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), 0, 1];
Catenate @ Cases[lines, Line[pts : _, _ ..] :> pts, Infinity]
Out[5]= -Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), 0, 1, Sqrt[
5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), 0,
1, -Sqrt[5/8 + Sqrt[5]/8], 1/4 (-1 + Sqrt[5]), Sqrt[
5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), Sqrt[5/8 - Sqrt[5]/8],
1/4 (-1 - Sqrt[5]), 0, 1
$endgroup$
$begingroup$
Thank you @SjoerdSmit! Tbh, for my case, it makes more sense to not Catenate them and keep them within the sublists as initially. When I removeCatenateit gives a list of lists of points representing each line, as expected. But then it again looses the sublist's structure.
$endgroup$
– amator2357
Feb 14 at 12:21
add a comment |
$begingroup$
You can use Cases for this:
lines = Line[-Sqrt[5/8 - Sqrt[5]/8],
1/4 (-1 - Sqrt[5]), 0, 1],
Line[Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), 0,
1], Line[-Sqrt[5/8 + Sqrt[5]/8],
1/4 (-1 + Sqrt[5]), Sqrt[5/8 - Sqrt[5]/8],
1/4 (-1 - Sqrt[5])],
Line[Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), 0, 1];
Catenate @ Cases[lines, Line[pts : _, _ ..] :> pts, Infinity]
Out[5]= -Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), 0, 1, Sqrt[
5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), 0,
1, -Sqrt[5/8 + Sqrt[5]/8], 1/4 (-1 + Sqrt[5]), Sqrt[
5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), Sqrt[5/8 - Sqrt[5]/8],
1/4 (-1 - Sqrt[5]), 0, 1
$endgroup$
You can use Cases for this:
lines = Line[-Sqrt[5/8 - Sqrt[5]/8],
1/4 (-1 - Sqrt[5]), 0, 1],
Line[Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), 0,
1], Line[-Sqrt[5/8 + Sqrt[5]/8],
1/4 (-1 + Sqrt[5]), Sqrt[5/8 - Sqrt[5]/8],
1/4 (-1 - Sqrt[5])],
Line[Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), 0, 1];
Catenate @ Cases[lines, Line[pts : _, _ ..] :> pts, Infinity]
Out[5]= -Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), 0, 1, Sqrt[
5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), 0,
1, -Sqrt[5/8 + Sqrt[5]/8], 1/4 (-1 + Sqrt[5]), Sqrt[
5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), Sqrt[5/8 - Sqrt[5]/8],
1/4 (-1 - Sqrt[5]), 0, 1
answered Feb 14 at 12:07
Sjoerd SmitSjoerd Smit
3,880715
3,880715
$begingroup$
Thank you @SjoerdSmit! Tbh, for my case, it makes more sense to not Catenate them and keep them within the sublists as initially. When I removeCatenateit gives a list of lists of points representing each line, as expected. But then it again looses the sublist's structure.
$endgroup$
– amator2357
Feb 14 at 12:21
add a comment |
$begingroup$
Thank you @SjoerdSmit! Tbh, for my case, it makes more sense to not Catenate them and keep them within the sublists as initially. When I removeCatenateit gives a list of lists of points representing each line, as expected. But then it again looses the sublist's structure.
$endgroup$
– amator2357
Feb 14 at 12:21
$begingroup$
Thank you @SjoerdSmit! Tbh, for my case, it makes more sense to not Catenate them and keep them within the sublists as initially. When I remove
Catenate it gives a list of lists of points representing each line, as expected. But then it again looses the sublist's structure.$endgroup$
– amator2357
Feb 14 at 12:21
$begingroup$
Thank you @SjoerdSmit! Tbh, for my case, it makes more sense to not Catenate them and keep them within the sublists as initially. When I remove
Catenate it gives a list of lists of points representing each line, as expected. But then it again looses the sublist's structure.$endgroup$
– amator2357
Feb 14 at 12:21
add a comment |
$begingroup$
What about /.Line->List
lines /. Line -> (Flatten[ List[#], 1] &)
$endgroup$
2
$begingroup$
+1. Or perhaps evenlines /. Line -> Sequence.
$endgroup$
– WReach
Feb 14 at 15:20
add a comment |
$begingroup$
What about /.Line->List
lines /. Line -> (Flatten[ List[#], 1] &)
$endgroup$
2
$begingroup$
+1. Or perhaps evenlines /. Line -> Sequence.
$endgroup$
– WReach
Feb 14 at 15:20
add a comment |
$begingroup$
What about /.Line->List
lines /. Line -> (Flatten[ List[#], 1] &)
$endgroup$
What about /.Line->List
lines /. Line -> (Flatten[ List[#], 1] &)
edited Feb 14 at 14:08
answered Feb 14 at 13:48
Ulrich NeumannUlrich Neumann
9,513616
9,513616
2
$begingroup$
+1. Or perhaps evenlines /. Line -> Sequence.
$endgroup$
– WReach
Feb 14 at 15:20
add a comment |
2
$begingroup$
+1. Or perhaps evenlines /. Line -> Sequence.
$endgroup$
– WReach
Feb 14 at 15:20
2
2
$begingroup$
+1. Or perhaps even
lines /. Line -> Sequence.$endgroup$
– WReach
Feb 14 at 15:20
$begingroup$
+1. Or perhaps even
lines /. Line -> Sequence.$endgroup$
– WReach
Feb 14 at 15:20
add a comment |
$begingroup$
Answer to what you want
lines =
Line[-Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), 0,1]
, Line[Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), 0,1]
, Line[-Sqrt[5/8 + Sqrt[5]/8], 1/4 (-1 + Sqrt[5]), Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5])]
, Line[Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), 0, 1]
;
lines /. Line[ l : _, _ .. ] :> l
(* or as WReach has pointed out: lines /. Line -> Sequence *)
Simple and expressive - no remembering whether its Flatten[ ... , 1 ] or Flatten[ ... , 1 ], or Flatten[ ... , -1 ] and what have you.
Different Approach Making Use of Datatypes
While having lists is very compact we can easily get lost in different levels. Why not simply give each single point the head Point (Point can also contain a list of x,y tuples like Line, but giving each point a head of its own simplifies counting and identification of duplicates imo). You can then use pattern matching quite easily, count points, and immediately visualize the data:
Here I turn a list of lines into a list of points, thus not adding another level for each line:
points = lines /. Line[ l : _, _ .. ] :> ( Sequence @@ Point /@ l )
(* if you do not want this you can simply do: lines /. Line -> Point *)
Point[-Sqrt[5/8-Sqrt[5]/8],1/4 (-1-Sqrt[5])],Point[0,1],Point[Sqrt[5/8-Sqrt[5]/8],1/4 (-1-Sqrt[5])],Point[0,1],Point[-Sqrt[5/8+Sqrt[5]/8],1/4 (-1+Sqrt[5])],Point[Sqrt[5/8-Sqrt[5]/8],1/4 (-1-Sqrt[5])],Point[Sqrt[5/8-Sqrt[5]/8],1/4 (-1-Sqrt[5])],Point[0,1]
Now it is easy to count points:
Count[ points, Point[ _ ], Infinity ]
8
It is easy to discover that there are only four unique points:
Flatten @ points // Union
Point[0, 1], Point[-Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5])],
Point[Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5])],
Point[-Sqrt[5/8 + Sqrt[5]/8], 1/4 (-1 + Sqrt[5])]
And we can immediately visualize the results:
Graphics[ Red, PointSize -> Large, points ]

Don't care for Point anymore? Get back to lists:
points /. Point[ pos : _, _ ] :> pos
$endgroup$
add a comment |
$begingroup$
Answer to what you want
lines =
Line[-Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), 0,1]
, Line[Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), 0,1]
, Line[-Sqrt[5/8 + Sqrt[5]/8], 1/4 (-1 + Sqrt[5]), Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5])]
, Line[Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), 0, 1]
;
lines /. Line[ l : _, _ .. ] :> l
(* or as WReach has pointed out: lines /. Line -> Sequence *)
Simple and expressive - no remembering whether its Flatten[ ... , 1 ] or Flatten[ ... , 1 ], or Flatten[ ... , -1 ] and what have you.
Different Approach Making Use of Datatypes
While having lists is very compact we can easily get lost in different levels. Why not simply give each single point the head Point (Point can also contain a list of x,y tuples like Line, but giving each point a head of its own simplifies counting and identification of duplicates imo). You can then use pattern matching quite easily, count points, and immediately visualize the data:
Here I turn a list of lines into a list of points, thus not adding another level for each line:
points = lines /. Line[ l : _, _ .. ] :> ( Sequence @@ Point /@ l )
(* if you do not want this you can simply do: lines /. Line -> Point *)
Point[-Sqrt[5/8-Sqrt[5]/8],1/4 (-1-Sqrt[5])],Point[0,1],Point[Sqrt[5/8-Sqrt[5]/8],1/4 (-1-Sqrt[5])],Point[0,1],Point[-Sqrt[5/8+Sqrt[5]/8],1/4 (-1+Sqrt[5])],Point[Sqrt[5/8-Sqrt[5]/8],1/4 (-1-Sqrt[5])],Point[Sqrt[5/8-Sqrt[5]/8],1/4 (-1-Sqrt[5])],Point[0,1]
Now it is easy to count points:
Count[ points, Point[ _ ], Infinity ]
8
It is easy to discover that there are only four unique points:
Flatten @ points // Union
Point[0, 1], Point[-Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5])],
Point[Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5])],
Point[-Sqrt[5/8 + Sqrt[5]/8], 1/4 (-1 + Sqrt[5])]
And we can immediately visualize the results:
Graphics[ Red, PointSize -> Large, points ]

Don't care for Point anymore? Get back to lists:
points /. Point[ pos : _, _ ] :> pos
$endgroup$
add a comment |
$begingroup$
Answer to what you want
lines =
Line[-Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), 0,1]
, Line[Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), 0,1]
, Line[-Sqrt[5/8 + Sqrt[5]/8], 1/4 (-1 + Sqrt[5]), Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5])]
, Line[Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), 0, 1]
;
lines /. Line[ l : _, _ .. ] :> l
(* or as WReach has pointed out: lines /. Line -> Sequence *)
Simple and expressive - no remembering whether its Flatten[ ... , 1 ] or Flatten[ ... , 1 ], or Flatten[ ... , -1 ] and what have you.
Different Approach Making Use of Datatypes
While having lists is very compact we can easily get lost in different levels. Why not simply give each single point the head Point (Point can also contain a list of x,y tuples like Line, but giving each point a head of its own simplifies counting and identification of duplicates imo). You can then use pattern matching quite easily, count points, and immediately visualize the data:
Here I turn a list of lines into a list of points, thus not adding another level for each line:
points = lines /. Line[ l : _, _ .. ] :> ( Sequence @@ Point /@ l )
(* if you do not want this you can simply do: lines /. Line -> Point *)
Point[-Sqrt[5/8-Sqrt[5]/8],1/4 (-1-Sqrt[5])],Point[0,1],Point[Sqrt[5/8-Sqrt[5]/8],1/4 (-1-Sqrt[5])],Point[0,1],Point[-Sqrt[5/8+Sqrt[5]/8],1/4 (-1+Sqrt[5])],Point[Sqrt[5/8-Sqrt[5]/8],1/4 (-1-Sqrt[5])],Point[Sqrt[5/8-Sqrt[5]/8],1/4 (-1-Sqrt[5])],Point[0,1]
Now it is easy to count points:
Count[ points, Point[ _ ], Infinity ]
8
It is easy to discover that there are only four unique points:
Flatten @ points // Union
Point[0, 1], Point[-Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5])],
Point[Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5])],
Point[-Sqrt[5/8 + Sqrt[5]/8], 1/4 (-1 + Sqrt[5])]
And we can immediately visualize the results:
Graphics[ Red, PointSize -> Large, points ]

Don't care for Point anymore? Get back to lists:
points /. Point[ pos : _, _ ] :> pos
$endgroup$
Answer to what you want
lines =
Line[-Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), 0,1]
, Line[Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), 0,1]
, Line[-Sqrt[5/8 + Sqrt[5]/8], 1/4 (-1 + Sqrt[5]), Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5])]
, Line[Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5]), 0, 1]
;
lines /. Line[ l : _, _ .. ] :> l
(* or as WReach has pointed out: lines /. Line -> Sequence *)
Simple and expressive - no remembering whether its Flatten[ ... , 1 ] or Flatten[ ... , 1 ], or Flatten[ ... , -1 ] and what have you.
Different Approach Making Use of Datatypes
While having lists is very compact we can easily get lost in different levels. Why not simply give each single point the head Point (Point can also contain a list of x,y tuples like Line, but giving each point a head of its own simplifies counting and identification of duplicates imo). You can then use pattern matching quite easily, count points, and immediately visualize the data:
Here I turn a list of lines into a list of points, thus not adding another level for each line:
points = lines /. Line[ l : _, _ .. ] :> ( Sequence @@ Point /@ l )
(* if you do not want this you can simply do: lines /. Line -> Point *)
Point[-Sqrt[5/8-Sqrt[5]/8],1/4 (-1-Sqrt[5])],Point[0,1],Point[Sqrt[5/8-Sqrt[5]/8],1/4 (-1-Sqrt[5])],Point[0,1],Point[-Sqrt[5/8+Sqrt[5]/8],1/4 (-1+Sqrt[5])],Point[Sqrt[5/8-Sqrt[5]/8],1/4 (-1-Sqrt[5])],Point[Sqrt[5/8-Sqrt[5]/8],1/4 (-1-Sqrt[5])],Point[0,1]
Now it is easy to count points:
Count[ points, Point[ _ ], Infinity ]
8
It is easy to discover that there are only four unique points:
Flatten @ points // Union
Point[0, 1], Point[-Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5])],
Point[Sqrt[5/8 - Sqrt[5]/8], 1/4 (-1 - Sqrt[5])],
Point[-Sqrt[5/8 + Sqrt[5]/8], 1/4 (-1 + Sqrt[5])]
And we can immediately visualize the results:
Graphics[ Red, PointSize -> Large, points ]

Don't care for Point anymore? Get back to lists:
points /. Point[ pos : _, _ ] :> pos
edited Feb 14 at 15:22
answered Feb 14 at 14:45
gwrgwr
8,53322761
8,53322761
add a comment |
add a comment |
$begingroup$
To make things easier to read, let's use these lines (with the same nested structure as in the question):
lines =
Line[60, 50, 60, 70]
, Line[40, 30, 40, 50]
, Line[20, 10, 20, 30]
;
Furthermore, let's define the list points as this:
points = 20, 10, 20, 30, 40, 30, 40, 50, 60, 50, 60, 70;
We can get the positions of each point in this list as follows:
pointPosition = points // PositionIndex // Map[First]
(* <|20, 10 -> 1, 20, 30 -> 2, 40, 30 -> 3, 40, 50 -> 4, 60, 50 -> 5, 60, 70 -> 6|> *)
This association can tell us, for example, that the point 40, 30 appears at position 3 in points.
pointPosition[40, 30]
(* 3 *)
We can now convert the line list into a similarly structured list where each point has been replaced by a pair of corresponding positions:
lines /. Line[pts__] :> pointPosition /@ pts
(* 5, 6, 3, 4, 1, 2 *)
Or, should we desire, we could retain the List heads in the result:
lines /. Line[pts__] :> Line[pointPosition /@ pts]
(* Line[5, 6], Line[3, 4], Line[1, 2] *)
This latter form has the advantage that we can still draw the resultant lines by means of GraphicsComplex:
newLines = lines /. Line[pts__] :> Line[pointPosition /@ pts];
Graphics[GraphicsComplex[points, newLines]]

$endgroup$
add a comment |
$begingroup$
To make things easier to read, let's use these lines (with the same nested structure as in the question):
lines =
Line[60, 50, 60, 70]
, Line[40, 30, 40, 50]
, Line[20, 10, 20, 30]
;
Furthermore, let's define the list points as this:
points = 20, 10, 20, 30, 40, 30, 40, 50, 60, 50, 60, 70;
We can get the positions of each point in this list as follows:
pointPosition = points // PositionIndex // Map[First]
(* <|20, 10 -> 1, 20, 30 -> 2, 40, 30 -> 3, 40, 50 -> 4, 60, 50 -> 5, 60, 70 -> 6|> *)
This association can tell us, for example, that the point 40, 30 appears at position 3 in points.
pointPosition[40, 30]
(* 3 *)
We can now convert the line list into a similarly structured list where each point has been replaced by a pair of corresponding positions:
lines /. Line[pts__] :> pointPosition /@ pts
(* 5, 6, 3, 4, 1, 2 *)
Or, should we desire, we could retain the List heads in the result:
lines /. Line[pts__] :> Line[pointPosition /@ pts]
(* Line[5, 6], Line[3, 4], Line[1, 2] *)
This latter form has the advantage that we can still draw the resultant lines by means of GraphicsComplex:
newLines = lines /. Line[pts__] :> Line[pointPosition /@ pts];
Graphics[GraphicsComplex[points, newLines]]

$endgroup$
add a comment |
$begingroup$
To make things easier to read, let's use these lines (with the same nested structure as in the question):
lines =
Line[60, 50, 60, 70]
, Line[40, 30, 40, 50]
, Line[20, 10, 20, 30]
;
Furthermore, let's define the list points as this:
points = 20, 10, 20, 30, 40, 30, 40, 50, 60, 50, 60, 70;
We can get the positions of each point in this list as follows:
pointPosition = points // PositionIndex // Map[First]
(* <|20, 10 -> 1, 20, 30 -> 2, 40, 30 -> 3, 40, 50 -> 4, 60, 50 -> 5, 60, 70 -> 6|> *)
This association can tell us, for example, that the point 40, 30 appears at position 3 in points.
pointPosition[40, 30]
(* 3 *)
We can now convert the line list into a similarly structured list where each point has been replaced by a pair of corresponding positions:
lines /. Line[pts__] :> pointPosition /@ pts
(* 5, 6, 3, 4, 1, 2 *)
Or, should we desire, we could retain the List heads in the result:
lines /. Line[pts__] :> Line[pointPosition /@ pts]
(* Line[5, 6], Line[3, 4], Line[1, 2] *)
This latter form has the advantage that we can still draw the resultant lines by means of GraphicsComplex:
newLines = lines /. Line[pts__] :> Line[pointPosition /@ pts];
Graphics[GraphicsComplex[points, newLines]]

$endgroup$
To make things easier to read, let's use these lines (with the same nested structure as in the question):
lines =
Line[60, 50, 60, 70]
, Line[40, 30, 40, 50]
, Line[20, 10, 20, 30]
;
Furthermore, let's define the list points as this:
points = 20, 10, 20, 30, 40, 30, 40, 50, 60, 50, 60, 70;
We can get the positions of each point in this list as follows:
pointPosition = points // PositionIndex // Map[First]
(* <|20, 10 -> 1, 20, 30 -> 2, 40, 30 -> 3, 40, 50 -> 4, 60, 50 -> 5, 60, 70 -> 6|> *)
This association can tell us, for example, that the point 40, 30 appears at position 3 in points.
pointPosition[40, 30]
(* 3 *)
We can now convert the line list into a similarly structured list where each point has been replaced by a pair of corresponding positions:
lines /. Line[pts__] :> pointPosition /@ pts
(* 5, 6, 3, 4, 1, 2 *)
Or, should we desire, we could retain the List heads in the result:
lines /. Line[pts__] :> Line[pointPosition /@ pts]
(* Line[5, 6], Line[3, 4], Line[1, 2] *)
This latter form has the advantage that we can still draw the resultant lines by means of GraphicsComplex:
newLines = lines /. Line[pts__] :> Line[pointPosition /@ pts];
Graphics[GraphicsComplex[points, newLines]]

answered Feb 14 at 16:00
WReachWReach
53.3k2114212
53.3k2114212
add a comment |
add a comment |
$begingroup$
As bad as it may look, this seems to be working fine:
Partition[Partition[Flatten[Position[points, #] & /@ Catenate @ Cases[lines, Line[pts : _, _ ..] :> pts, Infinity]],2],Length[l]]
$endgroup$
$begingroup$
Working code is one thing - readable and debuggable code another (try understanding that code three months from now). I tend to go for the union of these. :)
$endgroup$
– gwr
Feb 14 at 14:48
add a comment |
$begingroup$
As bad as it may look, this seems to be working fine:
Partition[Partition[Flatten[Position[points, #] & /@ Catenate @ Cases[lines, Line[pts : _, _ ..] :> pts, Infinity]],2],Length[l]]
$endgroup$
$begingroup$
Working code is one thing - readable and debuggable code another (try understanding that code three months from now). I tend to go for the union of these. :)
$endgroup$
– gwr
Feb 14 at 14:48
add a comment |
$begingroup$
As bad as it may look, this seems to be working fine:
Partition[Partition[Flatten[Position[points, #] & /@ Catenate @ Cases[lines, Line[pts : _, _ ..] :> pts, Infinity]],2],Length[l]]
$endgroup$
As bad as it may look, this seems to be working fine:
Partition[Partition[Flatten[Position[points, #] & /@ Catenate @ Cases[lines, Line[pts : _, _ ..] :> pts, Infinity]],2],Length[l]]
answered Feb 14 at 12:53
amator2357amator2357
997
997
$begingroup$
Working code is one thing - readable and debuggable code another (try understanding that code three months from now). I tend to go for the union of these. :)
$endgroup$
– gwr
Feb 14 at 14:48
add a comment |
$begingroup$
Working code is one thing - readable and debuggable code another (try understanding that code three months from now). I tend to go for the union of these. :)
$endgroup$
– gwr
Feb 14 at 14:48
$begingroup$
Working code is one thing - readable and debuggable code another (try understanding that code three months from now). I tend to go for the union of these. :)
$endgroup$
– gwr
Feb 14 at 14:48
$begingroup$
Working code is one thing - readable and debuggable code another (try understanding that code three months from now). I tend to go for the union of these. :)
$endgroup$
– gwr
Feb 14 at 14:48
add a comment |
Thanks for contributing an answer to Mathematica Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f191548%2ffinding-the-location-of-a-point-in-a-given-list%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$begingroup$
If you already call the list
pointswhy not use Mathematica's expressiveness, give it the headPointand really call it that? Readability often beats shortness (and maybe also performance).$endgroup$
– gwr
Feb 14 at 15:33