I couldn't find the definition or lemma for transposing a vector of type (real,'n) vec in Finite_Cartesian_Product theory. I'm trying to substitute a transposed vector by a transpose matrix and a vector, for instance, if the vector e = A x then the transpose of e(e^T) leads to transposing A and x (e^T = A^T x^T). Can I do this in Isabelle/HOL?
First of all, unless my linear algebra is completely failing me right now, (AB)^T = B^T A^T, not A^T B^T, so your second equation should be e^T = x^T A^T
To answer your actual question: I suggest you have a look at the constants rowvector, columnvector, and transpose from ~~/src/HOL/Analysis/Cartesian_Euclidean_Space. The first two allow you to transform a vector of length n into a 1 × n (resp. n × 1) matrix and the latter allows you to transpose a matrix.
I guess your e = A x would look like columnvector e = A ** columnvector x and your e^T = x^T A^T would be rowvector e = rowvector x ** transpose A.
Related
I have two matrices: A (k rows, m columns), B(k rows, n columns)
I want to operate on all pairs of columns (one from A and one from B), the result should be a matrix C (m rows, n columns) where C[i,j] = f(A[,i],B[,j])
now, if the function f was the sum of the dot product, then the whole thing was just a simple multiplication of matrices (C = t(A) %*% B)
but my f is different (specifically, I count the number equal entries:
f = function(x,y) sum(x==y)
my question if there is a simple (and fast, because my matrices are big) way to compute the result?
preferably in R, but possibly in python (numpy). I thought about using outer(A,B,"==") but this results in a 4 dimensional array which I havent figured out what exactly to do with it.
Any help is appreciated
In R, we can split them into list and apply the function f with a nested lapply/sapply
lapply(asplit(A, 2), function(x) sapply(asplit(B, 2), function(y) f(x, y)))
Or using outer after converting to data.frame because the unit will be column, while for matrix, it is a single element (as matrix is a vector with dim attributes)
outer(as.data.frame(A), as.data.frame(B), FUN = Vectorize(f))
data
A <- cbind(1:5, 6:10)
B <- cbind(c(1:3, 1:2), c(5:7, 6:7))
I need to create a custom function in R that return the product between a vector of dimension m x 1 and a matrix of dimension m x m. However, the default value of the vector must be 0 and the default value of the matrix must be the identity matrix of dimension m x m.
I have worked creating basic functions in R like factorial or pow functions, but I have no idea how to create a function that involves vectors and matrixes.
Thank you :)
Do you mean initialize the vector and matrix like below?
m <- 5
v <- matrix(0,m)
mat <- diag(m)
If you are looking for matrix production, try %*%, e.g.,
mat %in% v
What is this? I can't find help by using ?. (Sorry for being dumb)
> 1%*%1
[,1]
[1,] 1
> 10%*%10
[,1]
[1,] 100
> c(1:2)%*%c(1:2)
[,1]
[1,] 5
It's a matrix multiplication operator!
From the documentation:
Description:
Multiplies two matrices, if they are conformable. If one argument is a vector, it will be promoted to either a row or column matrix to make the two arguments conformable. If both are vectors of the same length, it will return the inner product (as a matrix).
Usage:
x %*% y
Arguments:
x, y numeric or complex matrices or vectors
> c(1,2,3) %*% c(4,5,6)
[,1]
[1,] 32
> c(1,2,3) * c(4,5,6)
[1] 4 10 18
Like MadSeb said, it is the matrix multiplication operator. If you give it two vectors, it will coerce them to (logical) 1-row & a 1-col matrix and multiply them.
It is also the inner (or dot) product between two vectors and finds wide usage in linear algebra, computational geometry and a host of other applications.
http://en.wikipedia.org/wiki/Dot_product
BTW, the vectors have to be in the same space (same number of dimensions)
> c(1,2,3) %*% c(4,5,6,7)
Error in c(1, 2, 3) %*% c(4, 5, 6, 7) : non-conformable arguments
I created a question 'What is the calculation behind the %*% operator in R?' which was marked as a duplicate of this question. The %*% operator is used to multiply two matrices. I didn't realise 'matrix multiplication' was an established algebraic method so it was useful to learn the underlying calculation, not yet described explicitly in other answers here. Passing on useful links from comments in the duplicate question
https://en.m.wikipedia.org/wiki/Matrix_multiplication#Definition
http://matrixmultiplication.xyz/
This operator is used to multiply a matrix with its transpose.
M = matrix( c(2,6,5,1,10,4), nrow = 2,ncol = 3,byrow = TRUE)
t = M %*% t(M)
print(t)
from tutorialspoints
I have following problem with R function findInterval()
Given a vector X and a matrix Y, I want to find in which interval lie elements of X. Intervals are constructed, having breakpoints in Y rows. In other words for X = c(2,3) and Y = matrix(c(3,1,4,2,5,4),2,3), the output would be c(0,2). I wrote following code:
X <- c(2,3)
Y <- matrix(c(3,1,4,2,5,4),2,3)
output <- diag(apply(Y,1,function(z)findInterval(X,z)))
and it works. However, I think, it can be optimised, since the apply function returns 2 x 2 matrix (that's why i had to get diagonal of that). Is there a way to do the same, but using function, which will return a vector, taking as an argument my vector X and matrix Y? I perform this operation on high-demensional vectors, so obtaining unnecessary matrixes size 10000 x 10000 is not a good idea imho. To maximize efficiency, I don't want to use loops.
Thanks in advance for any feedback.
You can do
rowSums(X > Y)
# [1] 0 2
I need to calculate the mean (or other summary functions) on the top x and bottom x portions on list of vectors of varying lengths.
Here is a list of 3 vectors of different lengths similar in format with what I am working with:
t <- list(a = exp(-4:3), b = exp(-2:12), c = exp(-5:3))
Ideally, I would like a single vector of numbers for each type of means (I manually ran mean(head(t$a),2)) and mean(tail(t$a),2)) for each vectors):
Ideal output yielding a nameless vector of means of the first two elements from each vector:
[1] 0.2516074 1.859141 0.09256118
Second vector of means for last two entries in each vector:
[1] 1.859141 15064.77 1.859141
Looking for a clever lapply-type construct to get a vector of numbers for each means without the attached names (in this case a,b,c). Thanks!
What about
n = 2
v = lapply(t, function(i) mean(head(i, n)))
The variable v is list. So to get a vector, just use unlist
v = unlist(v)
To extract the numbers use as.vector
as.vector(v)
For the tail, just use
lapply(t, function(i) mean(tail(i, n)))
Using sapply you can wrap this in a function:
sapply(dat,function(x,length=2)
c(mean(head(x,length)),mean(head(x,length))))
# a b c
# [1,] 0.03405135 0.2516074 0.01252679
# [2,] 0.03405135 0.2516074 0.01252679