Element-wise multiplication of a matrix and a vector in DolphinDB - linear-algebra

Is there a built-in function in DolphinDB to element-wise multiply each column of a m * n matrix by a vector of size m?
To my knowledge, I can do this by using a for loop. Is there a more efficient way?
m = 1..1000000$1000:1000
n = 1..1000
for (i in 0:m.cols())
m[i] *= n

DolphinDB doesn't have a built-in function to do what you want exactly. But you can achieve this using higher order function each.
each(*, m, n)

Related

How to do a cumulative sum inverse square weighting based upon how many items you want to calculate off a vector in R?

Suppose I have a vector of length 10
vec <- c(10,9,8,7,6,5,4,3,2,1)
and I wanted to create a function that takes in a subset length value (say 3) and computes the squared inverse up to that length. I would like to compute:
10+(9/(2^2))+(8/(3^2))
which would be
vec[1]+(vec[2]/(2^2))+(vec[3]/(3^2))
but with a function that can take input of the subset length.
The only solution I can think of is a for loop, is there a faster more elegant solution in R?
Yes, you can use the fact that most operations in R are vectorised to do this without a loop:
vec <- c(10,9,8,7,6,5,4,3,2,1)
cum_inverse_square <- function(vec, n) {
sum(vec[1:n] / (1:n)^2)
}
cum_inverse_square(vec, 3) == 10+(9/(2^2))+(8/(3^2)) # TRUE

Name of operation R uses when multiplying a matrix by a vector

For m by n matrix A and m by 1 vector b, what do you call the operation R performs when you run A*b? The result is another m by n matrix where each column in A has been multiplied element-wise with the column vector b, but is there a succinct name for this? How would you write this operation using matrix notation? Thanks!

Custom function with vectors and matrixes in R

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

Avoiding a loop in matrix index

dist is an nxn matrix of costs:
dist <-matrix(c(0,3.2,1.2,3.2,0,0.5,1.2,0.5,0),nrow=3,ncol=3)
v is a vector of length n, where the index of the vector corresponds to the row of dist, and the value in the vector corresponds to the column of dist
v <- c(2,2,3)
I want to sum the costs like this:
cost <- 0
for(i in 1:length(v)){
cost <- dist[i,v[i]] + cost
}
but this seems clumsy and slow. What is the trick to doing this without the for loop? Is the for loop not taking advantage of some magical R alternative? Suggestions please!
We need to cbind with the row index to extract the values and sum
sum(dist[cbind(1:nrow(dist), v)])

R, iterating over the row vectors of a matrix

I have some vector vect and I want to iterate over the row vectors vof a matrix and calculate:
cov(v, vect).
I tried:
for(vect in mat2) #where mat2 is a 215 by 31 matrix
However, each vector appeared to be a scalar with value 1.
How do I iterate over the row vectors of a matrix?
To make this even better, since I am interested in calculating the sum of cov(v, vect) where v is a row vector, how can I use the higher-order functions left-fold and right-fold
Are you looking for apply ?
apply(mat2, 1, function(v)cov(v,vect))
If I understand that vect is a separate vector from mat2:
apply(mat2, 1, function(v) cov(v, vect))
The apply function allows you to apply an arbitrary function over the rows (if the second argument is 1) or columns (if 2) or a higher dimension (if >2). It is also much faster than using a loop.

Resources