Linking Vertex ID to coordinates in PyMeshLab - vertex

I am looking to be able able to link the Vertex ID with the coordinate positions.
Using m.edge_matrix I can generate the list of vertex IDs for the edges that form a polyline. What is the easiest way to link this back to the actual coordinates?
Using m.vertex_matrix produces the list of coordinates, but has no reference to the Vertex ID. The order in which the coordinates are listed in m.vertex_matrix doesn't appear to link to the order in which they appear in m.edge_matrix
Many thanks.

The matrix returned by m.vertex_matrix() is indexable. The row n corresponds to coordinates of vertex with id=n, so you just need to use [ ] to read the row.
v = m.vertex_matrix()
for e in m.edge_matrix():
print("edge", e, "goes from", v[e[0]], "to", v[e[1]])
which produces this output:
edge [0 1] goes from [0.12843863 0.38690682 0.1] to [0.13383933 0.3839188 0.1]
edge [2 3] goes from [0.14307424 0.38100217 0.1] to [0.13592989 0.38318165 0.1]
edge [3 1] goes from [0.13592989 0.38318165 0.1] to [0.13383933 0.3839188 0.1]
edge [4 5] goes from [0.25161905 0.21663008 0.1] to [0.2520413 0.21464215 0.1]
edge [6 5] goes from [0.25537567 0.20097797 0.1] to [0.2520413 0.21464215 0.1]

Related

How many tours exist in a connected graph with V vertices?

Assuming every vertex has an edge to every other vertex, how many tours exist in this graph, where you must start a vertex v and end up back at v?
I'm assuming you want your tours to be simple - otherwise the answer would be "infinitely many".
Now, your tours can be of three kinds:
First, the tour that only contains v
Second, any cycle of length 2 that contains v, i.e., from v, walk to any vertex w and back. There's n-1 of those.
Third, any cycle containing v of length 3 or more. All of those have the form "v -> a -> … -> b -> v", where a -> … -> b can be any simple path from a to b that does not contain v. How many simple paths of length k, starting at a, are there? Well, for the first vertex, you can walk from a to any of (n-2) other vertices. For the second vertex, you can choose from (n-3) vertices, and so on. Thus, there are (n-2) * (n-3) * … (n - k - 1) simple paths of length k that start at a and don't include v. Since k can be anything between 1 and n-2, you have paths per vertex a - and there are n-1 choices for a.
Summing it all up, you end up with:

PARI/GP: How to get the max prime factor of the integer?

