Need to find the magnitude for the following two vectors below
The magnitude of the projection of this vector (5, -2, 3, 6) onto this vector, (1, 2, 7, 3,) is:
The magnitude of a projection is found by the dot product.
a = (5,-2,3,6)
b = (1,2,7,3)
Make the second vector have unit magnitude with
b = b/sqrt(b.b)
where b.b is b dot-product with b
And the projection magnitude is then:
p = a.b
or altogether
p = a.b/sqrt(b.b)
Related
I have a beginner Julia question regarding generating two types of vectors:
How to I generate a vector consisting out of n of the same elements, without having to type it out manually? For example, how do I generate a vector v consisting out of 7 times the number 5, thus v = [5,5,5,5,5,5,5].
How do I generate a vector with n sequential integers starting at integer x, again without having to type it out manually? For example how do I generate a vector z with 10 sequential integers starting at 1, thus z = [1,2,3,4,5,6,7,8,9,10].
julia> v = fill(5, 7);
julia> #show v;
v = [5, 5, 5, 5, 5, 5, 5]
julia> z = collect(1:10);
julia> #show z;
z = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Note that in the second case, often you can operate on the range 1:10 directly without having to do a collect on it (z = 1:10). 1:10 is a Range type, that works by saving just the starting and ending points, without allocating memory for all 10 values. collect converts that into a full Vector, which allocates memory for each element.
To generate the example: v = repeat([5],7])
To generate the example: z = [1:10;]
For three n-dimensional non-zero-variance variables a, b, and c, n > 2, if
r(ab), r(bc), and r(ac) are Pearson’s correlation coefficients between a and b, between b and c, and between a and c, respectively, then correlation coefficient r(abc) among a, b, and c is defined as:
r^2(abc) = ( r^2(ab) + r^2(bc) + r^2(ac) ) - ( 2 x r(ab) x r(bc) x r(ac) )
I was able to get the code for a manual way of doing it:
a <- c(4, 6, 2, 7)
b <- c(8, 1, 3, 5)
c <- c(6, 3, 1, 9)
al <- data.frame(a, b, c)
al
ab_cor <- cor(al$a, al$b, method = c("pearson"))
bc_cor <- cor(al$b, al$c, method = c("pearson"))
ac_cor <- cor(al$a, al$c, method = c("pearson"))
abc_cor <- sqrt( ( (ab_cor)^2 + (bc_cor)^2 + (ac_cor)^2 ) - ( 2 * ab_cor * bc_cor * ac_cor) )
abc_cor
But I was wondering if this could be done with less lines of code, for example with a for loop. Addittionaly, how would I write it so that I could do it with more than 3 variables as well, for example, r(abcd) i.e. r(ab), r(ac), r(ad), r(bc), r(bd), and r(cd).
The cor function already creates a matrix of the correlations. You just need to pick out the relevant ones and then use some vector operations.
cs <- cor(al, method = "pearson")
cs <- cs[upper.tri(cs)]
#sqrt(sum(cs^2)) - 2*prod(cs)
# apparently it's
sqrt(sum(cs^2) - 2*prod(cs))
This generalizes to your larger case as well assuming that you have all the variables you want in your al data.frame.
I do not know how the X or Y vector relate to each other in this question. The fact that the answer is a single vector is what is confusing me the most.
I tried doing the negative rank (i.e. rank(-s)) and then trying
rev(rank(X)) and rev(rank(Y)) and I am getting incorrect rank returns. Here's my code:
x <- c(-1, 0, 1, 2, -3) #vector x
y <- c(1, 3, 2, 5, 8) # Vector y
S <- x[order(match(x,y))] #orders and matches x with y
R <- rank(-S) #ranks S in element number order
print(R) #I want R = (5 2 1 3 4)
instead, I get [2 1 4 3 5]
with this other code:
never <- rev(rank(-x)) #takes the reverse rank of x
bird <- rev(rank(y)) #takes the reverse rank of y
kid <- x[order(match(never, bird))] #orders the match of the reverse ranks
tr <- rank(kid) #ranks the match of the reverse ranks
print(tr)
I get [2 1 3 4 5].
As I understand the question (which is not clear, I agree), there seems to be an error, but $x$ and $y$ are linked by the $s_i$ index.
Following the question: Let us find the $s_1$ index thanks to $y$. We know that $y_{s_1}$ is the largest value of $y$, hence $y_5$ in this case. Thus, $s_1 = 5$, so we need to find the reverse rank of $x_5 = -3$. As this is the lowest value, we have $r_1 = 5$.
Now with this approach, we would have $s_2 = 4$, so my guess is that there is an error in the example or the question.
In the example, I happen to have $R = (5, 1, 3, 2, 4)$.
You can notice that if you sort $y$, then apply the sorting rule to $x$ to create $x'$, the $R$ vector is the sorting rule of $x'$.
I figured it out.
x <- c(-1,0,1,2,-3)
y <- c(1,3,2,5,8)
myrank <- function(x,y)
{
R=c()
{
for(i in 1:length(x))
{
b=length(y)+1-rank(y)
a=length(y)+1-rank(x[b]) # here x[b] is to order the x values according to b
R=c(a)
}
}
print(R)
}
myrank(x,y)
Pretend the dataframe below is an edgelist (relation between inst2 and motherinst2), and that km is an attribute I want to calculate as a path that's been assigned to the edges. I'm too new at coding to make a reproducible edge list.
inst2 = c(2, 3, 4, 5, 6)
motherinst2 = c(7, 8, 9, 10, 11)
km = c(20, 30, 40, 25, 60)
df2 = data.frame(inst2, motherinst2)
edgelist = cbind(df2, km)
g = graph_from_data_frame(edgelist)
I know how to calculate the path length of vertices in a graph, but I have some attributes attached to the edges that I want to sum up as path lengths. They are simple attributes (distance in km, time in days, and speed as km/day).
This is how I was calculating the path of vertices (between roots and terminals/leaves):
roots = which(sapply(sapply(V(g),
function(x) neighbors(g, x, mode = 'in')), length) == 0)
#slight tweaking this piece of code will also calculate 'terminal' nodes (or leaves). (11):
terminals = which(sapply(sapply(V(g),
function(x) neighbors(g, x, mode = 'out')), length) == 0)
paths= lapply(roots, function(x) get.all.shortest.paths(g, from = x, to = terminals, mode = "out")$res)
named_paths= lapply(unlist(paths, recursive=FALSE), function(x) V(g)[x])
I just want to do essentially exactly as I did above, but summing up the distance, time, and rate (which I will compute the mean of) incurred between each of those paths. If it helps to know how the edges have been added as attributes, I've used cbind like so:
edgelist_df = cbind(edgelist_df, time, dist, speed)
and my graph object (g) is set up like this:
g <- graph_from_data_frame(edgelist_df, vertices = vattrib_df)
vattrib_df is the attributes of the vertices, which is not of interest to us here.
I'm thinking that #GGrothendieck's answer to the request for solutions to fractional roots of negative numbers deserves a graphical addendum:
Can someone plot the roots in a unit complex circle. as well as add the "graphical sum" of some of the roots, i.e. the sequential products of the same 5 roots of -8, vectors multiplied in sequence?
x <- as.complex(-8) # or x <- -8 + 0i
# find all three cube roots
xroot5 <- (x^(1/5) * exp(2*c(0:4)*1i*pi/5))
plot(xroot5, xlim=c(-8, 2), ylim=c(-5,5))
abline(h=0,v=0,lty=3)
Originally I was thinking this would be some sort of head to tail illustration but complex multiplication is a series of expansions and rotations around the origin.
The Reduce function with accumulate=TRUE will deliver the sequence of intermediate powers of each of the roots of x^5 = -8 up to the fith power:
x <- as.complex(-8) # or x <- -8 + 0i
# find all five roots
xroot5 <- (x^(1/5) * exp(2*c(0:4)*1i*pi/5))
xroot5
#[1] 1.226240+0.890916i -0.468382+1.441532i -1.515717+0.000000i -0.468382-1.441532i
#[5] 1.226240-0.890916i
(Reduce("*", rep( xroot5[4], 5),acc=TRUE) )
#[1] -0.468382-1.441532i -1.858633+1.350376i 2.817161+2.046787i 1.631001-5.019706i
#[5] -8.000000+0.000000i
# Using the fourth root:
beg <- (Reduce("*", rep( xroot5[4], 5),acc=TRUE) )[-5]
ends <- (Reduce("*", rep( xroot5[4], 5),acc=TRUE) )[-1]
# Need more space
plot(xroot5, xlim=c(-8, 2), ylim=c(-6,6))
abline(h=0,v=0,lty=3)
arrows(Re(beg),Im(beg), Re(ends), Im(ends), col="red")
# Plot sequence of powers of first root:
beg <- Reduce("*", rep( xroot5[1], 5),acc=TRUE)[-5]
ends <- Reduce("*", rep( xroot5[1], 5),acc=TRUE)[-1]
arrows(Re(beg),Im(beg), Re(ends), Im(ends), col="blue")
The circle is centered at 0, 0. The roots all have the same radius and picking any one of them, the radius is
r <- Mod(xroot[1])
The following gives a plot which looks similar to the plot in the question except we have imposed an aspect ratio of 1 in order to properly draw it and there is a circle drawn through the 5 points:
plot(Re(xroot5), Im(xroot5), asp = 1)
library(plotrix)
draw.circle(0, 0, r)
Multiplying any root by
e <- exp(2*pi*1i/5)
will rotate it into the next root. For example, this plots xroot5[1] in red:
i <- 0
points(Re(xroot5[1] * e^i), Im(xroot5[1] * e^i), pch = 20, col = "red")
and then repeat the last line for i = 1, 2, 3, 4 to see the others successively turn red.