I am new to pari/gp. I use factorint to find all the prime factors and it returns a matrix.
I am trying to traverse through a matrix to find the largest number inside but unable to find the length of rows and columns. Also how can i use the if to compare each element is higher or lower. My p is being generated on top.
temp = factorint(p-1);
num = 0;
for(i=1, size,
for(j=1, size,
if(num <= temp[i,j], num = temp[i,j]);
);
);
print("number is = " num);
Thanks in advance.
Please, note that factorint(p) always returns the nx2-matrix, where n is the number of the prime factors for p. First column is for the prime factors. Second column is for their multiplicities.
So all you need is to find the maximum element of the first column. It can be done as follows:
factors = factorint(p-1);
print("number is = ", vecmax(factors[, 1]));
By the way, the length of vector v is just #v in PARI/GP.
Besides matsize, you could also use #. For example,
factorint(30) gives a 3x2 matrix;
[2 1]
[3 1]
[5 1]
#factorint(30) gives the number of colums of that matrix (which is 2).
By transposing the matrix, the resulting matrix has 3 columns, which is the number of distinct prime factors of 30.
Transposing can be done as follows: append ~ to a matrix.
So we could do
#factorint(30)~
to get the number of distinct prime factors;
which prints
[2 3 5]
[1 1 1]
As those prime factors will be increasingly ordered in the first row, the last one in the first row is the largest one hence
factorint(30)[#factorint(30)~, 1] gives the largest prime factor of 30; 5
Now you could avoid factoring 30 twice by doing;
f = factorint(30); f[#f~]
to get 5 as desired.

clojure check if value in vector in vector in vector equals another

Basically I have vectors within vectors, within vectors, and those deepest vectors contain two coordinate points. for example
[[[3 4] [4 5]] [[5 6] [6 7]]]
I want to check to see if a certain x and y value match any of the coordinate points inside, and if they do, return -1. I tried doing this but the function isn't working.
(defn check-for-stuff
[vect x y]
(if (not (empty? vect))
(dotimes [n (count vect)]
(if (not (empty? (vect n)))
(dotimes [p (count (vect n))]
(if (and (= (((vect n) p) 0) x)
(= (((vect n) p) 1) y))
-1
))))))
For a literal answer to the question, this is my first top-of-head attempt:
(defn check-for-stuff [vect x y]
(when (some (fn [v] (some #(= % [x y]) v)) vect)
-1))
Observed values in testing:
(check-for-stuff [[[1 2] [3 4]]] 1 2)
-1
(check-for-stuff [[[1 2] [3 4]]] 1 3)
nil
That said -- the above is bad practice, since it needs to iterate through the vectors provided (and thus having O(n) performance), rather than being able to do a constant-time membership check as a hash-map or set can.
Also, it would be more conventional to return a more meaningful value for a truthy result, or true if no such meaningful value exists.
#CharlesDuffy is right on the money about this not being the ideal structure for something like this. Based on your description of what you're doing, I would recommend a set of sets of x-y vectors (representing points). For example:
#{#{[3 4] [4 5]} #{[5 6] [6 7]}}
This is a set that represents 2 planes, each represented as a set of x-y vectors. To determine whether any of these sets contains a given x-y point, you can use some and contains?, like this:
(defn check-for-stuff [planes x y]
(some #(contains? % [x y]) planes))
As #CharlesDuffy also pointed out, -1 might not be the best value to use as a return value. The idiomatic thing to do in Clojure when you're writing a function that checks inside a collection for a specific value and tells you whether it's in there or not, is to take advantage of the way that "truthiness" works in Clojure. Every value is considered "truthy" except for false and nil. In the function above, some returns either the plane containing [x y] if it exists in planes, or nil. You can use the return value of this function just like it were returning true or false, like this:
(if (check-for-stuff #{#{[3 4] [4 5]} #{[5 6] [6 7]}} 4 5)
(println "point found")
(println "point not found"))

Multidimensional Array Comprehension in Julia

I'm mucking about with Julia and can't seem to get multidimensional array comprehensions to work. I'm using a nightly build of 0.20-pre for OSX; this could conceivably be a bug in the build. I suspect, however, it's a bug in the user.
Lets say I want to wind up with something like:
5x2 Array
1 6
2 7
3 8
4 9
5 10
And I don't want to just call reshape. From what I can tell, a multidimensional array should be generated something like: [(x, y) for x in 1:5, y in 6:10]. But this generates a 5x5 Array of tuples:
julia> [(x, y) for x in 1:5, y in 6:10]
5x5 Array{(Int64,Int64),2}:
(1,6) (1,7) (1,8) (1,9) (1,10)
(2,6) (2,7) (2,8) (2,9) (2,10)
(3,6) (3,7) (3,8) (3,9) (3,10)
(4,6) (4,7) (4,8) (4,9) (4,10)
(5,6) (5,7) (5,8) (5,9) (5,10)
Or, maybe I want to generate a set of values and a boolean code for each:
5x2 Array
1 false
2 false
3 false
4 false
5 false
Again, I can only seem to create an array of tuples with {(x, y) for x in 1:5, y=false}. If I remove the parens around x, y I get ERROR: syntax: missing separator in array expression. If I wrap x, y in something, I always get output of that kind -- Array, Array{Any}, or Tuple.
My guess: there's something I just don't get here. Anybody willing to help me understand what?
I don't think a comprehension is appropriate for what you're trying to do. The reason can be found in the Array Comprehension section of the Julia Manual:
A = [ F(x,y,...) for x=rx, y=ry, ... ]
The meaning of this form is that F(x,y,...) is evaluated with the variables x, y, etc. taking on each value in their given list of values. Values can be specified as any iterable object, but will commonly be ranges like 1:n or 2:(n-1), or explicit arrays of values like [1.2, 3.4, 5.7]. The result is an N-d dense array with dimensions that are the concatenation of the dimensions of the variable ranges rx, ry, etc. and each F(x,y,...) evaluation returns a scalar.
A caveat here is that if you set one of the variables to a >1 dimensional Array, it seems to get flattened first; so the statement that the "the result is... an array with dimensions that are the concatenation of the dimensions of the variable ranges rx, ry, etc" is not really accurate, since if rx is 2x2 and ry is 3, then you will not get a 2x2x3 result but rather a 4x3. But the result you're getting should make sense in light of the above: you are returning a tuple, so that's what goes in the Array cell. There is no automatic expansion of the returned tuple into the row of an Array.
If you want to get a 5x2 Array from a comprhension, you'll need to make sure x has a length of 5 and y has a length of 2. Then each cell would contain the result of the function evaluated with each possible pairing of elements from x and y as arguments. The thing is that the values in the cells of your example Arrays don't really require evaluating a function of two arguments. Rather what you're trying to do is just to stick two predetermined columns together into a 2D array. For that, use hcat or a literal:
hcat(1:5, 6:10)
[ 1:5 5:10 ]
hcat(1:5, falses(5))
[ 1:5 falses(5) ]
If you wanted to create a 2D Array where column 2 contained the result of a function evaluated on column 1, you could do this with a comprehension like so:
f(x) = x + 5
[ y ? f(x) : x for x=1:5, y=(false,true) ]
But this is a little confusing and it seems more intuitive to me to just do
x = 1:5
hcat( x, map(f,x) )
I think you are just reading the list comprehension wrong
julia> [x+5y for x in 1:5, y in 0:1]
5x2 Array{Int64,2}:
1 6
2 7
3 8
4 9
5 10
When you use them in multiple dimensions you get two variables and need a function for the cell values based on the coordinates
For your second question I think that you should reconsider your requirements. Julia uses typed arrays for performance and storing different types in different columns is possible. To get an untyped array you can use {} instead of [], but I think the better solution is to have an array of tuples (Int, Bool) or even better just use two arrays (one for the ints and one for the bool).
julia> [(i,false) for i in 1:5]
5-element Array{(Int64,Bool),1}:
(1,false)
(2,false)
(3,false)
(4,false)
(5,false)
I kind of like the answer #fawr gave for the efficiency of the datatypes while retaining mutability, but this quickly gets you what you asked for (working off of Shawn's answer):
hcat(1:5,6:10)
hcat({i for i=1:5},falses(5))
The cell-array comprehension in the second part forces the datatype to be Any instead of IntXX
This also works:
hcat(1:5,{i for i in falses(5)})
I haven't found another way to explicitly convert an array to type Any besides the comprehension.
Your intuition was to write [(x, y) for x in 1:5, y in 6:10], but what you need is to wrap the ranges in zip, like this:
[i for i in zip(1:5, 6:10)]
Which gives you something very close to what you need, namely:
5-element Array{(Int64,Int64),1}:
(1,6)
(2,7)
(3,8)
(4,9)
(5,10)
To get exactly what you're looking for, you'll need:
hcat([[i...] for i in zip(1:5, 6:10)]...)'
This gives you:
5x2 Array{Int64,2}:
1 6
2 7
3 8
4 9
5 10
This is another (albeit convoluted) way:
x1 = 1
x2 = 5
y1 = 6
y2 = 10
x = [x for x in x1:x2, y in y1:y2]
y = [y for x in x1:x2, y in y1:y2]
xy = cat(2,x[:],y[:])
As #ivarne noted
[{x,false} for x in 1:5]
would work and give you something mutable
I found a way to produce numerical multidimensional arrays via vcat and the splat operator:
R = [ [x y] for x in 1:3, y in 4:6 ] # make the list of rows
A = vcat(R...) # make n-dim. array from the row list
Then R will be a 3x3 Array{Array{Int64,2},2} while A is a 9x2 Array{Int64,2}, as you want.
For the second case (a set of values and a Boolean code for each), one can do something like
R = [[x y > 5] for x in 1:3, y in 4:6] # condition is y > 5
A = vcat(R...)
where A will be a 9x2 Array{Int64,2}, where true/false is denote by 1/0.
I have tested those in Julia 0.4.7.

How to copy a vertex with it's respective edges (all/in/out) from a directed graph g, to a new directed graph g1?

Is there a method or a class in igraph to do this procedure fast and efectively?
Let's assume that your graph is in g and the set of vertices to be used is in sampled (which is a vector consisting of zero-based vertex IDs).
First, we select the set of edges where at least one endpoint is in sampled:
all.vertices <- (1:vcount(g)) - 1
es <- E(g) [ sampled %--% 1:n ]
es is now an "edge sequence" object that consists of the edges of interest. Next, we take the edge list of the graph (which is an m x 2 matrix) and select the rows corresponding to the edges:
el <- get.edgelist(g)[as.vector(es)+1]
Here, as.vector(es) converts the edge sequence into a vector consisting of the edge IDs of the edges in the edge sequence, and use it to select the appropriate subset of the edge list. Note that we had to add 1 to the edge IDs because R vectors are indexed from 1 but igraph edge IDs are from zero.
Next, we construct the result from the edge list:
g1 <- graph(el, vcount(g), directed=is.directed(g))
Note that g1 will contain exactly as many vertices as g. You can take the subgraph consisting of the sampled vertices as follows:
g1 <- subgraph(g1, sampled)
Note to users of igraph 0.6 and above: igraph 0.6 will switch to 1-based indexing instead of 0-based, so there is no need to subtract 1 from all.vertices and there is no need to add 1 to as.vector(es). Furthermore, igraph 0.6 will contain a function called subgraph.edges, so one could simply use this:
g1 <- subgraph.edges(g, es)

Resources